0

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?

Diogo Alves
  • 378
  • 2
  • 13
  • Have you tried returning a callback instead? Have a look at https://stackoverflow.com/questions/277914/how-can-i-get-phpunit-mockobjects-to-return-different-values-based-on-a-paramete – Nico Haase Dec 03 '18 at 20:11
  • exract `ThirdPartyApi` class (it should be injected into `MyCustomService`), mock for tests as you wish. – xmike Dec 04 '18 at 08:27
  • NicoHaase gives me the path... @xmike nice to decouple, but on original service class its all in one. – Diogo Alves Dec 04 '18 at 19:21

1 Answers1

0

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.

Diogo Alves
  • 378
  • 2
  • 13