Previous Next
Drag & Drop

Forms

Table of Contents

Formulare

All forms elements from HTML, that has class majaxForm.

The actor behaviour is to simulate objects that are difficult to set up or time consuming to set up for a test. The classic example is a database connection. Setting up a test database at the start of each test would slow testing to a crawl and would require the installation of the database engine and test data on the test machine. If we can simulate the connection and return data of our choosing we not only win on the pragmatics of testing, but can also feed our code spurious data to see how it responds. We can simulate databases being down or other extremes without having to create a broken database for real. In other words, we get greater control of the test environment.

If mock objects only behaved as actors they would simply be known as server.

However, the mock objects not only play a part (by supplying chosen return values on demand) they are also sensitive to the messages sent to them (via expectations). By setting expected parameters for a method call they act as a guard that the calls upon them are made correctly. If expectations are not met they save us the effort of writing a failed test assertion by performing that duty on our behalf. In the case of an imaginary database connection they can test that the query, say SQL, was correctly formed by the object that is using the connection. Set them up with fairly tight expectations and you will hardly need manual assertions at all.

Creating mock objects

In the same way that we create server stubs, all we need is an existing class, say a database connection that looks like this...

  1. class DatabaseConnection {
  2.     function DatabaseConnection({
  3.     }
  4.     
  5.     function query({
  6.     }
  7.     
  8.     function selectQuery({
  9.     }
  10. }
The class does not need to have been implemented yet. To create a mock version of the class we need to include the mock object library and run the generator...
  1. require_once('simpletest/unit_tester.php');
  2. require_once('simpletest/mock_objects.php');
  3. require_once('database_connection.php');
  4.  
  5. Mock::generate('DatabaseConnection');
This generates a clone class called MockDatabaseConnection. We can now create instances of the new class within our test case...
  1. require_once('simpletest/unit_tester.php');
  2. require_once('simpletest/mock_objects.php');
  3. require_once('database_connection.php');
  4.  
  5. Mock::generate('DatabaseConnection');
  6.  
  7. class MyTestCase extends UnitTestCase {
  8.     
  9.     function testSomething({
  10.         $connection &new MockDatabaseConnection($this);
  11.     }
  12. }
Unlike the generated stubs the mock constructor needs a reference to the test case so that it can dispatch passes and failures while checking its expectations. This means that mock objects can only be used within test cases. Despite this their extra power means that stubs are hardly ever used if mocks are available.

Mocks as actors

The mock version of a class has all the methods of the original so that operations like $connection->query() are still legal. As with stubs we can replace the default null return values...

  1. $connection->setReturnValue('query'37);
Now every time we call $connection->query() we get the result of 37. As with the stubs we can set wildcards and we can overload the wildcard parameter. We can also add extra methods to the mock when generating it and choose our own class name...
  1. Mock::generate('DatabaseConnection''MyMockDatabaseConnection'array('setOptions'));
Here the mock will behave as if the setOptions() existed in the original class. This is handy if a class has used the PHP overload() mechanism to add dynamic methods. You can create a special mock to simulate this situation.

All of the patterns available with server stubs are available to mock objects...

  1. class Iterator {
  2.     function Iterator({
  3.     }
  4.     
  5.     function next({
  6.     }
  7. }
Again, assuming that this iterator only returns text until it reaches the end, when it returns false, we can simulate it with...
  1. Mock::generate('Iterator');
  2.  
  3. class IteratorTest extends UnitTestCase({
  4.     
  5.     function testASequence({
  6.         $iterator &new MockIterator($this);
  7.         $iterator->setReturnValue('next'false);
  8.         $iterator->setReturnValueAt(0'next''First string');
  9.         $iterator->setReturnValueAt(1'next''Second string');
  10.         ...
  11.     }
  12. }
When next() is called on the mock iterator it will first return "First string", on the second call "Second string" will be returned and on any other call false will be returned. The sequenced return values take precedence over the constant return value. The constant one is a kind of default if you like.

A repeat of the stubbed information holder with name/value pairs...

  1. class Configuration {
  2.     function Configuration({
  3.     }
  4.     
  5.     function getValue($key{
  6.     }
  7. }
This is a classic situation for using mock objects as actual configuration will vary from machine to machine, hardly helping the reliability of our tests if we use it directly. The problem though is that all the data comes through the getValue() method and yet we want different results for different keys. Luckily the mocks have a filter system...
  1. $config &new MockConfiguration($this);
  2. $config->setReturnValue('getValue''primary'array('db_host'));
  3. $config->setReturnValue('getValue''admin'array('db_user'));
  4. $config->setReturnValue('getValue''secret'array('db_password'));
The extra parameter is a list of arguments to attempt to match. In this case we are trying to match only one argument which is the look up key. Now when the mock object has the getValue() method invoked like this...
  1. $config->getValue('db_user')
...it will return "admin". It finds this by attempting to match the calling arguments to its list of returns one after another until a complete match is found.

There are times when you want a specific object to be dished out by the mock rather than a copy. Again this is identical to the server stubs mechanism...

  1. class Thing {
  2. }
  3.  
  4. class Vector {
  5.     function Vector({
  6.     }
  7.     
  8.     function get($index{
  9.     }
  10. }
In this case you can set a reference into the mock's return list...
  1. $thing new Thing();
  2. $vector &new MockVector($this);
  3. $vector->setReturnReference('get'$thingarray(12));
With this arrangement you know that every time $vector->get(12) is called it will return the same $thing each time.

Mocks as critics

Although the server stubs approach insulates your tests from real world disruption, it is only half the benefit. You can have the class under test receiving the required messages, but is your new class sending correct ones? Testing this can get messy without a mock objects library.

By way of example, suppose we have a SessionPool class that we want to add logging to. Rather than grow the original class into something more complicated, we want to add this behaviour with a decorator (GOF). The SessionPool code currently looks like this...

  1. class SessionPool {
  2.     function SessionPool({
  3.         ...
  4.     }
  5.     
  6.     function &findSession($cookie{
  7.         ...
  8.     }
  9.     ...
  10. }
  11.  
  12. class Session {
  13.     ...
  14. }
  15. </php>
  16.                 While our logging code looks like this...
  17. <php>
  18. class Log {
  19.     function Log({
  20.         ...
  21.     }
  22.     
  23.     function message({
  24.         ...
  25.     }
  26. }
  27.  
  28. class LoggingSessionPool {
  29.     function LoggingSessionPool(&$session_pool&$log{
  30.         ...
  31.     }
  32.     
  33.     function &findSession($cookie{
  34.         ...
  35.     }
  36.     ...
  37. }
Out of all of this, the only class we want to test here is the LoggingSessionPool. In particular we would like to check that the findSession() method is called with the correct session ID in the cookie and that it sent the message "Starting session $cookie" to the logger.

Despite the fact that we are testing only a few lines of production code, here is what we would have to do in a conventional test case: Create a log object. Set a directory to place the log file. Set the directory permissions so we can write the log. Create a SessionPool object. Hand start a session, which probably does lot's of things. Invoke findSession(). Read the new Session ID (hope there is an accessor!). Raise a test assertion to confirm that the ID matches the cookie. Read the last line of the log file. Pattern match out the extra logging timestamps, etc. Assert that the session message is contained in the text. It is hardly surprising that developers hate writing tests when they are this much drudgery. To make things worse, every time the logging format changes or the method of creating new sessions changes, we have to rewrite parts of this test even though this test does not officially test those parts of the system. We are creating headaches for the writers of these other classes.

Instead, here is the complete test method using mock object magic...

  1. Mock::generate('Session');
  2. Mock::generate('SessionPool');
  3. Mock::generate('Log');
  4.  
  5. class LoggingSessionPoolTest extends UnitTestCase {
  6.     ...
  7.     function testFindSessionLogging({
  8.         $session &new MockSession($this);
  9.         $pool &new MockSessionPool($this);
  10.         $pool->setReturnReference('findSession'$session);
  11.         $pool->expectOnce('findSession'array('abc'));
  12.         
  13.         $log &new MockLog($this);
  14.         $log->expectOnce('message'array('Starting session abc'));
  15.         
  16.         $logging_pool &new LoggingSessionPool($pool$log);
  17.         $this->assertReference($logging_pool->findSession('abc')$session);
  18.         $pool->tally();
  19.         $log->tally();
  20.     }
  21. }
We start by creating a dummy session. We don't have to be too fussy about this as the check for which session we want is done elsewhere. We only need to check that it was the same one that came from the session pool.

findSession() is a factory method the simulation of which is described stub. The point of departure comes with the first expectOnce() call. This line states that whenever findSession() is invoked on the mock, it will test the incoming arguments. If it receives the single argument of a string "abc" then a test pass is sent to the unit tester, otherwise a fail is generated. This was the part where we checked that the right session was asked for. The argument list follows the same format as the one for setting return values. You can have wildcards and sequences and the order of evaluation is the same.

If the call is never made then neither a pass nor a failure will generated. To get around this we must tell the mock when the test is over so that the object can decide if the expectation has been met. The unit tester assertion for this is triggered by the tally() call at the end of the test.

We use the same pattern to set up the mock logger. We tell it that it should have message() invoked once only with the argument "Starting session abc". By testing the calling arguments, rather than the logger output, we insulate the test from any display changes in the logger.

We start to run our tests when we create the new LoggingSessionPool and feed it our preset mock objects. Everything is now under our control. Finally we confirm that the $session we gave our decorator is the one that we get back and tell the mocks to run their internal call count tests with the tally() calls.

This is still quite a bit of test code, but the code is very strict. If it still seems rather daunting there is a lot less of it than if we tried this without mocks and this particular test, interactions rather than output, is always more work to set up. More often you will be testing more complex situations without needing this level or precision. Also some of this can be refactored into a test case setUp() method.

Here is the full list of expectations you can set on a mock object in SimpleTest...

Expectation Needs tally()
expectArguments($method, $args) No
expectArgumentsAt($timing, $method, $args) No
expectCallCount($method, $count) Yes
expectMaximumCallCount($method, $count) No
expectMinimumCallCount($method, $count) Yes
expectNever($method) No
expectOnce($method, $args) Yes
expectAtLeastOnce($method, $args) Yes
Where the parameters are... $method The method name, as a string, to apply the condition to. $args The arguments as a list. Wildcards can be included in the same manner as for setReturn(). This argument is optional for expectOnce() and expectAtLeastOnce(). $timing The only point in time to test the condition. The first call starts at zero. $count The number of calls expected. The method expectMaximumCallCount() is slightly different in that it will only ever generate a failure. It is silent if the limit is never reached.

Like the assertions within test cases, all of the expectations can take a message override as an extra parameter. Also the original failure message can be embedded in the output as "%s".

Other approaches

There are three approaches to creating mocks including the one that SimpleTest employs. Coding them by hand using a base class, generating them to a file and dynamically generating them on the fly.

Mock objects generated with SimpleTest.html are dynamic. They are created at run time in memory, using eval(), rather than written out to a file. This makes the mocks easy to create, a one liner, especially compared with hand crafting them in a parallel class hierarchy. The problem is that the behaviour is usually set up in the tests themselves. If the original objects change the mock versions that the tests rely on can get out of sync. This can happen with the parallel hierarchy approach as well, but is far more quickly detected.

The solution, of course, is to add some real integration tests. You don't need very many and the convenience gained from the mocks more than outweighs the small amount of extra testing. You cannot trust code that was only tested with mocks.

If you are still determined to build static libraries of mocks because you want to simulate very specific behaviour, you can achieve the same effect using the SimpleTest class generator. In your library file, say mocks/connection.php for a database connection, create a mock and inherit to override special methods or add presets...

  1. <?php
  2.     require_once('simpletest/mock_objects.php');
  3.     require_once('../classes/connection.php');
  4.  
  5.     Mock::generate('Connection''BasicMockConnection');
  6.     class MockConnection extends BasicMockConnection {
  7.         function MockConnection(&$test$wildcard '*'{
  8.             $this->BasicMockConnection($test$wildcard);
  9.             $this->setReturn('query'false);
  10.         }}
  11. ?>
The generate call tells the class generator to create a class called BasicMockConnection rather than the usual MockConnection. We then inherit from this to get our version of MockConnection. By intercepting in this way we can add behaviour, here setting the default value of query() to be false. By using the default name we make sure that the mock class generator will not recreate a different one when invoked elsewhere in the tests. It never creates a class if it already exists. As long as the above file is included first then all tests that generated MockConnection should now be using our one instead. If we don't get the order right and the mock library creates one first then the class creation will simply fail.

Use this trick if you find you have a lot of common mock behaviour or you are getting frequent integration problems at later stages of testing.

I think SimpleTest stinks!

But at the time of writing it is the only one with mock objects, so are you stuck with it?

No, not at all. SimpleTest.html is a toolkit and one of those tools is the mock objects which can be employed independently. Suppose you have your own favourite unit tester and all your current test cases are written using it. Pretend that you have called your unit tester PHPUnit (everyone else has) and the core test class looks like this...

  1. class PHPUnit {
  2.     function PHPUnit({
  3.     }
  4.     
  5.     function assertion($message$assertion{
  6.     }
  7.     ...
  8. }
All the assertion() method does is print some fancy output and the boolean assertion parameter determines whether to print a pass or a failure. Let's say that it is used like this...
  1. $unit_test new PHPUnit();
  2. $unit_test>assertion('I hope this file exists'file_exists('my_file'));
How do you use mocks with this?

There is a protected method on the base mock class SimpleMock called _assertTrue() and by overriding this method we can use our own assertion format. We start with a subclass, in say my_mock.php...

  1. <?php
  2.     require_once('simpletest/mock_objects.php');
  3.     
  4.     class MyMock extends SimpleMock({
  5.         function MyMock(&$test$wildcard{
  6.             $this->SimpleMock($test$wildcard);
  7.         }
  8.         
  9.         function _assertTrue($assertion$message{
  10.             $test &$this->getTest();
  11.             $test->assertion($message$assertion);
  12.         }}
  13. ?>
Now instantiating MyMock will create an object that speaks the same language as your tester. The catch is of course that we never create such an object, the code generator does. We need just one more line of code to tell the generator to use your mock instead...
  1. <?php
  2.     require_once('simpletst/mock_objects.php');
  3.     
  4.     class MyMock extends SimpleMock({
  5.         function MyMock($test$wildcard{
  6.             $this->SimpleMock(&$test$wildcard);
  7.         }
  8.         
  9.         function _assertTrue($assertion$message &$test{
  10.             $test->assertion($message$assertion);
  11.         }}
  12.     SimpleTestOptions::setMockBaseClass('MyMock');
  13. ?>
From now on you just include my_mock.php instead of the default mock_objects.php version and you can introduce mock objects into your existing test suite.

Previous Next
Drag & Drop

Documentation generated on Tue, 23 Feb 2010 10:23:56 +0100 by phpDocumentor 1.4.3