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

universalRouteDataProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 40
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 21
c 1
b 0
f 1
dl 0
loc 40
rs 9.584
cc 1
nc 1
nop 0
1
<?php
2
namespace Mezon\Router\Tests\Simple;
3
4
use Mezon\Router\Tests\Utils;
5
use Mezon\Router\SimpleRouter;
6
7
/**
8
 *
9
 * @psalm-suppress PropertyNotSetInConstructor
10
 */
11
class UniversalRouteUnitTest extends \PHPUnit\Framework\TestCase
12
{
13
14
    const HELLO_WORLD = 'Hello world!';
15
16
    const HELLO_STATIC_WORLD = 'Hello static world!';
17
18
    const HELLO_STATIC_WORLD_METHOD = '\Mezon\Router\Tests\StaticRoutesUnitTest::staticHelloWorldOutput';
19
20
    use Utils;
21
22
    /**
23
     * Function simply returns string.
24
     */
25
    public function helloWorldOutput(): string
26
    {
27
        return UniversalRouteUnitTest::HELLO_WORLD;
28
    }
29
30
    /**
31
     * Function simply returns string.
32
     */
33
    static public function staticHelloWorldOutput(): string
34
    {
35
        return UniversalRouteUnitTest::HELLO_STATIC_WORLD;
36
    }
37
38
    /**
39
     * Default setup
40
     *
41
     * {@inheritdoc}
42
     * @see \PHPUnit\Framework\TestCase::setUp()
43
     */
44
    public function setUp(): void
45
    {
46
        $_SERVER['REQUEST_METHOD'] = 'GET';
47
    }
48
49
    /**
50
     * Method returns data sets
51
     *
52
     * @return array testing data
53
     */
54
    public function universalRouteDataProvider(): array
55
    {
56
        $setup = function (): array {
57
            return [
58
                [
59
                    '/hello/[s:name]/',
60
                    function () {
61
                        return 'hello';
62
                    }
63
                ],
64
                [
65
                    '*',
66
                    function () {
67
                        return 'universal';
68
                    }
69
                ],
70
                [
71
                    '/bye/[s:name]/',
72
                    function () {
73
                        return 'bye';
74
                    }
75
                ]
76
            ];
77
        };
78
79
        return [
80
            [
81
                $setup(),
82
                '/hello/joe/',
83
                'hello'
84
            ],
85
            [
86
                $setup(),
87
                '/bye/joe/',
88
                'bye'
89
            ],
90
            [
91
                $setup(),
92
                '/unexisting/',
93
                'universal'
94
            ]
95
        ];
96
    }
97
98
    /**
99
     * Testing one processor for all routes
100
     *
101
     * @param array $routes
102
     *            list of routes
103
     * @param string $uri
104
     *            uri to be requested
105
     * @param string $result
106
     *            expected result
107
     * @dataProvider universalRouteDataProvider
108
     */
109
    public function testUniversalRoute(array $routes, string $uri, string $result): void
110
    {
111
        // setup
112
        $router = new SimpleRouter();
113
114
        foreach ($routes as $route) {
115
            $router->addRoute($route[0], $route[1]);
116
        }
117
118
        // test body
119
        $content = $router->callRoute($uri);
120
121
        // assertions
122
        $this->assertEquals($result, $content);
123
    }
124
}
125