From 8bd0f18a53d26f9a701fea418851668b62c407d5 Mon Sep 17 00:00:00 2001 From: Mark Story Date: Sun, 5 Sep 2010 11:22:39 -0400 Subject: [PATCH] Adding a new line to the ConsoleErrorHandler Refactoring ConsoleErrorHandler test to use dynamic mocks. --- cake/console/console_error_handler.php | 1 + .../console/console_error_handler.test.php | 64 +++++++++---------- 2 files changed, 30 insertions(+), 35 deletions(-) diff --git a/cake/console/console_error_handler.php b/cake/console/console_error_handler.php index 942341bd85f..58ce9527dcf 100644 --- a/cake/console/console_error_handler.php +++ b/cake/console/console_error_handler.php @@ -97,6 +97,7 @@ public function error400($error) { public function error500($error) { $this->_outputMessage(); } + /** * Outputs the exception to STDERR. * diff --git a/cake/tests/cases/console/console_error_handler.test.php b/cake/tests/cases/console/console_error_handler.test.php index 5254134d3d2..a35083bc65e 100644 --- a/cake/tests/cases/console/console_error_handler.test.php +++ b/cake/tests/cases/console/console_error_handler.test.php @@ -19,27 +19,21 @@ */ require CAKE . 'console' . DS . 'console_error_handler.php'; -class TestConsoleErrorHandler extends ConsoleErrorHandler { - public $output = array(); - /** - * Override stderr() so it doesn't do bad things. + * ConsoleErrorHandler Test case. * - * @param string $line - * @return void + * @package cake.tests.cases.console */ - function stderr($line) { - $this->output[] = $line; - } -} - +class ConsoleErrorHandlerTest extends CakeTestCase { /** - * ConsoleErrorHandler Test case. + * Factory method for error handlers with stderr() mocked out. * - * @package cake.tests.cases.console + * @return Mock object */ -class ConsoleErrorHandlerTest extends CakeTestCase { + 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); + + $error->expects($this->once())->method('stderr') + ->with('Missing action'); - $result = $error->output; - $this->assertEquals(1, count($result)); - $this->assertEquals('Missing action', $result[0]); + $error->render(); } /** @@ -63,12 +57,12 @@ function testCakeErrors() { */ 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 @@ function testNonCakeExceptions() { */ 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 @@ function testError404Exception() { */ 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 @@ function testError500Exception() { */ 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.'); }