Skip to content

Commit

Permalink
Refactoring tests to better use PHPUnit's features
Browse files Browse the repository at this point in the history
Removing non-functional cruft.
  • Loading branch information
markstory committed Jul 23, 2011
1 parent 9641bcc commit 5240ede
Showing 1 changed file with 37 additions and 65 deletions.
102 changes: 37 additions & 65 deletions lib/Cake/Test/Case/Routing/DispatcherTest.php
Expand Up @@ -687,24 +687,24 @@ public function testQueryStringOnRoot() {
/** /**
* testMissingController method * testMissingController method
* *
* @expectedException MissingControllerException
* @expectedExceptionMessage Controller class SomeControllerController could not be found.
* @return void * @return void
*/ */
public function testMissingController() { public function testMissingController() {
try { $Dispatcher = new TestDispatcher();
$Dispatcher = new TestDispatcher(); Configure::write('App.baseUrl', '/index.php');
Configure::write('App.baseUrl', '/index.php'); $url = new CakeRequest('some_controller/home/param:value/param2:value2');
$url = new CakeRequest('some_controller/home/param:value/param2:value2'); $response = $this->getMock('CakeResponse');
$response = $this->getMock('CakeResponse');
$controller = $Dispatcher->dispatch($url, $response, array('return' => 1)); $controller = $Dispatcher->dispatch($url, $response, array('return' => 1));
$this->fail('No exception thrown');
} catch (MissingControllerException $e) {
$this->assertEquals('Controller class SomeControllerController could not be found.', $e->getMessage());
}
} }


/** /**
* testPrivate method * testPrivate method
* *
* @expectedException PrivateActionException
* @expectedExceptionMessage Private Action SomePagesController::_protected() is not directly accessible.
* @return void * @return void
*/ */
public function testPrivate() { public function testPrivate() {
Expand All @@ -713,19 +713,14 @@ public function testPrivate() {
$url = new CakeRequest('some_pages/_protected/param:value/param2:value2'); $url = new CakeRequest('some_pages/_protected/param:value/param2:value2');
$response = $this->getMock('CakeResponse'); $response = $this->getMock('CakeResponse');


try { $controller = $Dispatcher->dispatch($url, $response, array('return' => 1));
$controller = $Dispatcher->dispatch($url, $response, array('return' => 1));
$this->fail('No exception thrown');
} catch (PrivateActionException $e) {
$this->assertEquals(
'Private Action SomePagesController::_protected() is not directly accessible.', $e->getMessage()
);
}
} }


/** /**
* testMissingAction method * testMissingAction method
* *
* @expectedException MissingActionException
* @expectedExceptionMessage Action SomePagesController::home() could not be found.
* @return void * @return void
*/ */
public function testMissingAction() { public function testMissingAction() {
Expand All @@ -734,17 +729,14 @@ public function testMissingAction() {
$url = new CakeRequest('some_pages/home/param:value/param2:value2'); $url = new CakeRequest('some_pages/home/param:value/param2:value2');
$response = $this->getMock('CakeResponse'); $response = $this->getMock('CakeResponse');


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


/** /**
* test that methods declared in Controller are treated as missing methods. * test that methods declared in Controller are treated as missing methods.
* *
* @expectedException MissingActionException
* @expectedExceptionMessage Action SomePagesController::redirect() could not be found.
* @return void * @return void
*/ */
public function testMissingActionFromBaseClassMethods() { public function testMissingActionFromBaseClassMethods() {
Expand All @@ -753,12 +745,7 @@ public function testMissingActionFromBaseClassMethods() {
$url = new CakeRequest('some_pages/redirect/param:value/param2:value2'); $url = new CakeRequest('some_pages/redirect/param:value/param2:value2');
$response = $this->getMock('CakeResponse'); $response = $this->getMock('CakeResponse');


try { $controller = $Dispatcher->dispatch($url, $response, array('return'=> 1));
$controller = $Dispatcher->dispatch($url, $response, array('return'=> 1));
$this->fail('No exception thrown');
} catch (MissingActionException $e) {
$this->assertEquals('Action SomePagesController::redirect() could not be found.', $e->getMessage());
}
} }


/** /**
Expand Down Expand Up @@ -926,9 +913,6 @@ public function testAutomaticPluginDispatch() {
* @return void * @return void
*/ */
public function testAutomaticPluginControllerDispatch() { public function testAutomaticPluginControllerDispatch() {
$_POST = array();
$_SERVER['PHP_SELF'] = '/cake/repo/branches/1.2.x.x/index.php';

$plugins = App::objects('plugin'); $plugins = App::objects('plugin');
$plugins[] = 'MyPlugin'; $plugins[] = 'MyPlugin';
$plugins[] = 'ArticlesTest'; $plugins[] = 'ArticlesTest';
Expand Down Expand Up @@ -1089,64 +1073,55 @@ public function testPluginShortCutUrlsWithControllerThatNeedsToBeLoaded() {
/** /**
* testAutomaticPluginControllerMissingActionDispatch method * testAutomaticPluginControllerMissingActionDispatch method
* *
* @expectedException MissingActionException
* @expectedExceptionMessage Action MyPluginController::not_here() could not be found.
* @return void * @return void
*/ */
public function testAutomaticPluginControllerMissingActionDispatch() { public function testAutomaticPluginControllerMissingActionDispatch() {
$_POST = array();
$_SERVER['PHP_SELF'] = '/cake/repo/branches/1.2.x.x/index.php';

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


$url = new CakeRequest('my_plugin/not_here/param:value/param2:value2'); $url = new CakeRequest('my_plugin/not_here/param:value/param2:value2');
$response = $this->getMock('CakeResponse'); $response = $this->getMock('CakeResponse');


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


/**
* testAutomaticPluginControllerMissingActionDispatch method
*
* @expectedException MissingActionException
* @expectedExceptionMessage Action MyPluginController::param:value() could not be found.
* @return void
*/

public function testAutomaticPluginControllerIndexMissingAction() {
Router::reload(); Router::reload();
$Dispatcher = new TestDispatcher(); $Dispatcher = new TestDispatcher();
$Dispatcher->base = false;


$url = new CakeRequest('my_plugin/param:value/param2:value2'); $url = new CakeRequest('my_plugin/param:value/param2:value2');
try { $response = $this->getMock('CakeResponse');
$controller = $Dispatcher->dispatch($url, $response, array('return'=> 1));
$this->fail('No exception.'); $controller = $Dispatcher->dispatch($url, $response, array('return'=> 1));
} catch (MissingActionException $e) {
$this->assertEquals('Action MyPluginController::param:value() could not be found.', $e->getMessage());
}
} }


/** /**
* testPrefixProtection method * testPrefixProtection method
* *
* @expectedException PrivateActionException
* @expectedExceptionMessage Private Action TestDispatchPagesController::admin_index() is not directly accessible.
* @return void * @return void
*/ */
public function testPrefixProtection() { public function testPrefixProtection() {
$_POST = array();
$_SERVER['PHP_SELF'] = '/cake/repo/branches/1.2.x.x/index.php';

Router::reload(); Router::reload();
Router::connect('/admin/:controller/:action/*', array('prefix'=>'admin'), array('controller', 'action')); Router::connect('/admin/:controller/:action/*', array('prefix'=>'admin'), array('controller', 'action'));


$Dispatcher = new TestDispatcher(); $Dispatcher = new TestDispatcher();


$url = new CakeRequest('test_dispatch_pages/admin_index/param:value/param2:value2'); $url = new CakeRequest('test_dispatch_pages/admin_index/param:value/param2:value2');
$response = $this->getMock('CakeResponse'); $response = $this->getMock('CakeResponse');
try {
$controller = $Dispatcher->dispatch($url, $response, array('return'=> 1)); $controller = $Dispatcher->dispatch($url, $response, array('return'=> 1));
$this->fail('No exception.');
} catch (PrivateActionException $e) {
$this->assertEquals(
'Private Action TestDispatchPagesController::admin_index() is not directly accessible.',
$e->getMessage()
);
}
} }


/** /**
Expand Down Expand Up @@ -1183,7 +1158,6 @@ public function testTestPluginDispatch() {
* @return void * @return void
*/ */
public function testChangingParamsFromBeforeFilter() { public function testChangingParamsFromBeforeFilter() {
$_SERVER['PHP_SELF'] = '/cake/repo/branches/1.2.x.x/index.php';
$Dispatcher = new TestDispatcher(); $Dispatcher = new TestDispatcher();
$response = $this->getMock('CakeResponse'); $response = $this->getMock('CakeResponse');
$url = new CakeRequest('some_posts/index/param:value/param2:value2'); $url = new CakeRequest('some_posts/index/param:value/param2:value2');
Expand Down Expand Up @@ -1398,8 +1372,6 @@ public function testFullPageCachingDispatch() {
Configure::write('Cache.check', true); Configure::write('Cache.check', true);
Configure::write('debug', 2); Configure::write('debug', 2);


$_POST = array();
$_SERVER['PHP_SELF'] = '/';


Router::reload(); Router::reload();
Router::connect('/', array('controller' => 'test_cached_pages', 'action' => 'index')); Router::connect('/', array('controller' => 'test_cached_pages', 'action' => 'index'));
Expand Down

0 comments on commit 5240ede

Please sign in to comment.