Skip to content
Permalink
Browse files

Add tests for resource().

Still need a few more for the various connected routes.
  • Loading branch information...
markstory committed Jun 24, 2014
1 parent 655bbe9 commit 966388080dbd2341ed3e5f68fafe51dbcc1ab51d
Showing with 66 additions and 2 deletions.
  1. +6 −2 src/Routing/ScopedRouteCollection.php
  2. +60 −0 tests/TestCase/Routing/ScopedRouteCollectionTest.php
@@ -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
@@ -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'];
}
@@ -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);
}
}
@@ -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];
@@ -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.
You can’t perform that action at this time.