Refactoring test case to use a dataprovider for the uniform/copy paste test cases.

This commit is contained in:
mark_story 2010-08-29 21:53:44 -04:00
parent f1164c93d6
commit efaa2e1177

View file

@ -438,256 +438,155 @@ class ErrorHandlerTest extends CakeTestCase {
$this->assertPattern('/BlueberryComponent/', $result); $this->assertPattern('/BlueberryComponent/', $result);
} }
/* TODO: Integration test that needs to be moved
ob_start();
$dispatcher = new Dispatcher('/blueberry/inexistent');
$result = ob_get_clean();
$this->assertPattern('/<h2>Missing Method in BlueberryController<\/h2>/', $result);
$this->assertPattern('/<em>BlueberryController::<\/em><em>inexistent\(\)<\/em>/', $result);
$this->assertNoPattern('/Location: (.*)\/users\/login/', $result);
$this->assertNoPattern('/Stopped with status: 0/', $result);
*/
/** /**
* testMissingAction method * Returns an array of tests to run for the various CakeException classes.
* *
* @access public
* @return void * @return void
*/ */
function testMissingAction() { public static function testProvider() {
$exception = new MissingActionException(array('controller' => 'PostsController', 'action' => 'index')); return array(
$ErrorHandler = new ErrorHandler($exception); array(
new MissingActionException(array('controller' => 'PostsController', 'action' => 'index')),
ob_start(); array(
$ErrorHandler->render(); '/<h2>Missing Method in PostsController<\/h2>/',
$result = ob_get_clean(); '/<em>PostsController::<\/em><em>index\(\)<\/em>/'
),
$this->assertPattern('/<h2>Missing Method in PostsController<\/h2>/', $result); 404
$this->assertPattern('/<em>PostsController::<\/em><em>index\(\)<\/em>/', $result); ),
array(
/* TODO: Integration test that needs to be moved new PrivateActionException(array('controller' => 'PostsController' , 'action' => '_secretSauce')),
ob_start(); array(
$dispatcher = new Dispatcher('/blueberry/inexistent'); '/<h2>Private Method in PostsController<\/h2>/',
$result = ob_get_clean(); '/<em>PostsController::<\/em><em>_secretSauce\(\)<\/em>/'
$this->assertPattern('/<h2>Missing Method in BlueberryController<\/h2>/', $result); ),
$this->assertPattern('/<em>BlueberryController::<\/em><em>inexistent\(\)<\/em>/', $result); 404
$this->assertNoPattern('/Location: (.*)\/users\/login/', $result); ),
$this->assertNoPattern('/Stopped with status: 0/', $result); array(
*/ new MissingTableException(array('table' => 'articles', 'class' => 'Article')),
array(
'/<h2>Missing Database Table<\/h2>/',
'/table <em>articles<\/em> for model <em>Article<\/em>/'
),
500
),
array(
new MissingDatabaseException(array('connection' => 'default')),
array(
'/<h2>Missing Database Connection<\/h2>/',
'/Confirm you have created the file/'
),
500
),
array(
new MissingViewException(array('file' => '/posts/about.ctp')),
array(
"/posts\/about.ctp/"
),
500
),
array(
new MissingLayoutException(array('file' => 'layouts/my_layout.ctp')),
array(
"/Missing Layout/",
"/layouts\/my_layout.ctp/"
),
500
),
array(
new MissingConnectionException(array('class' => 'Article')),
array(
'/<h2>Missing Database Connection<\/h2>/',
'/Article requires a database connection/'
),
500
),
array(
new MissingHelperFileException(array('file' => 'my_custom.php', 'class' => 'MyCustomHelper')),
array(
'/<h2>Missing Helper File<\/h2>/',
'/Create the class below in file:/',
'/(\/|\\\)my_custom.php/'
),
500
),
array(
new MissingHelperClassException(array('file' => 'my_custom.php', 'class' => 'MyCustomHelper')),
array(
'/<h2>Missing Helper Class<\/h2>/',
'/The helper class <em>MyCustomHelper<\/em> can not be found or does not exist./',
'/(\/|\\\)my_custom.php/',
),
500
),
array(
new MissingBehaviorFileException(array('file' => 'my_custom.php', 'class' => 'MyCustomBehavior')),
array(
'/<h2>Missing Behavior File<\/h2>/',
'/Create the class below in file:/',
'/(\/|\\\)my_custom.php/',
),
500
),
array(
new MissingBehaviorClassException(array('file' => 'my_custom.php', 'class' => 'MyCustomBehavior')),
array(
'/The behavior class <em>MyCustomBehavior<\/em> can not be found or does not exist./',
'/(\/|\\\)my_custom.php/'
),
500
),
array(
new MissingComponentFileException(array('file' => 'sidebox.php', 'class' => 'SideboxComponent')),
array(
'/<h2>Missing Component File<\/h2>/',
'/Create the class <em>SideboxComponent<\/em> in file:/',
'/(\/|\\\)sidebox.php/'
),
500
),
array(
new MissingComponentClassException(array('file' => 'sidebox.php', 'class' => 'SideboxComponent')),
array(
'/<h2>Missing Component Class<\/h2>/',
'/Create the class <em>SideboxComponent<\/em> in file:/',
'/(\/|\\\)sidebox.php/'
),
500
)
);
} }
/** /**
* testPrivateAction method * Test the various CakeException sub classes
* *
* @access public * @dataProvider testProvider
* @return void * @return void
*/ */
function testPrivateAction() { function testCakeExceptionHandling($exception, $patterns, $code) {
$exception = new PrivateActionException(array('controller' => 'PostsController' , 'action' => '_secretSauce'));
$ErrorHandler = new ErrorHandler($exception);
ob_start();
$ErrorHandler->render();
$result = ob_get_clean();
$this->assertPattern('/<h2>Private Method in PostsController<\/h2>/', $result);
$this->assertPattern('/<em>PostsController::<\/em><em>_secretSauce\(\)<\/em>/', $result);
}
/**
* testMissingTable method
*
* @access public
* @return void
*/
function testMissingTable() {
$exception = new MissingTableException(array('table' => 'articles', 'class' => 'Article'));
$ErrorHandler = new ErrorHandler($exception); $ErrorHandler = new ErrorHandler($exception);
$ErrorHandler->controller->response = $this->getMock('CakeResponse', array('statusCode')); $ErrorHandler->controller->response = $this->getMock('CakeResponse', array('statusCode'));
$ErrorHandler->controller->response->expects($this->once())->method('statusCode')->with(500); $ErrorHandler->controller->response->expects($this->once())
->method('statusCode')
->with($code);
ob_start(); ob_start();
$ErrorHandler->render(); $ErrorHandler->render();
$result = ob_get_clean(); $result = ob_get_clean();
$this->assertPattern('/<h2>Missing Database Table<\/h2>/', $result); foreach ($patterns as $pattern) {
$this->assertPattern('/table <em>articles<\/em> for model <em>Article<\/em>/', $result); $this->assertPattern($pattern, $result);
}
} }
/**
* testMissingDatabase method
*
* @access public
* @return void
*/
function testMissingDatabase() {
$exception = new MissingDatabaseException(array('connection' => 'default'));
$ErrorHandler = new ErrorHandler($exception);
$ErrorHandler->controller->response = $this->getMock('CakeResponse', array('statusCode'));
$ErrorHandler->controller->response->expects($this->once())->method('statusCode')->with(500);
ob_start();
$ErrorHandler->render();
$result = ob_get_clean();
$this->assertPattern('/<h2>Missing Database Connection<\/h2>/', $result);
$this->assertPattern('/Confirm you have created the file/', $result);
}
/**
* testMissingView method
*
* @access public
* @return void
*/
function testMissingView() {
$exception = new MissingViewException(array('file' => '/posts/about.ctp'));
$ErrorHandler = new ErrorHandler($exception);
ob_start();
$ErrorHandler->render();
$result = ob_get_clean();
$this->assertPattern("/posts\/about.ctp/", $result);
}
/**
* testMissingLayout method
*
* @access public
* @return void
*/
function testMissingLayout() {
$exception = new MissingLayoutException(array('file' => 'layouts/my_layout.ctp'));
$ErrorHandler = new ErrorHandler($exception);
ob_start();
$ErrorHandler->render();
$result = ob_get_clean();
$this->assertPattern("/Missing Layout/", $result);
$this->assertPattern("/layouts\/my_layout.ctp/", $result);
}
/**
* testMissingConnection method
*
* @access public
* @return void
*/
function testMissingConnection() {
$exception = new MissingConnectionException(array('class' => 'Article'));
$ErrorHandler = new ErrorHandler($exception);
ob_start();
$ErrorHandler->render();
$result = ob_get_clean();
$this->assertPattern('/<h2>Missing Database Connection<\/h2>/', $result);
$this->assertPattern('/Article requires a database connection/', $result);
}
/**
* testMissingHelperFile method
*
* @access public
* @return void
*/
function testMissingHelperFile() {
$exception = new MissingHelperFileException(array('file' => 'my_custom.php', 'class' => 'MyCustomHelper'));
$ErrorHandler = new ErrorHandler($exception);
ob_start();
$ErrorHandler->render();
$result = ob_get_clean();
$this->assertPattern('/<h2>Missing Helper File<\/h2>/', $result);
$this->assertPattern('/Create the class below in file:/', $result);
$this->assertPattern('/(\/|\\\)my_custom.php/', $result);
}
/**
* testMissingHelperClass method
*
* @access public
* @return void
*/
function testMissingHelperClass() {
$exception = new MissingHelperClassException(array('file' => 'my_custom.php', 'class' => 'MyCustomHelper'));
$ErrorHandler = new ErrorHandler($exception);
ob_start();
$ErrorHandler->render();
$result = ob_get_clean();
$this->assertPattern('/<h2>Missing Helper Class<\/h2>/', $result);
$this->assertPattern('/The helper class <em>MyCustomHelper<\/em> can not be found or does not exist./', $result);
$this->assertPattern('/(\/|\\\)my_custom.php/', $result);
}
/**
* test missingBehaviorFile method
*
* @access public
* @return void
*/
function testMissingBehaviorFile() {
$exception = new MissingBehaviorFileException(array('file' => 'my_custom.php', 'class' => 'MyCustomBehavior'));
$ErrorHandler = new ErrorHandler($exception);
ob_start();
$ErrorHandler->render();
$result = ob_get_clean();
$this->assertPattern('/<h2>Missing Behavior File<\/h2>/', $result);
$this->assertPattern('/Create the class below in file:/', $result);
$this->assertPattern('/(\/|\\\)my_custom.php/', $result);
}
/**
* test MissingBehaviorClass method
*
* @access public
* @return void
*/
function testMissingBehaviorClass() {
$exception = new MissingBehaviorClassException(array('file' => 'my_custom.php', 'class' => 'MyCustomBehavior'));
$ErrorHandler = new ErrorHandler($exception);
ob_start();
$ErrorHandler->render();
$result = ob_get_clean();
$this->assertPattern('/The behavior class <em>MyCustomBehavior<\/em> can not be found or does not exist./', $result);
$this->assertPattern('/(\/|\\\)my_custom.php/', $result);
}
/**
* testMissingComponentFile method
*
* @access public
* @return void
*/
function testMissingComponentFile() {
$exception = new MissingComponentFileException(array('file' => 'sidebox.php', 'class' => 'SideboxComponent'));
$ErrorHandler = new ErrorHandler($exception);
ob_start();
$ErrorHandler->render();
$result = ob_get_clean();
$this->assertPattern('/<h2>Missing Component File<\/h2>/', $result);
$this->assertPattern('/Create the class <em>SideboxComponent<\/em> in file:/', $result);
$this->assertPattern('/(\/|\\\)sidebox.php/', $result);
}
/**
* testMissingComponentClass method
*
* @access public
* @return void
*/
function testMissingComponentClass() {
$exception = new MissingComponentClassException(array('file' => 'sidebox.php', 'class' => 'SideboxComponent'));
$ErrorHandler = new ErrorHandler($exception);
ob_start();
$ErrorHandler->render();
$result = ob_get_clean();
$this->assertPattern('/<h2>Missing Component Class<\/h2>/', $result);
$this->assertPattern('/Create the class <em>SideboxComponent<\/em> in file:/', $result);
$this->assertPattern('/(\/|\\\)sidebox.php/', $result);
}
} }