Skip to content

Commit

Permalink
Updating Dispatcher test to test for exceptions instead of cake error…
Browse files Browse the repository at this point in the history
… returns.

Removing a test that is no longer valid.
  • Loading branch information
markstory committed Aug 28, 2010
1 parent b301b0d commit e600f86
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 118 deletions.
4 changes: 2 additions & 2 deletions cake/dispatcher.php
Expand Up @@ -118,7 +118,7 @@ public function dispatch($url = null, $additionalParams = array()) {

if (!is_object($controller)) {
Router::setRequestInfo(array($this->params, array('base' => $this->base, 'webroot' => $this->webroot)));
throw new MissingControllerException($this->params['controller'] . 'Controller');
throw new MissingControllerException(Inflector::camelize($this->params['controller']) . 'Controller');
}
$privateAction = $this->params['action'][0] === '_';
$prefixes = Router::prefixes();
Expand Down Expand Up @@ -188,7 +188,7 @@ protected function _invoke(&$controller, $params) {
return new Scaffold($controller, $params);
}
$message = sprintf(
'%s::%s',
'%s::%s()',
Inflector::camelize($params['controller']) . "Controller",
$params['action']
);
Expand Down
190 changes: 74 additions & 116 deletions cake/tests/cases/dispatcher.test.php
Expand Up @@ -1206,17 +1206,15 @@ public function testBaseUrlAndWebrootWithBase() {
* @return void
*/
public function testMissingController() {
$Dispatcher = new TestDispatcher();
Configure::write('App.baseUrl', '/index.php');
$url = 'some_controller/home/param:value/param2:value2';
$controller = $Dispatcher->dispatch($url, array('return' => 1));
$expected = array('missingController', array(array(
'className' => 'SomeControllerController',
'webroot' => '/',
'url' => 'some_controller/home/param:value/param2:value2',
'base' => '/index.php'
)));
$this->assertEqual($expected, $controller);
try {
$Dispatcher = new TestDispatcher();
Configure::write('App.baseUrl', '/index.php');
$url = 'some_controller/home/param:value/param2:value2';
$controller = $Dispatcher->dispatch($url, array('return' => 1));
$this->fail('No exception thrown');
} catch (MissingControllerException $e) {
$this->assertEquals('SomeControllerController', $e->getMessage());
}
}

/**
Expand All @@ -1229,16 +1227,12 @@ public function testPrivate() {
Configure::write('App.baseUrl','/index.php');
$url = 'some_pages/_protected/param:value/param2:value2';

$controller = $Dispatcher->dispatch($url, array('return' => 1));

$expected = array('privateAction', array(array(
'className' => 'SomePagesController',
'action' => '_protected',
'webroot' => '/',
'url' => 'some_pages/_protected/param:value/param2:value2',
'base' => '/index.php'
)));
$this->assertEqual($controller, $expected);
try {
$controller = $Dispatcher->dispatch($url, array('return' => 1));
$this->fail('No exception thrown');
} catch (PrivateActionException $e) {
$this->assertEquals('SomePagesController::_protected()', $e->getMessage());
}
}

/**
Expand All @@ -1251,31 +1245,30 @@ public function testMissingAction() {
Configure::write('App.baseUrl', '/index.php');
$url = 'some_pages/home/param:value/param2:value2';

$controller = $Dispatcher->dispatch($url, array('return'=> 1));

$expected = array('missingAction', array(array(
'className' => 'SomePagesController',
'action' => 'home',
'webroot' => '/',
'url' => '/index.php/some_pages/home/param:value/param2:value2',
'base' => '/index.php'
)));
$this->assertEqual($expected, $controller);

try {
$controller = $Dispatcher->dispatch($url, array('return'=> 1));
$this->fail('No exception thrown');
} catch (MissingActionException $e) {
$this->assertEquals('SomePagesController::home()', $e->getMessage());
}
}

/**
* test that methods declared in Controller are treated as missing methods.
*
* @return void
*/
function testMissingActionFromBaseClassMethods() {
$Dispatcher = new TestDispatcher();
Configure::write('App.baseUrl','/index.php');
$url = 'some_pages/redirect/param:value/param2:value2';

$controller = $Dispatcher->dispatch($url, array('return'=> 1));

$expected = array('missingAction', array(array(
'className' => 'SomePagesController',
'action' => 'redirect',
'webroot' => '/',
'url' => '/index.php/some_pages/redirect/param:value/param2:value2',
'base' => '/index.php'
)));
$this->assertEqual($expected, $controller);
try {
$controller = $Dispatcher->dispatch($url, array('return'=> 1));
$this->fail('No exception thrown');
} catch (MissingActionException $e) {
$this->assertEquals('SomePagesController::redirect()', $e->getMessage());
}
}

/**
Expand Down Expand Up @@ -1655,32 +1648,25 @@ public function testAutomaticPluginControllerMissingActionDispatch() {
$Dispatcher->base = false;

$url = 'my_plugin/not_here/param:value/param2:value2';
$controller = $Dispatcher->dispatch($url, array('return'=> 1));

$expected = array('missingAction', array(array(
'className' => 'MyPluginController',
'action' => 'not_here',
'webroot' => '/cake/repo/branches/1.2.x.x/',
'url' => '/cake/repo/branches/1.2.x.x/my_plugin/not_here/param:value/param2:value2',
'base' => '/cake/repo/branches/1.2.x.x'
)));
$this->assertIdentical($expected, $controller);

try {
$controller = $Dispatcher->dispatch($url, array('return'=> 1));
$this->fail('No exception.');
} catch (MissingActionException $e) {
$this->assertEquals('MyPluginController::not_here()', $e->getMessage());
}

Router::reload();
$Dispatcher = new TestDispatcher();
$Dispatcher->base = false;

$url = 'my_plugin/param:value/param2:value2';
$controller = $Dispatcher->dispatch($url, array('return'=> 1));

$expected = array('missingAction', array(array(
'className' => 'MyPluginController',
'action' => 'param:value',
'webroot' => '/cake/repo/branches/1.2.x.x/',
'url' => '/cake/repo/branches/1.2.x.x/my_plugin/param:value/param2:value2',
'base' => '/cake/repo/branches/1.2.x.x'
)));
$this->assertIdentical($expected, $controller);
try {
$controller = $Dispatcher->dispatch($url, array('return'=> 1));
$this->fail('No exception.');
} catch (MissingActionException $e) {
$this->assertEquals('MyPluginController::param:value()', $e->getMessage());
}
}

/**
Expand All @@ -1699,16 +1685,12 @@ public function testPrefixProtection() {
$Dispatcher->base = false;

$url = 'test_dispatch_pages/admin_index/param:value/param2:value2';
$controller = $Dispatcher->dispatch($url, array('return' => 1));

$expected = array('privateAction', array(array(
'className' => 'TestDispatchPagesController',
'action' => 'admin_index',
'webroot' => '/cake/repo/branches/1.2.x.x/',
'url' => 'test_dispatch_pages/admin_index/param:value/param2:value2',
'base' => '/cake/repo/branches/1.2.x.x'
)));
$this->assertIdentical($expected, $controller);
try {
$controller = $Dispatcher->dispatch($url, array('return'=> 1));
$this->fail('No exception.');
} catch (PrivateActionException $e) {
$this->assertEquals('TestDispatchPagesController::admin_index()', $e->getMessage());
}
}

/**
Expand Down Expand Up @@ -1747,16 +1729,13 @@ public function testChangingParamsFromBeforeFilter() {
$_SERVER['PHP_SELF'] = '/cake/repo/branches/1.2.x.x/index.php';
$Dispatcher = new TestDispatcher();
$url = 'some_posts/index/param:value/param2:value2';
$controller = $Dispatcher->dispatch($url, array('return' => 1));

$expected = array('missingAction', array(array(
'className' => 'SomePostsController',
'action' => 'view',
'webroot' => '/cake/repo/branches/1.2.x.x/',
'url' => '/cake/repo/branches/1.2.x.x/some_posts/index/param:value/param2:value2',
'base' => '/cake/repo/branches/1.2.x.x'
)));
$this->assertEqual($expected, $controller);

try {
$controller = $Dispatcher->dispatch($url, array('return'=> 1));
$this->fail('No exception.');
} catch (MissingActionException $e) {
$this->assertEquals('SomePostsController::view()', $e->getMessage());
}

$url = 'some_posts/something_else/param:value/param2:value2';
$controller = $Dispatcher->dispatch($url, array('return' => 1));
Expand Down Expand Up @@ -1789,15 +1768,19 @@ public function testAssets() {
$debug = Configure::read('debug');
Configure::write('debug', 0);

ob_start();
$Dispatcher->dispatch('theme/test_theme/../webroot/css/test_asset.css');
$result = ob_get_clean();
$this->assertEquals($result, '');

ob_start();
$Dispatcher->dispatch('theme/test_theme/pdfs');
$result = ob_get_clean();
$this->assertEquals($result, '');
try {
$Dispatcher->dispatch('theme/test_theme/../webroot/css/test_asset.css');
$this->fail('No exception');
} catch (MissingControllerException $e) {
$this->assertEquals('ThemeController', $e->getMessage());
}

try {
$Dispatcher->dispatch('theme/test_theme/pdfs');
$this->fail('No exception');
} catch (MissingControllerException $e) {
$this->assertEquals('ThemeController', $e->getMessage());
}

ob_start();
$Dispatcher->dispatch('theme/test_theme/flash/theme_test.swf');
Expand Down Expand Up @@ -2321,31 +2304,6 @@ public function testEnvironmentDetection() {
$this->__loadEnvironment(array_merge(array('reload' => true), $backup));
}

/**
* Tests that the Dispatcher does not return an empty action
*
* @return void
*/
public function testTrailingSlash() {
$_POST = array();
$_SERVER['PHP_SELF'] = '/cake/repo/branches/1.2.x.x/index.php';

Router::reload();
$Dispatcher = new TestDispatcher();
Router::connect('/myalias/:action/*', array('controller' => 'my_controller', 'action' => null));

$Dispatcher->base = false;
$url = 'myalias/'; //Fails
$controller = $Dispatcher->dispatch($url, array('return' => 1));
$result = $Dispatcher->parseParams($url);
$this->assertEqual('index', $result['action']);

$url = 'myalias'; //Passes
$controller = $Dispatcher->dispatch($url, array('return' => 1));
$result = $Dispatcher->parseParams($url);
$this->assertEqual('index', $result['action']);
}

/**
* backupEnvironment method
*
Expand Down

0 comments on commit e600f86

Please sign in to comment.