PHPUnit:如何使用多个参数模拟多个方法调用而没有直接顺序?

时间:2016-10-19 12:02:10

标签: unit-testing zend-framework2 phpunit

我需要测试以下功能:

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

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

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

我知道,我可能会这样测试:

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();
    }
    [...]

[...]
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();
}
[...]

两者都运行正常,但如果我在createService函数中交换 - > get(xxx)行,则两个测试都将失败。 那么,我如何更改参数'FirstServiceKey','SecondServiceKey,...

不需要特定序列的测试用例

2 个答案:

答案 0 :(得分:4)

您可以尝试使用willReturnCallbackwillReturnMap策略,例如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();
}

希望这个帮助

答案 1 :(得分:0)

已经有一段时间了,所以有一个新方法可以解决这个问题:改为先知'

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 /