Skip to content
Permalink
Browse files

Start adding tests for ScopedRouteCollection

Make extensions an instance property instead of a global static that is
pulled in on each connected route. Having instance properties will allow
scopes to have different extensions if desired.
  • Loading branch information...
markstory committed Jun 21, 2014
1 parent 88c0791 commit 353324ae1a15e6e5b4b35b9c7773ac1d0a87c463
Showing with 183 additions and 8 deletions.
  1. +31 −8 src/Routing/ScopedRouteCollection.php
  2. +152 −0 tests/TestCase/Routing/ScopedRouteCollectionTest.php
@@ -54,19 +54,26 @@ class ScopedRouteCollection {
array('action' => 'edit', 'method' => 'POST', 'id' => true)
);
/**
* The extensions that should be set into the routes connected.
*
* @var array
*/
protected $_extensions;
/**
* The path prefix scope that this collection uses.
*
* @var string
*/
protected $path;
protected $_path;
/**
* The scope parameters if there are any.
*
* @var array
*/
protected $params;
protected $_params;
/**
* The routes connected to this collection.
@@ -88,9 +95,25 @@ class ScopedRouteCollection {
* @param string $path The path prefix the scope is for.
* @param array $params The scope's routing parameters.
*/
public function __construct($path, array $params = []) {
public function __construct($path, array $params = [], array $extensions = []) {
$this->_path = $path;
$this->_params = $params;
$this->_extensions = $extensions;
}
/**
* Get or set the extensions in this route collection.
*
* Setting extensions does not modify existing routes.
*
* @param null|array $extensions Either the extensions to use or null.
* @return array|void
*/
public function extensions($extensions = null) {
if ($extensions === null) {
return $this->_extensions;
}
$this->_extensions = $extensions;
}
/**
@@ -99,6 +122,10 @@ public function __construct($path, array $params = []) {
* @return string
*/
public function path() {
$routeKey = strpos($this->_path, ':');
if ($routeKey !== false) {
return substr($this->_path, 0, $routeKey);
}
return $this->_path;
}
@@ -308,7 +335,7 @@ public function connect($route, array $defaults = [], $options = []) {
}
if (empty($options['_ext'])) {
$options['_ext'] = Router::extensions();
$options['_ext'] = $this->_extensions;
}
// TODO don't hardcode
@@ -327,10 +354,6 @@ public function connect($route, array $defaults = [], $options = []) {
$defaults += $this->_params;
}
if (isset($options['_name']) && !empty($this->_named[$options['_name']])) {
throw new Error\Exception('Cannot have duplicated named routes.');
}
// Store the route and named index if possible.
$route = new $routeClass($route, $defaults, $options);
@@ -0,0 +1,152 @@
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 3.0.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Test\TestCase\Routing;
use Cake\Routing\Route\Route;
use Cake\Routing\Router;
use Cake\Routing\ScopedRouteCollection;
use Cake\TestSuite\TestCase;
/**
* ScopedRouteCollection test case
*/
class ScopedRouteCollectionTest extends TestCase {
/**
* Test path()
*
* @return void
*/
public function testPath() {
$routes = new ScopedRouteCollection('/some/path');
$this->assertEquals('/some/path', $routes->path());
$routes = new ScopedRouteCollection('/:book_id');
$this->assertEquals('/', $routes->path());
$routes = new ScopedRouteCollection('/path/:book_id');
$this->assertEquals('/path/', $routes->path());
$routes = new ScopedRouteCollection('/path/book:book_id');
$this->assertEquals('/path/book', $routes->path());
}
/**
* Test params()
*
* @return void
*/
public function testParams() {
$routes = new ScopedRouteCollection('/api', ['prefix' => 'api']);
$this->assertEquals(['prefix' => 'api'], $routes->params());
}
/**
* Test getting connected routes.
*
* @return void
*/
public function testRoutes() {
$routes = new ScopedRouteCollection('/l');
$routes->connect('/:controller', ['action' => 'index']);
$routes->connect('/:controller/:action/*');
$all = $routes->routes();
$this->assertCount(2, $all);
$this->assertInstanceOf('Cake\Routing\Route\Route', $all[0]);
$this->assertInstanceOf('Cake\Routing\Route\Route', $all[1]);
}
/**
* Test getting named routes.
*
* @return void
*/
public function testNamed() {
$routes = new ScopedRouteCollection('/l');
$routes->connect('/:controller', ['action' => 'index'], ['_name' => 'cntrl']);
$routes->connect('/:controller/:action/*');
$all = $routes->named();
$this->assertCount(1, $all);
$this->assertInstanceOf('Cake\Routing\Route\Route', $all['cntrl']);
$this->assertEquals('/l/:controller', $all['cntrl']->template);
}
/**
* Test getting named routes.
*
* @return void
*/
public function testGetNamed() {
$routes = new ScopedRouteCollection('/l');
$routes->connect('/:controller', ['action' => 'index'], ['_name' => 'cntrl']);
$routes->connect('/:controller/:action/*');
$this->assertFalse($routes->get('nope'));
$route = $routes->get('cntrl');
$this->assertInstanceOf('Cake\Routing\Route\Route', $route);
$this->assertEquals('/l/:controller', $route->template);
}
/**
* Test connecting basic routes.
*
* @return void
*/
public function testConnectBasic() {
$routes = new ScopedRouteCollection('/l', ['prefix' => 'api']);
$this->assertNull($routes->connect('/:controller'));
$route = $routes->routes()[0];
$this->assertInstanceOf('Cake\Routing\Route\Route', $route);
$this->assertEquals('/l/:controller', $route->template);
$expected = ['prefix' => 'api', 'action' => 'index', 'plugin' => null];
$this->assertEquals($expected, $route->defaults);
}
/**
* Test extensions being connected to routes.
*
* @return void
*/
public function testConnectExtensions() {
$routes = new ScopedRouteCollection('/l', [], ['json']);
$routes->connect('/:controller');
$route = $routes->routes()[0];
$this->assertEquals(['json'], $route->options['_ext']);
$routes->extensions(['xml', 'json']);
$routes->connect('/:controller/:action');
$new = $routes->routes()[1];
$this->assertEquals(['json'], $route->options['_ext']);
$this->assertEquals(['xml', 'json'], $new->options['_ext']);
}
/**
* Test error on invalid route class
*
* @expectedException \Cake\Error\Exception
* @expectedExceptionMessage Route class not found, or route class is not a subclass of
* @return void
*/
public function testConnectErrorInvalidRouteClass() {
$routes = new ScopedRouteCollection('/l', [], ['json']);
$routes->connect('/:controller', [], ['routeClass' => '\StdClass']);
}
}

0 comments on commit 353324a

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