title: How to unit test while loop in PHPUnit?
body: <p>I have a Service class and a test for that, follow below:</p>
  
  <p><strong>Class</strong></p>
  
  <pre><code>class MyCustomService
  {
      public function job()
      {
         while($this-&gt;getResponseFromThirdPartyApi()-&gt;data) {
              // do some stuff...
         }    
         return ...
      }
  
      protected function getResponseFromThirdPartyApi()
      {
          // Here do some curl and return stdClass
          // data attribute is populated only on first curl request
      }
  }
  </code></pre>
  
  <p><strong>Test mocking getResponseFromThirdPartyApi method</strong></p>
  
  <pre><code>class MyCustomServiceTest
  {
      public function testJobImportingData()
      {
          $myCustomServiceMock = $this-&gt;getMockBuilder('MyCustomService')
          -&gt;setMethods(array('getResponseFromThirdPartyApi'))
          -&gt;getMock();
  
          $myCustomServiceMock-&gt;expects($this-&gt;any())
              -&gt;method('getResponseFromThirdPartyApi')
              -&gt;willReturn($this-&gt;getResponseWithData());
  
          $jobResult = $myCustomServiceMock-&gt;job();
  
          // here some assertions on $jobResult
      }
  
      protected function getResponseWithData()
      {
          $response = new \stdClass;
          $response-&gt;data = ['foo', 'bar'];
  
          return $response;
      }
  }
  </code></pre>
  
  <p>How can I change getResponseWithData return after first call on MyCustomService while loop?</p>
  
  <p>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.</p>
  
  <p>Any direction?</p>