Skip to content

Commit

Permalink
Add tests for resource().
Browse files Browse the repository at this point in the history
Still need a few more for the various connected routes.
  • Loading branch information
markstory committed Jun 25, 2014
1 parent 655bbe9 commit 9663880
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/Routing/ScopedRouteCollection.php
Expand Up @@ -219,6 +219,10 @@ public function get($name) {
* @return array Array of mapped resources
*/
public function resource($name, $options = [], $callback = null) {
if (is_callable($options) && $callback === null) {
$callback = $options;
$options = [];
}
$options += array(
'connectOptions' => [],
'id' => static::ID . '|' . static::UUID
Expand All @@ -227,8 +231,8 @@ public function resource($name, $options = [], $callback = null) {
unset($options['connectOptions']);

$urlName = Inflector::underscore($name);
$ext = null;

$ext = null;
if (!empty($options['_ext'])) {
$ext = $options['_ext'];
}
Expand All @@ -251,7 +255,7 @@ public function resource($name, $options = [], $callback = null) {

if (is_callable($callback)) {
$idName = Inflector::singularize($urlName) . '_id';
$path = '/' . $urlName . '/:' . $idName;
$path = $this->_path . '/' . $urlName . '/:' . $idName;
Router::scope($path, $this->params(), $callback);
}
}
Expand Down
60 changes: 60 additions & 0 deletions tests/TestCase/Routing/ScopedRouteCollectionTest.php
Expand Up @@ -125,6 +125,8 @@ public function testConnectBasic() {
*/
public function testConnectExtensions() {
$routes = new ScopedRouteCollection('/l', [], ['json']);
$this->assertEquals(['json'], $routes->extensions());

$routes->connect('/:controller');
$route = $routes->routes()[0];

Expand Down Expand Up @@ -288,4 +290,62 @@ public function testMatch() {
$this->assertFalse($result, 'No matches');
}

/**
* Test matching plugin routes.
*
* @return void
*/
public function testMatchPlugin() {
$context = [
'_base' => '/',
'_scheme' => 'http',
'_host' => 'example.org',
];
$routes = new ScopedRouteCollection('/contacts', ['plugin' => 'Contacts']);
$routes->connect('/', ['controller' => 'Contacts']);

$result = $routes->match(
['plugin' => 'Contacts', 'controller' => 'Contacts', 'action' => 'index'],
$context
);
$this->assertFalse($result);

$result = $routes->match(['controller' => 'Contacts', 'action' => 'index'], $context);
$this->assertFalse($result);
}

/**
* Test connecting resources.
*
* @return void
*/
public function testResource() {
$routes = new ScopedRouteCollection('/api', ['prefix' => 'api']);
$routes->resource('Articles', ['_ext' => 'json']);

$all = $routes->routes();
$this->assertCount(6, $all);

$this->assertEquals('/api/articles', $all[0]->template);
$this->assertEquals('json', $all[0]->defaults['_ext']);
$this->assertEquals('Articles', $all[0]->defaults['controller']);
}

/**
* Test nesting resources
*
* @return void
*/
public function testResourceNested() {
$routes = new ScopedRouteCollection('/api', ['prefix' => 'api']);
$routes->resource('Articles', function($routes) {
$this->assertEquals('/api/articles/', $routes->path());
$this->assertEquals(['prefix' => 'api'], $routes->params());

$routes->resource('Comments');
$route = $routes->routes()[0];
$this->assertEquals('/api/articles/:article_id/comments', $route->template);
});
}

}

0 comments on commit 9663880

Please sign in to comment.