diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b23dd0..38a4b17 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,23 @@ #Changelog All Notable changes to `trello-php` will be documented in this file +## 0.3.6 - 2015-10-26 + +### Added +- Add support for RequestExceptions that do not have a Response object. + +### Deprecated +- Nothing + +### Fixed +- Nothing + +### Removed +- Nothing + +### Security +- Nothing + ## 0.3.5 - 2015-10-19 ### Added diff --git a/src/Http.php b/src/Http.php index 504414c..755166d 100644 --- a/src/Http.php +++ b/src/Http.php @@ -142,6 +142,25 @@ protected function getHeaders() return []; } + /** + * Prepares an array of important exception parts based on composition of a + * given exception. + * + * @param RequestException $requestException + * + * @return array + */ + private function getRequestExceptionParts(RequestException $requestException) + { + $response = $requestException->getResponse(); + $parts = []; + $parts['reason'] = $response ? $response->getReasonPhrase() : $requestException->getMessage(); + $parts['code'] = $response ? $response->getStatusCode() : $requestException->getCode(); + $parts['body'] = $response ? $response->getBody() : null; + + return $parts; + } + /** * Creates fully qualified domain from given path. * @@ -248,13 +267,15 @@ public function setClient(HttpClientInterface $httpClient) */ protected function throwRequestException(RequestException $requestException) { + $exceptionParts = $this->getRequestExceptionParts($requestException); + $exception = new Exceptions\Exception( - $requestException->getResponse()->getReasonPhrase(), - $requestException->getResponse()->getStatusCode(), + $exceptionParts['reason'], + $exceptionParts['code'], $requestException ); - $body = (string) $requestException->getResponse()->getBody(); + $body = $exceptionParts['body']; $json = json_decode($body); if (json_last_error() == JSON_ERROR_NONE) { diff --git a/tests/ClientTest.php b/tests/ClientTest.php index 6c2a723..11cdbf9 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -8,6 +8,7 @@ use Psr\Http\Message\StreamInterface; use Stevenmaguire\Services\Trello\Authorization; use Stevenmaguire\Services\Trello\Client; +use Stevenmaguire\Services\Trello\Exceptions\Exception as ServiceException; class ClientTest extends \PHPUnit_Framework_TestCase { @@ -98,26 +99,32 @@ protected function prepareFor($method, $path, $query = "", $payload = [], $statu return is_array($options); }); - if (is_string($payload)) { - $responseBody = $payload; + if (is_null($payload)) { + $response = null; } else { - $responseBody = json_encode($payload); - } + if (is_string($payload)) { + $responseBody = $payload; + } else { + $responseBody = json_encode($payload); + } - $stream = m::mock(StreamInterface::class); - $stream->shouldReceive('__toString')->andReturn($responseBody); + $stream = m::mock(StreamInterface::class); + $stream->shouldReceive('__toString')->andReturn($responseBody); - $response = m::mock(ResponseInterface::class); - $response->shouldReceive('getStatusCode')->andReturn($status); - $response->shouldReceive('getBody')->andReturn($stream); - $response->shouldReceive('getHeader')->with('content-type')->andReturn('application/json'); + $response = m::mock(ResponseInterface::class); + $response->shouldReceive('getStatusCode')->andReturn($status); + $response->shouldReceive('getBody')->andReturn($stream); + $response->shouldReceive('getHeader')->with('content-type')->andReturn('application/json'); + } $client = m::mock(HttpClient::class); if ($status == 200) { $client->shouldReceive('send')->with($request, $requestOptions)->andReturn($response); } else { $badRequest = m::mock(RequestInterface::class); - $response->shouldReceive('getReasonPhrase')->andReturn(""); + if ($response) { + $response->shouldReceive('getReasonPhrase')->andReturn(""); + } $exception = new BadResponseException('test exception', $badRequest, $response); $client->shouldReceive('send')->with($request, $requestOptions)->andThrow($exception); } @@ -170,6 +177,20 @@ public function testBadRequestThrowsExeptionWithoutValidJson() $result = $this->client->getHttp()->get($path); } + public function testBadRequestThrowsExeptionWithoutResponse() + { + $path = uniqid(); + $this->prepareFor("GET", $path, "", null, 400); + + try { + $result = $this->client->getHttp()->get($path); + } catch (ServiceException $e) { + $this->assertTrue(is_string($e->getMessage())); + $this->assertTrue(is_numeric($e->getCode())); + $this->assertNull($e->getResponseBody()); + } + } + /** * @expectedException BadMethodCallException */