Skip to content

Commit

Permalink
Add support for RequestExceptions that do not have a Response object
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenmaguire committed Oct 26, 2015
1 parent d39edac commit f74b80b
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 17 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
12 changes: 6 additions & 6 deletions src/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,13 +248,13 @@ public function setClient(HttpClientInterface $httpClient)
*/
protected function throwRequestException(RequestException $requestException)
{
$exception = new Exceptions\Exception(
$requestException->getResponse()->getReasonPhrase(),
$requestException->getResponse()->getStatusCode(),
$requestException
);
$response = $requestException->getResponse();
$reason = $response ? $response->getReasonPhrase() : $requestException->getMessage();
$code = $response ? $response->getStatusCode() : $requestException->getCode();
$body = $response ? $response->getBody() : null;

$exception = new Exceptions\Exception($reason, $code, $requestException);

$body = (string) $requestException->getResponse()->getBody();
$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 f74b80b

Please sign in to comment.