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()) + ); + } +} 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', '/');