PHPUnit: how to mock multiple method calls with multiple arguments and without direct order?

I need to check the following function:

[...]
public function createService(ServiceLocatorInterface $serviceManager)
{

    $firstService = $serviceManager->get('FirstServiceKey');
    $secondService = $serviceManager->get('SecondServiceKey');

    return new SnazzyService($firstService, $secondService);
}
[...]

I know, I can check it like this:

class MyTest extends \PHPUnit_Framework_TestCase
{
    public function testReturnValue()
    {
        $firstServiceMock = $this->createMock(FirstServiceInterface::class);
        $secondServiceMock = $this->createMock(SecondServiceInterface::class);

        $serviceManagerMock = $this->createMock(ServiceLocatorInterface::class);

        $serviceManagerMock->expects($this->at(0))
             ->method('get')
             ->with('FirstServiceKey')
             ->will($this->returnValue($firstService));

        $serviceManagerMock->expects($this->at(1))
             ->method('get')
             ->with('SecondServiceKey')
             ->will($this->returnValue($secondServiceMock));

        $serviceFactory = new ServiceFactory($serviceManagerMock);

        $result = $serviceFactory->createService();
    }
    [...]

or

[...]
public function testReturnValue()
{
    $firstServiceMock = $this->createMock(FirstServiceInterface::class);
    $secondServiceMock = $this->createMock(SecondServiceInterface::class);

    $serviceManagerMock = $this->createMock(ServiceLocatorInterface::class);

    $serviceManagerMock->expects($this->any())
         ->method('get')
         ->withConsecutive(
            ['FirstServiceKey'],
            ['SecondServiceKey'],
         )
         ->willReturnOnConsecutiveCalls(
            $this->returnValue($firstService),
            $this->returnValue($secondServiceMock)
         );

    $serviceFactory = new ServiceFactory($serviceManagerMock);

    $result = $serviceFactory->createService();
}
[...]

Both work fine, but if I change the lines -> get (xxx) in the createService function, both tests fail. So, how can I change the test tags that do not need specific sequencers for the parameters "FirstServiceKey", "SecondServiceKey, ...

+4
source share
2 answers

You can try using a strategy willReturnCallbackor willReturnMap, as an example willReturnCallback:

public function testReturnValue()
{
    $firstServiceMock = $this->createMock(FirstServiceInterface::class);
    $secondServiceMock = $this->createMock(SecondServiceInterface::class);

    $serviceManagerMock = $this->createMock(ServiceLocatorInterface::class);


    $serviceManagerMock->expects($this->any())
        ->method('get')
        ->willReturnCallback(
            function ($key) use($firstServiceMock, $secondServiceMock) {
                if ($key == 'FirstServiceKey') {
                    return $firstServiceMock;
                }
                if ($key == 'SecondServiceKey') {
                    return $secondServiceMock;
                }
                throw new \InvalidArgumentException; // or simply return;
            }
        );

    $serviceFactory = new ServiceFactory($serviceManagerMock);

    $result = $serviceFactory->createService();
}

Hope for this help

+4
source

- , : ""

public function testReturnValue()
{
    $this->container = $this->prophesize(ServiceLocatorInterface::class);
    $firstService = $this->prophesize(FirstServiceInterface::class);
    $secondService = $this->prophesize(SecondServiceKey::class);

    $this->container->get('FirstServiceKey')->willReturn(firstService);
    // And if you like
    $this->container->get('FirstServiceKey')->shouldBeCalled();

    $this->container->get('SecondServiceKey')->willReturn(secondService);
    [...]
    $serviceFactory = new ServiceFactory();

    /*
     * Little error in my example. ServiceLocatorInterface is parameter of 
     * createService() and not the constructor ;) (ZF2/3)
     */

    $result = $serviceFactory->createService(
        $this->container->reveal()
    );
    [...]
}   

, "$ serviceManager- > get ([...]); \o/

0

Source: https://habr.com/ru/post/1658201/


All Articles