|
5 | 5 | use CodeIgniter\HTTP\IncomingRequest; |
6 | 6 | use CodeIgniter\Test\CIUnitTestCase; |
7 | 7 | use Config\Modules; |
| 8 | +use CodeIgniter\Exceptions\PageNotFoundException; |
8 | 9 |
|
9 | 10 | class RouterTest extends CIUnitTestCase |
10 | 11 | { |
@@ -81,9 +82,9 @@ public function testZeroAsURIPath() |
81 | 82 | { |
82 | 83 | $router = new Router($this->collection, $this->request); |
83 | 84 |
|
84 | | - $router->handle('0'); |
| 85 | + $this->expectException(PageNotFoundException::class); |
85 | 86 |
|
86 | | - $this->assertEquals('0', $router->controllerName()); |
| 87 | + $router->handle('0'); |
87 | 88 | } |
88 | 89 |
|
89 | 90 | //-------------------------------------------------------------------- |
@@ -231,6 +232,153 @@ public function testAutoRouteFindsControllerWithSubfolder() |
231 | 232 |
|
232 | 233 | //-------------------------------------------------------------------- |
233 | 234 |
|
| 235 | + public function testAutoRouteFindsDashedSubfolder() |
| 236 | + { |
| 237 | + $router = new Router($this->collection, $this->request); |
| 238 | + $router->setTranslateURIDashes(true); |
| 239 | + |
| 240 | + mkdir(APPPATH . 'Controllers/Dash_folder'); |
| 241 | + |
| 242 | + $router->autoRoute('dash-folder/mycontroller/somemethod'); |
| 243 | + |
| 244 | + rmdir(APPPATH . 'Controllers/Dash_folder'); |
| 245 | + |
| 246 | + $this->assertEquals('Dash_folder/', $router->directory()); |
| 247 | + $this->assertEquals('Mycontroller', $router->controllerName()); |
| 248 | + $this->assertEquals('somemethod', $router->methodName()); |
| 249 | + } |
| 250 | + |
| 251 | + //-------------------------------------------------------------------- |
| 252 | + |
| 253 | + public function testAutoRouteFindsDashedController() |
| 254 | + { |
| 255 | + $router = new Router($this->collection, $this->request); |
| 256 | + $router->setTranslateURIDashes(true); |
| 257 | + |
| 258 | + mkdir(APPPATH . 'Controllers/Dash_folder'); |
| 259 | + file_put_contents(APPPATH . 'Controllers/Dash_folder/Dash_controller.php', ''); |
| 260 | + |
| 261 | + $router->autoRoute('dash-folder/dash-controller/somemethod'); |
| 262 | + |
| 263 | + unlink(APPPATH . 'Controllers/Dash_folder/Dash_controller.php'); |
| 264 | + rmdir(APPPATH . 'Controllers/Dash_folder'); |
| 265 | + |
| 266 | + $this->assertEquals('Dash_folder/', $router->directory()); |
| 267 | + $this->assertEquals('Dash_controller', $router->controllerName()); |
| 268 | + $this->assertEquals('somemethod', $router->methodName()); |
| 269 | + } |
| 270 | + |
| 271 | + //-------------------------------------------------------------------- |
| 272 | + |
| 273 | + public function testAutoRouteFindsDashedMethod() |
| 274 | + { |
| 275 | + $router = new Router($this->collection, $this->request); |
| 276 | + $router->setTranslateURIDashes(true); |
| 277 | + |
| 278 | + mkdir(APPPATH . 'Controllers/Dash_folder'); |
| 279 | + file_put_contents(APPPATH . 'Controllers/Dash_folder/Dash_controller.php', ''); |
| 280 | + |
| 281 | + $router->autoRoute('dash-folder/dash-controller/dash-method'); |
| 282 | + |
| 283 | + unlink(APPPATH . 'Controllers/Dash_folder/Dash_controller.php'); |
| 284 | + rmdir(APPPATH . 'Controllers/Dash_folder'); |
| 285 | + |
| 286 | + $this->assertEquals('Dash_folder/', $router->directory()); |
| 287 | + $this->assertEquals('Dash_controller', $router->controllerName()); |
| 288 | + $this->assertEquals('dash_method', $router->methodName()); |
| 289 | + } |
| 290 | + |
| 291 | + //-------------------------------------------------------------------- |
| 292 | + |
| 293 | + public function testAutoRouteFindsDefaultDashFolder() |
| 294 | + { |
| 295 | + $router = new Router($this->collection, $this->request); |
| 296 | + $router->setTranslateURIDashes(true); |
| 297 | + |
| 298 | + mkdir(APPPATH . 'Controllers/Dash_folder'); |
| 299 | + |
| 300 | + $router->autoRoute('dash-folder'); |
| 301 | + |
| 302 | + rmdir(APPPATH . 'Controllers/Dash_folder'); |
| 303 | + |
| 304 | + $this->assertEquals('Dash_folder/', $router->directory()); |
| 305 | + $this->assertEquals('Home', $router->controllerName()); |
| 306 | + $this->assertEquals('index', $router->methodName()); |
| 307 | + } |
| 308 | + |
| 309 | + //-------------------------------------------------------------------- |
| 310 | + |
| 311 | + public function testAutoRouteFindsMByteDir() |
| 312 | + { |
| 313 | + $router = new Router($this->collection, $this->request); |
| 314 | + $router->setTranslateURIDashes(true); |
| 315 | + |
| 316 | + mkdir(APPPATH . 'Controllers/Φ'); |
| 317 | + |
| 318 | + $router->autoRoute('Φ'); |
| 319 | + |
| 320 | + rmdir(APPPATH . 'Controllers/Φ'); |
| 321 | + |
| 322 | + $this->assertEquals('Φ/', $router->directory()); |
| 323 | + $this->assertEquals('Home', $router->controllerName()); |
| 324 | + $this->assertEquals('index', $router->methodName()); |
| 325 | + } |
| 326 | + |
| 327 | + //-------------------------------------------------------------------- |
| 328 | + |
| 329 | + public function testAutoRouteFindsMByteController() |
| 330 | + { |
| 331 | + $router = new Router($this->collection, $this->request); |
| 332 | + $router->setTranslateURIDashes(true); |
| 333 | + |
| 334 | + file_put_contents(APPPATH . 'Controllers/Φ', ''); |
| 335 | + |
| 336 | + $router->autoRoute('Φ'); |
| 337 | + |
| 338 | + unlink(APPPATH . 'Controllers/Φ'); |
| 339 | + |
| 340 | + $this->assertEquals('Φ', $router->controllerName()); |
| 341 | + $this->assertEquals('index', $router->methodName()); |
| 342 | + } |
| 343 | + |
| 344 | + //-------------------------------------------------------------------- |
| 345 | + |
| 346 | + public function testAutoRouteRejectsSingleDot() |
| 347 | + { |
| 348 | + $router = new Router($this->collection, $this->request); |
| 349 | + $router->setTranslateURIDashes(true); |
| 350 | + |
| 351 | + $this->expectException(PageNotFoundException::class); |
| 352 | + |
| 353 | + $router->autoRoute('.'); |
| 354 | + } |
| 355 | + |
| 356 | + //-------------------------------------------------------------------- |
| 357 | + |
| 358 | + public function testAutoRouteRejectsDoubleDot() |
| 359 | + { |
| 360 | + $router = new Router($this->collection, $this->request); |
| 361 | + $router->setTranslateURIDashes(true); |
| 362 | + |
| 363 | + $this->expectException(PageNotFoundException::class); |
| 364 | + |
| 365 | + $router->autoRoute('..'); |
| 366 | + } |
| 367 | + |
| 368 | + //-------------------------------------------------------------------- |
| 369 | + |
| 370 | + public function testAutoRouteRejectsMidDot() |
| 371 | + { |
| 372 | + $router = new Router($this->collection, $this->request); |
| 373 | + $router->setTranslateURIDashes(true); |
| 374 | + |
| 375 | + $this->expectException(PageNotFoundException::class); |
| 376 | + |
| 377 | + $router->autoRoute('Foo.bar'); |
| 378 | + } |
| 379 | + |
| 380 | + //-------------------------------------------------------------------- |
| 381 | + |
234 | 382 | public function testDetectsLocales() |
235 | 383 | { |
236 | 384 | $router = new Router($this->collection, $this->request); |
@@ -575,4 +723,35 @@ public function testRegularExpressionPlaceholderWithUnicode() |
575 | 723 | ]; |
576 | 724 | $this->assertEquals($expected, $router->params()); |
577 | 725 | } |
| 726 | + |
| 727 | + public function testRouterPriorDirectory() |
| 728 | + { |
| 729 | + $router = new Router($this->collection, $this->request); |
| 730 | + |
| 731 | + $router->setDirectory('foo/bar/baz', false, true); |
| 732 | + $router->handle('Some_controller/some_method/param1/param2/param3'); |
| 733 | + |
| 734 | + $this->assertEquals('foo/bar/baz/', $router->directory()); |
| 735 | + $this->assertEquals('Some_controller', $router->controllerName()); |
| 736 | + $this->assertEquals('some_method', $router->methodName()); |
| 737 | + } |
| 738 | + |
| 739 | + public function testSetDirectoryValid() |
| 740 | + { |
| 741 | + $router = new Router($this->collection, $this->request); |
| 742 | + $router->setDirectory('foo/bar/baz', false, true); |
| 743 | + |
| 744 | + $this->assertEquals('foo/bar/baz/', $router->directory()); |
| 745 | + } |
| 746 | + |
| 747 | + public function testSetDirectoryInvalid() |
| 748 | + { |
| 749 | + $router = new Router($this->collection, $this->request); |
| 750 | + $router->setDirectory('foo/bad-segment/bar', false, true); |
| 751 | + |
| 752 | + $internal = $this->getPrivateProperty($router, 'directory'); |
| 753 | + |
| 754 | + $this->assertNull($internal); |
| 755 | + $this->assertEquals('', $router->directory()); |
| 756 | + } |
578 | 757 | } |
0 commit comments