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