Skip to content
Permalink
Browse files

Further cleanup and test expansion.

Clean up the last bits and finish off the tests.
  • Loading branch information...
markstory committed Jul 6, 2014
1 parent 407388a commit d0f236290c92361fb1c8041ef4c0989c93430cd4
@@ -26,6 +26,8 @@
*
* Provides an interface for adding/removing routes
* and parsing/generating URLs with the routes it contains.
*
* @internal
*/
class RouteCollection {
@@ -76,17 +78,6 @@ public function add(Route $route, $options) {
krsort($this->_paths);
}
$this->_paths[$path][] = $route;
/*
// Index scopes by key params (for reverse routing).
$plugin = isset($params['plugin']) ? $params['plugin'] : '';
$prefix = isset($params['prefix']) ? $params['prefix'] : '';
if (!isset(static::$_paramScopes[$plugin][$prefix])) {
static::$_paramScopes[$plugin][$prefix] = $collection;
} else {
static::$_paramScopes[$plugin][$prefix]->merge($collection);
}
*/
}
/**
@@ -128,15 +119,12 @@ public function parse($url) {
* @return string The name of the url
*/
protected function _getNames($url) {
if (isset($url['_name'])) {
return [$url['_name']];
}
$plugin = false;
if (isset($url['plugin'])) {
$plugin = $url['plugin'];
$plugin = strtolower($url['plugin']);
}
$controller = $url['controller'];
$action = $url['action'];
$controller = strtolower($url['controller']);
$action = strtolower($url['action']);
$fallbacks = [
"${controller}:${action}",
@@ -171,14 +159,17 @@ protected function _getNames($url) {
public function match($url, $context) {
// Named routes support hack.
if (isset($url['_name'])) {
$route = false;
if (isset($this->_named[$url['_name']])) {
$route = $this->_named[$url['_name']];
$name = $url['_name'];
unset($url['_name']);
$out = false;
if (isset($this->_named[$name])) {
$route = $this->_named[$name];
$out = $route->match($url + $route->defaults, $context);
}
if ($route) {
unset($url['_name']);
return $route->match($url + $route->defaults, $context);
if ($out) {
return $out;
}
throw new MissingRouteException(['url' => $name]);
}
foreach ($this->_getNames($url) as $name) {
@@ -204,6 +195,11 @@ public function routes() {
return $this->_routes;
}
/**
* Get the connected named routes.
*
* @return array
*/
public function named() {
return $this->_named;
}
@@ -80,22 +80,6 @@ public function testRoutes() {
$this->assertInstanceOf('Cake\Routing\Route\Route', $all[1]);
}
/**
* Test getting named routes.
*
* @return void
*/
public function testNamed() {
$routes = new RouteBuilder($this->collection, '/l');
$routes->connect('/:controller', ['action' => 'index'], ['_name' => 'cntrl']);
$routes->connect('/:controller/:action/*');
$all = $this->collection->named();
$this->assertCount(1, $all);
$this->assertInstanceOf('Cake\Routing\Route\Route', $all['cntrl']);
$this->assertEquals('/l/:controller', $all['cntrl']->template);
}
/**
* Test connecting an instance routes.
*
@@ -123,6 +123,43 @@ public function testMatch() {
$this->assertEquals('b/thing', $result);
}
/**
* Test matching routes with names
*
* @return void
*/
public function testMatchNamed() {
$context = [
'_base' => '/',
'_scheme' => 'http',
'_host' => 'example.org',
];
$routes = new RouteBuilder($this->collection, '/b');
$routes->connect('/', ['controller' => 'Articles']);
$routes->connect('/:id', ['controller' => 'Articles', 'action' => 'view'], ['_name' => 'article:view']);
$result = $this->collection->match(['_name' => 'article:view', 'id' => '2'], $context);
$this->assertEquals('/b/2', $result);
}
/**
* Test matching routes with names and failing
*
* @expectedException Cake\Routing\Error\MissingRouteException
* @return void
*/
public function testMatchNamedError() {
$context = [
'_base' => '/',
'_scheme' => 'http',
'_host' => 'example.org',
];
$routes = new RouteBuilder($this->collection, '/b');
$routes->connect('/:id', ['controller' => 'Articles', 'action' => 'view'], ['_name' => 'article:view']);
$this->collection->match(['_name' => 'derp'], $context);
}
/**
* Test matching plugin routes.
*
@@ -141,4 +178,34 @@ public function testMatchPlugin() {
$this->assertEquals('contacts', $result);
}
/**
* Test getting named routes.
*
* @return void
*/
public function testNamed() {
$routes = new RouteBuilder($this->collection, '/l');
$routes->connect('/:controller', ['action' => 'index'], ['_name' => 'cntrl']);
$routes->connect('/:controller/:action/*');
$all = $this->collection->named();
$this->assertCount(1, $all);
$this->assertInstanceOf('Cake\Routing\Route\Route', $all['cntrl']);
$this->assertEquals('/l/:controller', $all['cntrl']->template);
}
/**
* Test adding with an error
*/
public function testAdd() {
}
/**
* Test the routes() method.
*
* @return void
*/
public function testRoutes() {
}
}

0 comments on commit d0f2362

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