Skip to content

Commit

Permalink
Bringing Router coverage up to 94.45%, minor router refactorings
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@8258 3807eeeb-6ff5-0310-8944-8be069107fe0
  • Loading branch information
DarkAngelBGE committed Jul 28, 2009
1 parent f53181b commit 9da7b6e
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 10 deletions.
19 changes: 9 additions & 10 deletions cake/libs/router.php
Expand Up @@ -311,9 +311,9 @@ function mapResources($controller, $options = array()) {
/**
* Builds a route regular expression
*
* @param string $route An empty string, or a route string "/"
* @param array $default NULL or an array describing the default route
* @param array $params An array matching the named elements in the route to regular expressions which that element should match.
* @param string $route An empty string, or a route string "/"
* @param array $default NULL or an array describing the default route
* @param array $params An array matching the named elements in the route to regular expressions which that element should match.
* @return array
* @see routes
* @access public
Expand Down Expand Up @@ -533,10 +533,7 @@ function __matchRoute($route, $url) {
function compile($i) {
$route = $this->routes[$i];

if (!list($pattern, $names) = $this->writeRoute($route[0], $route[1], $route[2])) {
unset($this->routes[$i]);
return array();
}
list($pattern, $names) = $this->writeRoute($route[0], $route[1], $route[2]);
$this->routes[$i] = array(
$route[0], $pattern, $names,
array_merge(array('plugin' => null, 'controller' => null), (array)$route[1]),
Expand Down Expand Up @@ -1271,9 +1268,9 @@ function stripEscape($param) {
return $param;
}

$return = preg_replace('/^(?:[\\t ]*(?:-!)+)/', '', $param);
return $return;
return preg_replace('/^(?:[\\t ]*(?:-!)+)/', '', $param);
}

foreach ($param as $key => $value) {
if (is_string($value)) {
$return[$key] = preg_replace('/^(?:[\\t ]*(?:-!)+)/', '', $value);
Expand Down Expand Up @@ -1346,7 +1343,9 @@ function getArgs($args, $options = array()) {
continue;
}
$param = $_this->stripEscape($param);
if ((!isset($options['named']) || !empty($options['named'])) && strpos($param, $_this->named['separator']) !== false) {

$separatorIsPresent = strpos($param, $_this->named['separator']) !== false;
if ((!isset($options['named']) || !empty($options['named'])) && $separatorIsPresent) {
list($key, $val) = explode($_this->named['separator'], $param, 2);
$hasRule = isset($rules[$key]);
$passIt = (!$hasRule && !$greedy) || ($hasRule && !Router::matchNamed($key, $val, $rules[$key], $context));
Expand Down
69 changes: 69 additions & 0 deletions cake/tests/cases/libs/router.test.php
Expand Up @@ -1670,5 +1670,74 @@ function testStripPlugin() {
$this->assertEqual(Router::stripPlugin($url), $url);
$this->assertEqual(Router::stripPlugin($url, null), $url);
}
/**
* testCurentRoute
*
* This test needs some improvement and actual requestAction() usage
*
* @return void
* @access public
*/
function testCurentRoute() {
$url = array('controller' => 'pages', 'action' => 'display', 'government');
Router::connect('/government', $url);
Router::parse('/government');
$route = Router::currentRoute();
$this->assertEqual(array_merge($url, array('plugin' => false)), $route[3]);
}
/**
* testRequestRoute
*
* @return void
* @access public
*/
function testRequestRoute() {
$url = array('controller' => 'products', 'action' => 'display', 5);
Router::connect('/government', $url);
Router::parse('/government');
$route = Router::requestRoute();
$this->assertEqual(array_merge($url, array('plugin' => false)), $route[3]);

// test that the first route is matched
$newUrl = array('controller' => 'products', 'action' => 'display', 6);
Router::connect('/government', $url);
Router::parse('/government');
$route = Router::requestRoute();
$this->assertEqual(array_merge($url, array('plugin' => false)), $route[3]);

// test that an unmatched route does not change the current route
$newUrl = array('controller' => 'products', 'action' => 'display', 6);
Router::connect('/actor', $url);
Router::parse('/government');
$route = Router::requestRoute();
$this->assertEqual(array_merge($url, array('plugin' => false)), $route[3]);
}
/**
* testGetParams
*
* @return void
* @access public
*/
function testGetParams() {
$paths = array('base' => '/', 'here' => '/products/display/5', 'webroot' => '/webroot');
$params = array('param1' => '1', 'param2' => '2');
Router::setRequestInfo(array($params, $paths));
$expected = array(
'plugin' => false, 'controller' => false, 'action' => false,
'param1' => '1', 'param2' => '2'
);
$this->assertEqual(Router::getparams(), $expected);
$this->assertEqual(Router::getparam('controller'), false);
$this->assertEqual(Router::getparam('param1'), '1');
$this->assertEqual(Router::getparam('param2'), '2');

Router::reload();

$params = array('controller' => 'pages', 'action' => 'display');
Router::setRequestInfo(array($params, $paths));
$expected = array('plugin' => false, 'controller' => 'pages', 'action' => 'display');
$this->assertEqual(Router::getparams(), $expected);
$this->assertEqual(Router::getparams(true), $expected);
}
}
?>

0 comments on commit 9da7b6e

Please sign in to comment.