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
|
|
|
|