Skip to content

Commit

Permalink
Add basic tests for Router::scope().
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Jun 25, 2014
1 parent 67d9b42 commit f0af8f8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Routing/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -1076,6 +1076,7 @@ public static function parseNamedParams(Request $request, $options = []) {
* @param array $params An array of routing defaults to add to each connected route.
* If you have no parameters, this argument can be a callable.
* @param callable $callback The callback to invoke with the scoped collection.
* @throws \InvalidArgumentException When an invalid callable is provided.
* @return void
*/
public static function scope($path, $params = [], $callback = null) {
Expand All @@ -1085,7 +1086,7 @@ public static function scope($path, $params = [], $callback = null) {
}
if (!is_callable($callback)) {
$msg = 'Need a callable function/object to connect routes.';
throw Error\Exception($msg);
throw new \InvalidArgumentException($msg);
}

$collection = new ScopedRouteCollection($path, $params, static::$_validExtensions);
Expand Down
29 changes: 29 additions & 0 deletions tests/TestCase/Routing/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2856,4 +2856,33 @@ public function testPromote() {
$this->assertEquals('/en/posts/index', $result, 'promote() should move 2nd route ahead.');
}

/**
* Test the scope() method
*
* @return void
*/
public function testScope() {
Router::scope('/path', ['param' => 'value'], function($routes) {
$this->assertInstanceOf('Cake\Routing\ScopedRouteCollection', $routes);
$this->assertCount(0, $routes->routes());
$this->assertEquals('/path', $routes->path());
$this->assertEquals(['param' => 'value'], $routes->params());

$routes->connect('/articles', ['controller' => 'Articles']);
});
Router::scope('/path', function($routes) {
$this->assertCount(0, $routes->routes());
});
}

/**
* Test the scope() method
*
* @expectedException \InvalidArgumentException
* @return void
*/
public function testScopeError() {
Router::scope('/path', 'derpy');
}

}

0 comments on commit f0af8f8

Please sign in to comment.