Skip to content

Commit

Permalink
Fix passed argument handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Apr 5, 2015
1 parent db796ad commit eb2134d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 37 deletions.
59 changes: 25 additions & 34 deletions src/Shell/RoutesShell.php
Expand Up @@ -36,7 +36,7 @@ public function main()
{
$output = [];
foreach (Router::routes() as $route) {
$output[] = [$route->getName(), $route->template, $this->_stringifyDefaults($route->defaults)];
$output[] = [$route->getName(), $route->template, json_encode($route->defaults)];
}

$this->_outWithColumns($output);
Expand All @@ -52,7 +52,7 @@ public function check($url)
{
try {
$route = Router::parse($url);
$this->_outWithColumns(['', $url, $this->_stringifyDefaults($route)]);
$this->_outWithColumns(['', $url, json_encode($route)]);
} catch (MissingRouteException $e) {
$this->err("<warning>'$url' did not match any routes.</warning>");
return false;
Expand All @@ -77,6 +77,24 @@ public function generate()
}
}

public function getOptionParser()
{
$parser = parent::getOptionParser();
$parser->description(
'Get the list of routes connected in this application. ' .
'This tool also lets you test URL generation and URL parsing.'
)->addSubcommand('check', [
'help' => 'Check a URL string against the routes. ' .
'Will output the routing parameters the route resolves to.'
])->addSubcommand('generate', [
'help' => 'Check a routing array agains the routes. ' .
"Will output the URL if there is a match.\n\n" .
"Routing parameters should be supplied in a key:value format. " .
"For example `controller:Articles action:view 2`"
]);
return $parser;
}

/**
* Split the CLI arguments into a hash.
*
Expand All @@ -87,12 +105,12 @@ protected function _splitArgs($args)
{
$out = [];
foreach ($args as $arg) {
if (strpos($arg, ':') === false) {
$this->err("<error>The '$arg' is malformed. It should be formated like `key:value`.");
continue;
if (strpos($arg, ':') !== false) {
list($key, $value) = explode(':', $arg);
$out[$key] = $value;
} else {
$out[] = $arg;
}
list($key, $value) = explode(':', $arg);
$out[$key] = $value;
}
return $out;
}
Expand Down Expand Up @@ -129,31 +147,4 @@ protected function _outWithColumns($rows)

$this->out();
}

/**
* Get defaults from the route object as a string
*
* @param array $defaults The defaults to use for creating a route array.
* @return string
*/
protected function _stringifyDefaults($defaults)
{
$results = [];
if (!empty($defaults['controller'])) {
$results['controller'] = $defaults['controller'];
}
if (!empty($defaults['action'])) {
$results['action'] = $defaults['action'];
}
if (!empty($defaults[0])) {
$pass = [];
$i = 0;
while (!empty($defaults[$i])) {
$pass[$i] = $defaults[$i];
$i++;
}
$results['pass'] = $pass;
}
return json_encode($results);
}
}
15 changes: 12 additions & 3 deletions tests/TestCase/Shell/RoutesShellTest.php
Expand Up @@ -38,7 +38,7 @@ public function setUp()
$this->io = $this->getMock('Cake\Console\ConsoleIo');

$this->shell = new RoutesShell($this->io);
Router::connect('/articles/:action', ['controller' => 'Articles']);
Router::connect('/articles/:action/*', ['controller' => 'Articles']);
Router::connect('/bake/:controller/:action', ['plugin' => 'Bake']);
}

Expand Down Expand Up @@ -73,7 +73,7 @@ public function testMain()
->with($this->logicalAnd(
$this->stringContains('articles:_action'),
$this->stringContains('/articles/:action'),
$this->stringContains('{"controller":"Articles",')
$this->stringContains('"controller":"Articles",')
));
$this->shell->main();
}
Expand All @@ -89,7 +89,7 @@ public function testCheck()
->method('out')
->with($this->logicalAnd(
$this->stringContains('/articles/index'),
$this->stringContains('{"controller":"Articles",')
$this->stringContains('"controller":"Articles",')
));
$this->shell->check('/articles/index');
}
Expand All @@ -114,11 +114,20 @@ public function testCheckNotFound()
*/
public function testGenerate()
{
$this->io->expects($this->never())
->method('err');
$this->io->expects($this->at(0))
->method('out')
->with($this->stringContains('> /articles/index'));
$this->io->expects($this->at(1))
->method('out')
->with($this->stringContains('> /articles/view/2/3'));

$this->shell->args = ['controller:Articles', 'action:index'];
$this->shell->generate();

$this->shell->args = ['controller:Articles', 'action:view', '2', '3'];
$this->shell->generate();
}

/**
Expand Down

0 comments on commit eb2134d

Please sign in to comment.