Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions app/Testing/GuzzleTapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,24 +105,22 @@ public function replaceMatchFile(string $method, string $urlPattern, string $fil

/**
* Given a RequestInterface (from a unit test trying to make a Guzzle request)
* Find the first appropriate match (NOT best-match!) in $this->matches
* - Log that the request happened (so we can assert later what requests happened in what order)
* - Even if the request is not matched, it will be logged so you can have confidence in your tests
* - Find the first appropriate match (NOT best-match!) in $this->matches
* - Run and return the behavior described in the match
* @returns Response a Guzzle response object, including status, headers, body
* @returns Exception if the behavior *returns* an exception, Guzzle will throw it, e.g. a BadResponseException
* @throws OutOfBoundsException if the RequestInterface does not match any $this->matches
*/
public function response(RequestInterface $request, array $options = [])
{
$requestMethod = $request->getMethod();
if (!array_key_exists($requestMethod, $this->matches)) {
return new \OutOfBoundsException("No responses match method {$requestMethod}");
Comment thread
jwadhams marked this conversation as resolved.
}
$this->calls->push($request);

$requestMethod = $request->getMethod();
$requestUrl = (string) $request->getUri();
foreach ($this->matches[$requestMethod] as $pattern => $response) {
foreach ($this->matches[$requestMethod] ?? [] as $pattern => $response) {
if (preg_match($pattern, $requestUrl)) {
$this->calls->push($request);
if (is_callable($response)) {
return $response($request);
}
Expand Down
28 changes: 19 additions & 9 deletions tests/Unit/GuzzleTapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use GuzzleHttp\Psr7\Response;
use Illuminate\Support\Str;
use Carsdotcom\ApiRequest\Testing\MocksGuzzleInstance;
use OutOfBoundsException;
use Tests\BaseTestCase;

class GuzzleTapperTest extends BaseTestCase
Expand All @@ -17,21 +18,30 @@ class GuzzleTapperTest extends BaseTestCase

public function testUnmatchedMethod()
{
$tapper = $this->mockGuzzleWithTapper();
$this->expectException(\OutOfBoundsException::class);
$this->expectExceptionMessage("match method GET");
$this->mockGuzzleWithTapper();

app()->make('guzzle')->get("http://hopeless.com");
try {
app()->make('guzzle')->get("http://hopeless.com");
self::fail("Should have thrown OutOfBoundsException");
} catch (OutOfBoundsException $exception) {
self::assertSame('No GET responses match URL http://hopeless.com', $exception->getMessage());
// Even misses should be logged as having been attempted
self::assertAllTapperRequestsLike([['GET', '#^http://hopeless.com$#']]);
}
}

public function testUnmatchedURL()
{
$tapper = $this->mockGuzzleWithTapper();
$tapper->addMatchBody('GET', '/hopeful/', 'true');
$this->expectException(\OutOfBoundsException::class);
$this->expectExceptionMessage("match URL ");
$this->mockGuzzleWithTapper()->addMatchBody('GET', '/hopeful/', 'true');

app()->make('guzzle')->get("http://hopeless.com");
try {
app()->make('guzzle')->get("http://hopeless.com");
self::fail("Should have thrown OutOfBoundsException");
} catch (OutOfBoundsException $exception) {
self::assertSame('No GET responses match URL http://hopeless.com', $exception->getMessage());
// Even misses should be logged as having been attempted
self::assertAllTapperRequestsLike([['GET', '#^http://hopeless.com$#']]);
}
}

public function testCanReuseCalls()
Expand Down