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

ExtractParametersUnitTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 7
eloc 39
c 1
b 0
f 1
dl 0
loc 111
rs 10

6 Methods

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