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
|
|
|
|
14
|
|
|
class ResponseTest extends Base |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var Response |
18
|
|
|
*/ |
19
|
|
|
private $response; |
20
|
|
|
|
21
|
|
|
public function setUp() |
22
|
|
|
{ |
23
|
|
|
parent::setUp(); // TODO: Change the autogenerated stub |
24
|
|
|
$this->response = new Response(); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function test_withArray() |
28
|
|
|
{ |
29
|
|
|
/** @var Response $response */ |
30
|
|
|
$response = $this->response->withArray(['status' => 'success'], 200); |
31
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
32
|
|
|
$this->assertEquals('{"status":"success"}', $response->getBody()->__toString()); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function test_withItem() |
36
|
|
|
{ |
37
|
|
|
/** @var Response $response */ |
38
|
|
|
$response = $this->response->withItem( |
39
|
|
|
new Book('harry', 'harryosmarsitohang', 'how to be a ninja', 100000, 2017), |
40
|
|
|
new \PhpRestfulApiResponse\Tests\unit\Lib\Transformer\Book, |
|
|
|
|
41
|
|
|
200 |
42
|
|
|
); |
43
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
44
|
|
|
$this->assertEquals('{"data":{"title":"how to be a ninja","author":{"name":"harry","email":"harryosmarsitohang"},"year":2017,"price":100000}}', $response->getBody()->__toString()); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function test_withCollection() |
48
|
|
|
{ |
49
|
|
|
/** @var Response $response */ |
50
|
|
|
$response = $this->response->withCollection( |
51
|
|
|
[ |
52
|
|
|
new Book('harry', 'harryosmarsitohang', 'how to be a ninja', 100000, 2017), |
53
|
|
|
new Book('harry', 'harryosmarsitohang', 'how to be a mage', 500000, 2016), |
54
|
|
|
new Book('harry', 'harryosmarsitohang', 'how to be a samurai', 25000, 2000), |
55
|
|
|
], |
56
|
|
|
new \PhpRestfulApiResponse\Tests\unit\Lib\Transformer\Book, |
|
|
|
|
57
|
|
|
200 |
58
|
|
|
); |
59
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
60
|
|
|
$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()); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
private function withError(Response $response, $code, $message = null) |
64
|
|
|
{ |
65
|
|
|
$this->assertEquals($code, $response->getStatusCode()); |
66
|
|
|
$this->assertEquals(json_encode([ |
67
|
|
|
'error' => array_filter([ |
68
|
|
|
'http_code' => $response->getStatusCode(), |
69
|
|
|
'phrase' => $response->getReasonPhrase(), |
70
|
|
|
'message' => $message |
71
|
|
|
]) |
72
|
|
|
]), $response->getBody()->__toString()); |
73
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function test_withError() |
77
|
|
|
{ |
78
|
|
|
$code = 400; |
79
|
|
|
$message = 'error occured'; |
80
|
|
|
$this->withError( |
81
|
|
|
$this->response->withError($message, $code), |
82
|
|
|
$code, |
83
|
|
|
$message |
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function test_errorNotFound() |
88
|
|
|
{ |
89
|
|
|
$code = 404; |
90
|
|
|
$message = ''; |
91
|
|
|
$this->withError( |
92
|
|
|
$this->response->errorNotFound($message), |
93
|
|
|
$code, |
94
|
|
|
$message |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function test_errorNotFound_with_message() |
99
|
|
|
{ |
100
|
|
|
$code = 404; |
101
|
|
|
$message = 'go back to home page'; |
102
|
|
|
$this->withError( |
103
|
|
|
$this->response->errorNotFound($message), |
104
|
|
|
$code, |
105
|
|
|
$message |
106
|
|
|
); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function test_errorForbidden() |
110
|
|
|
{ |
111
|
|
|
$code = 403; |
112
|
|
|
$message = ''; |
113
|
|
|
$this->withError( |
114
|
|
|
$this->response->errorForbidden($message), |
115
|
|
|
$code, |
116
|
|
|
$message |
117
|
|
|
); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function test_errorForbidden_with_message() |
121
|
|
|
{ |
122
|
|
|
$code = 403; |
123
|
|
|
$message = 'forbid to access this'; |
124
|
|
|
$this->withError( |
125
|
|
|
$this->response->errorForbidden($message), |
126
|
|
|
$code, |
127
|
|
|
$message |
128
|
|
|
); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function test_errorInternalError() |
132
|
|
|
{ |
133
|
|
|
$code = 500; |
134
|
|
|
$message = ''; |
135
|
|
|
$this->withError( |
136
|
|
|
$this->response->errorInternalError($message), |
137
|
|
|
$code, |
138
|
|
|
$message |
139
|
|
|
); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function test_errorInternalError_with_message() |
143
|
|
|
{ |
144
|
|
|
$code = 500; |
145
|
|
|
$message = 'something wrong'; |
146
|
|
|
$this->withError( |
147
|
|
|
$this->response->errorInternalError($message), |
148
|
|
|
$code, |
149
|
|
|
$message |
150
|
|
|
); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function test_errorUnauthorized() |
154
|
|
|
{ |
155
|
|
|
$code = 401; |
156
|
|
|
$message = ''; |
157
|
|
|
$this->withError( |
158
|
|
|
$this->response->errorUnauthorized($message), |
159
|
|
|
$code, |
160
|
|
|
$message |
161
|
|
|
); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
public function test_errorUnauthorized_with_message() |
165
|
|
|
{ |
166
|
|
|
$code = 401; |
167
|
|
|
$message = 'access token required'; |
168
|
|
|
$this->withError( |
169
|
|
|
$this->response->errorUnauthorized($message), |
170
|
|
|
$code, |
171
|
|
|
$message |
172
|
|
|
); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
public function test_errorWrongArgs() |
176
|
|
|
{ |
177
|
|
|
$code = 400; |
178
|
|
|
$message = [ |
179
|
|
|
'username' => 'required', |
180
|
|
|
'password' => 'required' |
181
|
|
|
]; |
182
|
|
|
$this->withError( |
183
|
|
|
$this->response->errorWrongArgs($message), |
184
|
|
|
$code, |
185
|
|
|
$message |
186
|
|
|
); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
public function test_errorGone() |
190
|
|
|
{ |
191
|
|
|
$code = 410; |
192
|
|
|
$message = ''; |
193
|
|
|
$this->withError( |
194
|
|
|
$this->response->errorGone($message), |
195
|
|
|
$code, |
196
|
|
|
$message |
197
|
|
|
); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
public function test_errorGone_with_message() |
201
|
|
|
{ |
202
|
|
|
$code = 410; |
203
|
|
|
$message = 'mysql gone away'; |
204
|
|
|
$this->withError( |
205
|
|
|
$this->response->errorGone($message), |
206
|
|
|
$code, |
207
|
|
|
$message |
208
|
|
|
); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
public function test_errorMethodNotAllowed() |
212
|
|
|
{ |
213
|
|
|
$code = 405; |
214
|
|
|
$message = ''; |
215
|
|
|
$this->withError( |
216
|
|
|
$this->response->errorMethodNotAllowed($message), |
217
|
|
|
$code, |
218
|
|
|
$message |
219
|
|
|
); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
public function test_errorMethodNotAllowed_with_message() |
223
|
|
|
{ |
224
|
|
|
$code = 405; |
225
|
|
|
$message = 'GET method is not allowed for this endpoint'; |
226
|
|
|
$this->withError( |
227
|
|
|
$this->response->errorMethodNotAllowed($message), |
228
|
|
|
$code, |
229
|
|
|
$message |
230
|
|
|
); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
public function test_errorUnwillingToProcess() |
234
|
|
|
{ |
235
|
|
|
$code = 431; |
236
|
|
|
$message = ''; |
237
|
|
|
$this->withError( |
238
|
|
|
$this->response->errorUnwillingToProcess($message), |
239
|
|
|
$code, |
240
|
|
|
$message |
241
|
|
|
); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
public function test_errorUnwillingToProcess_with_message() |
245
|
|
|
{ |
246
|
|
|
$code = 431; |
247
|
|
|
$message = 'Request size is too big'; |
248
|
|
|
$this->withError( |
249
|
|
|
$this->response->errorUnwillingToProcess($message), |
250
|
|
|
$code, |
251
|
|
|
$message |
252
|
|
|
); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
public function test_errorUnprocessable() |
256
|
|
|
{ |
257
|
|
|
$code = 422; |
258
|
|
|
$message = ''; |
259
|
|
|
$this->withError( |
260
|
|
|
$this->response->errorUnprocessable($message), |
261
|
|
|
$code, |
262
|
|
|
$message |
263
|
|
|
); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
public function test_errorUnprocessable_with_message() |
267
|
|
|
{ |
268
|
|
|
$code = 422; |
269
|
|
|
$message = 'Your request cannot be processed'; |
270
|
|
|
$this->withError( |
271
|
|
|
$this->response->errorUnprocessable($message), |
272
|
|
|
$code, |
273
|
|
|
$message |
274
|
|
|
); |
275
|
|
|
} |
276
|
|
|
} |
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: