Skip to content
This repository

returnValueMap() issue #89

Closed
mixegen opened this Issue · 1 comment

2 participants

mixegen Volker Dusch
mixegen

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;
Volker Dusch
Collaborator

I can't really reproduce this:

<?php


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->expects($this->any())
             ->method('doSomething')
             ->will($this->returnValueMap($map));

        // $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 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.