Making Router throw exceptions when invalid route classes are used for routes.

This commit is contained in:
Mark Story 2010-04-23 20:22:02 -04:00
parent e896901595
commit 797fa0009a
2 changed files with 17 additions and 5 deletions

View file

@ -269,11 +269,9 @@ class Router {
$routeClass = $options['routeClass'];
unset($options['routeClass']);
}
//TODO 2.0 refactor this to use a string class name, throw exception, and then construct.
$Route =& new $routeClass($route, $defaults, $options);
if ($routeClass !== 'CakeRoute' && !is_subclass_of($Route, 'CakeRoute')) {
trigger_error(__('Route classes must extend CakeRoute'), E_USER_WARNING);
return false;
$Route = new $routeClass($route, $defaults, $options);
if (!$Route instanceof CakeRoute) {
throw new Exception(__('Route classes must extend CakeRoute'));
}
$self->routes[] =& $Route;
return $self->routes;

View file

@ -1910,6 +1910,20 @@ class RouterTest extends CakeTestCase {
$this->assertEqual($result, $expected);
}
/**
* test that route classes must extend CakeRoute
*
* @return void
*/
function testCustomRouteException() {
try {
Router::connect('/:controller', array(), array('routeClass' => 'Object'));
$this->$this->assertFalse(true, 'No exception thrown when an invalid class was used.');
} catch (Exception $e) {
$this->assertTrue(true, 'Exception correctly thrown');
}
}
/**
* test reversing parameter arrays back into strings.
*