Implementing the ability to use custom Route classes when connecting routes. Tests added.

This commit is contained in:
mark_story 2009-12-03 20:50:13 -05:00
parent 218af686d7
commit 8c4d389760
2 changed files with 26 additions and 4 deletions

View file

@ -275,7 +275,12 @@ class Router {
$self->__prefixes = array_keys(array_flip($self->__prefixes)); $self->__prefixes = array_keys(array_flip($self->__prefixes));
} }
$defaults += array('action' => 'index', 'plugin' => null, 'controller' => null); $defaults += array('action' => 'index', 'plugin' => null, 'controller' => null);
$self->routes[] =& new RouterRoute($route, $defaults, $options); $routeClass = 'RouterRoute';
if (isset($options['routeClass'])) {
$routeClass = $options['routeClass'];
unset($options['routeClass']);
}
$self->routes[] =& new $routeClass($route, $defaults, $options);
return $self->routes; return $self->routes;
} }

View file

@ -1938,17 +1938,34 @@ class RouterTest extends CakeTestCase {
* *
* @return void * @return void
*/ */
function testRouterConnectDefaults() { function testDefaultsMethod() {
Router::defaults(false); Router::defaults(false);
Router::connect('/test/*', array('controller' => 'pages', 'action' => 'display', 2)); Router::connect('/test/*', array('controller' => 'pages', 'action' => 'display', 2));
$result = Router::parse('/posts/edit/5'); $result = Router::parse('/posts/edit/5');
$this->assertFalse(isset($result['controller'])); $this->assertFalse(isset($result['controller']));
$this->assertFalse(isset($result['action'])); $this->assertFalse(isset($result['action']));
} }
/**
* test using a custom route class for route connection
*
* @return void
*/
function testUsingCustomRouteClass() {
Mock::generate('RouterRoute', 'MockConnectedRoute');
$routes = Router::connect(
'/:slug',
array('controller' => 'posts', 'action' => 'view'),
array('routeClass' => 'MockConnectedRoute', 'slug' => '[a-z_-]+')
);
$this->assertTrue(is_a($routes[0], 'MockConnectedRoute'), 'Incorrect class used. %s');
$expected = array('controller' => 'posts', 'action' => 'view', 'slug' => 'test');
$routes[0]->setReturnValue('parse', $expected);
$result = Router::parse('/test');
$this->assertEqual($result, $expected);
}
} }
// SimpleTest::ignore('RouterTest');
// SimpleTest::ignore('RouterRouteTestCase');
/** /**
* Test case for RouterRoute * Test case for RouterRoute
* *