Skip to content

Commit

Permalink
Merge pull request #6 from stevenmaguire/check-exception-response
Browse files Browse the repository at this point in the history
Add support for RequestExceptions that do not have a Response object
  • Loading branch information
stevenmaguire committed Oct 26, 2015
2 parents d39edac + 33716d2 commit a6f184e
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 14 deletions.
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
27 changes: 24 additions & 3 deletions src/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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) {
Expand Down
43 changes: 32 additions & 11 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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
*/
Expand Down

0 comments on commit a6f184e

Please sign in to comment.