-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApiControllerTest.php
More file actions
370 lines (292 loc) · 12.5 KB
/
Copy pathApiControllerTest.php
File metadata and controls
370 lines (292 loc) · 12.5 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
<?php
declare(strict_types=1);
namespace VitekDev\Tests\Nette\Application;
use Nette\Application\Request;
use Nette\Application\Responses\JsonResponse;
use Nette\Application\Responses\VoidResponse;
use Nette\Http\IResponse;
use PHPUnit\Framework\TestCase;
use VitekDev\Nette\Application\ApiController;
use VitekDev\Nette\Application\Response\PlainTextResponse;
use VitekDev\Nette\Application\Response\StatusResponse;
use VitekDev\Tests\Nette\Application\Test\ApiTestControllers;
/**
* @covers \VitekDev\Nette\Application\ApiController
* @covers \VitekDev\Nette\Application\Response\StatusResponse
*/
class ApiControllerTest extends TestCase
{
use ApiTestControllers;
//region Generic
public function testOptionsAlwaysOk(): void
{
$response = $this->createPlainController()->run(
new Request('Api', 'OPTIONS', ['action' => 'randomMethodThatDontEvenExists']),
);
self::assertInstanceOf(StatusResponse::class, $response);
self::assertSame(IResponse::S204_NoContent, $response->code);
}
public function testMissingActionParameter(): void
{
$controller = $this->createPlainController();
$this->injectMockedServices($controller)['logger']
->expects(self::once())
->method('critical');
$response = $controller->run(
new Request('Api', 'GET', []),
);
self::assertInstanceOf(StatusResponse::class, $response);
self::assertSame(IResponse::S500_InternalServerError, $response->code);
$this->sendResponse($response, '{"status":"Endpoint is unable to route your request"}');
}
public function testNotFound(): void
{
$response = $this->createPlainController()->run(
new Request('Api', 'DELETE', ['action' => 'randomMethodThatDontEvenExists']),
);
self::assertInstanceOf(StatusResponse::class, $response);
self::assertSame(IResponse::S405_MethodNotAllowed, $response->code);
$this->sendResponse($response, '{"status":"Endpoint does not support DELETE method"}');
}
//endregion
//region Exception handling
public function testUnknownParameter(): void
{
$controller = $this->createExceptionHandlingController();
$this->injectMockedServices($controller)['logger']
->expects(self::once())
->method('critical');
$response = $controller->run(
new Request('Api', 'GET', ['action' => 'unknownParameter'])
);
self::assertInstanceOf(StatusResponse::class, $response);
self::assertSame(IResponse::S500_InternalServerError, $response->code);
$this->sendResponse($response, '{"status":"Endpoint is unable to handle your request"}');
}
public function testAuthenticationRequired(): void
{
$response = $this->createExceptionHandlingController()->run(
new Request('Api', 'GET', ['action' => 'authenticationRequired'])
);
self::assertInstanceOf(StatusResponse::class, $response);
self::assertSame(IResponse::S401_Unauthorized, $response->code);
$this->sendResponse($response, '{"status":"You need to authenticate first"}');
}
public function testInsufficientAuthorization(): void
{
$response = $this->createExceptionHandlingController()->run(
new Request('Api', 'GET', ['action' => 'insufficientAuthorization'])
);
self::assertInstanceOf(StatusResponse::class, $response);
self::assertSame(IResponse::S403_Forbidden, $response->code);
$this->sendResponse($response, '{"status":"You do not have permissions for that"}');
}
public function testResourceNotFound(): void
{
$response = $this->createExceptionHandlingController()->run(
new Request('Api', 'GET', ['action' => 'resourceNotFound'])
);
self::assertInstanceOf(StatusResponse::class, $response);
self::assertSame(IResponse::S404_NotFound, $response->code);
$this->sendResponse($response, '{"status":"Resource with identifier uuu-iii-ddd was not found"}');
}
public function testDomainException(): void
{
$controller = $this->createExceptionHandlingController();
$this->injectMockedServices($controller)['logger']
->expects(self::once())
->method('error');
$response = $controller->run(
new Request('Api', 'GET', ['action' => 'domainException'])
);
self::assertInstanceOf(StatusResponse::class, $response);
self::assertSame(IResponse::S500_InternalServerError, $response->code);
$this->sendResponse($response, '{"status":"This makes absolutely no sense"}');
}
public function testThrowable(): void
{
$controller = $this->createExceptionHandlingController();
$this->injectMockedServices($controller)['logger']
->expects(self::once())
->method('error');
$response = $controller->run(
new Request('Api', 'GET', ['action' => 'throwable'])
);
self::assertInstanceOf(StatusResponse::class, $response);
self::assertSame(IResponse::S500_InternalServerError, $response->code);
$this->sendResponse($response, '{"status":"An unexpected error occurred"}');
}
//endregion
//region Responses
public function testCustomResponse(): void
{
$response = $this->createActionResponseController()->run(
new Request('Api', 'GET', ['action' => 'customResponse'])
);
self::assertInstanceOf(VoidResponse::class, $response);
}
public function testStringTextResponse(): void
{
$response = $this->createActionResponseController()->run(
new Request('Api', 'GET', ['action' => 'stringResponse'])
);
self::assertInstanceOf(PlainTextResponse::class, $response);
$this->sendResponse($response, 'Just string response', 'text/plain');
}
public function testIntTextResponse(): void
{
$response = $this->createActionResponseController()->run(
new Request('Api', 'GET', ['action' => 'intResponse'])
);
self::assertInstanceOf(PlainTextResponse::class, $response);
$this->sendResponse($response, '42', 'text/plain');
}
public function testArrayResponse(): void
{
$response = $this->createActionResponseController()->run(
new Request('Api', 'GET', ['action' => 'arrayResponse'])
);
self::assertInstanceOf(JsonResponse::class, $response);
$this->sendResponse($response, '{"foo":"bar"}');
}
public function testObjectResponse(): void
{
$response = $this->createActionResponseController()->run(
new Request('Api', 'GET', ['action' => 'objectResponse'])
);
self::assertInstanceOf(JsonResponse::class, $response);
$this->sendResponse($response, '{"fofo":"barbar"}');
}
public function testNoResponse(): void
{
$response = $this->createActionResponseController()->run(
new Request('Api', 'GET', ['action' => 'noResponse'])
);
self::assertInstanceOf(StatusResponse::class, $response);
self::assertSame(IResponse::S204_NoContent, $response->code);
}
//endregion
//region Request body
public function testMappingRequestBodySuccess(): void
{
$response = $this->createRequestBodyController('{"name":"James","surname":"Bond"}')->run(
new Request('Api', 'POST', ['action' => 'index']),
);
self::assertInstanceOf(PlainTextResponse::class, $response);
self::assertSame('I am Bond, James Bond', $response->getPayload());
}
public function testMappingRequestBodyInvalid(): void
{
$response = $this->createRequestBodyController('{"name":42}')->run(
new Request('Api', 'POST', ['action' => 'index']),
);
self::assertInstanceOf(StatusResponse::class, $response);
self::assertSame(IResponse::S400_BadRequest, $response->code);
}
public function testMappingRequiredRequestBodyMissing(): void
{
$response = $this->createRequestBodyController(null)->run(
new Request('Api', 'POST', ['action' => 'index']),
);
self::assertInstanceOf(StatusResponse::class, $response);
self::assertSame(IResponse::S400_BadRequest, $response->code);
$this->sendResponse($response, '{"status":"Missing request body"}');
}
public function testMappingOptionalRequestBodyMissing(): void
{
$responseHasData = $this->createRequestBodyController('{"name":"James","surname":"Bond"}')->run(
new Request('Api', 'POST', ['action' => 'optional'])
);
$responseNoData = $this->createRequestBodyController(null)->run(
new Request('Api', 'POST', ['action' => 'optional'])
);
self::assertInstanceOf(PlainTextResponse::class, $responseNoData);
self::assertSame('no data, but that is fine', $responseNoData->getPayload());
self::assertInstanceOf(PlainTextResponse::class, $responseHasData);
self::assertSame('I am Bond, James Bond', $responseHasData->getPayload());
}
public function testRequestBodyMalformed(): void
{
$response = $this->createRequestBodyController('{"-_f[ads][]')->run(
new Request('Api', 'POST', ['action' => 'index']),
);
self::assertInstanceOf(StatusResponse::class, $response);
self::assertSame(IResponse::S400_BadRequest, $response->code);
$this->sendResponse($response, '{"status":"Malformed request body"}');
}
//endregion
//region Route parameters
public function testRouteParametersSuccess(): void
{
$response = $this->createRequestParameterController()->run(
new Request('Api', 'GET', [
'action' => 'index',
'name' => 'James',
'surname' => 'Bond',
'degree' => 'Ing',
]),
);
self::assertInstanceOf(PlainTextResponse::class, $response);
$this->sendResponse($response, 'I am Bond, Ing. James Bond', 'text/plain');
}
public function testRouteParametersMissingRequired(): void
{
$response = $this->createRequestParameterController()->run(
new Request('Api', 'GET', ['action' => 'index']),
);
self::assertInstanceOf(StatusResponse::class, $response);
self::assertSame(IResponse::S400_BadRequest, $response->code);
$this->sendResponse($response, '{"status":"Missing required parameter name"}');
}
public function testRouteParametersMissingOptionalWithDefaultValue(): void
{
$response = $this->createRequestParameterController()->run(
new Request('Api', 'GET', ['action' => 'index', 'name' => 'James'])
);
self::assertInstanceOf(PlainTextResponse::class, $response);
$this->sendResponse($response, 'I am Doe, Mr. James Doe', 'text/plain');
}
public function testRouteParametersCasting(): void
{
$controller = new readonly class extends ApiController {
public function getIndex(string $string, int $int, float $float, bool $bool1, bool $bool2, bool $bool3): string {
$values = [
is_string($string),
is_int($int),
is_float($float),
$bool1 === true,
$bool2 === true,
$bool3 === false,
];
return !in_array(false, $values, true) ? 'GOOD' : 'BAD';
}
};
$responseFail = $controller->run(
new Request('Api', 'GET', [
'action' => 'index',
'string' => 'string',
'int' => '42',
'float' => '3.14',
'bool1' => 'xx',
'bool2' => 'xx',
'bool3' => 'xx',
])
);
$requestSuccess = $controller->run(
new Request('Api', 'GET', [
'action' => 'index',
'string' => 'string',
'int' => '42',
'float' => '3.14',
'bool1' => 'true',
'bool2' => '1',
'bool3' => '0',
])
);
self::assertInstanceOf(PlainTextResponse::class, $responseFail);
self::assertSame('BAD', $responseFail->getPayload());
self::assertInstanceOf(PlainTextResponse::class, $requestSuccess);
self::assertSame('GOOD', $requestSuccess->getPayload());
}
//endregion
}