1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
/** |
4
|
|
|
* /src/Utils/Tests/RestTraitTestCase.php |
5
|
|
|
* |
6
|
|
|
* @author TLe, Tarmo Leppänen <[email protected]> |
7
|
|
|
*/ |
8
|
|
|
namespace App\Utils\Tests; |
9
|
|
|
|
10
|
|
|
use Ramsey\Uuid\Uuid; |
11
|
|
|
use Symfony\Component\HttpFoundation\Response; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class RestTraitTestCase |
15
|
|
|
* |
16
|
|
|
* @codeCoverageIgnore |
17
|
|
|
* |
18
|
|
|
* @package App\Utils\Tests |
19
|
|
|
* @author TLe, Tarmo Leppänen <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
abstract class RestTraitTestCase extends WebTestCase |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @return array |
25
|
|
|
*/ |
26
|
|
|
abstract public function getValidUsers(): array; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @return array |
30
|
|
|
*/ |
31
|
|
|
abstract public function getInvalidUsers(): array; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected static $route; |
37
|
|
|
|
38
|
|
|
/** @noinspection PhpUndefinedNamespaceInspection */ |
39
|
|
|
/** |
40
|
|
|
* @dataProvider dataProviderTestThatCountRouteDoesNotAllowNotSupportedHttpMethods |
41
|
|
|
* |
42
|
|
|
* |
43
|
|
|
* @param string|null $username |
44
|
|
|
* @param string|null $password |
45
|
|
|
* @param string $method |
46
|
|
|
*/ |
47
|
|
|
public function testThatCountRouteDoesNotAllowNotSupportedHttpMethods( |
48
|
|
|
string $username = null, |
49
|
|
|
string $password = null, |
50
|
|
|
string $method |
51
|
|
|
): void { |
52
|
|
|
$client = $this->getClient($username, $password); |
53
|
|
|
$client->request($method, static::$route . '/count'); |
54
|
|
|
|
55
|
|
|
$response = $client->getResponse(); |
56
|
|
|
|
57
|
|
|
static::assertInstanceOf(Response::class, $response); |
58
|
|
|
|
59
|
|
|
/** @noinspection NullPointerExceptionInspection */ |
60
|
|
|
static::assertSame(405, $response->getStatusCode(), $response->getContent()); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** @noinspection PhpUndefinedNamespaceInspection */ |
64
|
|
|
/** |
65
|
|
|
* @dataProvider dataProviderTestThatCountRouteWorksWithAllowedHttpMethods |
66
|
|
|
* |
67
|
|
|
* @param string|null $username |
68
|
|
|
* @param string|null $password |
69
|
|
|
* @param string $method |
70
|
|
|
*/ |
71
|
|
|
public function testThatCountRouteWorksWithAllowedHttpMethods( |
72
|
|
|
string $username = null, |
73
|
|
|
string $password = null, |
74
|
|
|
string $method |
75
|
|
|
): void { |
76
|
|
|
$client = $this->getClient($username, $password); |
77
|
|
|
$client->request($method, static::$route . '/count'); |
78
|
|
|
|
79
|
|
|
$response = $client->getResponse(); |
80
|
|
|
|
81
|
|
|
static::assertInstanceOf(Response::class, $response); |
82
|
|
|
|
83
|
|
|
/** @noinspection NullPointerExceptionInspection */ |
84
|
|
|
static::assertSame(500, $response->getStatusCode(), $response->getContent()); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** @noinspection PhpUndefinedNamespaceInspection */ |
88
|
|
|
/** |
89
|
|
|
* @dataProvider dataProviderTestThatCountRouteDoesNotAllowInvalidUser |
90
|
|
|
* |
91
|
|
|
* @param string|null $username |
92
|
|
|
* @param string|null $password |
93
|
|
|
* @param string $method |
94
|
|
|
*/ |
95
|
|
|
public function testThatCountRouteDoesNotAllowInvalidUser( |
96
|
|
|
string $username = null, |
97
|
|
|
string $password = null, |
98
|
|
|
string $method |
99
|
|
|
): void { |
100
|
|
|
$client = $this->getClient($username, $password); |
101
|
|
|
$client->request($method, static::$route . '/count'); |
102
|
|
|
|
103
|
|
|
$response = $client->getResponse(); |
104
|
|
|
|
105
|
|
|
static::assertInstanceOf(Response::class, $response); |
106
|
|
|
|
107
|
|
|
/** @noinspection NullPointerExceptionInspection */ |
108
|
|
|
static::assertSame($username === null ? 401 : 403, $response->getStatusCode(), $response->getContent()); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** @noinspection PhpUndefinedNamespaceInspection */ |
112
|
|
|
/** |
113
|
|
|
* @dataProvider dataProviderTestThatRootRouteDoesNotAllowNotSupportedHttpMethods |
114
|
|
|
* |
115
|
|
|
* @param string|null $username |
116
|
|
|
* @param string|null $password |
117
|
|
|
* @param string $method |
118
|
|
|
*/ |
119
|
|
|
public function testThatRootRouteDoesNotAllowNotSupportedHttpMethods( |
120
|
|
|
string $username = null, |
121
|
|
|
string $password = null, |
122
|
|
|
string $method |
123
|
|
|
): void { |
124
|
|
|
$client = $this->getClient($username, $password); |
125
|
|
|
$client->request($method, static::$route); |
126
|
|
|
|
127
|
|
|
$response = $client->getResponse(); |
128
|
|
|
|
129
|
|
|
static::assertInstanceOf(Response::class, $response); |
130
|
|
|
|
131
|
|
|
/** @noinspection NullPointerExceptionInspection */ |
132
|
|
|
static::assertSame(405, $response->getStatusCode(), $response->getContent()); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** @noinspection PhpUndefinedNamespaceInspection */ |
136
|
|
|
/** |
137
|
|
|
* @dataProvider dataProviderTestThatRootRouteWorksWithAllowedHttpMethods |
138
|
|
|
* |
139
|
|
|
* @param string|null $username |
140
|
|
|
* @param string|null $password |
141
|
|
|
* @param string $method |
142
|
|
|
*/ |
143
|
|
|
public function testThatRootRouteWorksWithAllowedHttpMethods( |
144
|
|
|
string $username = null, |
145
|
|
|
string $password = null, |
146
|
|
|
string $method |
147
|
|
|
): void { |
148
|
|
|
$client = $this->getClient($username, $password); |
149
|
|
|
$client->request($method, static::$route); |
150
|
|
|
|
151
|
|
|
$response = $client->getResponse(); |
152
|
|
|
|
153
|
|
|
static::assertInstanceOf(Response::class, $response); |
154
|
|
|
|
155
|
|
|
/** @noinspection NullPointerExceptionInspection */ |
156
|
|
|
static::assertSame(500, $response->getStatusCode(), $response->getContent()); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** @noinspection PhpUndefinedNamespaceInspection */ |
160
|
|
|
/** |
161
|
|
|
* @dataProvider dataProviderTestThatRootRouteDoesNotAllowInvalidUser |
162
|
|
|
* |
163
|
|
|
* @param string|null $username |
164
|
|
|
* @param string|null $password |
165
|
|
|
* @param string $method |
166
|
|
|
*/ |
167
|
|
|
public function testThatRootRouteDoesNotAllowInvalidUser( |
168
|
|
|
string $username = null, |
169
|
|
|
string $password = null, |
170
|
|
|
string $method |
171
|
|
|
): void { |
172
|
|
|
$client = $this->getClient($username, $password); |
173
|
|
|
$client->request($method, static::$route); |
174
|
|
|
|
175
|
|
|
$response = $client->getResponse(); |
176
|
|
|
|
177
|
|
|
static::assertInstanceOf(Response::class, $response); |
178
|
|
|
|
179
|
|
|
/** @noinspection NullPointerExceptionInspection */ |
180
|
|
|
static::assertSame($username === null ? 401 : 403, $response->getStatusCode(), $response->getContent()); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** @noinspection PhpUndefinedNamespaceInspection */ |
184
|
|
|
/** |
185
|
|
|
* @dataProvider dataProviderTestThatRootRouteWithIdDoesNotAllowNotSupportedHttpMethods |
186
|
|
|
* |
187
|
|
|
* @param string|null $username |
188
|
|
|
* @param string|null $password |
189
|
|
|
* @param string $method |
190
|
|
|
*/ |
191
|
|
|
public function testThatRootRouteWithIdDoesNotAllowNotSupportedHttpMethods( |
192
|
|
|
string $username = null, |
193
|
|
|
string $password = null, |
194
|
|
|
string $method |
195
|
|
|
): void { |
196
|
|
|
$uuid = Uuid::uuid4()->toString(); |
197
|
|
|
|
198
|
|
|
$client = $this->getClient($username, $password); |
199
|
|
|
$client->request($method, static::$route . '/' . $uuid); |
200
|
|
|
|
201
|
|
|
$response = $client->getResponse(); |
202
|
|
|
|
203
|
|
|
static::assertInstanceOf(Response::class, $response); |
204
|
|
|
|
205
|
|
|
/** @noinspection NullPointerExceptionInspection */ |
206
|
|
|
static::assertSame(405, $response->getStatusCode(), $response->getContent()); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** @noinspection PhpUndefinedNamespaceInspection */ |
210
|
|
|
/** |
211
|
|
|
* @dataProvider dataProviderTestThatRootRouteWithIdWorksWithAllowedHttpMethods |
212
|
|
|
* |
213
|
|
|
* @param string|null $username |
214
|
|
|
* @param string|null $password |
215
|
|
|
* @param string $method |
216
|
|
|
*/ |
217
|
|
|
public function testThatRootRouteWithIdWorksWithAllowedHttpMethods( |
218
|
|
|
string $username = null, |
219
|
|
|
string $password = null, |
220
|
|
|
string $method |
221
|
|
|
): void { |
222
|
|
|
$uuid = Uuid::uuid4()->toString(); |
223
|
|
|
|
224
|
|
|
$client = $this->getClient($username, $password); |
225
|
|
|
$client->request($method, static::$route . '/' . $uuid); |
226
|
|
|
|
227
|
|
|
$response = $client->getResponse(); |
228
|
|
|
|
229
|
|
|
static::assertInstanceOf(Response::class, $response); |
230
|
|
|
|
231
|
|
|
/** @noinspection NullPointerExceptionInspection */ |
232
|
|
|
static::assertSame(500, $response->getStatusCode(), $response->getContent()); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** @noinspection PhpUndefinedNamespaceInspection */ |
236
|
|
|
/** |
237
|
|
|
* @dataProvider dataProviderTestThatRootRouteWithIdDoesNotAllowInvalidUser |
238
|
|
|
* |
239
|
|
|
* @param string|null $username |
240
|
|
|
* @param string|null $password |
241
|
|
|
* @param string $method |
242
|
|
|
*/ |
243
|
|
|
public function testThatRootRouteWithIdDoesNotAllowInvalidUser( |
244
|
|
|
string $username = null, |
245
|
|
|
string $password = null, |
246
|
|
|
string $method |
247
|
|
|
): void { |
248
|
|
|
$uuid = Uuid::uuid4()->toString(); |
249
|
|
|
|
250
|
|
|
$client = $this->getClient($username, $password); |
251
|
|
|
$client->request($method, static::$route . '/' . $uuid); |
252
|
|
|
|
253
|
|
|
$response = $client->getResponse(); |
254
|
|
|
|
255
|
|
|
static::assertInstanceOf(Response::class, $response); |
256
|
|
|
|
257
|
|
|
/** @noinspection NullPointerExceptionInspection */ |
258
|
|
|
static::assertSame($username === null ? 401 : 403, $response->getStatusCode(), $response->getContent()); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** @noinspection PhpUndefinedNamespaceInspection */ |
262
|
|
|
/** |
263
|
|
|
* @dataProvider dataProviderTestThatIdsRouteDoesNotAllowNotSupportedHttpMethods |
264
|
|
|
* |
265
|
|
|
* @param string|null $username |
266
|
|
|
* @param string|null $password |
267
|
|
|
* @param string $method |
268
|
|
|
*/ |
269
|
|
|
public function testThatIdsRouteDoesNotAllowNotSupportedHttpMethods( |
270
|
|
|
string $username = null, |
271
|
|
|
string $password = null, |
272
|
|
|
string $method |
273
|
|
|
): void { |
274
|
|
|
$client = $this->getClient($username, $password); |
275
|
|
|
$client->request($method, static::$route . '/ids'); |
276
|
|
|
|
277
|
|
|
$response = $client->getResponse(); |
278
|
|
|
|
279
|
|
|
static::assertInstanceOf(Response::class, $response); |
280
|
|
|
|
281
|
|
|
/** @noinspection NullPointerExceptionInspection */ |
282
|
|
|
static::assertSame(405, $response->getStatusCode(), $response->getContent()); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** @noinspection PhpUndefinedNamespaceInspection */ |
286
|
|
|
/** |
287
|
|
|
* @dataProvider dataProviderTestThatIdsRouteWorksWithAllowedHttpMethods |
288
|
|
|
* |
289
|
|
|
* @param string|null $username |
290
|
|
|
* @param string|null $password |
291
|
|
|
* @param string $method |
292
|
|
|
*/ |
293
|
|
|
public function testThatIdsRouteWorksWithAllowedHttpMethods( |
294
|
|
|
string $username = null, |
295
|
|
|
string $password = null, |
296
|
|
|
string $method |
297
|
|
|
): void { |
298
|
|
|
$client = $this->getClient($username, $password); |
299
|
|
|
$client->request($method, static::$route . '/ids'); |
300
|
|
|
|
301
|
|
|
$response = $client->getResponse(); |
302
|
|
|
|
303
|
|
|
static::assertInstanceOf(Response::class, $response); |
304
|
|
|
|
305
|
|
|
/** @noinspection NullPointerExceptionInspection */ |
306
|
|
|
static::assertSame(500, $response->getStatusCode(), $response->getContent()); |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** @noinspection PhpUndefinedNamespaceInspection */ |
310
|
|
|
/** |
311
|
|
|
* @dataProvider dataProviderTestThatIdsRouteDoesNotAllowInvalidUser |
312
|
|
|
* |
313
|
|
|
* @param string|null $username |
314
|
|
|
* @param string|null $password |
315
|
|
|
* @param string $method |
316
|
|
|
*/ |
317
|
|
|
public function testThatIdsRouteDoesNotAllowInvalidUser( |
318
|
|
|
string $username = null, |
319
|
|
|
string $password = null, |
320
|
|
|
string $method |
321
|
|
|
): void { |
322
|
|
|
$client = $this->getClient($username, $password); |
323
|
|
|
$client->request($method, static::$route . '/ids'); |
324
|
|
|
|
325
|
|
|
$response = $client->getResponse(); |
326
|
|
|
|
327
|
|
|
static::assertInstanceOf(Response::class, $response); |
328
|
|
|
|
329
|
|
|
/** @noinspection NullPointerExceptionInspection */ |
330
|
|
|
static::assertSame($username === null ? 401 : 403, $response->getStatusCode(), $response->getContent()); |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* @return array |
335
|
|
|
*/ |
336
|
|
|
public function dataProviderTestThatCountRouteDoesNotAllowNotSupportedHttpMethods(): array |
337
|
|
|
{ |
338
|
|
|
$methods = [ |
339
|
|
|
['HEAD'], |
340
|
|
|
['POST'], |
341
|
|
|
['PUT'], |
342
|
|
|
['DELETE'], |
343
|
|
|
['OPTIONS'], |
344
|
|
|
['CONNECT'], |
345
|
|
|
['foobar'], |
346
|
|
|
]; |
347
|
|
|
|
348
|
|
|
return $this->createDataForTest($this->getValidUsers(), $methods); |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
/** |
352
|
|
|
* @return array |
353
|
|
|
*/ |
354
|
|
|
public function dataProviderTestThatCountRouteWorksWithAllowedHttpMethods(): array |
355
|
|
|
{ |
356
|
|
|
$methods = [ |
357
|
|
|
['GET'], |
358
|
|
|
]; |
359
|
|
|
|
360
|
|
|
return $this->createDataForTest($this->getValidUsers(), $methods); |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
/** |
364
|
|
|
* @return array |
365
|
|
|
*/ |
366
|
|
|
public function dataProviderTestThatCountRouteDoesNotAllowInvalidUser(): array |
367
|
|
|
{ |
368
|
|
|
$methods = [ |
369
|
|
|
['GET'], |
370
|
|
|
]; |
371
|
|
|
|
372
|
|
|
return $this->createDataForTest($this->getInvalidUsers(), $methods); |
373
|
|
|
} |
374
|
|
|
|
375
|
|
|
/** |
376
|
|
|
* @return array |
377
|
|
|
*/ |
378
|
|
|
public function dataProviderTestThatRootRouteDoesNotAllowNotSupportedHttpMethods(): array |
379
|
|
|
{ |
380
|
|
|
$methods = [ |
381
|
|
|
['PUT'], |
382
|
|
|
['DELETE'], |
383
|
|
|
['OPTIONS'], |
384
|
|
|
['CONNECT'], |
385
|
|
|
['foobar'], |
386
|
|
|
]; |
387
|
|
|
|
388
|
|
|
return $this->createDataForTest($this->getValidUsers(), $methods); |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
/** |
392
|
|
|
* @return array |
393
|
|
|
*/ |
394
|
|
|
public function dataProviderTestThatRootRouteWorksWithAllowedHttpMethods(): array |
395
|
|
|
{ |
396
|
|
|
$methods = [ |
397
|
|
|
['GET'], |
398
|
|
|
['POST'], |
399
|
|
|
]; |
400
|
|
|
|
401
|
|
|
return $this->createDataForTest($this->getValidUsers(), $methods); |
402
|
|
|
} |
403
|
|
|
|
404
|
|
|
/** |
405
|
|
|
* @return array |
406
|
|
|
*/ |
407
|
|
|
public function dataProviderTestThatRootRouteDoesNotAllowInvalidUser(): array |
408
|
|
|
{ |
409
|
|
|
$methods = [ |
410
|
|
|
['GET'], |
411
|
|
|
['POST'], |
412
|
|
|
]; |
413
|
|
|
|
414
|
|
|
return $this->createDataForTest($this->getInvalidUsers(), $methods); |
415
|
|
|
} |
416
|
|
|
|
417
|
|
|
/** |
418
|
|
|
* @return array |
419
|
|
|
*/ |
420
|
|
|
public function dataProviderTestThatRootRouteWithIdDoesNotAllowNotSupportedHttpMethods(): array |
421
|
|
|
{ |
422
|
|
|
$methods = [ |
423
|
|
|
['POST'], |
424
|
|
|
['OPTIONS'], |
425
|
|
|
['CONNECT'], |
426
|
|
|
['foobar'], |
427
|
|
|
]; |
428
|
|
|
|
429
|
|
|
return $this->createDataForTest($this->getValidUsers(), $methods); |
430
|
|
|
} |
431
|
|
|
|
432
|
|
|
/** |
433
|
|
|
* @return array |
434
|
|
|
*/ |
435
|
|
|
public function dataProviderTestThatRootRouteWithIdWorksWithAllowedHttpMethods(): array |
436
|
|
|
{ |
437
|
|
|
$methods = [ |
438
|
|
|
['DELETE'], |
439
|
|
|
['GET'], |
440
|
|
|
['PUT'], |
441
|
|
|
]; |
442
|
|
|
|
443
|
|
|
return $this->createDataForTest($this->getValidUsers(), $methods); |
444
|
|
|
} |
445
|
|
|
|
446
|
|
|
/** |
447
|
|
|
* @return array |
448
|
|
|
*/ |
449
|
|
|
public function dataProviderTestThatRootRouteWithIdDoesNotAllowInvalidUser(): array |
450
|
|
|
{ |
451
|
|
|
$methods = [ |
452
|
|
|
['DELETE'], |
453
|
|
|
['GET'], |
454
|
|
|
['PUT'], |
455
|
|
|
]; |
456
|
|
|
|
457
|
|
|
return $this->createDataForTest($this->getInvalidUsers(), $methods); |
458
|
|
|
} |
459
|
|
|
|
460
|
|
|
/** |
461
|
|
|
* @return array |
462
|
|
|
*/ |
463
|
|
|
public function dataProviderTestThatIdsRouteDoesNotAllowNotSupportedHttpMethods(): array |
464
|
|
|
{ |
465
|
|
|
$methods = [ |
466
|
|
|
['POST'], |
467
|
|
|
['PUT'], |
468
|
|
|
['DELETE'], |
469
|
|
|
['OPTIONS'], |
470
|
|
|
['CONNECT'], |
471
|
|
|
['foobar'], |
472
|
|
|
]; |
473
|
|
|
|
474
|
|
|
return $this->createDataForTest($this->getValidUsers(), $methods); |
475
|
|
|
} |
476
|
|
|
|
477
|
|
|
/** |
478
|
|
|
* @return array |
479
|
|
|
*/ |
480
|
|
|
public function dataProviderTestThatIdsRouteWorksWithAllowedHttpMethods(): array |
481
|
|
|
{ |
482
|
|
|
$methods = [ |
483
|
|
|
['GET'] |
484
|
|
|
]; |
485
|
|
|
|
486
|
|
|
return $this->createDataForTest($this->getValidUsers(), $methods); |
487
|
|
|
} |
488
|
|
|
|
489
|
|
|
/** |
490
|
|
|
* @return array |
491
|
|
|
*/ |
492
|
|
|
public function dataProviderTestThatIdsRouteDoesNotAllowInvalidUser(): array |
493
|
|
|
{ |
494
|
|
|
$methods = [ |
495
|
|
|
['GET'] |
496
|
|
|
]; |
497
|
|
|
|
498
|
|
|
return $this->createDataForTest($this->getInvalidUsers(), $methods); |
499
|
|
|
} |
500
|
|
|
|
501
|
|
|
/** |
502
|
|
|
* @param array $users |
503
|
|
|
* @param array $methods |
504
|
|
|
* |
505
|
|
|
* @return array |
506
|
|
|
*/ |
507
|
|
|
private function createDataForTest(array $users, array $methods): array |
508
|
|
|
{ |
509
|
|
|
$output = []; |
510
|
|
|
|
511
|
|
|
foreach ($users as $userData) { |
512
|
|
|
foreach ($methods as $method) { |
513
|
|
|
$output[] = \array_merge($userData, $method); |
514
|
|
|
} |
515
|
|
|
} |
516
|
|
|
|
517
|
|
|
return $output; |
518
|
|
|
} |
519
|
|
|
} |
520
|
|
|
|