From 1df8a1bd488fae8aa75642dea461b268b119c4ce Mon Sep 17 00:00:00 2001 From: kenjis Date: Tue, 11 Apr 2023 08:45:07 +0900 Subject: [PATCH] fix: spark routes shows invalid routes with Auto Routing Improved Shows invalid routes with `x` to let devs know there are methods. --- .../ControllerMethodReader.php | 6 ++++ .../ControllerMethodReaderTest.php | 35 +++++++++++++++++-- .../AutoRouterImproved/Controllers/Home.php | 35 +++++++++++++++++++ 3 files changed, 73 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 618747693d89..9504347b8fa1 100644 --- a/system/Commands/Utilities/Routes/AutoRouterImproved/ControllerMethodReader.php +++ b/system/Commands/Utilities/Routes/AutoRouterImproved/ControllerMethodReader.php @@ -117,6 +117,12 @@ public function read(string $class, string $defaultController = 'Home', string $ $params[$param->getName()] = $required; } + // If it is the default controller, the method will not be + // routed. + if ($classShortname === $defaultController) { + $route = 'x ' . $route; + } + $output[] = [ 'method' => $httpVerb, 'route' => $route, 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() + { + } +}