2
votes

Let me start by saying I'm new to writing tests so I may have my tools and/or testing concepts mixed up.

I've build out an api using Laravel 5.2. I've written tests in ./tests extending TestCase to cover nearly all elements of the api requests and responses.

Moving on to some of the functionality of the api I needed to make GET requests using query parameters. I found this wasn't easy or possible to do using Laravel's $this->call('GET',$url) method so I added Guzzle to accomplish this.

Great....everything works when I run one set of tests at a time.

But, when I run the entire test sequence for the api I get a TOO MANY CONNECTIONS error stemming from the number of HTTP requests triggered by the tests using Guzzle. To address this I tried to use Guzzle's Async Requests feature.

The issue now is that the PHPUnit is completing all the tests but the $promise()->then() is never executed.

Any suggestions?

public function testGet()
{

    $promise = $this->client->requestAsync('GET','clients');

    $promise->then(
        function (ResponseInterface $response) {
            $data = json_decode($response->getBody());

            // this never get called
            print_r($data);

        }
    );

    $promise->wait();
}
1
so far I've isolated this to Guzzle. literally copying and pasting from their examples I've been unable to replicate the functionality. $promise->then() is simply never firingBradley

1 Answers

1
votes

Check this issue, This is intentional in order to prevent recursion. You will need to call wait on a promise and tick the promise queue manually

This works for me (with Lumen)

use GuzzleHttp\Promise\FulfilledPromise;

class FulfilledPromiseTest extends TestCase
{
  public function testResolveCallback()
  {
    // Instance objects first
    $console = Mockery::mock('console');
    $promise = new FulfilledPromise('success');

    // Configure expectations
    $console->shouldReceive('log')->once()->with('success');

    // Execute test
    $p = $promise->then(function($response) use($console) {
      $console->log($response);
    });

    //  Tick the promise queue to trigger the callback
    $p->wait();
    \GuzzleHttp\Promise\queue();
  }
}