Ben Dowling Ben Dowling - 1 year ago 107
PHP Question

How can I get PHPUnit MockObjects to return different values based on a parameter?

I've got a PHPUnit mock object that returns

'return value'
no matter what its arguments:

// From inside a test...
$mock = $this->getMock('myObject', 'methodToMock');
->will($this->returnValue('return value'));

What I want to be able to do is return a different value based on the arguments passed to the mock method. I've tried something like:

$mock = $this->getMock('myObject', 'methodToMock');

// methodToMock('one')
->will($this->returnValue('method called with argument "one"'));

// methodToMock('two')
->will($this->returnValue('method called with argument "two"'));

But this causes PHPUnit to complain if the mock isn't called with the argument
, so I assume that the definition of
overwrites the definition of the first.

So my question is: Is there any way to get a PHPUnit mock object to return a different value based on its arguments? And if so, how?

Answer Source

Use a callback. e.g. (straight from PHPUnit documentation):

class StubTest extends PHPUnit_Framework_TestCase
    public function testReturnCallbackStub()
        $stub = $this->getMock(
          'SomeClass', array('doSomething')


        // $stub->doSomething() returns callback(...)

function callback() {
    $args = func_get_args();
    // ...

Do whatever processing you want in the callback() and return the result based on your $args as appropriate.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download