Phpunit: returnValueMap only works with at least 3 input parameters

Created on 11 Nov 2011  路  5Comments  路  Source: sebastianbergmann/phpunit

I'm trying to use returnValueMap as described here:
http://www.phpunit.de/manual/3.6/en/test-doubles.html#test-doubles.stubs.examples.StubTest5.php

It seems that it only works when using 4 elements in the array's
when trying like this:

    $map = array(
        array('foo', 'bar'),
    );

    $myClass= $this->getMock('myClass');
    $myClass->expects($this->any())
        ->method('myMethod')
        ->will($this->returnValueMap($map));

It doesn't work...
a dump of $invocation->parameters in method 'invoke' of class PHPUnit_Framework_MockObject_Stub_ReturnValueMap
comes up with

    array(3) {
      [0]=>
      string(11) "foo"
      [1]=>
      NULL
      [2]=>
      bool(false)
    }

why are there NULL and false added?

Most helpful comment

HA! This has been getting me for a while. I've wanted to use returnValueMap for a while, but have been avoiding because I could never figure out how to make it return the correct value. The optional parameters of methods I didn't write were the pitfall. I wish the documentation pointed that out, like "NOTE Remember to check the signature of the method you're stubbing and don't forget to fill in optional parameters."

Better documentation would have helped me.

All 5 comments

<?php

class mockingTest extends PHPUnit_Framework_TestCase {

    public function testMocking() {
        $mock = $this->getMock('MockMe');
        $mock->expects($this->once())
            ->method('stuff')
            ->will($this->returnValueMap(
                array(array('foo', 'bar'))
            )); 

        $this->assertSame('bar', $mock->stuff('foo'));
    }   

}

class MockMe {

    public function stuff($a) {
        die('I said mock me!');
    }   

}

This works out fine with only 2 parameters in the array. Since you didn't show how you call the mock i assume there is an issue with that part :)

Feel free to reopen this if you have reproduce case that says otherwhise :)

ah, damn.... I鈥檓 trying to Mock the Symfony2 Request-Object and it鈥檚 get-Method
signature of get looks like this:
public function get($key, $default = null, $deep = false)

I wasn鈥檛 aware of the fact that I need to deliver all parameters - even if they are optional
Thank you

As the invocation parameters are your "foo" then null then false it seems like it figures it out to some extend. Maybe this could be changed. I'll take a look

HA! This has been getting me for a while. I've wanted to use returnValueMap for a while, but have been avoiding because I could never figure out how to make it return the correct value. The optional parameters of methods I didn't write were the pitfall. I wish the documentation pointed that out, like "NOTE Remember to check the signature of the method you're stubbing and don't forget to fill in optional parameters."

Better documentation would have helped me.

But what about more than 3 arguments in a mocking method? I need pass 4 or 5 arguments in a method, for example. What should I do with such situation?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sebastianbergmann picture sebastianbergmann  路  4Comments

stemis picture stemis  路  3Comments

AnmSaiful picture AnmSaiful  路  4Comments

TiMESPLiNTER picture TiMESPLiNTER  路  3Comments

stephen-leavitt-sonyatv-com picture stephen-leavitt-sonyatv-com  路  4Comments