Skip to content

Commit

Permalink
Updating router tests to use/expect greedyNamed.
Browse files Browse the repository at this point in the history
Removing code that has been moved to CakeRoute or is no longer needed.
  • Loading branch information
markstory committed Mar 3, 2011
1 parent 217938a commit 9cf9408
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 153 deletions.
148 changes: 0 additions & 148 deletions cake/libs/router.php
Expand Up @@ -499,36 +499,7 @@ public static function parse($url) {

if (($r = $route->parse($url)) !== false) {
self::$_currentRoute[] =& $route;

$params = $route->options;
$argOptions = array();

if (array_key_exists('named', $params)) {
$argOptions['named'] = $params['named'];
unset($params['named']);
}
if (array_key_exists('greedy', $params)) {
$argOptions['greedy'] = $params['greedy'];
unset($params['greedy']);
}
$out = $r;

if (isset($out['_args_'])) {
$argOptions['context'] = array('action' => $out['action'], 'controller' => $out['controller']);
$parsedArgs = self::getArgs($out['_args_'], $argOptions);
$out['pass'] = array_merge($out['pass'], $parsedArgs['pass']);
$out['named'] = $parsedArgs['named'];
unset($out['_args_']);
}

if (isset($params['pass'])) {
$j = count($params['pass']);
while($j--) {
if (isset($out[$params['pass'][$j]])) {
array_unshift($out['pass'], $out[$params['pass'][$j]]);
}
}
}
break;
}
}
Expand Down Expand Up @@ -997,61 +968,6 @@ protected static function _handleNoRoute($url) {
return $output;
}

/**
* Takes an array of URL parameters and separates the ones that can be used as named arguments
*
* @param array $params Associative array of URL parameters.
* @param string $controller Name of controller being routed. Used in scoping.
* @param string $action Name of action being routed. Used in scoping.
* @return array
*/
public static function getNamedElements($params, $controller = null, $action = null) {
$named = array();

foreach ($params as $param => $val) {
if (isset(self::$_namedConfig['rules'][$param])) {
$rule = self::$_namedConfig['rules'][$param];
if (Router::matchNamed($param, $val, $rule, compact('controller', 'action'))) {
$named[substr($param, 1)] = $val;
unset($params[$param]);
}
}
}
return array($named, $params);
}

/**
* Return true if a given named $param's $val matches a given $rule depending on $context. Currently implemented
* rule types are controller, action and match that can be combined with each other.
*
* @param string $param The name of the named parameter
* @param string $val The value of the named parameter
* @param array $rule The rule(s) to apply, can also be a match string
* @param string $context An array with additional context information (controller / action)
* @return boolean
*/
public static function matchNamed($param, $val, $rule, $context = array()) {
if ($rule === true || $rule === false) {
return $rule;
}
if (is_string($rule)) {
$rule = array('match' => $rule);
}
if (!is_array($rule)) {
return false;
}

$controllerMatches = !isset($rule['controller'], $context['controller']) || in_array($context['controller'], (array)$rule['controller']);
if (!$controllerMatches) {
return false;
}
$actionMatches = !isset($rule['action'], $context['action']) || in_array($context['action'], (array)$rule['action']);
if (!$actionMatches) {
return false;
}
return (!isset($rule['match']) || preg_match('/' . $rule['match'] . '/', $val));
}

/**
* Generates a well-formed querystring from $q
*
Expand Down Expand Up @@ -1218,70 +1134,6 @@ public static function extensions() {
return self::$_validExtensions;
}

/**
* Takes a passed params and converts it to args
*
* @param array $params
* @return array Array containing passed and named parameters
*/
public static function getArgs($args, $options = array()) {
$pass = $named = array();
$args = explode('/', $args);

$greedy = isset($options['greedy']) ? $options['greedy'] : self::$_namedConfig['greedy'];
$context = array();
if (isset($options['context'])) {
$context = $options['context'];
}
$rules = self::$_namedConfig['rules'];
if (isset($options['named'])) {
$greedy = isset($options['greedy']) && $options['greedy'] === true;
foreach ((array)$options['named'] as $key => $val) {
if (is_numeric($key)) {
$rules[$val] = true;
continue;
}
$rules[$key] = $val;
}
}

foreach ($args as $param) {
if (empty($param) && $param !== '0' && $param !== 0) {
continue;
}

$separatorIsPresent = strpos($param, self::$_namedConfig['separator']) !== false;
if ((!isset($options['named']) || !empty($options['named'])) && $separatorIsPresent) {
list($key, $val) = explode(self::$_namedConfig['separator'], $param, 2);
$hasRule = isset($rules[$key]);
$passIt = (!$hasRule && !$greedy) || ($hasRule && !self::matchNamed($key, $val, $rules[$key], $context));
if ($passIt) {
$pass[] = $param;
} else {
if (preg_match_all('/\[([A-Za-z0-9_-]+)?\]/', $key, $matches, PREG_SET_ORDER)) {
$matches = array_reverse($matches);
$parts = explode('[', $key);
$key = array_shift($parts);
$arr = $val;
foreach ($matches as $match) {
if (empty($match[1])) {
$arr = array($arr);
} else {
$arr = array(
$match[1] => $arr
);
}
}
$val = $arr;
}
$named = array_merge_recursive($named, array($key => $val));
}
} else {
$pass[] = $param;
}
}
return compact('pass', 'named');
}
}

