|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace KochTest\Router; |
|
4
|
|
|
|
|
5
|
|
|
use Koch\Config\Config; |
|
6
|
|
|
use Koch\Http\HttpRequest; |
|
7
|
|
|
use Koch\Mvc\Mapper; |
|
8
|
|
|
use Koch\Router\Router; |
|
9
|
|
|
use Koch\Router\TargetRoute; |
|
10
|
|
|
|
|
11
|
|
|
class RouterTest extends \PHPUnit_Framework_TestCase |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var Router |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $router; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Sets up the fixture, for example, opens a network connection. |
|
20
|
|
|
* This method is called before a test is executed. |
|
21
|
|
|
*/ |
|
22
|
|
|
public function setUp() |
|
|
|
|
|
|
23
|
|
|
{ |
|
24
|
|
|
$_SERVER['REQUEST_URI'] = ''; |
|
25
|
|
|
|
|
26
|
|
|
$request = new HttpRequest(); |
|
27
|
|
|
|
|
28
|
|
|
$config = new Config(); |
|
29
|
|
|
|
|
30
|
|
|
$this->router = new Router($request, $config); |
|
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
// set the Fixtures folder with application and module classes for the autoloading |
|
33
|
|
|
set_include_path(realpath(__DIR__ . '/Fixtures') . PATH_SEPARATOR . get_include_path()); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Tears down the fixture, for example, closes a network connection. |
|
38
|
|
|
* This method is called after a test is executed. |
|
39
|
|
|
*/ |
|
40
|
|
|
public function tearDown() |
|
41
|
|
|
{ |
|
42
|
|
|
unset($this->router); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function testMethodaddRoute() |
|
46
|
|
|
{ |
|
47
|
|
|
$this->router->addRoute('/news/(:id)', ['controller', 'id']); |
|
48
|
|
|
|
|
49
|
|
|
$routes = $this->router->getRoutes(); |
|
50
|
|
|
|
|
51
|
|
|
$this->assertEquals(2, $routes['/news/([0-9]+)']['number_of_segments']); |
|
52
|
|
|
$this->assertEquals('#\/news\/?\/([0-9]+)\/?#', $routes['/news/([0-9]+)']['regexp']); |
|
53
|
|
|
|
|
54
|
|
|
$this->router->reset(); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function testMethoddelRoute() |
|
58
|
|
|
{ |
|
59
|
|
|
// static controller with dynamic id |
|
60
|
|
|
$this->router->addRoute('/news/(:id)', [':controller', 'id']); |
|
61
|
|
|
|
|
62
|
|
|
$this->assertTrue(1 === count($this->router->getRoutes())); |
|
63
|
|
|
|
|
64
|
|
|
// @todo this is odd, because the string to delete the route |
|
65
|
|
|
// does not match the string, when adding the route, |
|
66
|
|
|
// due to preplacing with regexps |
|
67
|
|
|
$this->router->delRoute('/news/([0-9]+)'); |
|
68
|
|
|
|
|
69
|
|
|
$this->assertTrue(0 === count($this->router->getRoutes())); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function testMethodReset() |
|
73
|
|
|
{ |
|
74
|
|
|
$this->assertTrue(0 === count($this->router->getRoutes())); |
|
75
|
|
|
|
|
76
|
|
|
$this->router->addRoute('/news', [':controller']); |
|
77
|
|
|
|
|
78
|
|
|
$this->assertTrue(1 === count($this->router->getRoutes())); |
|
79
|
|
|
|
|
80
|
|
|
$this->router->reset(); |
|
81
|
|
|
|
|
82
|
|
|
$this->assertTrue(0 === count($this->router->getRoutes())); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function testMethodResetResetsTargetRouteToo() |
|
86
|
|
|
{ |
|
87
|
|
|
TargetRoute::setAction('testclass'); |
|
88
|
|
|
$this->assertEquals('testclass', TargetRoute::getAction()); |
|
89
|
|
|
$this->router->reset(); |
|
90
|
|
|
|
|
91
|
|
|
// default module action is "list" |
|
92
|
|
|
// "list" was formerly "index" (Response Action for GET "/foos") |
|
93
|
|
|
$this->assertEquals('list', TargetRoute::getAction()); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function testMethodaddRoutes() |
|
97
|
|
|
{ |
|
98
|
|
|
$routes = [ |
|
99
|
|
|
'/news' => [':controller'], |
|
100
|
|
|
'/news/edit' => [':controller', ':action', 'id'], |
|
101
|
|
|
'/news/edit/(:id)' => [':controller', ':action', 'id'], |
|
102
|
|
|
'/news/admin/edit/(:id)' => [':controller', ':subcontroller', ':action', 'id'], |
|
103
|
|
|
'/news/:year/:month' => [':controller', ':year', ':month'], |
|
104
|
|
|
'/news/he-ad-li-ne-SEO' => [':controller', ':word'], |
|
105
|
|
|
]; |
|
106
|
|
|
|
|
107
|
|
|
$this->router->addRoutes($routes); |
|
108
|
|
|
|
|
109
|
|
|
$this->assertTrue(count($routes) === count($this->router->getRoutes())); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
public function testMethodaddRoutesViaArrayAccess() |
|
113
|
|
|
{ |
|
114
|
|
|
$r = $this->router; |
|
115
|
|
|
|
|
116
|
|
|
$r['/news'] = [':controller']; |
|
117
|
|
|
$r['/news/edit'] = [':controller', ':action', 'id']; |
|
118
|
|
|
$r['/news/edit/(:id)'] = [':controller', ':action', 'id']; |
|
119
|
|
|
$r['/news/admin/edit/(:id)'] = [':controller', ':subcontroller', ':action', 'id']; |
|
120
|
|
|
$r['/news/:year/:month'] = [':controller', ':year', ':month']; |
|
121
|
|
|
$r['/news/he-ad-li-ne-text'] = [':controller', ':word']; |
|
122
|
|
|
|
|
123
|
|
|
$this->assertTrue(6 === count($this->router->getRoutes())); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
public function testMethodremoveRoutesBySegmentCount() |
|
127
|
|
|
{ |
|
128
|
|
|
// adding 3 routes, each with different segment number |
|
129
|
|
|
$this->router->addRoute('/news', [':controller']); |
|
130
|
|
|
$this->router->addRoute('/news/edit', [':controller', ':action', 'id']); |
|
131
|
|
|
$this->router->addRoute('/news/edit/(:id)', [':controller', ':action', 'id']); |
|
132
|
|
|
|
|
133
|
|
|
$this->assertTrue(3 === count($this->router->getRoutes())); |
|
134
|
|
|
|
|
135
|
|
|
// add only one segment |
|
136
|
|
|
$this->router->uriSegments = ['0' => 'news']; |
|
137
|
|
|
|
|
138
|
|
|
// this makes all other routes irrelevant for the lookup |
|
139
|
|
|
$this->router->reduceRoutesToSegmentCount(); |
|
140
|
|
|
|
|
141
|
|
|
$this->assertTrue(1 === count($this->router->getRoutes())); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
public function testMethodprepareRequestURI() |
|
145
|
|
|
{ |
|
146
|
|
|
// prepends slash |
|
147
|
|
|
$this->assertEquals('/news', $this->router->prepareRequestURI('news')); |
|
148
|
|
|
|
|
149
|
|
|
// prepends slash and removes any trailing slashes |
|
150
|
|
|
$this->assertEquals('/news', $this->router->prepareRequestURI('news///')); |
|
151
|
|
|
|
|
152
|
|
|
// prepends slash |
|
153
|
|
|
$this->assertEquals('/news/edit', $this->router->prepareRequestURI('news/edit')); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
public function testMethodplaceholdersToRegexp() |
|
157
|
|
|
{ |
|
158
|
|
|
$this->assertEquals('/route/with/([0-9]+)', $this->router->placeholdersToRegexp('/route/with/(:id)')); |
|
159
|
|
|
$this->assertEquals('/route/with/([0-9]+)', $this->router->placeholdersToRegexp('/route/with/(:num)')); |
|
160
|
|
|
$this->assertEquals('/route/with/([a-zA-Z]+)', $this->router->placeholdersToRegexp('/route/with/(:alpha)')); |
|
161
|
|
|
$this->assertEquals('/route/with/([a-zA-Z0-9]+)', $this->router->placeholdersToRegexp('/route/with/(:alphanum)')); |
|
162
|
|
|
$this->assertEquals('/route/with/(.*)', $this->router->placeholdersToRegexp('/route/with/(:any)')); |
|
163
|
|
|
$this->assertEquals('/route/with/(\w+)', $this->router->placeholdersToRegexp('/route/with/(:word)')); |
|
164
|
|
|
$this->assertEquals('/route/with/([12][0-9]{3})', $this->router->placeholdersToRegexp('/route/with/(:year)')); |
|
165
|
|
|
$this->assertEquals('/route/with/(0[1-9]|1[012])', $this->router->placeholdersToRegexp('/route/with/(:month)')); |
|
166
|
|
|
$this->assertEquals('/route/with/(0[1-9]|1[012])', $this->router->placeholdersToRegexp('/route/with/(:day)')); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
public function testMethodProcessSegmentsRegExp() |
|
170
|
|
|
{ |
|
171
|
|
|
$segments = ['news', 'edit', '([0-9]+)']; |
|
172
|
|
|
$requirements = ['controller', 'action', ':num']; |
|
173
|
|
|
|
|
174
|
|
|
$this->assertSame( |
|
175
|
|
|
'#\/news\/?\/edit\/?\/([0-9]+)\/?#', |
|
176
|
|
|
$this->router->processSegmentsRegExp($segments, $requirements) |
|
177
|
|
|
); |
|
178
|
|
|
|
|
179
|
|
|
/* |
|
180
|
|
|
* Static Named Route |
|
181
|
|
|
*/ |
|
182
|
|
|
$segments = [':news']; |
|
183
|
|
|
$requirements = ['controller']; |
|
184
|
|
|
|
|
185
|
|
|
$this->assertSame( |
|
186
|
|
|
'#(?P<news>[a-z_-]+)\/?#', |
|
187
|
|
|
$this->router->processSegmentsRegExp($segments, $requirements) |
|
188
|
|
|
); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
public function testMethodMatchRestRoutes() |
|
|
|
|
|
|
192
|
|
|
{ |
|
193
|
|
|
$applicationNamespace = '\KochTest\Fixtures\Application'; |
|
194
|
|
|
Mapper::setApplicationNamespace($applicationNamespace); |
|
195
|
|
|
|
|
196
|
|
|
define('MOD_REWRITE_ON', true); |
|
197
|
|
|
$_ENV['FORCE_MOD_REWRITE_ON'] = true; |
|
198
|
|
|
|
|
199
|
|
|
$this->router->reset(true); |
|
200
|
|
|
|
|
201
|
|
|
// http://example.com/news |
|
202
|
|
|
// routes to |
|
203
|
|
|
// Controller: News |
|
204
|
|
|
// Action: actionList() |
|
205
|
|
|
// Type: GET [REST Route] |
|
206
|
|
|
|
|
207
|
|
|
HttpRequest::setRequestMethod('GET'); |
|
208
|
|
|
$this->router->prepareRequestURI('/news'); |
|
209
|
|
|
$route = $this->router->route(); |
|
210
|
|
|
|
|
211
|
|
|
$this->assertEquals($applicationNamespace . '\Modules\News\Controller\NewsController', $route->getClassname()); |
|
212
|
|
|
$this->assertEquals('News', $route->getModule()); |
|
213
|
|
|
$this->assertEquals('News', $route->getController()); |
|
214
|
|
|
$this->assertEquals('list', $route->getAction()); |
|
215
|
|
|
$this->assertEquals('actionList', $route->getMethod()); |
|
216
|
|
|
$this->assertEquals([], $route->getParameters()); |
|
217
|
|
|
$this->assertEquals('GET', $route->getRequestMethod()); |
|
218
|
|
|
$this->router->reset(true); |
|
219
|
|
|
|
|
220
|
|
|
// http://example.com/news/42 |
|
221
|
|
|
// routes to |
|
222
|
|
|
// Controller: News |
|
223
|
|
|
// Action: actionShow() |
|
224
|
|
|
// Id: 42 |
|
225
|
|
|
// Type: GET [REST Route] |
|
226
|
|
|
|
|
227
|
|
|
HttpRequest::setRequestMethod('GET'); |
|
228
|
|
|
$this->router->prepareRequestURI('/news/42'); |
|
229
|
|
|
$route = $this->router->route(); |
|
230
|
|
|
|
|
231
|
|
|
$this->assertEquals($applicationNamespace . '\Modules\News\Controller\NewsController', $route->getClassname()); |
|
232
|
|
|
$this->assertEquals('News', $route->getModule()); |
|
233
|
|
|
$this->assertEquals('News', $route->getController()); |
|
234
|
|
|
$this->assertEquals('actionShow', $route->getMethod()); |
|
235
|
|
|
$this->assertEquals(['id' => '42'], $route->getParameters()); |
|
236
|
|
|
$this->assertEquals('GET', $route->getRequestMethod()); |
|
237
|
|
|
$this->router->reset(true); |
|
238
|
|
|
|
|
239
|
|
|
// http://example.com/news/new |
|
240
|
|
|
// routes to |
|
241
|
|
|
// Controller: News |
|
242
|
|
|
// Action: actionNew() |
|
243
|
|
|
// Type: GET [REST Route] |
|
244
|
|
|
|
|
245
|
|
|
HttpRequest::setRequestMethod('GET'); |
|
246
|
|
|
$this->router->prepareRequestURI('/news/new'); |
|
247
|
|
|
$route = $this->router->route(); |
|
248
|
|
|
|
|
249
|
|
|
$this->assertEquals($applicationNamespace . '\Modules\News\Controller\NewsController', $route->getClassname()); |
|
250
|
|
|
$this->assertEquals('News', $route->getModule()); |
|
251
|
|
|
$this->assertEquals('News', $route->getController()); |
|
252
|
|
|
$this->assertEquals('actionNew', $route->getMethod()); |
|
253
|
|
|
$this->assertEquals('GET', $route->getRequestMethod()); |
|
254
|
|
|
$this->router->reset(true); |
|
255
|
|
|
|
|
256
|
|
|
// http://example.com/news/42/edit |
|
257
|
|
|
// routes to |
|
258
|
|
|
// Controller: News |
|
259
|
|
|
// Action: actionEdit() |
|
260
|
|
|
// Id: 42 |
|
261
|
|
|
// Type: GET [REST Route] |
|
262
|
|
|
|
|
263
|
|
|
HttpRequest::setRequestMethod('GET'); |
|
264
|
|
|
$this->router->prepareRequestURI('/news/42/edit'); |
|
265
|
|
|
$route = $this->router->route(); |
|
266
|
|
|
|
|
267
|
|
|
$this->assertEquals($applicationNamespace . '\Modules\News\Controller\NewsController', $route->getClassname()); |
|
268
|
|
|
$this->assertEquals('News', $route->getModule()); |
|
269
|
|
|
$this->assertEquals('News', $route->getController()); |
|
270
|
|
|
$this->assertEquals('actionEdit', $route->getMethod()); |
|
271
|
|
|
$this->assertSame(['id' => '42'], $route->getParameters()); |
|
272
|
|
|
$this->assertEquals('GET', $route->getRequestMethod()); |
|
273
|
|
|
$this->router->reset(true); |
|
274
|
|
|
|
|
275
|
|
|
// same as above with reversed last segements |
|
276
|
|
|
// http://example.com/news/edit/42 |
|
277
|
|
|
// routes to |
|
278
|
|
|
// Controller: News |
|
279
|
|
|
// Action: actionEdit() |
|
280
|
|
|
// Id: 42 |
|
281
|
|
|
// Type: GET [WEB] |
|
282
|
|
|
|
|
283
|
|
|
HttpRequest::setRequestMethod('GET'); |
|
284
|
|
|
$this->router->prepareRequestURI('/news/edit/42'); |
|
285
|
|
|
$route = $this->router->route(); |
|
286
|
|
|
|
|
287
|
|
|
$this->assertEquals($applicationNamespace . '\Modules\News\Controller\NewsController', $route->getClassname()); |
|
288
|
|
|
$this->assertEquals('News', $route->getModule()); |
|
289
|
|
|
$this->assertEquals('News', $route->getController()); |
|
290
|
|
|
$this->assertEquals('actionEdit', $route->getMethod()); |
|
291
|
|
|
$this->assertEquals(['id' => '42'], $route->getParameters()); |
|
292
|
|
|
$this->assertEquals('GET', $route->getRequestMethod()); |
|
293
|
|
|
$this->router->reset(true); |
|
294
|
|
|
|
|
295
|
|
|
// http://example.com/news/42 |
|
296
|
|
|
// routes to |
|
297
|
|
|
// Controller: News |
|
298
|
|
|
// Action: actionUpdate() |
|
299
|
|
|
// Id: 42 |
|
300
|
|
|
// Type: PUT [REST Route] |
|
301
|
|
|
// Post_parameters are filled. |
|
302
|
|
|
|
|
303
|
|
|
HttpRequest::setRequestMethod('PUT'); |
|
304
|
|
|
$this->router->prepareRequestURI('/news/42'); |
|
305
|
|
|
$route = $this->router->route(); |
|
306
|
|
|
|
|
307
|
|
|
$this->assertEquals($applicationNamespace . '\Modules\News\Controller\NewsController', $route->getClassname()); |
|
308
|
|
|
$this->assertEquals('News', $route->getModule()); |
|
309
|
|
|
$this->assertEquals('News', $route->getController()); |
|
310
|
|
|
$this->assertEquals('actionUpdate', $route->getMethod()); |
|
311
|
|
|
$this->assertEquals(['id' => '42'], $route->getParameters()); |
|
312
|
|
|
$this->assertEquals('PUT', $route->getRequestMethod()); |
|
313
|
|
|
$this->router->reset(true); |
|
314
|
|
|
|
|
315
|
|
|
// http://example.com/news |
|
316
|
|
|
// routes to |
|
317
|
|
|
// Controller: News |
|
318
|
|
|
// Action: actionInsert() |
|
319
|
|
|
// Type: POST [REST Route] |
|
320
|
|
|
// Post_parameters are filled. |
|
321
|
|
|
// fake incoming env data |
|
322
|
|
|
$_POST['id'] = '42'; |
|
323
|
|
|
$_POST['article_text'] = 'blabla'; |
|
324
|
|
|
HttpRequest::setRequestMethod('POST'); |
|
325
|
|
|
$this->router->prepareRequestURI('/news'); |
|
326
|
|
|
$route = $this->router->route(); |
|
327
|
|
|
|
|
328
|
|
|
$this->assertEquals($applicationNamespace . '\Modules\News\Controller\NewsController', $route->getClassname()); |
|
329
|
|
|
$this->assertEquals('News', $route->getModule()); |
|
330
|
|
|
$this->assertEquals('News', $route->getController()); |
|
331
|
|
|
$this->assertEquals('actionInsert', $route->getMethod()); |
|
332
|
|
|
$this->assertEquals(['id' => '42', 'article_text' => 'blabla'], $route->getParameters()); |
|
333
|
|
|
$this->assertEquals('POST', $route->getRequestMethod()); |
|
334
|
|
|
$this->router->reset(true); |
|
335
|
|
|
|
|
336
|
|
|
// http://example.com/news/42 |
|
337
|
|
|
// routes to |
|
338
|
|
|
// Controller: News |
|
339
|
|
|
// Action: actionDelete() |
|
340
|
|
|
// Id: 42 |
|
341
|
|
|
// Type: DELETE [REST Route] |
|
342
|
|
|
|
|
343
|
|
|
HttpRequest::setRequestMethod('DELETE'); |
|
344
|
|
|
$this->router->prepareRequestURI('/news/42'); |
|
345
|
|
|
$route = $this->router->route(); |
|
346
|
|
|
|
|
347
|
|
|
$this->assertEquals($applicationNamespace . '\Modules\News\Controller\NewsController', $route->getClassname()); |
|
348
|
|
|
$this->assertEquals('News', $route->getModule()); |
|
349
|
|
|
$this->assertEquals('News', $route->getController()); |
|
350
|
|
|
$this->assertEquals('actionDelete', $route->getMethod()); |
|
351
|
|
|
$this->assertEquals(['id' => '42'], $route->getParameters()); |
|
352
|
|
|
$this->assertEquals('DELETE', $route->getRequestMethod()); |
|
353
|
|
|
$this->router->reset(true); |
|
354
|
|
|
|
|
355
|
|
|
// same as above, web route |
|
356
|
|
|
// http://example.com/news/delete/42 |
|
357
|
|
|
// routes to |
|
358
|
|
|
// Controller: News |
|
359
|
|
|
// Action: actionDelete() |
|
360
|
|
|
// Id: 42 |
|
361
|
|
|
// Type: DELETE [WEB] |
|
362
|
|
|
|
|
363
|
|
|
HttpRequest::setRequestMethod('DELETE'); |
|
364
|
|
|
$this->router->prepareRequestURI('/news/delete/42'); |
|
365
|
|
|
$route = $this->router->route(); |
|
366
|
|
|
|
|
367
|
|
|
$this->assertEquals($applicationNamespace . '\Modules\News\Controller\NewsController', $route->getClassname()); |
|
368
|
|
|
$this->assertEquals('News', $route->getModule()); |
|
369
|
|
|
$this->assertEquals('News', $route->getController()); |
|
370
|
|
|
$this->assertEquals('actionDelete', $route->getMethod()); |
|
371
|
|
|
$this->assertEquals(['id' => '42'], $route->getParameters()); |
|
372
|
|
|
$this->assertEquals('DELETE', $route->getRequestMethod()); |
|
373
|
|
|
$this->router->reset(true); |
|
374
|
|
|
} |
|
375
|
|
|
|
|
376
|
|
|
public function testMethodmatch_StaticRoute() |
|
|
|
|
|
|
377
|
|
|
{ |
|
378
|
|
|
$applicationNamespace = '\KochTest\Router\Fixtures\Application'; |
|
379
|
|
|
Mapper::setApplicationNamespace($applicationNamespace); |
|
380
|
|
|
|
|
381
|
|
|
// http://example.com/login |
|
382
|
|
|
|
|
383
|
|
|
$this->router->addRoute('/:controller'); |
|
384
|
|
|
$this->router->addRoute('/:controller/:action'); |
|
385
|
|
|
$this->router->addRoute('/:controller/:action/(:id)'); |
|
386
|
|
|
|
|
387
|
|
|
$r = $this->router; |
|
388
|
|
|
$r['/login'] = ['module' => 'user', 'controller' => 'account', 'action' => 'login']; |
|
389
|
|
|
|
|
390
|
|
|
$this->router->setRequestURI('/login'); |
|
391
|
|
|
HttpRequest::setRequestMethod('GET'); |
|
392
|
|
|
$route = $this->router->route(); |
|
393
|
|
|
|
|
394
|
|
|
$this->assertEquals( |
|
395
|
|
|
$applicationNamespace . '\Modules\User\Controller\AccountController', $route->getClassname() |
|
396
|
|
|
); |
|
397
|
|
|
$this->assertEquals('User', $route->getModule()); |
|
398
|
|
|
$this->assertEquals('Account', $route->getController()); |
|
399
|
|
|
$this->assertEquals('actionLogin', $route->getMethod()); |
|
400
|
|
|
$this->assertEquals([], $route->getParameters()); |
|
401
|
|
|
$this->assertEquals('GET', $route->getRequestMethod()); |
|
402
|
|
|
|
|
403
|
|
|
unset($route); |
|
404
|
|
|
$r->reset(true); |
|
405
|
|
|
|
|
406
|
|
|
// http://example.com/about |
|
407
|
|
|
|
|
408
|
|
|
$r = $this->router; |
|
409
|
|
|
$r['/about'] = ['module' => 'index', 'controller' => 'index', 'action' => 'about']; |
|
410
|
|
|
|
|
411
|
|
|
$r->setRequestURI('/about'); |
|
412
|
|
|
HttpRequest::setRequestMethod('GET'); |
|
413
|
|
|
$route = $this->router->route(); |
|
414
|
|
|
|
|
415
|
|
|
$this->assertEquals( |
|
416
|
|
|
$applicationNamespace . '\Modules\Index\Controller\IndexController', $route->getClassname() |
|
417
|
|
|
); |
|
418
|
|
|
$this->assertEquals('Index', $route->getModule()); |
|
419
|
|
|
$this->assertEquals('Index', $route->getController()); |
|
420
|
|
|
$this->assertEquals('actionAbout', $route->getMethod()); |
|
421
|
|
|
$this->assertEquals([], $route->getParameters()); |
|
422
|
|
|
$this->assertEquals('GET', $route->getRequestMethod()); |
|
423
|
|
|
|
|
424
|
|
|
unset($route); |
|
425
|
|
|
$r->reset(true); |
|
426
|
|
|
} |
|
427
|
|
|
|
|
428
|
|
|
/*public function testMethodmatch_CustomActionNames() |
|
|
|
|
|
|
429
|
|
|
{ |
|
430
|
|
|
/* |
|
431
|
|
|
* Feature Idea: |
|
432
|
|
|
$this->router->route('controller_item', '/:controller/<:id>.:format', |
|
433
|
|
|
array('defaults' => array( |
|
434
|
|
|
'action' => 'view', |
|
435
|
|
|
'format' => 'html'), |
|
436
|
|
|
'get' => array('action' => 'show'), |
|
437
|
|
|
'put' => array('action' => 'update'), |
|
438
|
|
|
'delete' => array('action' => 'delete') |
|
439
|
|
|
) |
|
440
|
|
|
); |
|
441
|
|
|
*/ |
|
442
|
|
|
//} |
|
443
|
|
|
|
|
444
|
|
|
/* Feature not implemented yet. |
|
|
|
|
|
|
445
|
|
|
public function testMethodmatch_SEO_Dynamic_Routes() |
|
446
|
|
|
{ |
|
447
|
|
|
HttpRequest::setRequestMethod('GET'); |
|
448
|
|
|
$this->router->prepareRequestURI('http://example.com/category/movies/Se7en.htm'); |
|
449
|
|
|
$route = $this->router->route(); |
|
450
|
|
|
|
|
451
|
|
|
HttpRequest::setRequestMethod('GET'); |
|
452
|
|
|
$this->router->prepareRequestURI('http://example.com/feeds/news/atom.xml'); |
|
453
|
|
|
$route = $this->router->route(); |
|
454
|
|
|
|
|
455
|
|
|
HttpRequest::setRequestMethod('GET'); |
|
456
|
|
|
$this->router->prepareRequestURI('http://example.com/news/atom.xml'); |
|
457
|
|
|
$route = $this->router->route(); |
|
458
|
|
|
|
|
459
|
|
|
$this->markTestIncomplete('Test not implemented yet.'); |
|
460
|
|
|
}*/ |
|
461
|
|
|
|
|
462
|
|
|
/** |
|
463
|
|
|
* @expectedException OutOfBoundsException |
|
464
|
|
|
*/ |
|
465
|
|
|
public function testMethodmatch_throwsExceptionIfNoRoutesFound() |
|
|
|
|
|
|
466
|
|
|
{ |
|
467
|
|
|
$this->router->reset(); |
|
468
|
|
|
|
|
469
|
|
|
$this->assertTrue(0 === count($this->router->getRoutes())); |
|
470
|
|
|
|
|
471
|
|
|
$this->assertTrue($this->router->match()); |
|
472
|
|
|
} |
|
473
|
|
|
|
|
474
|
|
|
public function testMethodGenerateURL() |
|
475
|
|
|
{ |
|
476
|
|
|
/* |
|
|
|
|
|
|
477
|
|
|
$url = $this->router->generateURL($url_pattern); |
|
478
|
|
|
$this->assertEquals('url', $url); |
|
479
|
|
|
*/ |
|
480
|
|
|
$this->markTestSkipped('Test not implemented yet.'); |
|
481
|
|
|
} |
|
482
|
|
|
|
|
483
|
|
|
public function testMethodBuildURLWhenModRewriteOFF() |
|
484
|
|
|
{ |
|
485
|
|
|
/* |
|
486
|
|
|
* Do not build an URL, if FQDN is passed and mod_rewrite is off, |
|
487
|
|
|
* like http://application.com/tests/index.php?mod=news&action=show |
|
488
|
|
|
* Just return the URL (pass-through). |
|
489
|
|
|
*/ |
|
490
|
|
|
$urlstring = WWW_ROOT . 'index.php?mod=news&action=show'; |
|
491
|
|
|
$internal_url = false; |
|
|
|
|
|
|
492
|
|
|
$url = $this->router->buildURL($urlstring, false); |
|
493
|
|
|
$this->assertEquals(WWW_ROOT . 'index.php?mod=news&action=show', $url); |
|
494
|
|
|
|
|
495
|
|
|
$urlstring = WWW_ROOT . 'index.php?mod=news&action=show'; |
|
496
|
|
|
$internal_url = true; |
|
|
|
|
|
|
497
|
|
|
$url = $this->router->buildURL($urlstring, $internal_url); |
|
|
|
|
|
|
498
|
|
|
$this->assertEquals(WWW_ROOT . 'index.php?mod=news&action=show', $url); |
|
499
|
|
|
|
|
500
|
|
|
/* |
|
501
|
|
|
* Build FQDN URL from internal slashed URLs, like |
|
502
|
|
|
* /news |
|
503
|
|
|
* /news/show |
|
504
|
|
|
* /news/admin/show/2 |
|
505
|
|
|
* |
|
506
|
|
|
* So internally we use the mod_rewrite style. |
|
507
|
|
|
*/ |
|
508
|
|
|
/* |
|
509
|
|
|
* Parameter 1 - module |
|
510
|
|
|
*/ |
|
511
|
|
|
// removes crappy slashes - test 1 |
|
512
|
|
|
$urlstring = '////news///'; |
|
513
|
|
|
$internal_url = false; |
|
|
|
|
|
|
514
|
|
|
$url = $this->router->buildURL($urlstring, $internal_url); |
|
|
|
|
|
|
515
|
|
|
$this->assertEquals(WWW_ROOT . 'index.php?mod=news', $url); |
|
516
|
|
|
|
|
517
|
|
|
// removes crappy slashes - test 2 |
|
518
|
|
|
$urlstring = '/news///'; |
|
519
|
|
|
$internal_url = false; |
|
|
|
|
|
|
520
|
|
|
$url = $this->router->buildURL($urlstring, $internal_url); |
|
|
|
|
|
|
521
|
|
|
$this->assertEquals(WWW_ROOT . 'index.php?mod=news', $url); |
|
522
|
|
|
|
|
523
|
|
|
// route to module |
|
524
|
|
|
$urlstring = '/news'; |
|
525
|
|
|
$internal_url = false; |
|
|
|
|
|
|
526
|
|
|
$url = $this->router->buildURL($urlstring, $internal_url); |
|
|
|
|
|
|
527
|
|
|
$this->assertEquals(WWW_ROOT . 'index.php?mod=news', $url); |
|
528
|
|
|
|
|
529
|
|
|
/* |
|
530
|
|
|
* Parameter 2 - action or controller |
|
531
|
|
|
*/ |
|
532
|
|
|
// route to module/action |
|
533
|
|
|
$urlstring = ['/news/show' => 'module/action']; |
|
534
|
|
|
$internal_url = false; |
|
|
|
|
|
|
535
|
|
|
$url = $this->router->buildURL($urlstring, $internal_url); |
|
|
|
|
|
|
536
|
|
|
$this->assertEquals(WWW_ROOT . 'index.php?mod=news&action=show', $url); |
|
537
|
|
|
|
|
538
|
|
|
// route to module/action/id |
|
539
|
|
|
$urlstring = ['/news/show/42' => 'module/action/id']; |
|
540
|
|
|
$internal_url = true; |
|
|
|
|
|
|
541
|
|
|
$url = $this->router->buildURL($urlstring, $internal_url); |
|
|
|
|
|
|
542
|
|
|
$this->assertEquals(WWW_ROOT . 'index.php?mod=news&action=show&id=42', $url); |
|
543
|
|
|
|
|
544
|
|
|
// STANDARD PARAMETER ROUTING when MODREWRITE is OFF |
|
545
|
|
|
// we are not leaving any parameter out, so we don't need an urlstring description array |
|
546
|
|
|
// route to module/controller/action/id |
|
547
|
|
|
$urlstring = '/news/admin/edit/1'; |
|
548
|
|
|
$internal_url = true; |
|
|
|
|
|
|
549
|
|
|
$url = $this->router->buildURL($urlstring, $internal_url); |
|
|
|
|
|
|
550
|
|
|
$this->assertEquals(WWW_ROOT . 'index.php?mod=news&ctrl=admin&action=edit&id=1', $url); |
|
551
|
|
|
} |
|
552
|
|
|
|
|
553
|
|
|
public function testMethodBuildURLWhenModRewriteON() |
|
554
|
|
|
{ |
|
555
|
|
|
// precondition |
|
556
|
|
|
if (defined('REWRITE_ENGINE_ON') and REWRITE_ENGINE_ON === false) { |
|
|
|
|
|
|
557
|
|
|
$this->markTestSkipped('The Test depends on MOD_REWRITE.'); |
|
558
|
|
|
} else { |
|
559
|
|
|
$this->assertTrue(REWRITE_ENGINE_ON); |
|
560
|
|
|
} |
|
561
|
|
|
|
|
562
|
|
|
/* |
|
563
|
|
|
* Build URL from internal slashed URLs, like |
|
564
|
|
|
* /news |
|
565
|
|
|
* /news/show |
|
566
|
|
|
* /news/admin/show/2 |
|
567
|
|
|
* |
|
568
|
|
|
* So internally we use the mod_rewrite style. |
|
569
|
|
|
*/ |
|
570
|
|
|
/* |
|
571
|
|
|
* Parameter 1 - module |
|
572
|
|
|
*/ |
|
573
|
|
|
// removes crappy slashes - test 1 |
|
574
|
|
|
$urlstring = '////news///'; |
|
575
|
|
|
$internal_url = false; |
|
|
|
|
|
|
576
|
|
|
$url = $this->router->buildURL($urlstring, $internal_url); |
|
|
|
|
|
|
577
|
|
|
$this->assertEquals(WWW_ROOT . 'news', $url); |
|
578
|
|
|
|
|
579
|
|
|
// removes crappy slashes - test 2 |
|
580
|
|
|
$urlstring = '/news///'; |
|
581
|
|
|
$internal_url = false; |
|
|
|
|
|
|
582
|
|
|
$url = $this->router->buildURL($urlstring, $internal_url); |
|
|
|
|
|
|
583
|
|
|
$this->assertEquals(WWW_ROOT . 'news', $url); |
|
584
|
|
|
|
|
585
|
|
|
// removes crappy slashes - test 3 |
|
586
|
|
|
$urlstring = '/////news'; |
|
587
|
|
|
$internal_url = false; |
|
|
|
|
|
|
588
|
|
|
$url = $this->router->buildURL($urlstring, $internal_url); |
|
|
|
|
|
|
589
|
|
|
$this->assertEquals(WWW_ROOT . 'news', $url); |
|
590
|
|
|
|
|
591
|
|
|
/* |
|
592
|
|
|
* Parameter 2 - action or sub |
|
593
|
|
|
*/ |
|
594
|
|
|
$urlstring = '/news/show'; |
|
595
|
|
|
$internal_url = false; |
|
|
|
|
|
|
596
|
|
|
$url = $this->router->buildURL($urlstring, $internal_url); |
|
|
|
|
|
|
597
|
|
|
$this->assertEquals(WWW_ROOT . 'news/show', $url); |
|
598
|
|
|
|
|
599
|
|
|
/* |
|
600
|
|
|
* Internal URLs (mod_rewrite style) |
|
601
|
|
|
* This should by-pass... |
|
602
|
|
|
*/ |
|
603
|
|
|
$urlstring = '/news/show/42'; |
|
604
|
|
|
$internal_url = true; |
|
|
|
|
|
|
605
|
|
|
$url = $this->router->buildURL($urlstring, $internal_url); |
|
|
|
|
|
|
606
|
|
|
$this->assertEquals(WWW_ROOT . 'news/show/42', $url); |
|
607
|
|
|
|
|
608
|
|
|
$urlstring = '/news/admin/edit/1'; |
|
609
|
|
|
$internal_url = true; |
|
|
|
|
|
|
610
|
|
|
$url = $this->router->buildURL($urlstring, $internal_url); |
|
|
|
|
|
|
611
|
|
|
$this->assertEquals(WWW_ROOT . 'news/admin/edit/1', $url); |
|
612
|
|
|
} |
|
613
|
|
|
} |
|
614
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: