Add error response tests

This commit is contained in:
Toby Zerner 2021-09-01 21:22:46 +10:00
parent 69866ea247
commit 9299a7da3e
3 changed files with 65 additions and 5 deletions

24
tests/MockException.php Normal file
View File

@ -0,0 +1,24 @@
<?php
namespace Tobyz\Tests\JsonApiServer;
use JsonApiPhp\JsonApi\Error;
use Tobyz\JsonApiServer\ErrorProviderInterface;
class MockException implements ErrorProviderInterface
{
public function getJsonApiErrors(): array
{
return [
new Error(
new Error\Title('Mock Error'),
new Error\Status($this->getJsonApiStatus())
)
];
}
public function getJsonApiStatus(): string
{
return '400';
}
}

View File

@ -138,7 +138,6 @@ class CreatingResourcesTest extends AbstractTestCase
->withParsedBody([
'data' => [
'type' => 'pets',
'id' => '1',
],
])
);

View File

@ -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());
}
}