From 7d3cc1db1cd3b3bc0fe08795c2a7e866bb09c968 Mon Sep 17 00:00:00 2001 From: kenjis Date: Sun, 20 Feb 2022 16:27:05 +0900 Subject: [PATCH 1/2] fix: auto routes incorrectly display route filters with GET method --- system/Commands/Utilities/Routes.php | 4 +- tests/system/Commands/CommandTest.php | 10 ---- tests/system/Commands/RoutesTest.php | 73 +++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 11 deletions(-) create mode 100644 tests/system/Commands/RoutesTest.php diff --git a/system/Commands/Utilities/Routes.php b/system/Commands/Utilities/Routes.php index e4bfec05b5a5..754c1b9b2380 100644 --- a/system/Commands/Utilities/Routes.php +++ b/system/Commands/Utilities/Routes.php @@ -120,7 +120,9 @@ public function run(array $params) $autoRoutes = $autoRouteCollector->get(); foreach ($autoRoutes as &$routes) { - $filters = $filterCollector->get('get', $uriGenerator->get($routes[1])); + // There is no `auto` method, but it is intentional not to get route filters. + $filters = $filterCollector->get('auto', $uriGenerator->get($routes[1])); + $routes[] = implode(' ', array_map('class_basename', $filters['before'])); $routes[] = implode(' ', array_map('class_basename', $filters['after'])); } diff --git a/tests/system/Commands/CommandTest.php b/tests/system/Commands/CommandTest.php index 30cf3cce38b8..65f39dba3741 100644 --- a/tests/system/Commands/CommandTest.php +++ b/tests/system/Commands/CommandTest.php @@ -106,16 +106,6 @@ public function testNamespacesCommand() $this->assertStringContainsString('| Yes', $this->getBuffer()); } - public function testRoutesCommand() - { - command('routes'); - - $this->assertStringContainsString('| (Closure)', $this->getBuffer()); - $this->assertStringContainsString('| Route', $this->getBuffer()); - $this->assertStringContainsString('| testing', $this->getBuffer()); - $this->assertStringContainsString('\\TestController::index', $this->getBuffer()); - } - public function testInexistentCommandWithNoAlternatives() { command('app:oops'); diff --git a/tests/system/Commands/RoutesTest.php b/tests/system/Commands/RoutesTest.php new file mode 100644 index 000000000000..0f569a2e42ad --- /dev/null +++ b/tests/system/Commands/RoutesTest.php @@ -0,0 +1,73 @@ + + * + * For the full copyright and license information, please view + * the LICENSE file that was distributed with this source code. + */ + +namespace CodeIgniter\Commands; + +use CodeIgniter\Test\CIUnitTestCase; +use CodeIgniter\Test\Filters\CITestStreamFilter; +use Config\Services; + +/** + * @internal + */ +final class RoutesTest extends CIUnitTestCase +{ + private $streamFilter; + protected $logger; + protected $commands; + + protected function setUp(): void + { + parent::setUp(); + + CITestStreamFilter::$buffer = ''; + + $this->streamFilter = stream_filter_append(STDOUT, 'CITestStreamFilter'); + $this->streamFilter = stream_filter_append(STDERR, 'CITestStreamFilter'); + + $this->logger = Services::logger(); + $this->commands = Services::commands(); + } + + protected function tearDown(): void + { + stream_filter_remove($this->streamFilter); + } + + protected function getBuffer() + { + return CITestStreamFilter::$buffer; + } + + public function testRoutesCommand() + { + command('routes'); + + $this->assertStringContainsString('| (Closure)', $this->getBuffer()); + $this->assertStringContainsString('| Route', $this->getBuffer()); + $this->assertStringContainsString('| testing', $this->getBuffer()); + $this->assertStringContainsString('\\TestController::index', $this->getBuffer()); + } + + public function testRoutesCommandRouteFilterAndAutoRoute() + { + $routes = Services::routes(); + $routes->resetRoutes(); + $routes->get('/', 'Home::index', ['filter' => 'csrf']); + + command('routes'); + + $this->assertStringContainsString( + '|auto|/|\App\Controllers\Home::index||toolbar|', + str_replace(' ', '', $this->getBuffer()) + ); + } +} From 5c2995c584f7e7caadc9ebdea3c4853681a66001 Mon Sep 17 00:00:00 2001 From: kenjis Date: Sun, 20 Feb 2022 17:42:33 +0900 Subject: [PATCH 2/2] test: fix failed test --- .../system/Commands/Utilities/Routes/FilterCollectorTest.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/system/Commands/Utilities/Routes/FilterCollectorTest.php b/tests/system/Commands/Utilities/Routes/FilterCollectorTest.php index c27157ca7856..6002323d3393 100644 --- a/tests/system/Commands/Utilities/Routes/FilterCollectorTest.php +++ b/tests/system/Commands/Utilities/Routes/FilterCollectorTest.php @@ -12,6 +12,7 @@ namespace CodeIgniter\Commands\Utilities\Routes; use CodeIgniter\Test\CIUnitTestCase; +use Config\Services; /** * @internal @@ -20,6 +21,9 @@ final class FilterCollectorTest extends CIUnitTestCase { public function testGet() { + $routes = Services::routes(); + $routes->resetRoutes(); + $collector = new FilterCollector(); $filters = $collector->get('get', '/');