Skip to content

Commit

Permalink
Most of RouterRoute::match() working now. Failing tests for params wi…
Browse files Browse the repository at this point in the history
…th patterns included.
  • Loading branch information
markstory committed Nov 30, 2009
1 parent de0b90d commit 5999264
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 25 deletions.
42 changes: 21 additions & 21 deletions cake/libs/router.php
Expand Up @@ -1354,33 +1354,23 @@ function match($url) {
if (!$this->compiled()) {
$this->compile();
}
/* $url += array('controller' => null, 'plugin' => null);
$defaults = $this->defaults;
if (isset($defaults['prefix'])) {
$prefix = $defaults['prefix'];
unset($defaults['prefix']);
}
$diff = Set::diff($url, $defaults);
$url += array('controller' => null, 'plugin' => null);
$url += $this->defaults;

//check that all the key names are in the url
$keyNames = array_flip($this->keys);
if (array_keys(array_intersect_key($url, $keyNames)) != $this->keys) {
if (array_intersect_key($keyNames, $url) != $keyNames) {
return false;
}
$diff = Set::diff($url, $this->defaults);

//if the default keys aren't the same its not a match.
if (array_intersect_key($url, $this->defaults) != $this->defaults) {
//if a not a greedy route, no extra params are allowed.
if (!$this->_greedy && array_keys($diff) != $this->keys) {
return false;
}

//if this route is not greedy, make sure there are no more params
if (!$this->_greedy) {
if (array_diff_key($url, array_merge($this->defaults, $keyNames)) !== array()) {
return false;
}
//if the difference between the url and defaults contains keys from defaults its not a match
if (array_intersect_key($diff, $this->defaults) !== array()) {
return false;
}

//check that required passed parameters are the same.
Expand All @@ -1391,6 +1381,9 @@ function match($url) {
}
$i++;
}
$passedArgsAndParams = array_diff_key($diff, $this->defaults, $keyNames);
list($named, $params) = Router::getNamedElements($passedArgsAndParams, $url['controller'], $url['action']);


//remove any pass params, they have numeric indexes
$pass = array();
Expand All @@ -1400,10 +1393,11 @@ function match($url) {
unset($url[$i]);
$i++;
}
return $this->_writeUrl(array_merge($url, compact('pass', 'named', 'prefix')));


//*/
/*
$defaults = $this->defaults;
if (isset($defaults['prefix'])) {
Expand Down Expand Up @@ -1432,7 +1426,12 @@ function match($url) {
unset($params[$key]);
}
}
debug($params);
list($named, $params) = Router::getNamedElements($params);
debug($named);
debug($params);
debug($this);
echo '-----------<br>';
if (!strpos($this->template, '*') && (!empty($pass) || !empty($named))) {
return false;
Expand Down Expand Up @@ -1548,6 +1547,7 @@ function _writeUrl($params) {
if (strpos($this->template, '*')) {
$out = str_replace('*', $params['pass'], $out);
}
$out = str_replace('//', '/', $out);
return $out;
}
}
Expand Down
36 changes: 32 additions & 4 deletions cake/tests/cases/libs/router.test.php
Expand Up @@ -1945,11 +1945,11 @@ function testRouterConnectDefaults() {
$result = Router::parse('/posts/edit/5');
$this->assertFalse(isset($result['controller']));
$this->assertFalse(isset($result['action']));

}
}

//SimpleTest::ignore('RouterTest');
SimpleTest::ignore('RouterTest');
// SimpleTest::ignore('RouterRouteTestCase');
/**
* Test case for RouterRoute
*
Expand Down Expand Up @@ -2149,10 +2149,16 @@ function testComplexRouteCompilingAndParsing() {
* @return void
**/
function testMatchBasic() {
$route = new RouterRoute('/:controller/:action/:id');
$result = $route->match(array('controller' => 'posts', 'action' => 'view'));
$route = new RouterRoute('/:controller/:action/:id', array('plugin' => null));
$result = $route->match(array('controller' => 'posts', 'action' => 'view', 'plugin' => null));
$this->assertFalse($result);

$result = $route->match(array('plugin' => null, 'controller' => 'posts', 'action' => 'view', 0));
$this->assertFalse($result);

$result = $route->match(array('plugin' => null, 'controller' => 'posts', 'action' => 'view', 'id' => 1));
$this->assertEqual($result, '/posts/view/1');

$route =& new RouterRoute('/', array('controller' => 'pages', 'action' => 'display', 'home'));
$result = $route->match(array('controller' => 'pages', 'action' => 'display', 'home'));
$this->assertEqual($result, '/');
Expand All @@ -2173,6 +2179,9 @@ function testMatchBasic() {
$result = $route->match(array('controller' => 'posts', 'action' => 'view'));
$this->assertEqual($result, '/blog/view');

$result = $route->match(array('controller' => 'nodes', 'action' => 'view'));
$this->assertFalse($result);

$result = $route->match(array('controller' => 'posts', 'action' => 'view', 1));
$this->assertFalse($result);

Expand Down Expand Up @@ -2204,6 +2213,25 @@ function testMatchBasic() {
$this->assertEqual($result, $expected);
}

/**
* test match() with greedy routes, named parameters and passed args.
*
* @return void
*/
function testMatchWithNamedParameters() {
Router::connectNamed(true);

$route = new RouterRoute('/:controller/:action/*', array('plugin' => null));
$result = $route->match(array('controller' => 'posts', 'action' => 'index', 'plugin' => null, 'page' => 1));
$this->assertEqual($result, '/posts/index/page:1');

$result = $route->match(array('controller' => 'posts', 'action' => 'view', 'plugin' => null, 5));
$this->assertEqual($result, '/posts/view/5');

$result = $route->match(array('controller' => 'posts', 'action' => 'view', 'plugin' => null, 5, 'page' => 1, 'limit' => 20, 'order' => 'title'));
$this->assertEqual($result, '/posts/view/5/page:1/limit:20/order:title');
}

/**
* test that match with patterns works.
*
Expand Down

0 comments on commit 5999264

Please sign in to comment.