Skip to content

Commit

Permalink
More tests and more passing tests for Route compilation.
Browse files Browse the repository at this point in the history
Basic route compiling complete.
  • Loading branch information
markstory committed Nov 26, 2009
1 parent 6661c2f commit c4ceeab
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 17 deletions.
11 changes: 7 additions & 4 deletions cake/libs/router.php
Original file line number Diff line number Diff line change
Expand Up @@ -1286,14 +1286,17 @@ function _writeRoute($route, $default, $params) {
if ($name !== 'plugin' && array_key_exists($name, $default)) {
$option = '?';
}
$replacements[] = '(?:/(' . $params[$name] . ')' . $option . ')' . $option;
$replacements[] = '(?:(' . $params[$name] . ')' . $option . ')' . $option;
} else {
$replacements[] = '(?:/(^[\/]+))?';
$replacements[] = '(?:([^\/]+))?';
}
$names[] = $name;
}
$route = str_replace($namedElements[0], $replacements, $route);
$this->_compiledRoute = '#^' . $route . '[\/]*$#';
$parsed = str_replace($namedElements[0], $replacements, $route);
if (preg_match('#\/\*$#', $route)) {
$parsed = preg_replace('#\/*$#', '(?:/(.*))?', $parsed);
}
$this->_compiledRoute = '#^' . $parsed . '[\/]*$#';
$this->keys = $names;
/*
$elements = explode('/', $route);
Expand Down
59 changes: 46 additions & 13 deletions cake/tests/cases/libs/router.test.php
Original file line number Diff line number Diff line change
Expand Up @@ -2036,18 +2036,24 @@ function testBasicRouteCompiling() {

$route =& new RouterRoute('/:controller/:action', array('controller' => 'posts'));
$result = $route->compile();
$expected = '#^(?:/([^\/]+))?(?:/([^\/]+))?[\/]*$#';
$this->assertEqual($result, $expected);
// $expected = '#^(?:/([^\/]+))?(?:/([^\/]+))?[\/]*$#';
// $this->assertEqual($result, $expected);

$route =& new RouterRoute('/posts/foo:id', array('controller' => 'posts', 'action' => 'view'));
$result = $route->compile();
$expected = '#^/posts(?:/foo([^\/]+))?[\/]*$#';
$this->assertEqual($result, $expected);
$this->assertEqual($route->keys, array('id'));

$this->assertPattern($result, '/posts/foo:1');
$this->assertPattern($result, '/posts/foo:param');
$this->assertNoPattern($result, '/posts');
$this->assertNoPattern($result, '/posts/');

$this->assertEqual($route->keys, array('id'));

$route =& new RouterRoute('/:plugin/:controller/:action/*', array('plugin' => 'test_plugin', 'action' => 'index'));
$result = $route->compile();
$this->assertPattern($result, '/test_plugin/posts/index');
$this->assertPattern($result, '/test_plugin/posts/edit/5');
$this->assertPattern($result, '/test_plugin/posts/edit/5/name:value/nick:name');
}
/**
* test compiling routes with keys that have patterns
Expand All @@ -2059,8 +2065,13 @@ function testRouteCompilingWithParamPatterns() {

$route = new RouterRoute('/:controller/:action/:id', array('controller' => 'testing4', 'id' => null), array('id' => $ID));
$result = $route->compile();
$expected = '#^(?:/([^\/]+))?(?:/([^\/]+))?(?:/([0-9]+)?)?[\/]*$#';
$this->assertEqual($result, $expected);
$this->assertPattern($result, '/posts/edit/1');
$this->assertPattern($result, '/posts/view/518098');
$this->assertNoPattern($result, '/posts/edit/name-of-post');
$this->assertNoPattern($result, '/posts/edit/4/other:param');

// $expected = '#^(?:/([^\/]+))?(?:/([^\/]+))?(?:/([0-9]+)?)?[\/]*$#';
// $this->assertEqual($result, $expected);

$this->assertEqual($route->keys, array('controller', 'action', 'id'));

Expand All @@ -2070,15 +2081,25 @@ function testRouteCompilingWithParamPatterns() {
array('id' => $ID, 'lang' => '[a-z]{3}')
);
$result = $route->compile();
$expected = '#^(?:/([a-z]{3}))(?:/([^\/]+))?(?:/([^\/]+))?(?:/([0-9]+))[\/]*$#';
$this->assertEqual($result, $expected);
$this->assertPattern($result, '/eng/posts/edit/1');
$this->assertPattern($result, '/cze/articles/view/1');
$this->assertNoPattern($result, '/language/articles/view/2');
$this->assertNoPattern($result, '/eng/articles/view/name-of-article');

// $expected = '#^(?:/([a-z]{3}))(?:/([^\/]+))?(?:/([^\/]+))?(?:/([0-9]+))[\/]*$#';
// $this->assertEqual($result, $expected);
$this->assertEqual($route->keys, array('lang', 'controller', 'action', 'id'));

foreach (array(':', '@', ';', '$', '-') as $delim) {
$route =& new RouterRoute('/posts/:id'.$delim.':title');
$result = $route->compile();
$expected = '#^/posts(?:/([^\/]+))?(?:'.preg_quote($delim, '#').'([^\/]+))?[\/]*$#';
$this->assertEqual($result, $expected);
//$expected = '#^/posts(?:/([^\/]+))?(?:'.preg_quote($delim, '#').'([^\/]+))?[\/]*$#';
//$this->assertEqual($result, $expected);

$this->assertPattern($result, '/posts/1' . $delim . 'name-of-article');
$this->assertPattern($result, '/posts/13244' . $delim . 'name-of_Article[]');
$this->assertNoPattern($result, '/posts/11!!nameofarticle');
$this->assertNoPattern($result, '/posts/11');

$this->assertEqual($route->keys, array('id', 'title'));
}
Expand All @@ -2089,7 +2110,13 @@ function testRouteCompilingWithParamPatterns() {
array('id' => $ID, 'year' => $Year, 'title' => '[a-z-_]+')
);
$result = $route->compile();
$this->assertEqual($result, '#^/posts(?:/([0-9]+))(?:\\:([a-z-_]+))(?:/([12][0-9]{3}))[\/]*$#');
$this->assertPattern($result, '/posts/1:name-of-article/2009/');
$this->assertPattern($result, '/posts/13244:name-of-article/1999');
$this->assertNoPattern($result, '/posts/hey_now:nameofarticle');
$this->assertNoPattern($result, '/posts/:nameofarticle/2009');
$this->assertNoPattern($result, '/posts/:nameofarticle/01');

// $this->assertEqual($result, '#^/posts(?:/([0-9]+))(?:\\:([a-z-_]+))(?:/([12][0-9]{3}))[\/]*$#');
$this->assertEqual($route->keys, array('id', 'title', 'year'));

$route =& new RouterRoute(
Expand All @@ -2098,7 +2125,13 @@ function testRouteCompilingWithParamPatterns() {
array('pass' => array('id', 'url_title'), 'id' => $ID)
);
$result = $route->compile();
$this->assertEqual($result, '#^/posts(?:/([^\/]+))?(?:-\(uuid\:([0-9]+)\))[\/]*$#');
$this->assertPattern($result, '/posts/some_title_for_article-(uuid:12534)/');
$this->assertPattern($result, '/posts/some_title_for_article-(uuid:12534)');
$this->assertNoPattern($result, '/posts/');
$this->assertNoPattern($result, '/posts/nameofarticle');
$this->assertNoPattern($result, '/posts/nameofarticle-12347');

// $this->assertEqual($result, '#^/posts(?:/([^\/]+))?(?:-\(uuid\:([0-9]+)\))[\/]*$#');
$this->assertEqual($route->keys, array('url_title', 'id'));
}

Expand Down

0 comments on commit c4ceeab

Please sign in to comment.