Skip to content

Commit

Permalink
Making Router throw exceptions when invalid route classes are used fo…
Browse files Browse the repository at this point in the history
…r routes.
  • Loading branch information
markstory committed Apr 24, 2010
1 parent e896901 commit 797fa00
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
8 changes: 3 additions & 5 deletions cake/libs/router.php
Expand Up @@ -269,11 +269,9 @@ public static function connect($route, $defaults = array(), $options = array())
$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;
Expand Down
14 changes: 14 additions & 0 deletions cake/tests/cases/libs/router.test.php
Expand Up @@ -1910,6 +1910,20 @@ function testUsingCustomRouteClass() {
$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.
*
Expand Down

0 comments on commit 797fa00

Please sign in to comment.