Skip to content
Permalink
Browse files

Remove Router::defaultRouteClass()

This method doesn't work very will with the scopes, and is the kind of
thing that I don't think many people ever even used. Now that you can
easily inject instances, I don't think it is as important either.

Fix a number of tests that been failing as well.
  • Loading branch information...
markstory committed Jun 27, 2014
1 parent ac4f411 commit 42fb1a442e95b6fb01cb30f8c6fb2c337caf4094
Showing with 14 additions and 113 deletions.
  1. +0 −26 src/Routing/Router.php
  2. +6 −2 src/Routing/ScopedRouteCollection.php
  3. +8 −85 tests/TestCase/Routing/RouterTest.php
@@ -200,28 +200,6 @@ class Router {
*/
protected static $_urlFilters = [];
/**
* Default route class to use
*
* @var string
*/
protected static $_routeClass = 'Cake\Routing\Route\Route';
/**
* Set the default route class to use or return the current one
*
* @param string $routeClass to set as default
* @return mixed void|string
* @throws \Cake\Error\Exception
*/
public static function defaultRouteClass($routeClass = null) {
if ($routeClass === null) {
return static::$_routeClass;
}
static::$_routeClass = static::_validateRouteClass($routeClass);
}
/**
* Validates that the passed route class exists and is a subclass of Cake Route
*
@@ -352,10 +330,6 @@ public static function resourceMap($resourceMap = null) {
*/
public static function connect($route, $defaults = [], $options = []) {
static::$initialized = true;
if (empty($options['routeClass'])) {
$options['routeClass'] = static::$_routeClass;
}
Router::scope('/', function($routes) use ($route, $defaults, $options) {
$routes->connect($route, $defaults, $options);
});
@@ -372,17 +372,21 @@ public function connect($route, array $defaults = [], $options = []) {
* @param array $defaults Default parameters.
* @param array $options Additional options parameters.
* @return \Cake\Routing\Route\Route
* @throws \Cake\Error\Exception when route class or route object is invalid.
*/
protected function _makeRoute($route, $defaults, $options) {
if (is_string($route)) {
$routeClass = 'Cake\Routing\Route\Route';
if (isset($options['routeClass'])) {
$routeClass = App::className($options['routeClass'], 'Routing/Route');
unset($options['routeClass']);
}
if ($routeClass === false) {
throw new Error\Exception(sprintf('Cannot find route class %s', $options['routeClass']));
}
if ($routeClass === 'Cake\Routing\Route\RedirectRoute' && isset($defaults['redirect'])) {
$defaults = $defaults['redirect'];
}
unset($options['routeClass']);
$route = str_replace('//', '/', $this->_path . $route);
if (is_array($defaults)) {
@@ -556,7 +560,7 @@ public function match($url, $context) {
protected function _getNames($url) {
$name = false;
if (isset($url['_name'])) {
$name = $url['_name'];
return [$url['_name']];
}
$plugin = false;
if (isset($url['plugin'])) {
@@ -330,25 +330,6 @@ public function testMapResourcesConnectOptions() {
$this->assertEquals('^(bar)$', $route->options['foo']);
}
/**
* Test that RouterCollection::all() gets the list of all connected routes.
*
* @return void
*/
public function testRouteCollectionRoutes() {
$collection = new RouteCollection();
Router::setRouteCollection($collection);
Router::mapResources('Posts');
$routes = $collection->all();
$this->assertEquals(count($routes), 6);
$this->assertInstanceOf('Cake\Routing\Route\Route', $routes[0]);
$this->assertEquals($collection->get(0), $routes[0]);
$this->assertInstanceOf('Cake\Routing\Route\Route', $routes[5]);
$this->assertEquals($collection->get(5), $routes[5]);
}
/**
* Test mapResources with a plugin and prefix.
*
@@ -2625,10 +2606,15 @@ public function testUrlWithRequestAction() {
public function testUrlFullUrlReturnFromRoute() {
$url = 'http://example.com/posts/view/1';
$routes = $this->getMock('Cake\Routing\RouteCollection');
Router::setRouteCollection($routes);
$routes->expects($this->any())->method('match')
$route = $this->getMock(
'Cake\Routing\Route\Route',
['match'],
['/:controller/:action/*']
);
$route->expects($this->any())
->method('match')
->will($this->returnValue($url));
Router::connect($route);
$result = Router::url(array('controller' => 'posts', 'action' => 'view', 1));
$this->assertEquals($url, $result);
@@ -2731,69 +2717,6 @@ public function testResourceMap() {
Router::resourceMap($default);
}
/**
* Test setting the default route class
*
* @return void
*/
public function testDefaultRouteClass() {
$routes = $this->getMock('Cake\Routing\RouteCollection');
$this->getMock('Cake\Routing\Route\Route', [], array('/test'), 'TestDefaultRouteClass');
$routes->expects($this->once())
->method('add')
->with($this->isInstanceOf('\TestDefaultRouteClass'));
Router::setRouteCollection($routes);
Router::defaultRouteClass('\TestDefaultRouteClass');
Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
}
/**
* Test getting the default route class
*
* @return void
*/
public function testDefaultRouteClassGetter() {
$routeClass = '\TestDefaultRouteClass';
Router::defaultRouteClass($routeClass);
$this->assertEquals($routeClass, Router::defaultRouteClass());
$this->assertEquals($routeClass, Router::defaultRouteClass(null));
}
/**
* Test that route classes must extend Cake\Routing\Route\Route
*
* @expectedException \Cake\Error\Exception
* @return void
*/
public function testDefaultRouteException() {
Router::defaultRouteClass('');
Router::connect('/:controller', []);
}
/**
* Test that route classes must extend Cake\Routing\Route\Route
*
* @expectedException \Cake\Error\Exception
* @return void
*/
public function testSettingInvalidDefaultRouteException() {
Router::defaultRouteClass('Object');
}
/**
* Test that class must exist
*
* @expectedException \Cake\Error\Exception
* @return void
*/
public function testSettingNonExistentDefaultRouteException() {
Router::defaultRouteClass('NonExistentClass');
}
/**
* Test that the compatibility method for incoming urls works.
*

0 comments on commit 42fb1a4

Please sign in to comment.
You can’t perform that action at this time.