Passed
Push — master ( 4cfdc5...37c7d0 )
by Alex
07:00
created

ExtractParametersUnitTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 40
c 0
b 0
f 0
dl 0
loc 112
rs 10

6 Methods

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