如何在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?

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.