Misled by PHPUnit at() method

August 29, 2015 6 By addshore

So it turns out the at() method doesn’t quite do what I had initially thought….

I have recently been working on some tests for the new Newsletter extension for Mediawiki, specifically to test the NewslettterTablePager class. This thing extends the TablePager class in Mediawiki which is designed to make displaying information from a database table on a special page on a MediaWiki site easy, and also easily enable things such as sorting.

The code interacts with the database and gets a ResultWrapper object, and the Pager uses the numRows(), seek() and fetchObject() methods, all of which I thought would be incredibly simple to mock.

Attempt 1

My first attempt where I first notice I have been thinking about the at() method all wrong can be seen below:

private function getMockDatabase( array $resultObjects ) {
    $mockResult = $this->getMock( 'ResultWrapper' );
    $mockResult->expects( $this->atLeastOnce() )
        ->method( 'numRows' )
        ->will( $this->returnValue( count( $resultObjects ) ) );
    $mockResult->expects( $this->any() )
        ->method( 'seek' );
    foreach ( $resultObjects as $index => $resultObject ) {
        $mockResult->expects( $this->at( $index ) )
            ->method( 'fetchObject' )
            ->will( $this->returnValue( $resultObject ) );
    $mockDb = $this->getMock( 'IDatabase' );
    $mockDb->expects( $this->atLeastOnce() )
        ->method( 'select' )
        ->will( $this->returnValue( $mockResult ) );
    return $mockDb;
Code language: PHP (php)

This method returns a mock Database that the Pager will use. As you can see the only parameter is an array of objects to be returned by fetchObject() and I am using the at() method provided by PHPUnit to return each object at the index that it is stored in the array. This is when I discovered that at() in PHPUnit does not work in the way I first thought…

at() refers to the index of calls made to the mocked object as a whole. This means that in the code sample above, all of the calls to numRows() and seek() are increasing the current call counter index for the object and thus my mocked fetchObject() method is never returning the correct value or returning null.

Attempt 2

In my second attempt, I made a guess that PHPUnit might allow multiple method mocks to stack and thus the return values of those methods be returned in the order that they were created. Thus I changed my loop to simply use any():

foreach ( $resultObjects as $index => $resultObject ) {
    $mockResult->expects( $this->any() )
        ->method( 'fetchObject' )
        ->will( $this->returnValue( $resultObject ) );
}Code language: PHP (php)

But of course, this also does not work and this result in the same $resultObject being returned for all calls.

Final version

I ended up having to do something a little bit nasty (in my opinion) and use returnCallback() and use a private member of the test case within the callback as a call counter / per method index:

$testcase = $this;
$mockResult->expects( $this->any() )
    ->method( 'fetchObject' )
    ->will( $this->returnCallback( function () use ( $testCase, $resultObjects ) {
        $obj = $resultObjects[$testCase->mockSeekCounter];
        $testCase->mockSeekCounter =+ 1;
        return $obj;
} ) );Code language: PHP (php)


It would be great if PHPUnit would have some form of per method index expectation!

Some of the code examples here are slightly cut down, the full change can be seen at https://gerrit.wikimedia.org/r/#/c/234727/ in the NewsletterTablePagerTest file.

If only I had Googled the issue sooner! http://www.andrejfarkas.com/2012/07/phpunit-at-method-to-check-method-invocation-at-certain-index/