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) { public function error500($error) {
$this->_outputMessage(); $this->_outputMessage();
} }
/** /**
* Outputs the exception to STDERR. * Outputs the exception to STDERR.
* *

View file

@ -19,21 +19,6 @@
*/ */
require CAKE . 'console' . DS . 'console_error_handler.php'; 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. * ConsoleErrorHandler Test case.
* *
@ -41,6 +26,15 @@ class TestConsoleErrorHandler extends ConsoleErrorHandler {
*/ */
class ConsoleErrorHandlerTest extends CakeTestCase { 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. * test that the console error handler can deal with CakeExceptions.
* *
@ -48,12 +42,12 @@ class ConsoleErrorHandlerTest extends CakeTestCase {
*/ */
function testCakeErrors() { function testCakeErrors() {
$exception = new MissingActionException('Missing action'); $exception = new MissingActionException('Missing action');
$error = new TestConsoleErrorHandler($exception); $error = $this->getErrorHandler($exception);
$error->render();
$result = $error->output; $error->expects($this->once())->method('stderr')
$this->assertEquals(1, count($result)); ->with('Missing action');
$this->assertEquals('Missing action', $result[0]);
$error->render();
} }
/** /**
@ -63,12 +57,12 @@ class ConsoleErrorHandlerTest extends CakeTestCase {
*/ */
function testNonCakeExceptions() { function testNonCakeExceptions() {
$exception = new InvalidArgumentException('Too many parameters.'); $exception = new InvalidArgumentException('Too many parameters.');
$error = new TestConsoleErrorHandler($exception); $error = $this->getErrorHandler($exception);
$error->render();
$result = $error->output; $error->expects($this->once())->method('stderr')
$this->assertEquals(1, count($result)); ->with('Too many parameters.');
$this->assertEquals('Too many parameters.', $result[0]);
$error->render();
} }
/** /**
@ -78,12 +72,12 @@ class ConsoleErrorHandlerTest extends CakeTestCase {
*/ */
function testError404Exception() { function testError404Exception() {
$exception = new NotFoundException('dont use me in cli.'); $exception = new NotFoundException('dont use me in cli.');
$error = new TestConsoleErrorHandler($exception); $error = $this->getErrorHandler($exception);
$error->render();
$result = $error->output; $error->expects($this->once())->method('stderr')
$this->assertEquals(1, count($result)); ->with('dont use me in cli.');
$this->assertEquals('dont use me in cli.', $result[0]);
$error->render();
} }
/** /**
@ -93,12 +87,12 @@ class ConsoleErrorHandlerTest extends CakeTestCase {
*/ */
function testError500Exception() { function testError500Exception() {
$exception = new InternalErrorException('dont use me in cli.'); $exception = new InternalErrorException('dont use me in cli.');
$error = new TestConsoleErrorHandler($exception); $error = $this->getErrorHandler($exception);
$error->render();
$result = $error->output; $error->expects($this->once())->method('stderr')
$this->assertEquals(1, count($result)); ->with('dont use me in cli.');
$this->assertEquals('dont use me in cli.', $result[0]);
$error->render();
} }
/** /**
@ -108,7 +102,7 @@ class ConsoleErrorHandlerTest extends CakeTestCase {
*/ */
function testStdErrFilehandle() { function testStdErrFilehandle() {
$exception = new InternalErrorException('dont use me in cli.'); $exception = new InternalErrorException('dont use me in cli.');
$error = new TestConsoleErrorHandler($exception); $error = new ConsoleErrorHandler($exception);
$this->assertTrue(is_resource($error->stderr), 'No handle.'); $this->assertTrue(is_resource($error->stderr), 'No handle.');
} }