Skip to content

Commit

Permalink
Use table macro in RoutesShell.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed May 15, 2015
1 parent a089ded commit 454146a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 57 deletions.
47 changes: 9 additions & 38 deletions src/Shell/RoutesShell.php
Expand Up @@ -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);
}

/**
Expand All @@ -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("<warning>'$url' did not match any routes.</warning>");
return false;
Expand Down Expand Up @@ -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();
}
}
44 changes: 25 additions & 19 deletions tests/TestCase/Shell/RoutesShellTest.php
Expand Up @@ -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();
}

Expand All @@ -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');
}

Expand Down

0 comments on commit 454146a

Please sign in to comment.