views:

86

answers:

1

I'm using PHPUnit but find it difficult to make it create good mocks and stubs for objects used as datastore.

Example:

class urlDisplayer {
    private $storage;
    public function __construct(IUrlStorage $storage) { $this->storage = $storage; }
    public function displayUrl($name) {}
    public function displayLatestUrls($count) {}
}

interface IUrlStorage {
    public function addUrl($name, $url);
    public function getUrl($name);
}

class MysqlUrlStorage implements IUrlStorage {
    // saves and retrieves from database
}

class NonPersistentStorage implements IUrlStorage {
    // just stores for this request
}

Eg how to have PHPUnit stubs returning more than one possible value on two calls with different $names?

Edit: example test:

public function testUrlDisplayerDisplaysLatestUrls {
    // get mock storage and have it return latest x urls so I can test whether
    // UrlDisplayer really shows the latest x
}

In this test the mock should return a number of urls, however in the documentation I only how to return one value.

+1  A: 

Your question is not very clear - but I assume you are asking how to use phpunit's mock objects to return a different value in different situations?

PHPUnit's mock classes allow you specify a custom function (ie: a callback function/method) - which is practically unlimited in what it can do.

In the below example, I created a mock IUrlStorage class that will return the next url in its storage each time it is called.

public function setUp()
{
    parent::setUp();
    $this->fixture = new UrlDisplayer(); //change this to however you create your object

    //Create a list of expected URLs for testing across all test cases
    $this->expectedUrls = array(
          'key1' => 'http://www.example.com/url1/'
        , 'key2' => 'http://www.example.net/url2/'
        , 'key3' => 'http://www.example.com/url3/'
    );
}

public function testUrlDisplayerDisplaysLatestUrls {
    //Init        
    $mockStorage = $this->getMock('IUrlStorage');
    $mockStorage->expects($this->any())
        ->method('getUrl')
        ->will( $this->returnCallback(array($this, 'mockgetUrl')) );

    reset($this->expectedUrls); //reset array before testing

    //Actual Tests
    $this->assertGreaterThan(0, count($this->expectedUrls));
    foreach ( $this->expectedUrls as $key => $expected ) {
        $actual = $this->fixture->displayUrl($key);
        $this->assertEquals($expected, $actual);
    }
}

public function mockGetUrl($name)
{
    $value = current($this->expectedUrls);
    next($this->expectedUrls);

    //Return null instead of false when end of array is reached
    return ($value === false) ? null : $value;
}


Alternatively, sometimes it is easier to simply create a real class that mocks up the necessary functionality. This is especially easy with well defined and small interfaces.

In this specific case, I would suggest using the below class instead:

class MockStorage implements IUrlStorage
{
    protected $urls = array();

    public function addUrl($name, $url)
    {
        $this->urls[$name] = $url;
    }

    public function getUrl($name)
    {
        if ( isset($this->urls[$name]) ) {
            return $this->urls[$name];
        }
        return null;
    }
}
?>

Then in your unit test class you simply instantiate your fixture like below:

public function setUp() {
   $mockStorage = new MockStorage();

   //Add as many expected URLs you want to test for
   $mockStorage->addUrl('name1', 'http://example.com');
   //etc...

   $this->fixture = new UrlDisplayer($mockStorage);
}
catchdave