PHPUnit stubbing: default return value from map

1.9k views Asked by At

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?

2

There are 2 answers

0
Fraser On

You can use returnCallback instead of returnValueMap, and reproduce what the value map is doing:

<?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.
        $valueMap = array(
            array( 'a', 'b', 'c', 'd' ),
            array( 'e', 'f', 'g', 'h' )
        );

        $default = 'l';

        // Configure the stub.
        $stub->method( 'doSomething' )
            ->will( $this->returnCallback( function () use ( $valueMap, $default )
            {
                $arguments      = func_get_args();
                $parameterCount = count( $arguments );

                foreach( $valueMap as $map )
                {
                    if( !is_array( $map ) || $parameterCount != count( $map ) - 1 )
                    {
                        continue;
                    }

                    $return = array_pop( $map );
                    if( $arguments === $map )
                    {
                        return $return;
                    }
                }

                return $default;
            } ) );


        // $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' ) );
        $this->assertEquals( 'l', $stub->doSomething( 'i', 'j', 'k' ) );
        $this->assertEquals( 'l', $stub->doSomething( 'any', 'arguments', 'at', 'all' ) );
    }
}
0
jjoselon On
 /**
 * @test
 */
public function testReturnValueMapStub()
{
    $f = new Response;
    $r = new Request;
    $s = 'lol';

    $project = $this->getMockBuilder('AppBundle\Controller\ProjectController')
                 ->getMock();

    // Create a map of arguments to return values.
    $map = array(
        array($r,$f),
        array($r,$s)
    );

    $project->expects($this->exactly(2))
            ->method('getProjects')
            ->will($this->returnValueMap($map));

    $this->assertEquals("$f",$project->getProjects($r));
    $this->assertEquals("$s",$project->getProjects($r)); // will fail the assertion

}

RULES

  1. The firts array's argument of $map variable must be the test method's argument, and the rest (in this case $f) are the possibilitys expects that could return the method tested.
  2. every array() within $map variable must be tries on calls differents:

    // firts $map's element variable $this->assertEquals("$f",$project->getSupervisorfromUnits($r)); // second $map's element variable $this->assertEquals("$s",$project->getSupervisorfromUnits($r));

Also, the exactly() method must be the number of calls.