Permalink
Browse files

disallow append/prepend after a match() attempt

  • Loading branch information...
1 parent 98afc57 commit 4772d459cc0e138c5ebe8acacaeb773d2a5c7429 @pmjones pmjones committed Oct 16, 2013
Showing with 32 additions and 14 deletions.
  1. +22 −14 src/Aura/Router/Map.php
  2. +10 −0 tests/Aura/Router/MapTest.php
View
@@ -28,7 +28,7 @@ class Map
* @var array
*
*/
- protected $attach_common = null;
+ protected $attach_common = [];
/**
*
@@ -37,7 +37,7 @@ class Map
* @var array
*
*/
- protected $attach_routes = null;
+ protected $attach_routes = [];
/**
*
@@ -167,16 +167,14 @@ public function attach($path_prefix, $spec)
*/
public function appendMap(Map $map)
{
- // append the map
+ if ($this->routes) {
+ throw new Exception("Cannot append a map after attempting match().");
+ }
+
$this->definitions = array_merge(
$this->definitions,
$map->definitions
);
-
- // reset existing routes and tracking properties
- $this->attach_common = null;
- $this->attach_routes = null;
- $this->routes = [];
}
/**
@@ -190,16 +188,26 @@ public function appendMap(Map $map)
*/
public function prependMap(Map $map)
{
- // prepend the map
+ if ($this->routes) {
+ throw new Exception("Cannot prepend a map after attempting match().");
+ }
+
$this->definitions = array_merge(
$map->definitions,
$this->definitions
);
-
- // reset existing routes and tracking properties
- $this->attach_common = null;
- $this->attach_routes = null;
- $this->routes = [];
+ }
+
+ /**
+ *
+ * Clears out all existing definitions.
+ *
+ * @return void
+ *
+ */
+ public function reset()
+ {
+ return $this->setRoutes([]);
}
/**
@@ -868,6 +868,11 @@ public function testAppendMap()
$actual = array_keys($this->map->getRoutes());
$expect = ['foo', 'bar'];
$this->assertSame($expect, $actual);
+
+ // append after matching
+ $route = $this->map->match('/', $this->server);
+ $this->setExpectedException('Aura\Router\Exception');
+ $this->map->appendMap($append_map);
}
public function testPrependMap()
@@ -881,5 +886,10 @@ public function testPrependMap()
$actual = array_keys($this->map->getRoutes());
$expect = ['bar', 'foo'];
$this->assertSame($expect, $actual);
+
+ // prepend after matching
+ $route = $this->map->match('/', $this->server);
+ $this->setExpectedException('Aura\Router\Exception');
+ $this->map->prependMap($prepend_map);
}
}

0 comments on commit 4772d45

Please sign in to comment.