Completed
Push — master ( 15f476...52f8b5 )
by Alex
07:26
created

DynamicRoutesUnitTest::testNegativeFloatI()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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