From 5a527d4a90273dbcf31e459ccecd56b7fb5b95bf Mon Sep 17 00:00:00 2001 From: kenjis Date: Sat, 8 Apr 2023 17:31:18 +0900 Subject: [PATCH] fix: `spark routes` shows invalid routes with Auto Routing Improved --- .../ControllerMethodReader.php | 5 +++ .../ControllerMethodReaderTest.php | 35 +++++++++++++++++-- .../AutoRouterImproved/Controllers/Home.php | 35 +++++++++++++++++++ 3 files changed, 72 insertions(+), 3 deletions(-) create mode 100644 tests/system/Commands/Utilities/Routes/AutoRouterImproved/Controllers/Home.php diff --git a/system/Commands/Utilities/Routes/AutoRouterImproved/ControllerMethodReader.php b/system/Commands/Utilities/Routes/AutoRouterImproved/ControllerMethodReader.php index 3f373c433f1b..d2e54fa54462 100644 --- a/system/Commands/Utilities/Routes/AutoRouterImproved/ControllerMethodReader.php +++ b/system/Commands/Utilities/Routes/AutoRouterImproved/ControllerMethodReader.php @@ -93,6 +93,11 @@ public function read(string $class, string $defaultController = 'Home', string $ continue; } + // Skip the default controller. + if (class_basename($class) === $defaultController) { + continue; + } + $route = $classInUri . '/' . $methodInUri; $params = []; diff --git a/tests/system/Commands/Utilities/Routes/AutoRouterImproved/ControllerMethodReaderTest.php b/tests/system/Commands/Utilities/Routes/AutoRouterImproved/ControllerMethodReaderTest.php index 6a77f0173ab5..fae587b1e8cb 100644 --- a/tests/system/Commands/Utilities/Routes/AutoRouterImproved/ControllerMethodReaderTest.php +++ b/tests/system/Commands/Utilities/Routes/AutoRouterImproved/ControllerMethodReaderTest.php @@ -11,6 +11,7 @@ namespace CodeIgniter\Commands\Utilities\Routes\AutoRouterImproved; +use CodeIgniter\Commands\Utilities\Routes\AutoRouterImproved\Controllers\Home; use CodeIgniter\Test\CIUnitTestCase; use Tests\Support\Controllers\Newautorouting; use Tests\Support\Controllers\Remap; @@ -22,13 +23,13 @@ */ final class ControllerMethodReaderTest extends CIUnitTestCase { - private function createControllerMethodReader(): ControllerMethodReader - { + private function createControllerMethodReader( + string $namespace = 'Tests\Support\Controllers' + ): ControllerMethodReader { $methods = [ 'get', 'post', ]; - $namespace = 'Tests\Support\Controllers'; return new ControllerMethodReader($namespace, $methods); } @@ -63,6 +64,34 @@ public function testRead() $this->assertSame($expected, $routes); } + public function testReadDefaultController() + { + $reader = $this->createControllerMethodReader( + 'CodeIgniter\Commands\Utilities\Routes\AutoRouterImproved\Controllers' + ); + + $routes = $reader->read(Home::class); + + $expected = [ + 0 => [ + 'method' => 'get', + 'route' => '/', + 'route_params' => '', + 'handler' => '\CodeIgniter\Commands\Utilities\Routes\AutoRouterImproved\Controllers\Home::getIndex', + 'params' => [], + ], + [ + 'method' => 'post', + 'route' => '/', + 'route_params' => '', + 'handler' => '\CodeIgniter\Commands\Utilities\Routes\AutoRouterImproved\Controllers\Home::postIndex', + 'params' => [], + ], + ]; + + $this->assertSame($expected, $routes); + } + public function testReadControllerWithRemap() { $reader = $this->createControllerMethodReader(); diff --git a/tests/system/Commands/Utilities/Routes/AutoRouterImproved/Controllers/Home.php b/tests/system/Commands/Utilities/Routes/AutoRouterImproved/Controllers/Home.php new file mode 100644 index 000000000000..4666a709a07b --- /dev/null +++ b/tests/system/Commands/Utilities/Routes/AutoRouterImproved/Controllers/Home.php @@ -0,0 +1,35 @@ + + * + * For the full copyright and license information, please view + * the LICENSE file that was distributed with this source code. + */ + +namespace CodeIgniter\Commands\Utilities\Routes\AutoRouterImproved\Controllers; + +use CodeIgniter\Controller; + +/** + * The default controller for Auto Routing (Improved) + */ +class Home extends Controller +{ + public function getIndex() + { + } + + public function postIndex() + { + } + + /** + * This method cannot be accessible. + */ + public function getFoo() + { + } +}