PHPUnit offers returnValueMap()
for cases where the input parameters must trigger a specific return value.
// Create a map of arguments to return values. $map = array( array(0, 'SearchResult0'), array(1, 'SearchResult1') ); // Configure the stub. $searchResults->expects($this->any()) ->method('getItem') ->will($this->returnValueMap($map)); $this->assertEquals('SearchResult0', $searchResults->getItem(0)); $this->assertEquals('SearchResult1', $searchResults->getItem(1)); $this->assertEquals('SearchResult0', $searchResults->getItem(0));
At first, the map looks a little strange, because there is no direct assignment of the key-> value, but this is because this map also works with several input parameters for the scoffing method.
$mockedAddition = array( array(0, 0, 0), array(0, 1, 1), array(1, 0, 1), array(1, 1, 2), ); $calculator->expects($this->any()) ->method('add') ->will($this->returnValueMap($mockedAddition); $this->assertSame(0, $calculator->add(0, 0));
source share