duannai5879 2018-12-03 20:08
浏览 43
已采纳

如何在PHPUnit中循环测试单元?

I have a Service class and a test for that, follow below:

Class

class MyCustomService
{
    public function job()
    {
       while($this->getResponseFromThirdPartyApi()->data) {
            // do some stuff...
       }    
       return ...
    }

    protected function getResponseFromThirdPartyApi()
    {
        // Here do some curl and return stdClass
        // data attribute is populated only on first curl request
    }
}

Test mocking getResponseFromThirdPartyApi method

class MyCustomServiceTest
{
    public function testJobImportingData()
    {
        $myCustomServiceMock = $this->getMockBuilder('MyCustomService')
        ->setMethods(array('getResponseFromThirdPartyApi'))
        ->getMock();

        $myCustomServiceMock->expects($this->any())
            ->method('getResponseFromThirdPartyApi')
            ->willReturn($this->getResponseWithData());

        $jobResult = $myCustomServiceMock->job();

        // here some assertions on $jobResult
    }

    protected function getResponseWithData()
    {
        $response = new \stdClass;
        $response->data = ['foo', 'bar'];

        return $response;
    }
}

How can I change getResponseWithData return after first call on MyCustomService while loop?

I've tried creating a custom flag on MyCustomServiceTest and checking on getResponseWithData, but fails once that mocked object doesn't call getResponseWithData method again on MyCustomServiceTest.

Any direction?

  • 写回答

1条回答 默认 最新

  • 普通网友 2018-12-04 19:28
    关注

    As Nico Haase suggested above, the path is to use callback.

    After some research I achieved passing mock object reference to method mocked and checking flag, this results on:

    class MyCustomServiceTest
    {
        public function testJobImportingData()
        {
            $myCustomServiceMock = $this->getMockBuilder('MyCustomService')
            ->setMethods(array('getResponseFromThirdPartyApi'))
            ->getMock();
    
            $myCustomServiceMock->expects($this->any())
                ->method('getResponseFromThirdPartyApi')
                ->will($this->returnCallback(
                    function () use ($myCustomServiceMock) {
                        return $this->getResponseWithData($myCustomServiceMock)
                    }
                ));
    
            $jobResult = $myCustomServiceMock->job();
    
            // here some assertions on $jobResult
        }
    
        protected function getResponseWithData(&$myCustomServiceMock)
        {
            $response = new \stdClass;
    
            if (isset($myCustomServiceMock->imported)) {
                $response->data = false;
                return $response;
            }
    
            $myCustomServiceMock->imported = true;
    
            $response = new \stdClass;
            $response->data = ['foo', 'bar'];
    
            return $response;
        }
    }
    

    Then while loop will be called only once and we be able to test without forever loop.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作