Skip to content

Commit fbab163

Browse files
authored
Merge pull request #44 from chadicus/master
Add and use custom API exception
2 parents 7295a19 + 836175e commit fbab163

6 files changed

Lines changed: 98 additions & 4 deletions

File tree

.scrutinizer.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ tools:
99
php_mess_detector: true
1010
php_code_sniffer:
1111
config:
12-
standard: PSR1
12+
standard: PSR2
1313
sensiolabs_security_checker: true
1414
php_loc:
1515
excluded_dirs:

src/Collection.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,12 @@ public function next()
156156
$indexResponse = $this->client->index($this->resource, $this->filters);
157157

158158
$httpCode = $indexResponse->getHttpCode();
159-
Util::ensure(200, $httpCode, "Did not receive 200 from API. Instead received {$httpCode}");
159+
Util::ensure(
160+
200,
161+
$httpCode,
162+
Exception::class,
163+
["Did not receive 200 from API. Instead received {$httpCode}", $indexResponse]
164+
);
160165

161166
$response = $indexResponse->getResponse();
162167
$this->limit = $response['pagination']['limit'];

src/Exception.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace TraderInteractive\Api;
4+
5+
class Exception extends \Exception
6+
{
7+
/**
8+
* @var Response
9+
*/
10+
private $response;
11+
12+
public function __construct(string $message, Response $response)
13+
{
14+
parent::__construct($message);
15+
$this->response = $response;
16+
}
17+
18+
public function getResponse() : Response
19+
{
20+
return $this->response;
21+
}
22+
}

tests/CacheHelperTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ public function getCacheKey()
2020
{
2121
$request = new Psr7\Request('GET', 'http://localhost:8080/id', []);
2222
$this->assertSame(
23-
'GET|http_COLON__FSLASH__FSLASH_localhost_COLON_8080_FSLASH_id|',
24-
CacheHelper::getCacheKey($request)
23+
'GET|http_COLON__FSLASH__FSLASH_localhost_COLON_8080_FSLASH_id|',
24+
CacheHelper::getCacheKey($request)
2525
);
2626
}
2727
}

tests/CollectionTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,44 @@ public function selectMissingKeys()
300300
);
301301
}
302302

303+
/**
304+
* @test
305+
* @covers ::next
306+
*/
307+
public function collectionThrowsApiExceptionWith400Response()
308+
{
309+
$response = new Response(400, ['Content-Type' => ['application/json']], []);
310+
$mockClient = $this->getMockBuilder(ClientInterface::class)->getMock();
311+
$mockClient->method('index')->willReturn($response);
312+
$collection = new Collection($mockClient, 'basic', []);
313+
try {
314+
$collection->next();
315+
$this->fail('No Exception throw');
316+
} catch (Exception $e) {
317+
$this->assertSame('Did not receive 200 from API. Instead received 400', $e->getMessage());
318+
$this->assertSame($response, $e->getResponse());
319+
}
320+
}
321+
322+
/**
323+
* @test
324+
* @covers ::next
325+
*/
326+
public function collectionThrowsApiExceptionWith500Response()
327+
{
328+
$response = new Response(500, ['Content-Type' => ['application/json']], []);
329+
$mockClient = $this->getMockBuilder(ClientInterface::class)->getMock();
330+
$mockClient->method('index')->willReturn($response);
331+
$collection = new Collection($mockClient, 'basic', []);
332+
try {
333+
$collection->next();
334+
$this->fail('No Exception throw');
335+
} catch (Exception $e) {
336+
$this->assertSame('Did not receive 200 from API. Instead received 500', $e->getMessage());
337+
$this->assertSame($response, $e->getResponse());
338+
}
339+
}
340+
303341
private function getClient(array $items = self::DEFAULT_RESULT_SET) : ClientInterface
304342
{
305343
$callback = function (string $resource, array $filters) use ($items) {

tests/ExceptionTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace TraderInteractive\Api;
4+
5+
use GuzzleHttp\Psr7;
6+
use PHPUnit\Framework\TestCase;
7+
8+
/**
9+
* Defines unit tests for the Exception class
10+
*
11+
* @coversDefaultClass \TraderInteractive\Api\Exception
12+
* @covers ::__construct
13+
*/
14+
final class ExceptionTest extends TestCase
15+
{
16+
/**
17+
* @test
18+
* @covers ::getResponse
19+
*/
20+
public function getResponseReturnsResponse()
21+
{
22+
$httpCode = 200;
23+
$body = ['doesnt' => 'matter'];
24+
$headers = ['Content-Type' => ['text/json']];
25+
$response = new Response($httpCode, $headers, $body);
26+
$exception = new Exception('the error message', $response);
27+
$this->assertSame($response, $exception->getResponse());
28+
}
29+
}

0 commit comments

Comments
 (0)