Completed
Push — master ( 6c2bdc...71996f )
by Alex
01:53
created

ExtractParametersUnitTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 1
c 1
b 1
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
namespace Mezon\Router\Tests;
3
4
use Mezon\Router\Router;
5
6
class ExtractParametersUnitTest extends \PHPUnit\Framework\TestCase
7
{
8
9
    /**
10
     * Default setup
11
     *
12
     * {@inheritdoc}
13
     * @see \PHPUnit\Framework\TestCase::setUp()
14
     */
15
    public function setUp(): void
16
    {
17
        $_SERVER['REQUEST_METHOD'] = 'GET';
18
    }
19
20
    /**
21
     * Testing hasParam method
22
     */
23
    public function testValidatingParameter(): void
24
    {
25
        // setup
26
        $router = new \Mezon\Router\Router();
27
        $router->addRoute('/catalog/[i:foo]/', function () {
28
            // do nothing
29
        });
30
31
        $router->callRoute('/catalog/1/');
32
33
        // test body and assertions
34
        $this->assertTrue($router->hasParam('foo'));
35
        $this->assertFalse($router->hasParam('unexisting'));
36
    }
37
38
    /**
39
     * Testing parameter extractor.
40
     */
41
    public function testValidExtractedParameter(): void
42
    {
43
        $router = new \Mezon\Router\Router();
44
        $router->addRoute('/catalog/[a:cat_id]/', function ($route, $parameters) {
45
            return $parameters['cat_id'];
46
        });
47
48
        $result = $router->callRoute('/catalog/foo/');
49
50
        $this->assertEquals($result, 'foo', 'Invalid extracted parameter');
51
    }
52
53
    /**
54
     * Testing parameter extractor.
55
     */
56
    public function testValidExtractedParameters(): void
57
    {
58
        $router = new \Mezon\Router\Router();
59
        $router->addRoute(
60
            '/catalog/[a:cat_id]/[i:item_id]',
61
            function ($route, $parameters) {
62
                return $parameters['cat_id'] . $parameters['item_id'];
63
            });
64
65
        $result = $router->callRoute('catalog/foo/1024');
66
67
        $this->assertEquals($result, 'foo1024', 'Invalid extracted parameter');
68
    }
69
70
    /**
71
     * Testing parameter extractor.
72
     */
73
    public function testValidRouteParameter(): void
74
    {
75
        $router = new \Mezon\Router\Router();
76
        $router->addRoute('/catalog/all/', function ($route) {
77
            return $route;
78
        });
79
        $router->addRoute('/catalog/[i:cat_id]', function ($route) {
80
            return $route;
81
        });
82
83
        // first reading
84
        $result = $router->callRoute('/catalog/all/');
85
        $this->assertEquals($result, 'catalog/all');
86
87
        // second route
88
        $result = $router->callRoute('/catalog/1024/');
89
        $this->assertEquals($result, 'catalog/1024');
90
91
        // reading regexp from cache in the _getRouteMatcherRegExPattern method
92
        $router->warmCache();
93
        $result = $router->callRoute('/catalog/1024/');
94
        $this->assertEquals($result, 'catalog/1024');
95
    }
96
97
    /**
98
     * Testing multyple routes
99
     */
100
    public function testMultyple(): void
101
    {
102
        // setup
103
        $router = new Router();
104
        for ($i = 0; $i < 15; $i ++) {
105
            $router->addRoute('/multiple/' . $i . '/[i:id]', function () {
106
                return 'done!';
107
            });
108
        }
109
110
        // test body
111
        $result = $router->callRoute('/multiple/' . rand(0, 14) . '/12345');
112
113
        // assertions
114
        $this->assertEquals('done!', $result);
115
        $this->assertEquals('12345', $router->getParam('id'));
116
    }
117
}
118