From 9299a7da3e554ff4b2d9e5eba652b5c41a959296 Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Wed, 1 Sep 2021 21:22:46 +1000 Subject: [PATCH] Add error response tests --- tests/MockException.php | 24 ++++++++++ tests/specification/CreatingResourcesTest.php | 1 - tests/unit/JsonApiTest.php | 45 +++++++++++++++++-- 3 files changed, 65 insertions(+), 5 deletions(-) create mode 100644 tests/MockException.php diff --git a/tests/MockException.php b/tests/MockException.php new file mode 100644 index 0000000..198852f --- /dev/null +++ b/tests/MockException.php @@ -0,0 +1,24 @@ +getJsonApiStatus()) + ) + ]; + } + + public function getJsonApiStatus(): string + { + return '400'; + } +} diff --git a/tests/specification/CreatingResourcesTest.php b/tests/specification/CreatingResourcesTest.php index 66aff5b..47cb6a8 100644 --- a/tests/specification/CreatingResourcesTest.php +++ b/tests/specification/CreatingResourcesTest.php @@ -138,7 +138,6 @@ class CreatingResourcesTest extends AbstractTestCase ->withParsedBody([ 'data' => [ 'type' => 'pets', - 'id' => '1', ], ]) ); diff --git a/tests/unit/JsonApiTest.php b/tests/unit/JsonApiTest.php index ef8ebda..2714cd7 100644 --- a/tests/unit/JsonApiTest.php +++ b/tests/unit/JsonApiTest.php @@ -11,17 +11,54 @@ namespace Tobyz\Tests\JsonApiServer\unit; -use PHPUnit\Framework\TestCase; +use Exception; +use Tobyz\JsonApiServer\JsonApi; +use Tobyz\Tests\JsonApiServer\AbstractTestCase; +use Tobyz\Tests\JsonApiServer\MockException; -class JsonApiTest extends TestCase +class JsonApiTest extends AbstractTestCase { + /** + * @var JsonApi + */ + private $api; + + public function setUp(): void + { + $this->api = new JsonApi('http://example.com'); + } + public function test_error_converts_error_provider_to_json_api_response() { - $this->markTestIncomplete(); + $response = $this->api->error( + new MockException() + ); + + $this->assertEquals(400, $response->getStatusCode()); + $this->assertJsonApiDocumentSubset([ + 'errors' => [ + [ + 'title' => 'Mock Error', + 'status' => '400', + ], + ], + ], $response->getBody()); } public function test_error_converts_non_error_provider_to_internal_server_error() { - $this->markTestIncomplete(); + $response = $this->api->error( + new Exception() + ); + + $this->assertEquals(500, $response->getStatusCode()); + $this->assertJsonApiDocumentSubset([ + 'errors' => [ + [ + 'title' => 'Internal Server Error', + 'status' => '500', + ], + ], + ], $response->getBody()); } }