Updating ConsoleErrorHandler to match ErrorHandler.

Updating test cases for ConsoleErrorHandler.
This commit is contained in:
mark_story 2010-11-14 22:47:35 -05:00
parent 3bc708ba19
commit 74bf455c49
2 changed files with 62 additions and 74 deletions

View file

@ -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 @@ class ConsoleErrorHandler extends ErrorHandler {
* @return void
*/
public static function handleException($exception) {
$error = new ConsoleErrorHandler($exception);
$error->render();
$stderr = self::getStderr();
$stderr->write(sprintf(
__("<error>Error:</error> %s\n%s"),
$exception->getMessage(),
$exception->getTraceAsString()
));
}
/**
* Do nothing, no controllers are needed in the console.
* Handle errors in the console environment.
*
* @return void
*/
protected function _getController($exception) {
return null;
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
));
}
/**
* Overwrite how _cakeError behaves for console. There is no reason
* to prepare urls as they are not relevant for this.
* undocumented function
*
* @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();
}
/**
* Override error500 method
*
* @param Exception $error Exception
* @return void
*/
public function error500($error) {
$this->_outputMessage();
}
/**
* Outputs the exception to STDERR.
*
* @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(),

View file

@ -27,14 +27,36 @@ require_once CONSOLE_LIBS . 'console_error_handler.php';
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);
}
/**
@ -44,12 +66,10 @@ class ConsoleErrorHandlerTest extends CakeTestCase {
*/
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 @@ class ConsoleErrorHandlerTest extends CakeTestCase {
*/
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 @@ class ConsoleErrorHandlerTest extends CakeTestCase {
*/
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 @@ class ConsoleErrorHandlerTest extends CakeTestCase {
*/
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.');
}
}