-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathJsonApiResponseFormatterTest.php
126 lines (120 loc) · 4.14 KB
/
JsonApiResponseFormatterTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
/**
* @author Anton Tuyakhov <atuyakhov@gmail.com>
*/
namespace tuyakhov\jsonapi\tests;
use tuyakhov\jsonapi\JsonApiResponseFormatter;
use tuyakhov\jsonapi\Serializer;
use tuyakhov\jsonapi\tests\data\ResourceModel;
use yii\base\Controller;
use yii\helpers\Json;
use yii\web\Response;
use yii\web\ServerErrorHttpException;
class JsonApiResponseFormatterTest extends TestCase
{
public function testFormatException()
{
$formatter = new JsonApiResponseFormatter();
$exception = new ServerErrorHttpException('Server error');
$response = new Response();
$response->setStatusCode($exception->statusCode);
$response->data = [
'name' => $exception->getName(),
'message' => $exception->getMessage(),
'code' => $exception->getCode(),
'status' => $exception->statusCode
];
$formatter->format($response);
$this->assertJson($response->content);
$this->assertSame(Json::encode([
'errors' => [
[
'code' => '0',
'status' => '500',
'title' => Response::$httpStatuses[500],
'detail' => 'Server error',
]
]
]), $response->content);
}
public function testFormModelError()
{
$formatter = new JsonApiResponseFormatter();
$exception = new ServerErrorHttpException('Server error');
$response = new Response();
$response->setStatusCode($exception->statusCode);
$serializer = new Serializer();
$model = new ResourceModel();
$model->addError('field1', 'Error');
$model->addError('field2', 'Test Error');
$response->data = $serializer->serialize($model);
$formatter->format($response);
$this->assertJson($response->content);
$this->assertSame(Json::encode([
'errors' => [
[
'source' => ['pointer' => "/data/attributes/field1"],
'detail' => 'Error',
'status' => '422'
],
[
'source' => ['pointer' => "/data/attributes/field2"],
'detail' => 'Test Error',
'status' => '422'
]
]
]), $response->content);
}
public function testSuccessModel()
{
$formatter = new JsonApiResponseFormatter();
$response = new Response();
$serializer = new Serializer();
$model = new ResourceModel();
$response->data = $serializer->serialize($model);
$response->setStatusCode(200);
$formatter->format($response);
$this->assertJson($response->content);
$this->assertSame(Json::encode([
'data' => [
'id' => '123',
'type' => 'resource-models',
'attributes' => [
'field1' => 'test',
'field2' => 2,
],
'links' => [
'self' => [
'href' => 'http://example.com/resource/123'
]
]
]
]), $response->content);
}
public function testEmptyData()
{
$formatter = new JsonApiResponseFormatter();
$response = new Response();
$response->setStatusCode('200');
$serializer = new Serializer();
$response->data = $serializer->serialize(null);
$formatter->format($response);
$this->assertJson($response->content);
$this->assertSame(Json::encode([
'data' => null
]), $response->content);
\Yii::$app->controller = new Controller('test', \Yii::$app);
$formatter->format($response);
$this->assertJson($response->content);
$this->assertSame(Json::encode([
'data' => null,
'links' => [
'self' => ['href' => '/index.php?r=test']
]
]), $response->content);
$response->clear();
$response->setStatusCode(201);
$formatter->format($response);
$this->assertNull($response->content);
}
}