Adding a new line to the ConsoleErrorHandler

Refactoring ConsoleErrorHandler test to use dynamic mocks.
This commit is contained in:
Mark Story 2010-09-05 11:22:39 -04:00
parent 9fee81cda0
commit 8bd0f18a53
2 changed files with 31 additions and 36 deletions

View file

@ -97,6 +97,7 @@ class ConsoleErrorHandler extends ErrorHandler {
public function error500($error) {
$this->_outputMessage();
}
/**
* Outputs the exception to STDERR.
*

View file

@ -19,21 +19,6 @@
*/
require CAKE . 'console' . DS . 'console_error_handler.php';
class TestConsoleErrorHandler extends ConsoleErrorHandler {
public $output = array();
/**
* Override stderr() so it doesn't do bad things.
*
* @param string $line
* @return void
*/
function stderr($line) {
$this->output[] = $line;
}
}
/**
* ConsoleErrorHandler Test case.
*
@ -41,6 +26,15 @@ class TestConsoleErrorHandler extends ConsoleErrorHandler {
*/
class ConsoleErrorHandlerTest extends CakeTestCase {
/**
* Factory method for error handlers with stderr() mocked out.
*
* @return Mock object
*/
function getErrorHandler($exception) {
return $this->getMock('ConsoleErrorHandler', array('stderr'), array($exception));
}
/**
* test that the console error handler can deal with CakeExceptions.
*
@ -48,12 +42,12 @@ class ConsoleErrorHandlerTest extends CakeTestCase {
*/
function testCakeErrors() {
$exception = new MissingActionException('Missing action');
$error = new TestConsoleErrorHandler($exception);
$error->render();
$error = $this->getErrorHandler($exception);
$result = $error->output;
$this->assertEquals(1, count($result));
$this->assertEquals('Missing action', $result[0]);
$error->expects($this->once())->method('stderr')
->with('Missing action');
$error->render();
}
/**
@ -63,12 +57,12 @@ class ConsoleErrorHandlerTest extends CakeTestCase {
*/
function testNonCakeExceptions() {
$exception = new InvalidArgumentException('Too many parameters.');
$error = new TestConsoleErrorHandler($exception);
$error->render();
$error = $this->getErrorHandler($exception);
$result = $error->output;
$this->assertEquals(1, count($result));
$this->assertEquals('Too many parameters.', $result[0]);
$error->expects($this->once())->method('stderr')
->with('Too many parameters.');
$error->render();
}
/**
@ -78,12 +72,12 @@ class ConsoleErrorHandlerTest extends CakeTestCase {
*/
function testError404Exception() {
$exception = new NotFoundException('dont use me in cli.');
$error = new TestConsoleErrorHandler($exception);
$error->render();
$error = $this->getErrorHandler($exception);
$result = $error->output;
$this->assertEquals(1, count($result));
$this->assertEquals('dont use me in cli.', $result[0]);
$error->expects($this->once())->method('stderr')
->with('dont use me in cli.');
$error->render();
}
/**
@ -93,12 +87,12 @@ class ConsoleErrorHandlerTest extends CakeTestCase {
*/
function testError500Exception() {
$exception = new InternalErrorException('dont use me in cli.');
$error = new TestConsoleErrorHandler($exception);
$error->render();
$error = $this->getErrorHandler($exception);
$result = $error->output;
$this->assertEquals(1, count($result));
$this->assertEquals('dont use me in cli.', $result[0]);
$error->expects($this->once())->method('stderr')
->with('dont use me in cli.');
$error->render();
}
/**
@ -108,7 +102,7 @@ class ConsoleErrorHandlerTest extends CakeTestCase {
*/
function testStdErrFilehandle() {
$exception = new InternalErrorException('dont use me in cli.');
$error = new TestConsoleErrorHandler($exception);
$error = new ConsoleErrorHandler($exception);
$this->assertTrue(is_resource($error->stderr), 'No handle.');
}