|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
class RouterUnitTest extends \PHPUnit\Framework\TestCase |
|
4
|
|
|
{ |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Default setup |
|
8
|
|
|
* |
|
9
|
|
|
* {@inheritdoc} |
|
10
|
|
|
* @see \PHPUnit\Framework\TestCase::setUp() |
|
11
|
|
|
*/ |
|
12
|
|
|
public function setUp(): void |
|
13
|
|
|
{ |
|
14
|
|
|
$_SERVER['REQUEST_METHOD'] = 'GET'; |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Function simply returns string. |
|
19
|
|
|
*/ |
|
20
|
|
|
public function helloWorldOutput(): string |
|
21
|
|
|
{ |
|
22
|
|
|
return 'Hello world!'; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Testing action #1. |
|
27
|
|
|
*/ |
|
28
|
|
|
public function actionA1(): string |
|
29
|
|
|
{ |
|
30
|
|
|
return 'action #1'; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Testing action #2. |
|
35
|
|
|
*/ |
|
36
|
|
|
public function actionA2(): string |
|
37
|
|
|
{ |
|
38
|
|
|
return 'action #2'; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function actionDoubleWord(): string |
|
42
|
|
|
{ |
|
43
|
|
|
return 'action double word'; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Testing one component router. |
|
48
|
|
|
*/ |
|
49
|
|
|
public function testOneComponentRouterClassMethod(): void |
|
50
|
|
|
{ |
|
51
|
|
|
$router = new \Mezon\Router\Router(); |
|
52
|
|
|
|
|
53
|
|
|
$router->addRoute('/index/', [ |
|
54
|
|
|
$this, |
|
55
|
|
|
'helloWorldOutput' |
|
56
|
|
|
]); |
|
57
|
|
|
|
|
58
|
|
|
$content = $router->callRoute('/index/'); |
|
59
|
|
|
|
|
60
|
|
|
$this->assertEquals('Hello world!', $content); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Testing one component router. |
|
65
|
|
|
*/ |
|
66
|
|
|
public function testOneComponentRouterLambda(): void |
|
67
|
|
|
{ |
|
68
|
|
|
$router = new \Mezon\Router\Router(); |
|
69
|
|
|
|
|
70
|
|
|
$router->addRoute('/index/', function () { |
|
71
|
|
|
return 'Hello world!'; |
|
72
|
|
|
}); |
|
73
|
|
|
|
|
74
|
|
|
$content = $router->callRoute('/index/'); |
|
75
|
|
|
|
|
76
|
|
|
$this->assertEquals('Hello world!', $content); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Testing unexisting route behaviour. |
|
81
|
|
|
*/ |
|
82
|
|
|
public function testUnexistingRoute(): void |
|
83
|
|
|
{ |
|
84
|
|
|
$exception = ''; |
|
85
|
|
|
$router = new \Mezon\Router\Router(); |
|
86
|
|
|
$router->addRoute('/index/', [ |
|
87
|
|
|
$this, |
|
88
|
|
|
'helloWorldOutput' |
|
89
|
|
|
]); |
|
90
|
|
|
|
|
91
|
|
|
try { |
|
92
|
|
|
$router->callRoute('/unexisting-route/'); |
|
93
|
|
|
} catch (Exception $e) { |
|
94
|
|
|
$exception = $e->getMessage(); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
$msg = "The processor was not found for the route"; |
|
98
|
|
|
|
|
99
|
|
|
$this->assertNotFalse(strpos($exception, $msg), 'Valid error handling expected'); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Testing action fetching method. |
|
104
|
|
|
*/ |
|
105
|
|
|
public function testClassActions(): void |
|
106
|
|
|
{ |
|
107
|
|
|
$router = new \Mezon\Router\Router(); |
|
108
|
|
|
$router->fetchActions($this); |
|
109
|
|
|
|
|
110
|
|
|
$content = $router->callRoute('/a1/'); |
|
111
|
|
|
$this->assertEquals('action #1', $content, 'Invalid a1 route'); |
|
112
|
|
|
|
|
113
|
|
|
$content = $router->callRoute('/a2/'); |
|
114
|
|
|
$this->assertEquals('action #2', $content, 'Invalid a2 route'); |
|
115
|
|
|
|
|
116
|
|
|
$content = $router->callRoute('/double-word/'); |
|
117
|
|
|
$this->assertEquals('action double word', $content, 'Invalid a2 route'); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Method tests POST actions |
|
122
|
|
|
*/ |
|
123
|
|
|
public function testPostClassAction(): void |
|
124
|
|
|
{ |
|
125
|
|
|
$_SERVER['REQUEST_METHOD'] = 'POST'; |
|
126
|
|
|
|
|
127
|
|
|
$router = new \Mezon\Router\Router(); |
|
128
|
|
|
$router->fetchActions($this); |
|
129
|
|
|
$content = $router->callRoute('/a1/'); |
|
130
|
|
|
$this->assertEquals('action #1', $content, 'Invalid a1 route'); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Testing case when both GET and POST processors exists. |
|
135
|
|
|
*/ |
|
136
|
|
|
public function testGetPostPostDeleteRouteConcurrency(): void |
|
137
|
|
|
{ |
|
138
|
|
|
$router = new \Mezon\Router\Router(); |
|
139
|
|
|
$router->addRoute('/catalog/', function () { |
|
140
|
|
|
return 'POST'; |
|
141
|
|
|
}, 'POST'); |
|
142
|
|
|
$router->addRoute('/catalog/', function () { |
|
143
|
|
|
return 'GET'; |
|
144
|
|
|
}, 'GET'); |
|
145
|
|
|
$router->addRoute('/catalog/', function () { |
|
146
|
|
|
return 'PUT'; |
|
147
|
|
|
}, 'PUT'); |
|
148
|
|
|
$router->addRoute('/catalog/', function () { |
|
149
|
|
|
return 'DELETE'; |
|
150
|
|
|
}, 'DELETE'); |
|
151
|
|
|
|
|
152
|
|
|
$_SERVER['REQUEST_METHOD'] = 'POST'; |
|
153
|
|
|
$result = $router->callRoute('/catalog/'); |
|
154
|
|
|
$this->assertEquals($result, 'POST'); |
|
155
|
|
|
|
|
156
|
|
|
$_SERVER['REQUEST_METHOD'] = 'GET'; |
|
157
|
|
|
$result = $router->callRoute('/catalog/'); |
|
158
|
|
|
$this->assertEquals($result, 'GET'); |
|
159
|
|
|
|
|
160
|
|
|
$_SERVER['REQUEST_METHOD'] = 'PUT'; |
|
161
|
|
|
$result = $router->callRoute('/catalog/'); |
|
162
|
|
|
$this->assertEquals($result, 'PUT'); |
|
163
|
|
|
|
|
164
|
|
|
$_SERVER['REQUEST_METHOD'] = 'DELETE'; |
|
165
|
|
|
$result = $router->callRoute('/catalog/'); |
|
166
|
|
|
$this->assertEquals($result, 'DELETE'); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Testing 'clear' method. |
|
171
|
|
|
*/ |
|
172
|
|
|
public function testClearMethod(): void |
|
173
|
|
|
{ |
|
174
|
|
|
$router = new \Mezon\Router\Router(); |
|
175
|
|
|
$router->addRoute('/catalog/', function () { |
|
176
|
|
|
return 'POST'; |
|
177
|
|
|
}, 'POST'); |
|
178
|
|
|
$router->addRoute('/catalog/', function () { |
|
179
|
|
|
return 'GET'; |
|
180
|
|
|
}, 'GET'); |
|
181
|
|
|
$router->addRoute('/catalog/', function () { |
|
182
|
|
|
return 'PUT'; |
|
183
|
|
|
}, 'PUT'); |
|
184
|
|
|
$router->addRoute('/catalog/', function () { |
|
185
|
|
|
return 'DELETE'; |
|
186
|
|
|
}, 'DELETE'); |
|
187
|
|
|
$router->clear(); |
|
188
|
|
|
|
|
189
|
|
|
try { |
|
190
|
|
|
$_SERVER['REQUEST_METHOD'] = 'POST'; |
|
191
|
|
|
$router->callRoute('/catalog/'); |
|
192
|
|
|
$flag = 'not cleared'; |
|
193
|
|
|
} catch (Exception $e) { |
|
194
|
|
|
$flag = 'cleared'; |
|
195
|
|
|
} |
|
196
|
|
|
$this->assertEquals($flag, 'cleared', 'Data was not cleared'); |
|
197
|
|
|
|
|
198
|
|
|
try { |
|
199
|
|
|
$_SERVER['REQUEST_METHOD'] = 'GET'; |
|
200
|
|
|
$router->callRoute('/catalog/'); |
|
201
|
|
|
$flag = 'not cleared'; |
|
202
|
|
|
} catch (Exception $e) { |
|
203
|
|
|
$flag = 'cleared'; |
|
204
|
|
|
} |
|
205
|
|
|
$this->assertEquals($flag, 'cleared', 'Data was not cleared'); |
|
206
|
|
|
|
|
207
|
|
|
try { |
|
208
|
|
|
$_SERVER['REQUEST_METHOD'] = 'PUT'; |
|
209
|
|
|
$router->callRoute('/catalog/'); |
|
210
|
|
|
$flag = 'not cleared'; |
|
211
|
|
|
} catch (Exception $e) { |
|
212
|
|
|
$flag = 'cleared'; |
|
213
|
|
|
} |
|
214
|
|
|
$this->assertEquals($flag, 'cleared', 'Data was not cleared'); |
|
215
|
|
|
|
|
216
|
|
|
try { |
|
217
|
|
|
$_SERVER['REQUEST_METHOD'] = 'DELETE'; |
|
218
|
|
|
$router->callRoute('/catalog/'); |
|
219
|
|
|
$flag = 'not cleared'; |
|
220
|
|
|
} catch (Exception $e) { |
|
221
|
|
|
$flag = 'cleared'; |
|
222
|
|
|
} |
|
223
|
|
|
$this->assertEquals($flag, 'cleared', 'Data was not cleared'); |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* Method increments assertion count |
|
228
|
|
|
*/ |
|
229
|
|
|
protected function errorHandler(): void |
|
230
|
|
|
{ |
|
231
|
|
|
$this->addToAssertionCount(1); |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
/** |
|
235
|
|
|
* Test validate custom error handlers. |
|
236
|
|
|
*/ |
|
237
|
|
|
public function testSetErrorHandler(): void |
|
238
|
|
|
{ |
|
239
|
|
|
$router = new \Mezon\Router\Router(); |
|
240
|
|
|
$router->setNoProcessorFoundErrorHandler(function () { |
|
241
|
|
|
$this->errorHandler(); |
|
242
|
|
|
}); |
|
243
|
|
|
|
|
244
|
|
|
$_SERVER['REQUEST_METHOD'] = 'POST'; |
|
245
|
|
|
$router->callRoute('/unexisting/'); |
|
246
|
|
|
} |
|
247
|
|
|
} |
|
248
|
|
|
|