1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: harry |
5
|
|
|
* Date: 2/14/18 |
6
|
|
|
* Time: 12:02 PM |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace PhpRestfulApiResponse\Tests\unit; |
10
|
|
|
|
11
|
|
|
use PhpRestfulApiResponse\Response; |
12
|
|
|
use PhpRestfulApiResponse\Tests\unit\Lib\Book; |
13
|
|
|
use ReflectionClass; |
14
|
|
|
use InvalidArgumentException; |
15
|
|
|
use Zend\Diactoros\Response\ArraySerializer; |
16
|
|
|
|
17
|
|
|
class ResponseTest extends Base |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var Response |
21
|
|
|
*/ |
22
|
|
|
private $response; |
23
|
|
|
|
24
|
|
|
public function setUp() |
25
|
|
|
{ |
26
|
|
|
parent::setUp(); // TODO: Change the autogenerated stub |
27
|
|
|
$this->response = new Response(); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function test_withArray() |
31
|
|
|
{ |
32
|
|
|
/** @var Response $response */ |
33
|
|
|
$response = $this->response->withArray(['status' => 'success'], 200); |
34
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
35
|
|
|
$this->assertEquals('{"status":"success"}', ArraySerializer::toArray($response)['body']); |
36
|
|
|
$this->assertEquals('{"status":"success"}', $response->getBody()->__toString()); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function test_withItem() |
40
|
|
|
{ |
41
|
|
|
/** @var Response $response */ |
42
|
|
|
$response = $this->response->withItem( |
43
|
|
|
new Book('harry', 'harryosmarsitohang', 'how to be a ninja', 100000, 2017), |
44
|
|
|
new \PhpRestfulApiResponse\Tests\unit\Lib\Transformer\Book, |
|
|
|
|
45
|
|
|
200 |
46
|
|
|
); |
47
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
48
|
|
|
$this->assertEquals('{"data":{"title":"how to be a ninja","author":{"name":"harry","email":"harryosmarsitohang"},"year":2017,"price":100000}}', $response->getBody()->__toString()); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function test_withCollection() |
52
|
|
|
{ |
53
|
|
|
/** @var Response $response */ |
54
|
|
|
$response = $this->response->withCollection( |
55
|
|
|
[ |
56
|
|
|
new Book('harry', 'harryosmarsitohang', 'how to be a ninja', 100000, 2017), |
57
|
|
|
new Book('harry', 'harryosmarsitohang', 'how to be a mage', 500000, 2016), |
58
|
|
|
new Book('harry', 'harryosmarsitohang', 'how to be a samurai', 25000, 2000), |
59
|
|
|
], |
60
|
|
|
new \PhpRestfulApiResponse\Tests\unit\Lib\Transformer\Book, |
|
|
|
|
61
|
|
|
200 |
62
|
|
|
); |
63
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
64
|
|
|
$this->assertEquals('{"data":[{"title":"how to be a ninja","author":{"name":"harry","email":"harryosmarsitohang"},"year":2017,"price":100000},{"title":"how to be a mage","author":{"name":"harry","email":"harryosmarsitohang"},"year":2016,"price":500000},{"title":"how to be a samurai","author":{"name":"harry","email":"harryosmarsitohang"},"year":2000,"price":25000}]}', $response->getBody()->__toString()); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function test_withError() |
68
|
|
|
{ |
69
|
|
|
$code = 400; |
70
|
|
|
$message = 'error occured'; |
71
|
|
|
$this->withError( |
72
|
|
|
$this->response->withError($message, $code), |
73
|
|
|
$code, |
74
|
|
|
$message |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function test_withError_with_param_errorCode() |
79
|
|
|
{ |
80
|
|
|
$code = 400; |
81
|
|
|
$message = 'error occured'; |
82
|
|
|
$errorCode = 'ERROR-CODE'; |
83
|
|
|
$this->withError( |
84
|
|
|
$this->response->withError($message, $code, $errorCode), |
85
|
|
|
$code, |
86
|
|
|
$message, |
87
|
|
|
$errorCode |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function test_errorNotFound() |
92
|
|
|
{ |
93
|
|
|
$code = 404; |
94
|
|
|
$message = ''; |
95
|
|
|
$this->withError( |
96
|
|
|
$this->response->errorNotFound($message), |
97
|
|
|
$code, |
98
|
|
|
$message |
99
|
|
|
); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function test_errorNotFound_with_message() |
103
|
|
|
{ |
104
|
|
|
$code = 404; |
105
|
|
|
$message = 'go back to home page'; |
106
|
|
|
$this->withError( |
107
|
|
|
$this->response->errorNotFound($message), |
108
|
|
|
$code, |
109
|
|
|
$message |
110
|
|
|
); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function test_errorForbidden() |
114
|
|
|
{ |
115
|
|
|
$code = 403; |
116
|
|
|
$message = ''; |
117
|
|
|
$this->withError( |
118
|
|
|
$this->response->errorForbidden($message), |
119
|
|
|
$code, |
120
|
|
|
$message |
121
|
|
|
); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function test_errorForbidden_with_message() |
125
|
|
|
{ |
126
|
|
|
$code = 403; |
127
|
|
|
$message = 'forbid to access this'; |
128
|
|
|
$this->withError( |
129
|
|
|
$this->response->errorForbidden($message), |
130
|
|
|
$code, |
131
|
|
|
$message |
132
|
|
|
); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function test_errorInternalError() |
136
|
|
|
{ |
137
|
|
|
$code = 500; |
138
|
|
|
$message = ''; |
139
|
|
|
$this->withError( |
140
|
|
|
$this->response->errorInternalError($message), |
141
|
|
|
$code, |
142
|
|
|
$message |
143
|
|
|
); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function test_errorInternalError_with_message() |
147
|
|
|
{ |
148
|
|
|
$code = 500; |
149
|
|
|
$message = 'something wrong'; |
150
|
|
|
$this->withError( |
151
|
|
|
$this->response->errorInternalError($message), |
152
|
|
|
$code, |
153
|
|
|
$message |
154
|
|
|
); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public function test_errorUnauthorized() |
158
|
|
|
{ |
159
|
|
|
$code = 401; |
160
|
|
|
$message = ''; |
161
|
|
|
$this->withError( |
162
|
|
|
$this->response->errorUnauthorized($message), |
163
|
|
|
$code, |
164
|
|
|
$message |
165
|
|
|
); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
public function test_errorUnauthorized_with_message() |
169
|
|
|
{ |
170
|
|
|
$code = 401; |
171
|
|
|
$message = 'access token required'; |
172
|
|
|
$this->withError( |
173
|
|
|
$this->response->errorUnauthorized($message), |
174
|
|
|
$code, |
175
|
|
|
$message |
176
|
|
|
); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
public function test_errorWrongArgs() |
180
|
|
|
{ |
181
|
|
|
$code = 400; |
182
|
|
|
$message = [ |
183
|
|
|
'username' => 'required', |
184
|
|
|
'password' => 'required' |
185
|
|
|
]; |
186
|
|
|
$this->withError( |
187
|
|
|
$this->response->errorWrongArgs($message), |
188
|
|
|
$code, |
189
|
|
|
$message |
190
|
|
|
); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
public function test_errorGone() |
194
|
|
|
{ |
195
|
|
|
$code = 410; |
196
|
|
|
$message = ''; |
197
|
|
|
$this->withError( |
198
|
|
|
$this->response->errorGone($message), |
199
|
|
|
$code, |
200
|
|
|
$message |
201
|
|
|
); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
public function test_errorGone_with_message() |
205
|
|
|
{ |
206
|
|
|
$code = 410; |
207
|
|
|
$message = 'mysql gone away'; |
208
|
|
|
$this->withError( |
209
|
|
|
$this->response->errorGone($message), |
210
|
|
|
$code, |
211
|
|
|
$message |
212
|
|
|
); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
public function test_errorMethodNotAllowed() |
216
|
|
|
{ |
217
|
|
|
$code = 405; |
218
|
|
|
$message = ''; |
219
|
|
|
$this->withError( |
220
|
|
|
$this->response->errorMethodNotAllowed($message), |
221
|
|
|
$code, |
222
|
|
|
$message |
223
|
|
|
); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
public function test_errorMethodNotAllowed_with_message() |
227
|
|
|
{ |
228
|
|
|
$code = 405; |
229
|
|
|
$message = 'GET method is not allowed for this endpoint'; |
230
|
|
|
$this->withError( |
231
|
|
|
$this->response->errorMethodNotAllowed($message), |
232
|
|
|
$code, |
233
|
|
|
$message |
234
|
|
|
); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
public function test_errorUnwillingToProcess() |
238
|
|
|
{ |
239
|
|
|
$code = 431; |
240
|
|
|
$message = ''; |
241
|
|
|
$this->withError( |
242
|
|
|
$this->response->errorUnwillingToProcess($message), |
243
|
|
|
$code, |
244
|
|
|
$message |
245
|
|
|
); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
public function test_errorUnwillingToProcess_with_message() |
249
|
|
|
{ |
250
|
|
|
$code = 431; |
251
|
|
|
$message = 'Request size is too big'; |
252
|
|
|
$this->withError( |
253
|
|
|
$this->response->errorUnwillingToProcess($message), |
254
|
|
|
$code, |
255
|
|
|
$message |
256
|
|
|
); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
public function test_errorUnprocessable() |
260
|
|
|
{ |
261
|
|
|
$code = 422; |
262
|
|
|
$message = ''; |
263
|
|
|
$this->withError( |
264
|
|
|
$this->response->errorUnprocessable($message), |
265
|
|
|
$code, |
266
|
|
|
$message |
267
|
|
|
); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
public function test_errorUnprocessable_with_message() |
271
|
|
|
{ |
272
|
|
|
$code = 422; |
273
|
|
|
$message = 'Your request cannot be processed'; |
274
|
|
|
$this->withError( |
275
|
|
|
$this->response->errorUnprocessable($message), |
276
|
|
|
$code, |
277
|
|
|
$message |
278
|
|
|
); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
public function test_withStatus() |
282
|
|
|
{ |
283
|
|
|
$this->response->withStatus(200); |
284
|
|
|
$this->assertEquals(200, $this->response->getStatusCode()); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
public function test_setStatusCode_less_than_min_status_code() |
288
|
|
|
{ |
289
|
|
|
$this->run_setStatusCode($this->getMethodSetStatusCode(), 99); |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
public function test_setStatusCode_greater_than_max_status_code() |
293
|
|
|
{ |
294
|
|
|
$this->run_setStatusCode($this->getMethodSetStatusCode(), 600); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
public function test_setStatusCode() |
298
|
|
|
{ |
299
|
|
|
$this->run_setStatusCode($this->getMethodSetStatusCode(), 200); |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
public function test_setErrorCode() |
303
|
|
|
{ |
304
|
|
|
$this->run_setErrorCode($this->getMethodSetErrorCode(), "ERROR-CODE"); |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
private function run_setStatusCode(\ReflectionMethod $method, $code) |
308
|
|
|
{ |
309
|
|
|
try { |
310
|
|
|
$method->invokeArgs($this->response, [$code]); |
311
|
|
|
$this->assertEquals($code, $this->response->getStatusCode()); |
312
|
|
|
} catch (InvalidArgumentException $exception) { |
313
|
|
|
$this->assertEquals( |
314
|
|
|
sprintf('Invalid status code "%s"; must be an integer between %d and %d, inclusive', $code, Response::MIN_STATUS_CODE_VALUE, Response::MAX_STATUS_CODE_VALUE), |
315
|
|
|
$exception->getMessage() |
316
|
|
|
); |
317
|
|
|
} |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
private function run_setErrorCode(\ReflectionMethod $method, $code) |
321
|
|
|
{ |
322
|
|
|
$method->invokeArgs($this->response, [$code]); |
323
|
|
|
$this->assertEquals($code, $this->response->getErrorCode()); |
324
|
|
|
} |
325
|
|
|
|
326
|
|
View Code Duplication |
private function getMethodSetErrorCode() |
|
|
|
|
327
|
|
|
{ |
328
|
|
|
$responseReflect = new ReflectionClass(Response::class); |
329
|
|
|
$method = $responseReflect->getMethod('setErrorCode'); |
330
|
|
|
$method->setAccessible(true); |
331
|
|
|
return $method; |
332
|
|
|
} |
333
|
|
|
|
334
|
|
View Code Duplication |
private function getMethodSetStatusCode() |
|
|
|
|
335
|
|
|
{ |
336
|
|
|
$responseReflect = new ReflectionClass(Response::class); |
337
|
|
|
$method = $responseReflect->getMethod('setStatusCode'); |
338
|
|
|
$method->setAccessible(true); |
339
|
|
|
return $method; |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
|
343
|
|
|
private function withError(Response $response, $code, $message = null, $errorCode = null) |
344
|
|
|
{ |
345
|
|
|
$this->assertEquals($code, $response->getStatusCode()); |
346
|
|
|
$this->assertEquals($errorCode, $response->getErrorCode()); |
347
|
|
|
|
348
|
|
|
$this->assertEquals(json_encode([ |
349
|
|
|
'error' => array_filter([ |
350
|
|
|
'http_code' => $response->getStatusCode(), |
351
|
|
|
'code' => $response->getErrorCode(), |
352
|
|
|
'phrase' => $response->getReasonPhrase(), |
353
|
|
|
'message' => $message |
354
|
|
|
]) |
355
|
|
|
]), $response->getBody()->__toString()); |
356
|
|
|
|
357
|
|
|
} |
358
|
|
|
} |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: