From 74bf455c4963e58898df1e63993852db33b6e7da Mon Sep 17 00:00:00 2001 From: mark_story Date: Sun, 14 Nov 2010 22:47:35 -0500 Subject: [PATCH] Updating ConsoleErrorHandler to match ErrorHandler. Updating test cases for ConsoleErrorHandler. --- cake/console/libs/console_error_handler.php | 70 +++++++------------ .../libs/console_error_handler.test.php | 66 +++++++++-------- 2 files changed, 62 insertions(+), 74 deletions(-) diff --git a/cake/console/libs/console_error_handler.php b/cake/console/libs/console_error_handler.php index 5dda75a3cab..fc841476729 100644 --- a/cake/console/libs/console_error_handler.php +++ b/cake/console/libs/console_error_handler.php @@ -35,17 +35,19 @@ class ConsoleErrorHandler extends ErrorHandler { * @var filehandle * @access public */ - public $stderr; + public static $stderr; /** - * Class constructor. + * Get the stderr object for the console error handling. * * @param Exception $error Exception to handle. * @param array $messages Error messages */ - function __construct($error) { - $this->stderr = new ConsoleOutput('php://stderr'); - parent::__construct($error); + public static function getStderr() { + if (empty(self::$stderr)) { + self::$stderr = new ConsoleOutput('php://stderr'); + } + return self::$stderr; } /** @@ -54,57 +56,37 @@ function __construct($error) { * @return void */ public static function handleException($exception) { - $error = new ConsoleErrorHandler($exception); - $error->render(); - } - -/** - * Do nothing, no controllers are needed in the console. - * - * @return void - */ - protected function _getController($exception) { - return null; - } - -/** - * Overwrite how _cakeError behaves for console. There is no reason - * to prepare urls as they are not relevant for this. - * - * @param $error Exception Exception being handled. - * @return void - */ - protected function _cakeError($error) { - $this->_outputMessage(); - } - -/** - * Override error404 method - * - * @param Exception $error Exception - * @return void - */ - public function error400($error) { - $this->_outputMessage(); + $stderr = self::getStderr(); + $stderr->write(sprintf( + __("Error: %s\n%s"), + $exception->getMessage(), + $exception->getTraceAsString() + )); } /** - * Override error500 method + * Handle errors in the console environment. * - * @param Exception $error Exception * @return void */ - public function error500($error) { - $this->_outputMessage(); + public static function handleError($code, $description, $file = null, $line = null, $context = null) { + $errorConfig = Configure::read('Error'); + if (isset($errorConfig['level']) && ($code & ~$errorConfig['level'])) { + return; + } + $stderr = self::getStderr(); + list($name, $log) = self::_mapErrorCode($code); + $stderr->write(sprintf( + __("%s Error: %s in [%s, line %s]\n"), $name, $description, $file, $line + )); } /** - * Outputs the exception to STDERR. + * undocumented function * - * @param string $template The name of the template to render. * @return void */ - public function _outputMessage($template = null) { + public function render() { $this->stderr->write(sprintf( __("Error: %s\n%s"), $this->error->getMessage(), diff --git a/cake/tests/cases/console/libs/console_error_handler.test.php b/cake/tests/cases/console/libs/console_error_handler.test.php index 81c3b5aae70..0b0719a144f 100644 --- a/cake/tests/cases/console/libs/console_error_handler.test.php +++ b/cake/tests/cases/console/libs/console_error_handler.test.php @@ -27,14 +27,36 @@ class ConsoleErrorHandlerTest extends CakeTestCase { /** - * Factory method for error handlers with stderr() mocked out. + * setup, create mocks * * @return Mock object */ - function getErrorHandler($exception) { - $error = new ConsoleErrorHandler($exception); - $error->stderr = $this->getMock('ConsoleOutput', array(), array(), '', false); - return $error; + function setUp() { + parent::setUp(); + ConsoleErrorHandler::$stderr = $this->getMock('ConsoleOutput', array(), array(), '', false); + } + +/** + * teardown + * + * @return void + */ + function tearDown() { + parent::tearDown(); + ConsoleErrorHandler::$stderr = null; + } + +/** + * test that the console error handler can deal with CakeExceptions. + * + * @return void + */ + function testHandleError() { + $content = 'Notice Error: This is a notice error in [/some/file, line 275]'; + ConsoleErrorHandler::$stderr->expects($this->once())->method('write') + ->with($content); + + ConsoleErrorHandler::handleError(E_NOTICE, 'This is a notice error', '/some/file', 275); } /** @@ -44,12 +66,10 @@ function getErrorHandler($exception) { */ function testCakeErrors() { $exception = new MissingActionException('Missing action'); - $error = $this->getErrorHandler($exception); - - $error->stderr->expects($this->once())->method('write') + ConsoleErrorHandler::$stderr->expects($this->once())->method('write') ->with($this->stringContains('Missing action')); - $error->render(); + ConsoleErrorHandler::handleException($exception); } /** @@ -59,12 +79,11 @@ function testCakeErrors() { */ function testNonCakeExceptions() { $exception = new InvalidArgumentException('Too many parameters.'); - $error = $this->getErrorHandler($exception); - $error->stderr->expects($this->once())->method('write') + ConsoleErrorHandler::$stderr->expects($this->once())->method('write') ->with($this->stringContains('Too many parameters.')); - $error->render(); + ConsoleErrorHandler::handleException($exception); } /** @@ -74,12 +93,11 @@ function testNonCakeExceptions() { */ function testError404Exception() { $exception = new NotFoundException('dont use me in cli.'); - $error = $this->getErrorHandler($exception); - - $error->stderr->expects($this->once())->method('write') + + ConsoleErrorHandler::$stderr->expects($this->once())->method('write') ->with($this->stringContains('dont use me in cli.')); - $error->render(); + ConsoleErrorHandler::handleException($exception); } /** @@ -89,23 +107,11 @@ function testError404Exception() { */ function testError500Exception() { $exception = new InternalErrorException('dont use me in cli.'); - $error = $this->getErrorHandler($exception); - $error->stderr->expects($this->once())->method('write') + ConsoleErrorHandler::$stderr->expects($this->once())->method('write') ->with($this->stringContains('dont use me in cli.')); - $error->render(); + ConsoleErrorHandler::handleException($exception); } -/** - * test that ConsoleErrorHandler has a stderr file handle. - * - * @return void - */ - function testStdErrFilehandle() { - $exception = new InternalErrorException('dont use me in cli.'); - $error = new ConsoleErrorHandler($exception); - - $this->assertType('ConsoleOutput', $error->stderr, 'No handle.'); - } } \ No newline at end of file