Skip to content

Commit 454146a

Browse files
committed
Use table macro in RoutesShell.
1 parent a089ded commit 454146a

File tree

2 files changed

+34
-57
lines changed

2 files changed

+34
-57
lines changed

src/Shell/RoutesShell.php

Lines changed: 9 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,13 @@ class RoutesShell extends Shell
3434
*/
3535
public function main()
3636
{
37-
$output = [];
37+
$output = [
38+
['Route name', 'URI template', 'Defaults']
39+
];
3840
foreach (Router::routes() as $route) {
3941
$output[] = [$route->getName(), $route->template, json_encode($route->defaults)];
4042
}
41-
42-
$this->_outWithColumns($output);
43+
$this->_io->macro('table', $output);
4344
}
4445

4546
/**
@@ -52,7 +53,11 @@ public function check($url)
5253
{
5354
try {
5455
$route = Router::parse($url);
55-
$this->_outWithColumns(['', $url, json_encode($route)]);
56+
$output = [
57+
['Route name', 'URI template', 'Defaults'],
58+
['', $url, json_encode($route)]
59+
];
60+
$this->_io->macro('table', $output);
5661
} catch (MissingRouteException $e) {
5762
$this->err("<warning>'$url' did not match any routes.</warning>");
5863
return false;
@@ -119,38 +124,4 @@ protected function _splitArgs($args)
119124
}
120125
return $out;
121126
}
122-
123-
/**
124-
* Takes an array to represent rows, of arrays to represent columns.
125-
* Will pad strings to the maximum character length of each column.
126-
*
127-
* @param array $rows The rows to print
128-
* @return void
129-
*/
130-
protected function _outWithColumns($rows)
131-
{
132-
if (!is_array($rows[0])) {
133-
$rows = [$rows];
134-
}
135-
$maxCharacterLength = [];
136-
array_unshift($rows, ['Route name', 'URI template', 'Defaults']);
137-
138-
foreach ($rows as $line) {
139-
for ($i = 0, $len = count($line); $i < $len; $i++) {
140-
$elementLength = strlen($line[$i]);
141-
if ($elementLength > (isset($maxCharacterLength[$i]) ? $maxCharacterLength[$i] : 0)) {
142-
$maxCharacterLength[$i] = $elementLength;
143-
}
144-
}
145-
}
146-
147-
foreach ($rows as $line) {
148-
for ($i = 0, $len = count($line); $i < $len; $i++) {
149-
$line[$i] = str_pad($line[$i], $maxCharacterLength[$i], " ", STR_PAD_RIGHT);
150-
}
151-
$this->out(implode(' ', $line));
152-
}
153-
154-
$this->out();
155-
}
156127
}

tests/TestCase/Shell/RoutesShellTest.php

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -62,19 +62,18 @@ public function tearDown()
6262
public function testMain()
6363
{
6464
$this->io->expects($this->at(0))
65-
->method('out')
66-
->with($this->logicalAnd(
67-
$this->stringContains('Route name'),
68-
$this->stringContains('URI template'),
69-
$this->stringContains('Defaults')
70-
));
71-
$this->io->expects($this->at(1))
72-
->method('out')
73-
->with($this->logicalAnd(
74-
$this->stringContains('articles:_action'),
75-
$this->stringContains('/articles/:action'),
76-
$this->stringContains('"controller":"Articles",')
77-
));
65+
->method('macro')
66+
->with(
67+
'table',
68+
$this->logicalAnd(
69+
$this->contains(['Route name', 'URI template', 'Defaults']),
70+
$this->contains([
71+
'articles:_action',
72+
'/articles/:action/*',
73+
'{"controller":"Articles","action":"index","plugin":null}'
74+
])
75+
)
76+
);
7877
$this->shell->main();
7978
}
8079

@@ -85,12 +84,19 @@ public function testMain()
8584
*/
8685
public function testCheck()
8786
{
88-
$this->io->expects($this->at(1))
89-
->method('out')
90-
->with($this->logicalAnd(
91-
$this->stringContains('/articles/index'),
92-
$this->stringContains('"controller":"Articles",')
93-
));
87+
$this->io->expects($this->at(0))
88+
->method('macro')
89+
->with(
90+
'table',
91+
$this->logicalAnd(
92+
$this->contains(['Route name', 'URI template', 'Defaults']),
93+
$this->contains([
94+
'',
95+
'/articles/index',
96+
'{"action":"index","pass":[],"controller":"Articles","plugin":null}'
97+
])
98+
)
99+
);
94100
$this->shell->check('/articles/index');
95101
}
96102

0 commit comments

Comments
 (0)