returnValueMap() issue #89

mixegen opened this Issue Apr 20, 2012 · 1 comment

2 participants


I have little problem with mock object. I need the mock to be able to return different values according to the argument given to the method. Documentation says that we should you returnValueMap() for this purpose. So I try it and found next issue:

  • when we build map with more than two elements, for example:
    $map = array( array('a', 'b', 'c') )
    method returns 'c' as we expecting;
  • when we try to build map with only two elements (for example I want to call method with 'a' and get 'c' as result):
    $map = array( array('a', 'c') )
    after method's call we got NULL;

I can't really reproduce this:


class SomeClass {

    public function doSomething() {

class StubTest extends PHPUnit_Framework_TestCase
    public function testReturnValueMapStub()
        // Create a stub for the SomeClass class.
        $stub = $this->getMock('SomeClass');

        // Create a map of arguments to return values.
        $map = array(
          array('a', 'd'),
          array('e', 'h')

        // Configure the stub.

        // $stub->doSomething() returns different values depending on
        // the provided arguments.
        $this->assertEquals('d', $stub->doSomething('a'));
        $this->assertEquals('h', $stub->doSomething('e'));

Maybe your method has more than one parameter? then you'd need to specify all them as returnValueMap doesn't take default values into account.

If you have further problems please note that this is just the bugtracker and we (saidly) don't have time to help out everyone here.

You can ask usage questions in the #phpunit channel on freenode or on Stackoverflow :)

But this seems to be a documentation issue so let me know if my answer helped or not, maybe a small doc fix can help out others too :)

@edorian edorian closed this Jan 30, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment