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

如何在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条回答 默认 最新

  • dszr18929 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.

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题