Skip to content

Commit

Permalink
Adding tests for named parameter rules on the route level.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Mar 3, 2011
1 parent 636188e commit 3eef281
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
2 changes: 2 additions & 0 deletions cake/libs/route/cake_route.php
Expand Up @@ -242,6 +242,8 @@ public function parse($url) {
* The local and global configuration for named parameters will be used.
*
* @param string $args A string with the passed & named params. eg. /1/page:2
* @param string $controller The current actions controller name, used to match named parameter rules
* @param string $action The current action name, used to match named parameter rules.
* @return array Array of ($pass, $named)
*/
public function parseArgs($args, $controller = null, $action = null) {
Expand Down
54 changes: 52 additions & 2 deletions cake/tests/cases/libs/route/cake_route.test.php
Expand Up @@ -480,7 +480,7 @@ function testPatternOnAction() {
*
* @return void
*/
function testPassedArgumentParsing() {
function testParsePassedArgument() {
$route = new CakeRoute('/:controller/:action/*');
$result = $route->parse('/posts/edit/1/2/0');
$expected = array(
Expand All @@ -504,12 +504,62 @@ function testPassedArgumentParsing() {
$this->assertEquals($expected, $result);
}

/**
* test that only named parameter rules are followed.
*
* @return void
*/
function testParseNamedParametersWithRules() {
$route = new CakeRoute('/:controller/:action/*', array(), array(
'named' => array(
'wibble',
'fish' => array('action' => 'index'),
'fizz' => array('controller' => 'comments')
)
));
$result = $route->parse('/posts/display/wibble:spin/fish:trout/fizz:buzz');
$expected = array(
'controller' => 'posts',
'action' => 'display',
'pass' => array('fish:trout', 'fizz:buzz'),
'named' => array(
'wibble' => 'spin'
)
);
$this->assertEquals($expected, $result, 'Fish should not be parsed, as action != index');

$result = $route->parse('/posts/index/wibble:spin/fish:trout/fizz:buzz');
$expected = array(
'controller' => 'posts',
'action' => 'index',
'pass' => array('fizz:buzz'),
'named' => array(
'wibble' => 'spin',
'fish' => 'trout'
)
);
$this->assertEquals($expected, $result, 'Fish should be parsed, as action == index');

$result = $route->parse('/comments/index/wibble:spin/fish:trout/fizz:buzz');
$expected = array(
'controller' => 'comments',
'action' => 'index',
'pass' => array(),
'named' => array(
'wibble' => 'spin',
'fish' => 'trout',
'fizz' => 'buzz'
)
);
$this->assertEquals($expected, $result, 'All params should be parsed as conditions were met.');
}

/**
* test that parsing array format named parameters works
*
* @return void
*/
function testArrayNamedParameters() {
function testParseArrayNamedParameters() {
$route = new CakeRoute('/:controller/:action/*');
$result = $route->parse('/tests/action/var[]:val1/var[]:val2');
$expected = array(
Expand Down

0 comments on commit 3eef281

Please sign in to comment.