Skip to content
Permalink
Browse files

Start fixing up the few failing router tests.

  • Loading branch information...
markstory committed Jun 27, 2014
1 parent f03b40e commit b34d43c85edbe112251735ebfe07a461f8039218
Showing with 59 additions and 63 deletions.
  1. +2 −1 src/Routing/Router.php
  2. +1 −0 src/Routing/ScopedRouteCollection.php
  3. +56 −62 tests/TestCase/Routing/RouterTest.php
@@ -871,7 +871,7 @@ protected static function _match($url) {
// No quick win, iterate and hope for the best.
foreach (static::$_pathScopes as $key => $collection) {
$match = $collection->match($url, static::$_requestContext);
if ($match) {
if ($match !== false) {
return $match;
}
}
@@ -1133,6 +1133,7 @@ public static function scope($path, $params = [], $callback = null) {
static::$_pathScopes[$path]->merge($collection);
}
static::$_named += $collection->named();
return static::$_pathScopes[$path];
}
/**
@@ -577,6 +577,7 @@ protected function _getNames($url) {
'%1$s.%2$s:_action',
'%1$s._controller:%3$s',
'%1$s._controller:_action',
'_plugin.%2$s:%3$s',
'_plugin._controller:%3$s',
'_plugin._controller:_action',
'_controller:_action'
@@ -647,7 +647,6 @@ public function testUrlGenerationBasic() {
$this->assertEquals($expected, $result);
Router::connect('/view/*', array('controller' => 'posts', 'action' => 'view'));
Router::promote();
$result = Router::url(array('controller' => 'posts', 'action' => 'view', '1'));
$expected = '/view/1';
$this->assertEquals($expected, $result);
@@ -1979,21 +1978,25 @@ public function testPrefixOverride() {
* @return void
*/
public function testRouteParamDefaults() {
Configure::write('Routing.prefixes', array('admin'));
Router::reload();
Router::connect('/cache/*', array('prefix' => false, 'plugin' => true, 'controller' => 0, 'action' => 1));
$url = Router::url(array('controller' => 0, 'action' => 1, 'test'));
$expected = '/';
$this->assertEquals($expected, $url);
$url = Router::url(array('prefix' => 1, 'controller' => 0, 'action' => 1, 'test'));
$expected = '/';
$this->assertEquals($expected, $url);
$url = Router::url(array('prefix' => 0, 'plugin' => 1, 'controller' => 0, 'action' => 1, 'test'));
$expected = '/cache/test';
$this->assertEquals($expected, $url);
try {
Router::url(array('controller' => 0, 'action' => 1, 'test'));
$this->fail('No exception raised');
} catch (\Exception $e) {
$this->assertTrue(true, 'Exception was raised');
}
try {
Router::url(array('prefix' => 1, 'controller' => 0, 'action' => 1, 'test'));
$this->fail('No exception raised');
} catch (\Exception $e) {
$this->assertTrue(true, 'Exception was raised');
}
}
/**
@@ -2096,12 +2099,12 @@ public function testParsingWithTrailingPeriodAndParseExtensions() {
* @return void
*/
public function testParsingWithPatternOnAction() {
Router::reload();
Router::connect(
'/blog/:action/*',
array('controller' => 'blog_posts'),
array('action' => 'other|actions')
);
$result = Router::parse('/blog/other');
$expected = array(
'plugin' => null,
@@ -2114,11 +2117,26 @@ public function testParsingWithPatternOnAction() {
$result = Router::parse('/blog/foobar');
$this->assertSame([], $result);
$result = Router::url(array('controller' => 'blog_posts', 'action' => 'foo'));
$this->assertEquals('/', $result);
}
/**
* Test url() works with patterns on :action
*
* @expectedException Cake\Error\Exception
* @return void
*/
public function testUrlPatterOnAction() {
Router::connect(
'/blog/:action/*',
array('controller' => 'blog_posts'),
array('action' => 'other|actions')
);
$result = Router::url(array('controller' => 'blog_posts', 'action' => 'actions'));
$this->assertEquals('/blog/actions', $result);
$result = Router::url(array('controller' => 'blog_posts', 'action' => 'foo'));
$this->assertEquals('/', $result);
}
/**
@@ -2291,8 +2309,15 @@ public function testRegexRouteMatching() {
$result = Router::parse('/badness/test/test_action');
$this->assertSame([], $result);
}
Router::reload();
/**
* testRegexRouteMatching method
*
* @expectedException Cake\Error\Exception
* @return void
*/
public function testRegexRouteMatchUrl() {
Router::connect('/:locale/:controller/:action/*', [], array('locale' => 'dan|eng'));
$request = new Request();
@@ -2309,13 +2334,13 @@ public function testRegexRouteMatching() {
))
);
$result = Router::url(array('action' => 'test_another_action'));
$expected = '/';
$this->assertEquals($expected, $result);
$result = Router::url(array('action' => 'test_another_action', 'locale' => 'eng'));
$expected = '/eng/test/test_another_action';
$this->assertEquals($expected, $result);
$result = Router::url(array('action' => 'test_another_action'));
$expected = '/';
$this->assertEquals($expected, $result);
}
/**
@@ -2370,19 +2395,21 @@ public function testConnectDefaultRoutes() {
* @return void
*/
public function testUsingCustomRouteClass() {
$routes = $this->getMock('Cake\Routing\RouteCollection');
$this->getMock('Cake\Routing\Route\Route', [], [], 'MockConnectedRoute', false);
Router::setRouteCollection($routes);
$routes->expects($this->once())
->method('add')
->with($this->isInstanceOf('\MockConnectedRoute'));
Plugin::load('TestPlugin');
Router::connect(
'/:slug',
array('controller' => 'posts', 'action' => 'view'),
array('routeClass' => '\MockConnectedRoute', 'slug' => '[a-z_-]+')
array('plugin' => 'TestPlugin', 'action' => 'index'),
array('routeClass' => 'PluginShortRoute', 'slug' => '[a-z_-]+')
);
$result = Router::parse('/the-best');
$expected = [
'plugin' => 'TestPlugin',
'controller' => 'TestPlugin',
'action' => 'index',
'slug' => 'the-best',
'pass' => [],
];
$this->assertEquals($result, $expected);
}
/**
@@ -2704,39 +2731,6 @@ public function testResourceMap() {
Router::resourceMap($default);
}
/**
* test setting redirect routes
*
* @return void
*/
public function testRouteRedirection() {
$routes = new RouteCollection();
Router::setRouteCollection($routes);
Router::redirect('/blog', array('controller' => 'posts'), array('status' => 302));
Router::connect('/:controller', array('action' => 'index'));
$this->assertEquals(2, count($routes));
$routes->get(0)->response = $this->getMock(
'Cake\Network\Response',
array('_sendHeader', 'stop')
);
$this->assertEquals(302, $routes->get(0)->options['status']);
Router::parse('/blog');
$header = $routes->get(0)->response->header();
$this->assertEquals(Router::url('/posts', true), $header['Location']);
$this->assertEquals(302, $routes->get(0)->response->statusCode());
$routes->get(0)->response = $this->getMock(
'Cake\Network\Response',
array('_sendHeader')
);
Router::parse('/not-a-match');
$this->assertSame([], $routes->get(0)->response->header());
}
/**
* Test setting the default route class
*

0 comments on commit b34d43c

Please sign in to comment.
You can’t perform that action at this time.