Making greedy -> greedyNamed its clearer and doesn't consume a possibly easy to use named parameter key.

Expanding tests for named parameter rules/conditions.
This commit is contained in:
mark_story 2011-03-03 06:49:11 -05:00
parent 3eef281e1c
commit 97175aac90
3 changed files with 50 additions and 6 deletions

View file

@ -252,10 +252,10 @@ class CakeRoute {
$context = compact('controller', 'action');
$namedConfig = Router::namedConfig();
$greedy = isset($this->options['greedy']) ? $this->options['greedy'] : $namedConfig['greedy'];
$greedy = isset($this->options['greedyNamed']) ? $this->options['greedyNamed'] : $namedConfig['greedy'];
$rules = $namedConfig['rules'];
if (isset($this->options['named'])) {
$greedy = isset($this->options['greedy']) && $this->options['greedy'] === true;
$greedy = isset($this->options['greedyNamed']) && $this->options['greedyNamed'] === true;
foreach ((array)$this->options['named'] as $key => $val) {
if (is_numeric($key)) {
$rules[$val] = true;

View file

@ -395,7 +395,7 @@ class Router {
self::$_namedConfig['rules'][$key] = $val;
}
}
self::$_namedConfig['greedy'] = $options['greedy'];
self::$_namedConfig['greedyNamed'] = $options['greedy'];
return self::$_namedConfig;
}

View file

@ -514,14 +514,15 @@ class CakeRouteTestCase extends CakeTestCase {
'named' => array(
'wibble',
'fish' => array('action' => 'index'),
'fizz' => array('controller' => 'comments')
'fizz' => array('controller' => 'comments'),
'pattern' => 'val-[\d]+'
)
));
$result = $route->parse('/posts/display/wibble:spin/fish:trout/fizz:buzz');
$result = $route->parse('/posts/display/wibble:spin/fish:trout/fizz:buzz/unknown:value');
$expected = array(
'controller' => 'posts',
'action' => 'display',
'pass' => array('fish:trout', 'fizz:buzz'),
'pass' => array('fish:trout', 'fizz:buzz', 'unknown:value'),
'named' => array(
'wibble' => 'spin'
)
@ -552,6 +553,49 @@ class CakeRouteTestCase extends CakeTestCase {
)
);
$this->assertEquals($expected, $result, 'All params should be parsed as conditions were met.');
$result = $route->parse('/comments/index/pattern:val--');
$expected = array(
'controller' => 'comments',
'action' => 'index',
'pass' => array('pattern:val--'),
'named' => array()
);
$this->assertEquals($expected, $result, 'Named parameter pattern unmet.');
$result = $route->parse('/comments/index/pattern:val-2');
$expected = array(
'controller' => 'comments',
'action' => 'index',
'pass' => array(),
'named' => array('pattern' => 'val-2')
);
$this->assertEquals($expected, $result, 'Named parameter pattern met.');
}
/**
* test that greedyNamed ignores rules.
*
* @return void
*/
function testParseGreedyNamed() {
$route = new CakeRoute('/:controller/:action/*', array(), array(
'named' => array(
'fizz' => array('controller' => 'comments'),
'pattern' => 'val-[\d]+',
),
'greedyNamed' => true
));
$result = $route->parse('/posts/display/wibble:spin/fizz:buzz/pattern:ignored');
$expected = array(
'controller' => 'posts',
'action' => 'display',
'pass' => array('fizz:buzz', 'pattern:ignored'),
'named' => array(
'wibble' => 'spin',
)
);
$this->assertEquals($expected, $result, 'Greedy named grabs everything, rules are followed');
}
/**