From f5b62eb84c5f854c4d0dda337b950dbffc482685 Mon Sep 17 00:00:00 2001 From: Tigran Gabrielyan Date: Fri, 13 Jan 2012 12:27:48 -0800 Subject: [PATCH] Fixed typo in DocBlock Router::defaultRouteClass will return the current routeClass if null is passed Router::connect and Router::defaultRouteClass both validate the routeClass now using Router::_validateRouteClass --- lib/Cake/Routing/Router.php | 42 +++++++++++++++------- lib/Cake/Test/Case/Routing/RouterTest.php | 44 +++++++++++++++++++++++ 2 files changed, 73 insertions(+), 13 deletions(-) diff --git a/lib/Cake/Routing/Router.php b/lib/Cake/Routing/Router.php index 8ae4fe25d..017b60e91 100644 --- a/lib/Cake/Routing/Router.php +++ b/lib/Cake/Routing/Router.php @@ -160,14 +160,33 @@ class Router { protected static $_routeClass = 'CakeRoute'; /** - * Set the default rotue class to use + * Set the default route class to use or return the current one * - * @param sting $routeClass to set as default - * @return void + * @param string $routeClass to set as default + * @return mixed void|string + * @throws RouterException */ - public static function defaultRouteClass($routeClass) { - self::$_routeClass = $routeClass; - } + public static function defaultRouteClass($routeClass = null) { + if (is_null($routeClass)) { + return self::$_routeClass; + } + + self::$_routeClass = self::_validateRouteClass($routeClass); + } + +/** + * Validates that the passed route class exists and is a subclass of CakeRoute + * + * @param $routeClass + * @return string + * @throws RouterException + */ + protected static function _validateRouteClass($routeClass) { + if (!class_exists($routeClass) || !is_subclass_of($routeClass, 'CakeRoute')) { + throw new RouterException(__d('cake_dev', 'Route classes must extend CakeRoute')); + } + return $routeClass; + } /** * Sets the Routing prefixes. @@ -278,14 +297,11 @@ class Router { } $routeClass = self::$_routeClass; if (isset($options['routeClass'])) { - $routeClass = $options['routeClass']; - if (!is_subclass_of($routeClass, 'CakeRoute')) { - throw new RouterException(__d('cake_dev', 'Route classes must extend CakeRoute')); - } + $routeClass = self::_validateRouteClass($options['routeClass']); unset($options['routeClass']); - if ($routeClass == 'RedirectRoute' && isset($defaults['redirect'])) { - $defaults = $defaults['redirect']; - } + } + if ($routeClass == 'RedirectRoute' && isset($defaults['redirect'])) { + $defaults = $defaults['redirect']; } self::$routes[] = new $routeClass($route, $defaults, $options); return self::$routes; diff --git a/lib/Cake/Test/Case/Routing/RouterTest.php b/lib/Cake/Test/Case/Routing/RouterTest.php index bafb37802..3abcb6ec0 100644 --- a/lib/Cake/Test/Case/Routing/RouterTest.php +++ b/lib/Cake/Test/Case/Routing/RouterTest.php @@ -2519,4 +2519,48 @@ class RouterTest extends CakeTestCase { $this->assertInstanceOf('TestDefaultRouteClass', $result[0]); } +/** + * Test getting the default route class + * + * @return void + */ + public function testDefaultRouteClassGetter() { + $routeClass = 'TestDefaultRouteClass'; + Router::defaultRouteClass($routeClass); + + $this->assertEqual($routeClass, Router::defaultRouteClass()); + $this->assertEqual($routeClass, Router::defaultRouteClass(null)); + } + +/** + * Test that route classes must extend CakeRoute + * + * @expectedException RouterException + * @return void + */ + public function testDefaultRouteException() { + Router::defaultRouteClass(''); + Router::connect('/:controller', array()); + } + +/** + * Test that route classes must extend CakeRoute + * + * @expectedException RouterException + * @return void + */ + public function testSettingInvalidDefaultRouteException() { + Router::defaultRouteClass('Object'); + } + +/** + * Test that class must exist + * + * @expectedException RouterException + * @return void + */ + public function testSettingNonExistentDefaultRouteException() { + Router::defaultRouteClass('NonExistentClass'); + } + }