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.

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

报告相同问题?

悬赏问题

  • ¥50 关于多次提交POST数据后,无法获取到POST数据参数的问题
  • ¥15 win10,这种情况怎么办
  • ¥15 如何在配置使用Prettier的VSCode中通过Better Align插件来对齐等式?(相关搜索:格式化)
  • ¥100 在连接内网VPN时,如何同时保持互联网连接
  • ¥15 MATLAB中使用parfor,矩阵Removal的有效索引在parfor循环中受限制
  • ¥20 Win 10 LTSC 1809版本如何无损提升到20H1版本
  • ¥50 win10 LTSC 虚拟键盘不弹出
  • ¥30 微信小程序请求失败,网页能正常带锁访问
  • ¥30 德飞莱51单片机实现C4炸弹
  • ¥50 CrossLink-LIF-MD6000 型 FPGA 的 CMOS 转 MIPI D-PHY IP 核功能使用异常