diff --git a/src/Shell/RoutesShell.php b/src/Shell/RoutesShell.php index 005010cd40e..905c6e709fd 100644 --- a/src/Shell/RoutesShell.php +++ b/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 @@ public function check($url) { 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("'$url' did not match any routes."); return false; @@ -119,38 +124,4 @@ protected function _splitArgs($args) } 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(); - } } diff --git a/tests/TestCase/Shell/RoutesShellTest.php b/tests/TestCase/Shell/RoutesShellTest.php index 2676ce0ce3f..a34ac90e41b 100644 --- a/tests/TestCase/Shell/RoutesShellTest.php +++ b/tests/TestCase/Shell/RoutesShellTest.php @@ -62,19 +62,18 @@ public function tearDown() 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 @@ public function testMain() */ 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'); }