Completed
Push — master ( 5565c3...5f559e )
by Alex
01:47
created

testPutRequestForExistingDynamicRoute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
c 0
b 0
f 0
dl 0
loc 12
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
class DynamicRoutesUnitTest extends \PHPUnit\Framework\TestCase
4
{
5
6
    /**
7
     * Default setup
8
     *
9
     * {@inheritdoc}
10
     * @see \PHPUnit\Framework\TestCase::setUp()
11
     */
12
    public function setUp(): void
13
    {
14
        $_SERVER['REQUEST_METHOD'] = 'GET';
15
    }
16
17
    /**
18
     * Testing hasParam method
19
     */
20
    public function testValidatingParameter(): void
21
    {
22
        // setup
23
        $router = new \Mezon\Router\Router();
24
        $router->addRoute('/catalog/[i:foo]/', function () {
25
            // do nothing
26
        });
27
28
        $router->callRoute('/catalog/1/');
29
30
        // test body and assertions
31
        $this->assertTrue($router->hasParam('foo'));
32
        $this->assertFalse($router->hasParam('unexisting'));
33
    }
34
35
    /**
36
     * Testing getParam for existing param
37
     */
38
    public function testGettingExistingParameter(): void
39
    {
40
        // setup
41
        $router = new \Mezon\Router\Router();
42
        $router->addRoute('/catalog/[i:foo]/', function () {
43
            // do nothing
44
        });
45
46
        $router->callRoute('/catalog/1/');
47
48
        // test body
49
        $foo = $router->getParam('foo');
50
51
        // assertions
52
        $this->assertEquals(1, $foo);
53
    }
54
55
    /**
56
     * Testing saving of the route parameters
57
     */
58
    public function testSavingParameters(): void
59
    {
60
        $router = new \Mezon\Router\Router();
61
        $router->addRoute('/catalog/[i:foo]/', function ($route, $parameters) {
62
            return $parameters['foo'];
63
        });
64
65
        $router->callRoute('/catalog/-1/');
66
67
        $this->assertEquals($router->getParam('foo'), '-1', 'Float data violation');
68
    }
69
70
    /**
71
     * Testing command special chars.
72
     */
73
    public function testCommandSpecialChars(): void
74
    {
75
        $router = new \Mezon\Router\Router();
76
77
        $router->addRoute('/[a:url]/', function () {
78
            return 'GET';
79
        }, 'GET');
80
81
        $result = $router->callRoute('/.-@/');
82
        $this->assertEquals($result, 'GET', 'Invalid selected route');
83
    }
84
85
    /**
86
     * Testing strings.
87
     */
88
    public function testStringSpecialChars(): void
89
    {
90
        $router = new \Mezon\Router\Router();
91
92
        $router->addRoute('/[s:url]/', function () {
93
            return 'GET';
94
        }, 'GET');
95
96
        $result = $router->callRoute('/, ;:/');
97
        $this->assertEquals($result, 'GET', 'Invalid selected route');
98
    }
99
100
    /**
101
     * Method for checking id list.
102
     */
103
    public function ilTest($route, $params): string
104
    {
105
        return $params['ids'];
106
    }
107
108
    /**
109
     * Testing valid id list data types behaviour.
110
     */
111
    public function testValidIdListParams(): void
112
    {
113
        $router = new \Mezon\Router\Router();
114
        $router->addRoute('/catalog/[il:ids]/', [
115
            $this,
116
            'ilTest'
117
        ]);
118
119
        $result = $router->callRoute('/catalog/123,456,789/');
120
121
        $this->assertEquals($result, '123,456,789', 'Invalid router response');
122
    }
123
124
    /**
125
     * Testing valid id list data types behaviour.
126
     */
127
    public function testStringParamSecurity(): void
128
    {
129
        $router = new \Mezon\Router\Router();
130
        $router->addRoute('/catalog/[s:foo]/', function ($route, $parameters) {
131
            return $parameters['foo'];
132
        });
133
134
        $result = $router->callRoute('/catalog/123&456/');
135
136
        $this->assertEquals($result, '123&amp;456', 'Security data violation');
137
    }
138
139
    /**
140
     * Testing float value.
141
     */
142
    public function testFloatI(): void
143
    {
144
        $router = new \Mezon\Router\Router();
145
        $router->addRoute('/catalog/[i:foo]/', function ($route, $parameters) {
146
            return $parameters['foo'];
147
        });
148
149
        $result = $router->callRoute('/catalog/1.1/');
150
151
        $this->assertEquals($result, '1.1', 'Float data violation');
152
    }
153
154
    /**
155
     * Testing negative float value.
156
     */
157
    public function testNegativeFloatI(): void
158
    {
159
        $router = new \Mezon\Router\Router();
160
        $router->addRoute('/catalog/[i:foo]/', function ($route, $parameters) {
161
            return $parameters['foo'];
162
        });
163
164
        $result = $router->callRoute('/catalog/-1.1/');
165
166
        $this->assertEquals($result, '-1.1', 'Float data violation');
167
    }
168
169
    /**
170
     * Testing positive float value.
171
     */
172
    public function testPositiveFloatI(): void
173
    {
174
        $router = new \Mezon\Router\Router();
175
        $router->addRoute('/catalog/[i:foo]/', function ($route, $parameters) {
176
            return $parameters['foo'];
177
        });
178
179
        $result = $router->callRoute('/catalog/+1.1/');
180
181
        $this->assertEquals($result, '+1.1', 'Float data violation');
182
    }
183
184
    /**
185
     * Testing negative integer value
186
     */
187
    public function testNegativeIntegerI(): void
188
    {
189
        $router = new \Mezon\Router\Router();
190
        $router->addRoute('/catalog/[i:foo]/', function ($route, $parameters) {
191
            return $parameters['foo'];
192
        });
193
194
        $result = $router->callRoute('/catalog/-1/');
195
196
        $this->assertEquals('-1', $result, 'Float data violation');
197
    }
198
199
    /**
200
     * Testing positive integer value
201
     */
202
    public function testPositiveIntegerI(): void
203
    {
204
        $router = new \Mezon\Router\Router();
205
        $router->addRoute('/catalog/[i:foo]/', function ($route, $parameters) {
206
            return $parameters['foo'];
207
        });
208
209
        $result = $router->callRoute('/catalog/1/');
210
211
        $this->assertEquals('1', $result, 'Float data violation');
212
    }
213
214
    /**
215
     * Testing dynamic routes for DELETE requests.
216
     */
217
    public function testDeleteRequestForExistingDynamicRoute(): void
218
    {
219
        $_SERVER['REQUEST_METHOD'] = 'DELETE';
220
221
        $router = new \Mezon\Router\Router();
222
        $router->addRoute('/catalog/[i:cat_id]', function ($route) {
223
            return $route;
224
        }, 'DELETE');
225
226
        $result = $router->callRoute('/catalog/1024/');
227
228
        $this->assertEquals($result, '/catalog/1024/', 'Invalid extracted route');
229
    }
230
231
    /**
232
     * Testing dynamic routes for PUT requests.
233
     */
234
    public function testPutRequestForExistingDynamicRoute(): void
235
    {
236
        $_SERVER['REQUEST_METHOD'] = 'PUT';
237
238
        $router = new \Mezon\Router\Router();
239
        $router->addRoute('/catalog/[i:cat_id]', function ($route) {
240
            return $route;
241
        }, 'PUT');
242
243
        $result = $router->callRoute('/catalog/1024/');
244
245
        $this->assertEquals($result, '/catalog/1024/', 'Invalid extracted route');
246
    }
247
248
    /**
249
     * Testing dynamic routes for POST requests.
250
     */
251
    public function testPostRequestForExistingDynamicRoute(): void
252
    {
253
        $_SERVER['REQUEST_METHOD'] = 'POST';
254
255
        $router = new \Mezon\Router\Router();
256
        $router->addRoute('/catalog/[i:cat_id]', function ($route) {
257
            return $route;
258
        }, 'POST');
259
260
        $result = $router->callRoute('/catalog/1024/');
261
262
        $this->assertEquals($result, '/catalog/1024/', 'Invalid extracted route');
263
    }
264
265
    /**
266
     * Testing invalid data types behaviour.
267
     */
268
    public function testInvalidType(): void
269
    {
270
        $router = new \Mezon\Router\Router();
271
        $router->addRoute('/catalog/[unexisting-type:i]/item/', [
272
            $this,
273
            'helloWorldOutput'
274
        ]);
275
276
        try {
277
            $router->callRoute('/catalog/1024/item/');
278
            $this->assertFalse(true, 'Exception expected');
279
        } catch (Exception $e) {
280
            $this->assertFalse(false, '');
281
        }
282
    }
283
284
    /**
285
     * Testing invalid data types behaviour.
286
     */
287
    public function testValidInvalidTypes(): void
288
    {
289
        $router = new \Mezon\Router\Router();
290
        $router->addRoute('/catalog/[i:cat_id]/item/[unexisting-type-trace:item_id]/', [
291
            $this,
292
            'helloWorldOutput'
293
        ]);
294
295
        try {
296
            $router->callRoute('/catalog/1024/item/2048/');
297
            $this->assertFalse(true, 'Exception expected');
298
        } catch (Exception $e) {
299
            $this->assertFalse(false, '');
300
        }
301
    }
302
303
    /**
304
     * Testing valid data types behaviour.
305
     */
306
    public function testValidTypes(): void
307
    {
308
        $exception = '';
309
        $router = new \Mezon\Router\Router();
310
        $router->addRoute('/catalog/[i:cat_id]/item/[i:item_id]/', [
311
            $this,
312
            'helloWorldOutput'
313
        ]);
314
315
        try {
316
            $router->callRoute('/catalog/1024/item/2048/');
317
        } catch (Exception $e) {
318
            $exception = $e->getMessage();
319
        }
320
321
        $msg = "Illegal parameter type";
322
323
        $this->assertFalse(strpos($exception, $msg), 'Valid type expected');
324
    }
325
326
    /**
327
     * Testing valid integer data types behaviour.
328
     */
329
    public function testValidIntegerParams(): void
330
    {
331
        $exception = '';
332
        $router = new \Mezon\Router\Router();
333
        $router->addRoute('/catalog/[i:cat_id]/', [
334
            $this,
335
            'helloWorldOutput'
336
        ]);
337
338
        try {
339
            $router->callRoute('/catalog/1024/');
340
        } catch (Exception $e) {
341
            $exception = $e->getMessage();
342
        }
343
344
        $msg = "Illegal parameter type";
345
346
        $this->assertFalse(strpos($exception, $msg), 'Valid type expected');
347
    }
348
349
    /**
350
     * Testing valid alnum data types behaviour.
351
     */
352
    public function testValidAlnumParams(): void
353
    {
354
        $exception = '';
355
        $router = new \Mezon\Router\Router();
356
        $router->addRoute('/catalog/[a:cat_id]/', [
357
            $this,
358
            'helloWorldOutput'
359
        ]);
360
361
        try {
362
            $router->callRoute('/catalog/foo/');
363
        } catch (Exception $e) {
364
            $exception = $e->getMessage();
365
        }
366
367
        $msg = "Illegal parameter type";
368
369
        $this->assertFalse(strpos($exception, $msg), 'Valid type expected');
370
    }
371
372
    /**
373
     * Testing invalid integer data types behaviour.
374
     */
375
    public function testInValidIntegerParams(): void
376
    {
377
        $exception = '';
378
        $router = new \Mezon\Router\Router();
379
        $router->addRoute('/catalog/[i:cat_id]/', [
380
            $this,
381
            'helloWorldOutput'
382
        ]);
383
384
        try {
385
            $router->callRoute('/catalog/a1024/');
386
        } catch (Exception $e) {
387
            $exception = $e->getMessage();
388
        }
389
390
        $msg = "The processor was not found for the route /catalog/a1024/";
391
392
        $this->assertNotFalse(strpos($exception, $msg), 'Invalid error response');
393
    }
394
395
    /**
396
     * Testing invalid alnum data types behaviour.
397
     */
398
    public function testInValidAlnumParams(): void
399
    {
400
        $exception = '';
401
        $router = new \Mezon\Router\Router();
402
        $router->addRoute('/catalog/[a:cat_id]/', [
403
            $this,
404
            'helloWorldOutput'
405
        ]);
406
407
        try {
408
            $router->callRoute('/catalog/~foo/');
409
        } catch (Exception $e) {
410
            $exception = $e->getMessage();
411
        }
412
413
        $msg = "The processor was not found for the route /catalog/~foo/";
414
415
        $this->assertNotFalse(strpos($exception, $msg), 'Invalid error response');
416
    }
417
418
    /**
419
     * Testing parameter extractor.
420
     */
421
    public function testValidExtractedParameter(): void
422
    {
423
        $router = new \Mezon\Router\Router();
424
        $router->addRoute('/catalog/[a:cat_id]/', function ($route, $parameters) {
425
            return $parameters['cat_id'];
426
        });
427
428
        $result = $router->callRoute('/catalog/foo/');
429
430
        $this->assertEquals($result, 'foo', 'Invalid extracted parameter');
431
    }
432
433
    /**
434
     * Testing parameter extractor.
435
     */
436
    public function testValidExtractedParameters(): void
437
    {
438
        $router = new \Mezon\Router\Router();
439
        $router->addRoute(
440
            '/catalog/[a:cat_id]/[i:item_id]',
441
            function ($route, $parameters) {
442
                return $parameters['cat_id'] . $parameters['item_id'];
443
            });
444
445
        $result = $router->callRoute('/catalog/foo/1024/');
446
447
        $this->assertEquals($result, 'foo1024', 'Invalid extracted parameter');
448
    }
449
450
    /**
451
     * Testing parameter extractor.
452
     */
453
    public function testValidRouteParameter(): void
454
    {
455
        $router = new \Mezon\Router\Router();
456
        $router->addRoute('/catalog/', function ($route) {
457
            return $route;
458
        });
459
        $router->addRoute('/catalog/[i:cat_id]', function ($route) {
460
            return $route;
461
        });
462
463
        $result = $router->callRoute('/catalog/');
464
465
        $this->assertEquals($result, '/catalog/', 'Invalid extracted route');
466
467
        $result = $router->callRoute('/catalog/1024/');
468
469
        $this->assertEquals($result, '/catalog/1024/', 'Invalid extracted route');
470
    }
471
}
472