Browse Source

Use table macro in RoutesShell.

Mark Story 11 years ago
parent
commit
454146acfc
2 changed files with 34 additions and 57 deletions
  1. 9 38
      src/Shell/RoutesShell.php
  2. 25 19
      tests/TestCase/Shell/RoutesShellTest.php

+ 9 - 38
src/Shell/RoutesShell.php

@@ -34,12 +34,13 @@ class RoutesShell extends Shell
      */
     public function main()
     {
-        $output = [];
+        $output = [
+            ['Route name', 'URI template', 'Defaults']
+        ];
         foreach (Router::routes() as $route) {
             $output[] = [$route->getName(), $route->template, json_encode($route->defaults)];
         }
-
-        $this->_outWithColumns($output);
+        $this->_io->macro('table', $output);
     }
 
     /**
@@ -52,7 +53,11 @@ class RoutesShell extends Shell
     {
         try {
             $route = Router::parse($url);
-            $this->_outWithColumns(['', $url, json_encode($route)]);
+            $output = [
+                ['Route name', 'URI template', 'Defaults'],
+                ['', $url, json_encode($route)]
+            ];
+            $this->_io->macro('table', $output);
         } catch (MissingRouteException $e) {
             $this->err("<warning>'$url' did not match any routes.</warning>");
             return false;
@@ -119,38 +124,4 @@ class RoutesShell extends Shell
         }
         return $out;
     }
-
-    /**
-     * Takes an array to represent rows, of arrays to represent columns.
-     * Will pad strings to the maximum character length of each column.
-     *
-     * @param array $rows The rows to print
-     * @return void
-     */
-    protected function _outWithColumns($rows)
-    {
-        if (!is_array($rows[0])) {
-            $rows = [$rows];
-        }
-        $maxCharacterLength = [];
-        array_unshift($rows, ['Route name', 'URI template', 'Defaults']);
-
-        foreach ($rows as $line) {
-            for ($i = 0, $len = count($line); $i < $len; $i++) {
-                $elementLength = strlen($line[$i]);
-                if ($elementLength > (isset($maxCharacterLength[$i]) ? $maxCharacterLength[$i] : 0)) {
-                    $maxCharacterLength[$i] = $elementLength;
-                }
-            }
-        }
-
-        foreach ($rows as $line) {
-            for ($i = 0, $len = count($line); $i < $len; $i++) {
-                $line[$i] = str_pad($line[$i], $maxCharacterLength[$i], " ", STR_PAD_RIGHT);
-            }
-            $this->out(implode('    ', $line));
-        }
-
-        $this->out();
-    }
 }

+ 25 - 19
tests/TestCase/Shell/RoutesShellTest.php

@@ -62,19 +62,18 @@ class RoutesShellTest extends TestCase
     public function testMain()
     {
         $this->io->expects($this->at(0))
-            ->method('out')
-            ->with($this->logicalAnd(
-                $this->stringContains('Route name'),
-                $this->stringContains('URI template'),
-                $this->stringContains('Defaults')
-            ));
-        $this->io->expects($this->at(1))
-            ->method('out')
-            ->with($this->logicalAnd(
-                $this->stringContains('articles:_action'),
-                $this->stringContains('/articles/:action'),
-                $this->stringContains('"controller":"Articles",')
-            ));
+            ->method('macro')
+            ->with(
+                'table',
+                $this->logicalAnd(
+                    $this->contains(['Route name', 'URI template', 'Defaults']),
+                    $this->contains([
+                        'articles:_action',
+                        '/articles/:action/*',
+                        '{"controller":"Articles","action":"index","plugin":null}'
+                    ])
+                )
+            );
         $this->shell->main();
     }
 
@@ -85,12 +84,19 @@ class RoutesShellTest extends TestCase
      */
     public function testCheck()
     {
-        $this->io->expects($this->at(1))
-            ->method('out')
-            ->with($this->logicalAnd(
-                $this->stringContains('/articles/index'),
-                $this->stringContains('"controller":"Articles",')
-            ));
+        $this->io->expects($this->at(0))
+            ->method('macro')
+            ->with(
+                'table',
+                $this->logicalAnd(
+                    $this->contains(['Route name', 'URI template', 'Defaults']),
+                    $this->contains([
+                        '',
+                        '/articles/index',
+                        '{"action":"index","pass":[],"controller":"Articles","plugin":null}'
+                    ])
+                )
+            );
         $this->shell->check('/articles/index');
     }