Skip to content

Commit

Permalink
update to hadle api request failures (too many requests)
Browse files Browse the repository at this point in the history
  • Loading branch information
nigel committed May 22, 2019
1 parent 5d29042 commit 544f616
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/Service/PostcodeApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Hurnell\PostcodeApiBundle\Service;

use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Exception\GuzzleException;
use Hurnell\PostcodeApiBundle\Exception\InvalidHouseNumberException;
Expand Down Expand Up @@ -107,6 +108,12 @@ public function makeRequest(string $postcode, $number, string $extra): self
} catch (InvalidJsonException $exception) {
throw new InvalidApiResponseException('The API response could not be parsed');
}
} catch (ClientException $e) {
$message = 'The API request failed';
if (429 === $e->getResponse()->getStatusCode()) {
$message = 'The API request failed due to too many requests';
}
throw new InvalidApiResponseException($message);
} catch (GuzzleException $e) {
throw new InvalidApiResponseException('The Guzzle client failed');
}
Expand Down
24 changes: 24 additions & 0 deletions tests/Service/PostcodeApiClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,30 @@ public function invalidExtraButValidWhenWithoutExtraProvider(): array
];
}

public function testPostcodeApiTooManyRequests():void
{
$this->guzzleExpectsApiException(429);
$this->expectException(InvalidApiResponseException::class);
$this->expectExceptionMessage('The API request failed due to too many requests');
$this->postcodeApiClient
->makeRequest(
'2013AH',
34,
''
);
}
public function testPostcodeApiTooStatusCodeNotFound():void
{
$this->guzzleExpectsApiException(404);
$this->expectException(InvalidApiResponseException::class);
$this->expectExceptionMessage('The API request failed');
$this->postcodeApiClient
->makeRequest(
'2013AH',
34,
''
);
}

/**
* Finally check that a valid request does not throw an error and
Expand Down
15 changes: 15 additions & 0 deletions tests/Service/PostcodeApiClientTestMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Hurnell\PostcodeApiBundle\Tests\Service;

use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Psr7\Response;
use Psr\Http\Message\RequestInterface;
use PHPUnit\Framework\TestCase;
use GuzzleHttp\ClientInterface;
Expand All @@ -28,13 +30,15 @@ class PostcodeApiClientTestMock extends TestCase

private $guzzleException;


public function setUp(): void
{
$this->guzzleClient = $this->mockGuzzleClientInterface();
$this->request = $this->mockRequestInterface();
$this->response = $this->mockResponseInterface();
$this->stream = $this->mockStreamInterface();
$this->guzzleException = $this->mockGuzzleException();

}

/**
Expand Down Expand Up @@ -64,6 +68,11 @@ private function mockGuzzleException(): ConnectException
{
return new ConnectException('this is a guzzle exception', $this->request);
}
private function mockGuzzleApiException($statusCode): ClientException
{
$response = new Response($statusCode);
return new ClientException('this is a guzzle exception', $this->request, $response);
}


protected function guzzleExpectsResponse(): void
Expand All @@ -79,6 +88,12 @@ protected function guzzleExpectsException(): void
->method('send')
->willThrowException($this->guzzleException);
}
protected function guzzleExpectsApiException($statusCode): void
{
$this->guzzleClient->expects($this->once())
->method('send')
->willThrowException($this->mockGuzzleApiException($statusCode));
}

/**
* @param int $statusCode
Expand Down

0 comments on commit 544f616

Please sign in to comment.