|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace EllipseSynergie\ApiResponse\Tests; |
|
4
|
|
|
|
|
5
|
|
|
use EllipseSynergie\ApiResponse\Tests\Laravel\ResponseFake; |
|
6
|
|
|
use EllipseSynergie\ApiResponse\Tests\ResponseFaker as Response; |
|
7
|
|
|
use League\Fractal\Manager; |
|
8
|
|
|
use League\Fractal\Pagination\Cursor; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class ResponseTest |
|
12
|
|
|
* |
|
13
|
|
|
* @package EllipseSynergie\ApiResponse\Tests |
|
14
|
|
|
* @author Maxime Beaudoin <[email protected]> |
|
15
|
|
|
*/ |
|
16
|
|
|
class ResponseTest extends \PHPUnit_Framework_TestCase |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var Response |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $response; |
|
22
|
|
|
|
|
23
|
|
|
public function setUp() |
|
24
|
|
|
{ |
|
25
|
|
|
$this->response = new Response(new Manager()); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function testImplementContractProperly() |
|
29
|
|
|
{ |
|
30
|
|
|
$this->assertInstanceOf('EllipseSynergie\ApiResponse\Contracts\Response', $this->response); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function testGetManager() |
|
34
|
|
|
{ |
|
35
|
|
|
$this->assertInstanceOf('League\Fractal\Manager', $this->response->getManager()); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function testSetStatusCodeWorkProperly() |
|
39
|
|
|
{ |
|
40
|
|
|
$this->response->setStatusCode(201); |
|
41
|
|
|
|
|
42
|
|
|
$this->assertSame(201, $this->response->getStatusCode()); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function testDefaultStatusCodeIs200() |
|
46
|
|
|
{ |
|
47
|
|
|
$this->assertSame(200, $this->response->getStatusCode()); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function testErrorForbiddenStatusCodeIs403() |
|
51
|
|
|
{ |
|
52
|
|
|
$response = $this->response->errorForbidden(); |
|
53
|
|
|
$this->assertSame(403, $response['status']); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function testErrorInternalErrorStatusCodeIs500() |
|
57
|
|
|
{ |
|
58
|
|
|
$response = $this->response->errorInternalError(); |
|
59
|
|
|
$this->assertSame(500, $response['status']); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function testErrorNotFoundStatusCodeIs404() |
|
63
|
|
|
{ |
|
64
|
|
|
$response = $this->response->errorNotFound(); |
|
65
|
|
|
$this->assertSame(404, $response['status']); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function testErrorUnauthorizedStatusCodeIs401() |
|
69
|
|
|
{ |
|
70
|
|
|
$response = $this->response->errorUnauthorized(); |
|
71
|
|
|
$this->assertSame(401, $response['status']); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function testErrorWrongArgsStatusCodeIs400() |
|
75
|
|
|
{ |
|
76
|
|
|
$response = $this->response->errorWrongArgs(); |
|
77
|
|
|
$this->assertSame(400, $response['status']); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function testErrorGoneStatusCodeIs410() |
|
81
|
|
|
{ |
|
82
|
|
|
$response = $this->response->errorGone(); |
|
83
|
|
|
$this->assertSame(410, $response['status']); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function testErrorMethodNotAllowedStatusCodeIs405() |
|
87
|
|
|
{ |
|
88
|
|
|
$response = $this->response->errorMethodNotAllowed(); |
|
89
|
|
|
$this->assertSame(405, $response['status']); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function testErrorUnwillingToProcessStatusCodeIs431() |
|
93
|
|
|
{ |
|
94
|
|
|
$response = $this->response->errorUnwillingToProcess(); |
|
95
|
|
|
$this->assertSame(431, $response['status']); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function testErrorUnprocessableStatusCodeIs422() |
|
99
|
|
|
{ |
|
100
|
|
|
$response = $this->response->errorUnprocessable(); |
|
101
|
|
|
$this->assertSame(422, $response['status']); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function testWithItemReturnDataProperly() |
|
105
|
|
|
{ |
|
106
|
|
|
$item = ['foo' => 'bar']; |
|
107
|
|
|
|
|
108
|
|
|
$response = $this->response->withItem($item, function ($data) { |
|
109
|
|
|
return $data; |
|
110
|
|
|
}); |
|
111
|
|
|
|
|
112
|
|
|
$this->assertSame('bar', $response['data']['foo']); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
public function testWithItemReturnMetaProperly() |
|
116
|
|
|
{ |
|
117
|
|
|
$item = ['foo' => 'bar']; |
|
118
|
|
|
|
|
119
|
|
|
$response = $this->response->withItem($item, function ($data) { |
|
120
|
|
|
return $data; |
|
121
|
|
|
}, 'data', ['foo' => 'bar']); |
|
122
|
|
|
|
|
123
|
|
|
$this->assertSame('bar', $response['meta']['foo']); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
public function testWithCollectionReturnDataProperly() |
|
127
|
|
|
{ |
|
128
|
|
|
$item = [ |
|
129
|
|
|
['foo' => 'bar'], |
|
130
|
|
|
['foo' => 'maxime'], |
|
131
|
|
|
]; |
|
132
|
|
|
|
|
133
|
|
|
$response = $this->response->withCollection($item, function ($data) { |
|
134
|
|
|
return $data; |
|
135
|
|
|
}); |
|
136
|
|
|
|
|
137
|
|
|
$this->assertSame('bar', $response['data'][0]['foo']); |
|
138
|
|
|
$this->assertSame('maxime', $response['data'][1]['foo']); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
public function testWithCollectionReturnMetaProperly() |
|
142
|
|
|
{ |
|
143
|
|
|
$item = [ |
|
144
|
|
|
['foo' => 'bar'], |
|
145
|
|
|
['foo' => 'maxime'], |
|
146
|
|
|
]; |
|
147
|
|
|
|
|
148
|
|
|
$response = $this->response->withCollection($item, function ($data) { |
|
149
|
|
|
return $data; |
|
150
|
|
|
}, null, null, ['foo' => 'bar']); |
|
151
|
|
|
|
|
152
|
|
|
$this->assertSame('bar', $response['meta']['foo']); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
public function testWithCollectionReturnCursorProperly() |
|
156
|
|
|
{ |
|
157
|
|
|
$item = [ |
|
158
|
|
|
['foo' => 'bar'], |
|
159
|
|
|
['foo' => 'maxime'], |
|
160
|
|
|
]; |
|
161
|
|
|
|
|
162
|
|
|
$response = $this->response->withCollection($item, function ($data) { |
|
163
|
|
|
return $data; |
|
164
|
|
|
}, null, new Cursor(100, 1, 200, 300), ['foo' => 'bar']); |
|
165
|
|
|
|
|
166
|
|
|
$this->assertSame( |
|
167
|
|
|
[ |
|
168
|
|
|
'current' => 100, |
|
169
|
|
|
'prev' => 1, |
|
170
|
|
|
'next' => 200, |
|
171
|
|
|
'count' => 300, |
|
172
|
|
|
], |
|
173
|
|
|
$response['meta']['cursor'] |
|
174
|
|
|
); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
public function testWithErrorUsingCustomHeaders() |
|
178
|
|
|
{ |
|
179
|
|
|
$response = $this->response |
|
180
|
|
|
->setStatusCode(401) |
|
181
|
|
|
->withError('Custom message', ResponseFake::CODE_UNAUTHORIZED, [ |
|
182
|
|
|
'Authorization' => 'Foo' |
|
183
|
|
|
]); |
|
184
|
|
|
|
|
185
|
|
|
$this->assertSame([ |
|
186
|
|
|
'status' => 401, |
|
187
|
|
|
'headers' => [ |
|
188
|
|
|
'Authorization' => 'Foo' |
|
189
|
|
|
], |
|
190
|
|
|
'error' => [ |
|
191
|
|
|
'code' => 'GEN-UNAUTHORIZED', |
|
192
|
|
|
'http_code' => 401, |
|
193
|
|
|
'message' => 'Custom message' |
|
194
|
|
|
] |
|
195
|
|
|
], $response); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
public function testResponseWithArrayContainsArray() |
|
199
|
|
|
{ |
|
200
|
|
|
$response = $this->response ->withArray(['test' => 'array']); |
|
201
|
|
|
|
|
202
|
|
|
$this->assertSame('array', $response['test']); |
|
203
|
|
|
} |
|
204
|
|
|
} |
|
205
|
|
|
|