Skip to content
Browse files

More tests and more passing tests for Route compilation.

Basic route compiling complete.
  • Loading branch information...
1 parent 6661c2f commit c4ceeabcf5cc90728b6e9ce1515327a563eb02f0 @markstory markstory committed
Showing with 53 additions and 17 deletions.
  1. +7 −4 cake/libs/router.php
  2. +46 −13 cake/tests/cases/libs/router.test.php
View
11 cake/libs/router.php
@@ -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);
View
59 cake/tests/cases/libs/router.test.php
@@ -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
@@ -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'));
@@ -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'));
}
@@ -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(
@@ -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'));
}

0 comments on commit c4ceeab

Please sign in to comment.
Something went wrong with that request. Please try again.