Browse Source

Merge pull request #7219 from cakephp/routes-check-name

Add the route's name to the array generated by RoutesShell check subcommand
Mark Story 10 years ago
parent
commit
c299d7794a
2 changed files with 36 additions and 3 deletions
  1. 12 1
      src/Shell/RoutesShell.php
  2. 24 2
      tests/TestCase/Shell/RoutesShellTest.php

+ 12 - 1
src/Shell/RoutesShell.php

@@ -42,6 +42,7 @@ class RoutesShell extends Shell
             $output[] = [$name, $route->template, json_encode($route->defaults)];
         }
         $this->helper('table')->output($output);
+        $this->out();
     }
 
     /**
@@ -54,13 +55,21 @@ class RoutesShell extends Shell
     {
         try {
             $route = Router::parse($url);
+            foreach (Router::routes() as $r) {
+                if ($r->match($route)) {
+                    $name = isset($r->options['_name']) ? $r->options['_name'] : $r->getName();
+                    break;
+                }
+            }
             $output = [
                 ['Route name', 'URI template', 'Defaults'],
-                ['', $url, json_encode($route)]
+                [$name, $url, json_encode($route)]
             ];
             $this->helper('table')->output($output);
+            $this->out();
         } catch (MissingRouteException $e) {
             $this->err("<warning>'$url' did not match any routes.</warning>");
+            $this->out();
             return false;
         }
     }
@@ -77,8 +86,10 @@ class RoutesShell extends Shell
             $args = $this->_splitArgs($this->args);
             $url = Router::url($args);
             $this->out("> $url");
+            $this->out();
         } catch (MissingRouteException $e) {
             $this->err("<warning>The provided parameters do not match any routes.</warning>");
+            $this->out();
             return false;
         }
     }

+ 24 - 2
tests/TestCase/Shell/RoutesShellTest.php

@@ -105,7 +105,7 @@ class RoutesShellTest extends TestCase
                 $this->logicalAnd(
                     $this->contains(['Route name', 'URI template', 'Defaults']),
                     $this->contains([
-                        '',
+                        'articles:_action',
                         '/articles/index',
                         '{"action":"index","pass":[],"controller":"Articles","plugin":null}'
                     ])
@@ -115,6 +115,28 @@ class RoutesShellTest extends TestCase
     }
 
     /**
+     * Test checking an existing route with named route.
+     *
+     * @return void
+     */
+    public function testCheckWithNamedRoute()
+    {
+        $this->table->expects($this->once())
+            ->method('output')
+            ->with(
+                $this->logicalAnd(
+                    $this->contains(['Route name', 'URI template', 'Defaults']),
+                    $this->contains([
+                        'testName',
+                        '/tests/index',
+                        '{"action":"index","pass":[],"controller":"Tests","plugin":null}'
+                    ])
+                )
+            );
+        $this->shell->check('/tests/index');
+    }
+
+    /**
      * Test checking an non-existing route.
      *
      * @return void
@@ -139,7 +161,7 @@ class RoutesShellTest extends TestCase
         $this->io->expects($this->at(0))
             ->method('out')
             ->with($this->stringContains('> /articles/index'));
-        $this->io->expects($this->at(1))
+        $this->io->expects($this->at(2))
             ->method('out')
             ->with($this->stringContains('> /articles/view/2/3'));