Passed
Push — master ( a4ddd3...82bd79 )
by Alex
06:44
created

ExtractParametersTestClass   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 6
eloc 26
c 1
b 1
f 0
dl 0
loc 96
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testValidExtractedParameters() 0 17 1
A testMultyple() 0 17 2
A setUp() 0 3 1
A testValidExtractedParameter() 0 17 1
A testValidatingParameter() 0 13 1
1
<?php
2
declare(strict_types = 1);
3
namespace Mezon\Router\Tests\Base;
4
5
use PHPUnit\Framework\TestCase;
6
7
/**
8
 *
9
 * @psalm-suppress PropertyNotSetInConstructor
10
 */
11
abstract class ExtractParametersTestClass extends BaseRouterUnitTestClass
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 = $this->getRouter();
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 = $this->getRouter();
49
        $router->addRoute('/catalog/[a:cat_id]/', /**
50
         *
51
         * @param string $route
52
         *            route
53
         * @param string[] $parameters
54
         */
55
        function (string $route, array $parameters): string {
56
            return $parameters['cat_id'];
57
        });
58
59
        /** @var string $result */
60
        $result = $router->callRoute('/catalog/foo/');
61
62
        $this->assertEquals('foo', $result);
63
    }
64
65
    /**
66
     * Testing parameter extractor.
67
     */
68
    public function testValidExtractedParameters(): void
69
    {
70
        $router = $this->getRouter();
71
        $router->addRoute('/catalog/[a:cat_id]/[i:item_id]', /**
72
         *
73
         * @param string $route
74
         *            route
75
         * @param string[] $parameters
76
         */
77
        function (string $route, array $parameters): string {
78
            return $parameters['cat_id'] . $parameters['item_id'];
79
        });
80
81
        /** @var string $result */
82
        $result = $router->callRoute('catalog/foo/1024');
83
84
        $this->assertEquals('foo1024', $result);
85
    }
86
87
    /**
88
     * Testing multyple routes
89
     */
90
    public function testMultyple(): void
91
    {
92
        // setup
93
        $router = $this->getRouter();
94
        for ($i = 0; $i < 15; $i ++) {
95
            $router->addRoute('/multiple/' . $i . '/[i:id]', function () {
96
                return 'done!';
97
            });
98
        }
99
100
        // test body
101
        /** @var string $result */
102
        $result = $router->callRoute('/multiple/' . rand(0, 14) . '/12345');
103
104
        // assertions
105
        $this->assertEquals('done!', $result);
106
        $this->assertEquals('12345', $router->getParam('id'));
107
    }
108
}
109