Browse Source

add --with-middlewares option to RoutesCommand

Kevin Pfeifer 2 years ago
parent
commit
18cb32b4e5

+ 11 - 0
src/Command/RoutesCommand.php

@@ -37,6 +37,9 @@ class RoutesCommand extends Command
     public function execute(Arguments $args, ConsoleIo $io): ?int
     {
         $header = ['Route name', 'URI template', 'Plugin', 'Prefix', 'Controller', 'Action', 'Method(s)'];
+        if ($args->getOption('with-middlewares') || $args->getOption('verbose')) {
+            $header[] = 'Middlewares';
+        }
         if ($args->getOption('verbose')) {
             $header[] = 'Defaults';
         }
@@ -57,6 +60,9 @@ class RoutesCommand extends Command
                 implode(', ', $methods),
             ];
 
+            if ($args->getOption('with-middlewares') || $args->getOption('verbose')) {
+                $item[] = implode(', ', $route->getMiddleware());
+            }
             if ($args->getOption('verbose')) {
                 ksort($route->defaults);
                 $item[] = json_encode($route->defaults, JSON_THROW_ON_ERROR);
@@ -134,6 +140,11 @@ class RoutesCommand extends Command
                 'help' => 'Sorts alphabetically by route name A-Z',
                 'short' => 's',
                 'boolean' => true,
+            ])
+            ->addOption('with-middlewares', [
+                'help' => 'Show route specific middlewares',
+                'short' => 'm',
+                'boolean' => true,
             ]);
 
         return $parser;

+ 23 - 2
tests/TestCase/Command/RoutesCommandTest.php

@@ -118,6 +118,7 @@ class RoutesCommandTest extends TestCase
             '<info>Controller</info>',
             '<info>Action</info>',
             '<info>Method(s)</info>',
+            '<info>Middlewares</info>',
             '<info>Defaults</info>',
         ]);
         $this->assertOutputContainsRow([
@@ -128,6 +129,7 @@ class RoutesCommandTest extends TestCase
             'Articles',
             'index',
             '',
+            'dumb, sample',
             '{"action":"index","controller":"Articles","plugin":null}',
         ]);
     }
@@ -147,6 +149,25 @@ class RoutesCommandTest extends TestCase
     }
 
     /**
+     * Test routes with --with-middlewares option
+     */
+    public function testRouteWithMiddlewares(): void
+    {
+        $this->exec('routes -m');
+        $this->assertExitCode(CommandInterface::CODE_SUCCESS);
+        $this->assertOutputContainsRow([
+            'articles:_action',
+            '/app/articles/{action}/*',
+            '',
+            '',
+            'Articles',
+            'index',
+            '',
+            'dumb, sample',
+        ]);
+    }
+
+    /**
      * Ensure help for `routes` works
      */
     public function testCheckHelp(): void
@@ -182,7 +203,7 @@ class RoutesCommandTest extends TestCase
         $this->assertOutputContainsRow([
             'articles:_action',
             '/app/articles/check',
-            '{"action":"check","controller":"Articles","pass":[],"plugin":null}',
+            '{"_middleware":["dumb","sample"],"action":"check","controller":"Articles","pass":[],"plugin":null}',
         ]);
     }
 
@@ -201,7 +222,7 @@ class RoutesCommandTest extends TestCase
         $this->assertOutputContainsRow([
             'testName',
             '/app/tests/index',
-            '{"_name":"testName","action":"index","controller":"Tests","pass":[],"plugin":null}',
+            '{"_middleware":["dumb","sample"],"_name":"testName","action":"index","controller":"Tests","pass":[],"plugin":null}',
         ]);
     }
 

+ 5 - 0
tests/test_app/TestApp/Application.php

@@ -31,6 +31,8 @@ use stdClass;
 use TestApp\Command\AbortCommand;
 use TestApp\Command\DependencyCommand;
 use TestApp\Command\FormatSpecifierCommand;
+use TestApp\Middleware\DumbMiddleware;
+use TestApp\Middleware\SampleMiddleware;
 
 class Application extends BaseApplication
 {
@@ -72,7 +74,10 @@ class Application extends BaseApplication
      */
     public function routes(RouteBuilder $routes): void
     {
+        $routes->registerMiddleware('dumb', new DumbMiddleware());
+        $routes->registerMiddleware('sample', new SampleMiddleware());
         $routes->scope('/app', function (RouteBuilder $routes): void {
+            $routes->applyMiddleware('dumb', 'sample');
             $routes->connect('/articles', ['controller' => 'Articles']);
             $routes->connect('/articles/{action}/*', ['controller' => 'Articles']);