Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Updating ConsoleErrorHandler to match ErrorHandler.
Updating test cases for ConsoleErrorHandler.
  • Loading branch information
markstory committed Nov 26, 2010
1 parent 3bc708b commit 74bf455
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 74 deletions.
70 changes: 26 additions & 44 deletions cake/console/libs/console_error_handler.php
Expand Up @@ -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;
}

/**
Expand All @@ -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>Error:</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(
__("<error>%s Error:</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>Error:</error> %s\n%s"),
$this->error->getMessage(),
Expand Down
66 changes: 36 additions & 30 deletions cake/tests/cases/console/libs/console_error_handler.test.php
Expand Up @@ -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 = '<error>Notice Error:</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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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.');
}
}

0 comments on commit 74bf455

Please sign in to comment.