У меня есть класс обслуживания и тест для этого, следуйте ниже:
Класс
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
}
}
Проверка метода насмешки getResponseFromThirdPartyApi
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;
}
}
Как я могу изменить возвращение getResponseWithData после первого вызова MyCustomService в цикле while?
Я попытался создать пользовательский флаг на MyCustomServiceTest и проверить getResponseWithData, но не удалось, как только смоделированный объект не выполняется.не вызывать метод getResponseWithData снова в MyCustomServiceTest.
В любом направлении?