Skip to content

Commit bed07be

Browse files
committed
test added
1 parent 56b90ed commit bed07be

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace JsonApiException\Test\TestCase;
4+
5+
use Cake\ORM\Entity;
6+
use Cake\Http\ServerRequest;
7+
use Cake\TestSuite\TestCase;
8+
use JsonApiException\Error\JsonApiExceptionRenderer;
9+
use JsonApiException\Error\Exception\JsonApiException;
10+
11+
class JsonApiExceptionRendererTest extends TestCase
12+
{
13+
protected $message = 'Save failed';
14+
protected $validationError = 'name is required';
15+
16+
public function testJsonApiWithEntityError()
17+
{
18+
$entity = new Entity();
19+
$entity->setError('name', ['_empty' => $this->validationError]);
20+
$exception = new JsonApiException($entity, $this->message);
21+
22+
$request = (new ServerRequest())
23+
->withParam('controller', 'Foo')
24+
->withParam('action', 'bar');
25+
$exceptionRenderer = new JsonApiExceptionRenderer($exception, $request);
26+
27+
$response = $exceptionRenderer->render();
28+
29+
$this->assertEquals(400, $response->getStatusCode());
30+
$this->assertEquals('application/json', $response->getType());
31+
32+
$responseBody = json_decode($response->__toString());
33+
$this->assertEquals($this->message, $responseBody->message);
34+
$this->assertEquals('/', $responseBody->url);
35+
$this->assertEquals(1, $responseBody->errorCount);
36+
$this->assertEquals($this->validationError, $responseBody->errors->name->_empty);
37+
}
38+
39+
public function testJsonApiWithErrorCode()
40+
{
41+
$entity = new Entity();
42+
$entity->setError('name', ['_empty' => $this->validationError]);
43+
$exception = new JsonApiException($entity, $this->message, 406);
44+
45+
$request = (new ServerRequest())
46+
->withParam('controller', 'Foo')
47+
->withParam('action', 'bar');
48+
$exceptionRenderer = new JsonApiExceptionRenderer($exception, $request);
49+
50+
$response = $exceptionRenderer->render();
51+
52+
$this->assertEquals(406, $response->getStatusCode());
53+
}
54+
}

0 commit comments

Comments
 (0)