Skip to content

Commit 9299a7d

Browse files
committed
Add error response tests
1 parent 69866ea commit 9299a7d

File tree

3 files changed

+65
-5
lines changed

3 files changed

+65
-5
lines changed

tests/MockException.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Tobyz\Tests\JsonApiServer;
4+
5+
use JsonApiPhp\JsonApi\Error;
6+
use Tobyz\JsonApiServer\ErrorProviderInterface;
7+
8+
class MockException implements ErrorProviderInterface
9+
{
10+
public function getJsonApiErrors(): array
11+
{
12+
return [
13+
new Error(
14+
new Error\Title('Mock Error'),
15+
new Error\Status($this->getJsonApiStatus())
16+
)
17+
];
18+
}
19+
20+
public function getJsonApiStatus(): string
21+
{
22+
return '400';
23+
}
24+
}

tests/specification/CreatingResourcesTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ public function test_conflict_error_if_type_does_not_match_endpoint()
138138
->withParsedBody([
139139
'data' => [
140140
'type' => 'pets',
141-
'id' => '1',
142141
],
143142
])
144143
);

tests/unit/JsonApiTest.php

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,54 @@
1111

1212
namespace Tobyz\Tests\JsonApiServer\unit;
1313

14-
use PHPUnit\Framework\TestCase;
14+
use Exception;
15+
use Tobyz\JsonApiServer\JsonApi;
16+
use Tobyz\Tests\JsonApiServer\AbstractTestCase;
17+
use Tobyz\Tests\JsonApiServer\MockException;
1518

16-
class JsonApiTest extends TestCase
19+
class JsonApiTest extends AbstractTestCase
1720
{
21+
/**
22+
* @var JsonApi
23+
*/
24+
private $api;
25+
26+
public function setUp(): void
27+
{
28+
$this->api = new JsonApi('http://example.com');
29+
}
30+
1831
public function test_error_converts_error_provider_to_json_api_response()
1932
{
20-
$this->markTestIncomplete();
33+
$response = $this->api->error(
34+
new MockException()
35+
);
36+
37+
$this->assertEquals(400, $response->getStatusCode());
38+
$this->assertJsonApiDocumentSubset([
39+
'errors' => [
40+
[
41+
'title' => 'Mock Error',
42+
'status' => '400',
43+
],
44+
],
45+
], $response->getBody());
2146
}
2247

2348
public function test_error_converts_non_error_provider_to_internal_server_error()
2449
{
25-
$this->markTestIncomplete();
50+
$response = $this->api->error(
51+
new Exception()
52+
);
53+
54+
$this->assertEquals(500, $response->getStatusCode());
55+
$this->assertJsonApiDocumentSubset([
56+
'errors' => [
57+
[
58+
'title' => 'Internal Server Error',
59+
'status' => '500',
60+
],
61+
],
62+
], $response->getBody());
2663
}
2764
}

0 commit comments

Comments
 (0)