I have read in the PHPUnit Manual that with the following example, the method call doSomething('a','b','c')
will return d
and the method call doSomething('e','f','g')
will return h
.
<?php
require_once 'SomeClass.php';
class StubTest extends PHPUnit_Framework_TestCase
{
public function testReturnValueMapStub()
{
// Create a stub for the SomeClass class.
$stub = $this->getMockBuilder('SomeClass')
->getMock();
// Create a map of arguments to return values.
$map = array(
array('a', 'b', 'c', 'd'),
array('e', 'f', 'g', 'h')
);
// Configure the stub.
$stub->method('doSomething')
->will($this->returnValueMap($map));
// $stub->doSomething() returns different values depending on
// the provided arguments.
$this->assertEquals('d', $stub->doSomething('a', 'b', 'c'));
$this->assertEquals('h', $stub->doSomething('e', 'f', 'g'));
}
}
?>
Is there also a way to define such a return value map, but with a default return value when the particular input arguments do not have a specific return value?
You can use returnCallback instead of returnValueMap, and reproduce what the value map is doing: