Skip to content

Commit 9663880

Browse files
committed
Add tests for resource().
Still need a few more for the various connected routes.
1 parent 655bbe9 commit 9663880

File tree

2 files changed

+66
-2
lines changed

2 files changed

+66
-2
lines changed

src/Routing/ScopedRouteCollection.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,10 @@ public function get($name) {
219219
* @return array Array of mapped resources
220220
*/
221221
public function resource($name, $options = [], $callback = null) {
222+
if (is_callable($options) && $callback === null) {
223+
$callback = $options;
224+
$options = [];
225+
}
222226
$options += array(
223227
'connectOptions' => [],
224228
'id' => static::ID . '|' . static::UUID
@@ -227,8 +231,8 @@ public function resource($name, $options = [], $callback = null) {
227231
unset($options['connectOptions']);
228232

229233
$urlName = Inflector::underscore($name);
230-
$ext = null;
231234

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

252256
if (is_callable($callback)) {
253257
$idName = Inflector::singularize($urlName) . '_id';
254-
$path = '/' . $urlName . '/:' . $idName;
258+
$path = $this->_path . '/' . $urlName . '/:' . $idName;
255259
Router::scope($path, $this->params(), $callback);
256260
}
257261
}

tests/TestCase/Routing/ScopedRouteCollectionTest.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ public function testConnectBasic() {
125125
*/
126126
public function testConnectExtensions() {
127127
$routes = new ScopedRouteCollection('/l', [], ['json']);
128+
$this->assertEquals(['json'], $routes->extensions());
129+
128130
$routes->connect('/:controller');
129131
$route = $routes->routes()[0];
130132

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

293+
/**
294+
* Test matching plugin routes.
295+
*
296+
* @return void
297+
*/
298+
public function testMatchPlugin() {
299+
$context = [
300+
'_base' => '/',
301+
'_scheme' => 'http',
302+
'_host' => 'example.org',
303+
];
304+
$routes = new ScopedRouteCollection('/contacts', ['plugin' => 'Contacts']);
305+
$routes->connect('/', ['controller' => 'Contacts']);
306+
307+
$result = $routes->match(
308+
['plugin' => 'Contacts', 'controller' => 'Contacts', 'action' => 'index'],
309+
$context
310+
);
311+
$this->assertFalse($result);
312+
313+
$result = $routes->match(['controller' => 'Contacts', 'action' => 'index'], $context);
314+
$this->assertFalse($result);
315+
}
316+
317+
/**
318+
* Test connecting resources.
319+
*
320+
* @return void
321+
*/
322+
public function testResource() {
323+
$routes = new ScopedRouteCollection('/api', ['prefix' => 'api']);
324+
$routes->resource('Articles', ['_ext' => 'json']);
325+
326+
$all = $routes->routes();
327+
$this->assertCount(6, $all);
328+
329+
$this->assertEquals('/api/articles', $all[0]->template);
330+
$this->assertEquals('json', $all[0]->defaults['_ext']);
331+
$this->assertEquals('Articles', $all[0]->defaults['controller']);
332+
}
333+
334+
/**
335+
* Test nesting resources
336+
*
337+
* @return void
338+
*/
339+
public function testResourceNested() {
340+
$routes = new ScopedRouteCollection('/api', ['prefix' => 'api']);
341+
$routes->resource('Articles', function($routes) {
342+
$this->assertEquals('/api/articles/', $routes->path());
343+
$this->assertEquals(['prefix' => 'api'], $routes->params());
344+
345+
$routes->resource('Comments');
346+
$route = $routes->routes()[0];
347+
$this->assertEquals('/api/articles/:article_id/comments', $route->template);
348+
});
349+
}
350+
291351
}

0 commit comments

Comments
 (0)