From e31ddeb3c419d5e105a45c064d626fe8890671c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amel=20Junuzovi=C4=87?= Date: Wed, 2 Oct 2024 09:54:30 +0200 Subject: [PATCH] fix: TypeError for OPTIONS routes when translateURIDashes is enabled --- system/Router/Router.php | 2 +- tests/system/Router/RouterTest.php | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/system/Router/Router.php b/system/Router/Router.php index 2bb050de30ae..0cd1614991ee 100644 --- a/system/Router/Router.php +++ b/system/Router/Router.php @@ -247,7 +247,7 @@ public function getFilters(): array */ public function controllerName() { - return $this->translateURIDashes + return $this->translateURIDashes && ! $this->controller instanceof Closure ? str_replace('-', '_', $this->controller) : $this->controller; } diff --git a/tests/system/Router/RouterTest.php b/tests/system/Router/RouterTest.php index bb5302e721dc..5d51bf2fd38c 100644 --- a/tests/system/Router/RouterTest.php +++ b/tests/system/Router/RouterTest.php @@ -13,6 +13,7 @@ namespace CodeIgniter\Router; +use Closure; use CodeIgniter\Config\Factories; use CodeIgniter\Config\Services; use CodeIgniter\Exceptions\PageNotFoundException; @@ -69,6 +70,7 @@ private function createRouteCollection(?Routing $routingConfig = null): void 'posts/(:num)/edit' => 'Blog::edit/$1', 'books/(:num)/(:alpha)/(:num)' => 'Blog::show/$3/$1', 'closure/(:num)/(:alpha)' => static fn ($num, $str) => $num . '-' . $str, + 'closure-dash/(:num)/(:alpha)' => static fn ($num, $str) => $num . '-' . $str, '{locale}/pages' => 'App\Pages::list_all', 'test/(:any)/lang/{locale}' => 'App\Pages::list_all', 'admin/admins' => 'App\Admin\Admins::list_all', @@ -216,6 +218,22 @@ public function testClosures(): void $this->assertSame($expects, '123-alpha'); } + public function testClosuresWithTranslateURIDashes(): void + { + $router = new Router($this->collection, $this->request); + $router->setTranslateURIDashes(true); + + $router->handle('closure-dash/123/alpha'); + $closure = $router->controllerName(); + + $this->assertInstanceOf(Closure::class, $closure); + + $expects = $closure(...$router->params()); + + $this->assertIsCallable($router->controllerName()); + $this->assertSame($expects, '123-alpha'); + } + public function testAutoRouteFindsDefaultControllerAndMethod(): void { $this->collection->setAutoRoute(true);