Skip to content

Commit

Permalink
Expanding tests on Router and RouterRoute.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Nov 27, 2009
1 parent feb0dec commit fd98bc5
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 10 deletions.
8 changes: 4 additions & 4 deletions cake/libs/router.php
Expand Up @@ -1295,13 +1295,13 @@ function _writeRoute($route, $default, $params) {
}

$parsed = str_replace($search, $replacements, $parsed);
if (preg_match('#\/\*$#', $route, $m)) {
$parsed = preg_replace('#/\\\\\*$#', '(?:/(.*))?', $parsed);
if (preg_match('#\/\*#', $route, $m)) {
$parsed = preg_replace('#/\\\\\*#', '(?:/(.*))?', $parsed);
}

$this->_compiledRoute = '#^' . $parsed . '[/]*$#';
$this->keys = $names;
/*
/*
$elements = explode('/', $route);
foreach ($elements as $element) {
Expand Down Expand Up @@ -1358,7 +1358,7 @@ function _writeRoute($route, $default, $params) {
}
$this->_compiledRoute = '#^' . join('', $parsed) . '[\/]*$#';
$this->keys = $names;
*/
//*/
}

/**
Expand Down
63 changes: 57 additions & 6 deletions cake/tests/cases/libs/router.test.php
Expand Up @@ -858,7 +858,7 @@ function testUrlParsing() {
$this->assertEqual($result, $expected);

$this->router->routes = array();
Router::connect('/posts/:month/:day/:year//*', array('controller' => 'posts', 'action' => 'view'), array('year' => $Year, 'month' => $Month, 'day' => $Day));
Router::connect('/posts/:month/:day/:year/*', array('controller' => 'posts', 'action' => 'view'), array('year' => $Year, 'month' => $Month, 'day' => $Day));
$result = Router::parse('/posts/08/01/2007/title-of-post-here');
$expected = array('month' => '08', 'day' => '01', 'year' => '2007', 'controller' => 'posts', 'action' => 'view', 'plugin' =>'', 'pass' => array('0' => 'title-of-post-here'), 'named' => array());
$this->assertEqual($result, $expected);
Expand Down Expand Up @@ -1034,11 +1034,10 @@ function testRouteSymmetry() {
$this->assertEqual($result, $expected);

$result = Router::parse('/page/this_is_the_slug');
$expected = array( 'pass' => array(), 'named' => array(), 'plugin' => null, 'controller' => 'pages', 'action' => 'view', 'slug' => 'this_is_the_slug', 'extra' => null);
$expected = array('pass' => array(), 'named' => array(), 'plugin' => null, 'controller' => 'pages', 'action' => 'view', 'slug' => 'this_is_the_slug', 'extra' => null);
$this->assertEqual($result, $expected);

Router::reload();

Router::connect(
"/:extra/page/:slug/*",
array('controller' => 'pages', 'action' => 'view', 'extra' => null),
Expand Down Expand Up @@ -1660,7 +1659,7 @@ function testPagesUrlParsing() {
Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));

$result = Router::parse('/');
$expected = array('pass'=>array('home'), 'named' => array(), 'plugin' => null, 'controller' => 'pages', 'action' => 'display');
$expected = array('pass'=> array('home'), 'named' => array(), 'plugin' => null, 'controller' => 'pages', 'action' => 'display');
$this->assertEqual($result, $expected);

$result = Router::parse('/pages/home/');
Expand Down Expand Up @@ -1980,7 +1979,8 @@ function testRouterConnectDefaults() {

}
}
SimpleTest::ignore('RouterTest');

// SimpleTest::ignore('RouterTest');
/**
* Test case for RouterRoute
*
Expand Down Expand Up @@ -2067,7 +2067,11 @@ function testBasicRouteCompiling() {
function testRouteCompilingWithParamPatterns() {
extract(Router::getNamedExpressions());

$route = new RouterRoute('/:controller/:action/:id', array('controller' => 'testing4', 'id' => null), array('id' => $ID));
$route = new RouterRoute(
'/:controller/:action/:id',
array('controller' => 'testing4', 'id' => null),
array('id' => $ID)
);
$result = $route->compile();
$this->assertPattern($result, '/posts/edit/1');
$this->assertPattern($result, '/posts/view/518098');
Expand Down Expand Up @@ -2126,6 +2130,53 @@ function testRouteCompilingWithParamPatterns() {
$this->assertEqual($route->keys, array('url_title', 'id'));
}

/**
* test more complex route compiling & parsing with mid route greedy stars
* and //
*
* @return void
*/
function testComplexRouteCompilingAndParsing() {
extract(Router::getNamedExpressions());

$route =& new RouterRoute(
'/pages/*/:event',
array('controller' => 'pages', 'action' => 'display'),
array('event' => '[a-z0-9_-]+')
);
$result = $route->compile();
$this->assertPattern($result, '/pages/view/today');
$this->assertPattern($result, '/pages/view/today/tomorrow');
$this->assertNoPattern($result, '/pages/view/tomorrow/something:else');

$route =& new RouterRoute(
'/posts/:month/:day/:year/*',
array('controller' => 'posts', 'action' => 'view'), array('year' => $Year, 'month' => $Month, 'day' => $Day)
);
$result = $route->compile();
$this->assertPattern($result, '/posts/08/01/2007/title-of-post');
$result = $route->parse('/posts/08/01/2007/title-of-post');
$this->assertEqual(count($result), 5);

$route =& new RouterRoute(
"/:extra/page/:slug/*",
array('controller' => 'pages', 'action' => 'view', 'extra' => null),
array("extra" => '[a-z1-9_]*', "slug" => '[a-z1-9_]+', "action" => 'view')
);
$result = $route->compile();
$this->assertEqual($route->keys, array('extra', 'slug'));
$this->assertEqual($route->params, array('extra' => '[a-z1-9_]*', 'slug' => '[a-z1-9_]+', 'action' => 'view'));
$expected = array(
'controller' => 'pages',
'action' => 'view',
'extra' => null,
'plugin' => null
);
$this->assertEqual($route->defaults, $expected);
$this->assertPattern($result, '/some_extra/page/this_is_the_slug');
$this->assertNoPattern($result, '/page/this_is_the_slug');
}

/**
* test that routes match their pattern.
*
Expand Down

0 comments on commit fd98bc5

Please sign in to comment.