//Save the initial state
Expand Down
13 changes: 8 additions & 5 deletions cake/tests/cases/libs/router.test.php
Expand Up @@ -436,6 +436,7 @@ function testArrayNamedParameters() {
$expected = '/tests/index/namedParam[keyed]:is an array/namedParam[0]:test';
$this->assertEqual($result, $expected);

//@todo Delete from here down, tests are in CakeRoute now.
$result = Router::parse('/tests/action/var[]:val1/var[]:val2');
$expected = array(
'controller' => 'tests',
Expand Down Expand Up @@ -1017,7 +1018,7 @@ function testUrlParsing() {
$this->assertEqual($result, $expected);

Router::reload();
Router::connect('/posts/view/*', array('controller' => 'posts', 'action' => 'view'), array('named' => array('foo', 'answer'), 'greedy' => true));
Router::connect('/posts/view/*', array('controller' => 'posts', 'action' => 'view'), array('named' => array('foo', 'answer'), 'greedyNamed' => true));
$result = Router::parse('/posts/view/foo:bar/routing:fun/answer:42');
$expected = array('pass' => array(), 'named' => array('foo' => 'bar', 'routing' => 'fun', 'answer' => '42'), 'plugin' => null, 'controller' => 'posts', 'action' => 'view');
$this->assertEqual($result, $expected);
Expand Down Expand Up @@ -1328,7 +1329,7 @@ function testQuerystringGeneration() {
*/
function testConnectNamed() {
$named = Router::connectNamed(false, array('default' => true));
$this->assertFalse($named['greedy']);
$this->assertFalse($named['greedyNamed']);
$this->assertEqual(array_keys($named['rules']), $named['default']);

Router::reload();
Expand Down Expand Up @@ -1429,7 +1430,7 @@ function testNamedArgsUrlParsing() {
Router::reload();
$result = Router::connectNamed(false);
$this->assertEqual(array_keys($result['rules']), array());
$this->assertFalse($result['greedy']);
$this->assertFalse($result['greedyNamed']);
$result = Router::parse('/controller/action/param1:value1:1/param2:value2:3/param:value');
$expected = array('pass' => array('param1:value1:1', 'param2:value2:3', 'param:value'), 'named' => array(), 'controller' => 'controller', 'action' => 'action', 'plugin' => null);
$this->assertEqual($result, $expected);
Expand All @@ -1438,15 +1439,16 @@ function testNamedArgsUrlParsing() {
$result = Router::connectNamed(true);
$named = Router::namedConfig();
$this->assertEqual(array_keys($result['rules']), $named['default']);
$this->assertTrue($result['greedy']);
$this->assertTrue($result['greedyNamed']);
Router::reload();
Router::connectNamed(array('param1' => 'not-matching'));
$result = Router::parse('/controller/action/param1:value1:1/param2:value2:3/param:value');
$expected = array('pass' => array('param1:value1:1'), 'named' => array('param2' => 'value2:3', 'param' => 'value'), 'controller' => 'controller', 'action' => 'action', 'plugin' => null);
$this->assertEqual($result, $expected);

//@todo delete this test.
Router::reload();
Router::connect('/foo/:action/*', array('controller' => 'bar'), array('named' => array('param1' => array('action' => 'index')), 'greedy' => true));
Router::connect('/foo/:action/*', array('controller' => 'bar'), array('named' => array('param1' => array('action' => 'index')), 'greedyNamed' => true));
$result = Router::parse('/foo/index/param1:value1:1/param2:value2:3/param:value');
$expected = array('pass' => array(), 'named' => array('param1' => 'value1:1', 'param2' => 'value2:3', 'param' => 'value'), 'controller' => 'bar', 'action' => 'index', 'plugin' => null);
$this->assertEqual($result, $expected);
Expand All @@ -1473,6 +1475,7 @@ function testNamedArgsUrlParsing() {
$expected = array('pass' => array('param2:value2:3', 'param3:value'), 'named' => array('param1' => 'value1:1'), 'controller' => 'controller', 'action' => 'action', 'plugin' => null);
$this->assertEqual($result, $expected);

//@todo delete this test.
Router::reload();
Router::connect('/foo/*', array('controller' => 'bar', 'action' => 'fubar'), array('named' => array('param1' => 'value[\d]:[\d]')));
Router::connectNamed(array(), array('greedy' => false));
Expand Down

0 comments on commit 9cf9408

Please sign in to comment.