Browse Source

Further cleanup and test expansion.

Clean up the last bits and finish off the tests.
mark_story 11 years ago
parent
commit
d0f236290c

+ 19 - 23
src/Routing/RouteCollection.php

@@ -26,6 +26,8 @@ use Cake\Utility\Inflector;
  *
  * Provides an interface for adding/removing routes
  * and parsing/generating URLs with the routes it contains.
+ *
+ * @internal
  */
 class RouteCollection {
 
@@ -76,17 +78,6 @@ class RouteCollection {
 			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 @@ class RouteCollection {
  * @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 @@ class RouteCollection {
 	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 @@ class RouteCollection {
 		return $this->_routes;
 	}
 
+/**
+ * Get the connected named routes.
+ *
+ * @return array
+ */
 	public function named() {
 		return $this->_named;
 	}

+ 0 - 16
tests/TestCase/Routing/RouteBuilderTest.php

@@ -81,22 +81,6 @@ class RouteBuilderTest extends TestCase {
 	}
 
 /**
- * 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.
  *
  * @return void

+ 67 - 0
tests/TestCase/Routing/RouteCollectionTest.php

@@ -124,6 +124,43 @@ class RouteCollectionTest extends TestCase {
 	}
 
 /**
+ * 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.
  *
  * @return void
@@ -141,4 +178,34 @@ class RouteCollectionTest extends TestCase {
 		$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() {
+	}
+
 }