PHPUnit stubbing: default return value from map

I read in the PHPUnit manual that in the following example, a method call doSomething('a','b','c')

will return d

and a 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 a way to define such a return value map, but with a default return value when specific input arguments don't have a specific return value?

+3


source to share


2 answers


You can use returnCallback instead of returnValueMap and reproduce what the value map does:



<?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' ) );
    }
}

      

+3


source


 /**
 * @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

}

      

REGULATIONS



  • The $map

    firts argument array argument should be the test method argument, and the rest (in this case $f

    ) is the likelihood that it will be able to return the tested method.
  • each array()

    inside a variable $map

    must try to diff the calls:

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

Also, the method exactly()

must be the number of calls.

0


source







All Articles