mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
Updating ConsoleErrorHandler to match ErrorHandler.
Updating test cases for ConsoleErrorHandler.
This commit is contained in:
parent
3bc708ba19
commit
74bf455c49
2 changed files with 62 additions and 74 deletions
|
@ -35,17 +35,19 @@ class ConsoleErrorHandler extends ErrorHandler {
|
||||||
* @var filehandle
|
* @var filehandle
|
||||||
* @access public
|
* @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 Exception $error Exception to handle.
|
||||||
* @param array $messages Error messages
|
* @param array $messages Error messages
|
||||||
*/
|
*/
|
||||||
function __construct($error) {
|
public static function getStderr() {
|
||||||
$this->stderr = new ConsoleOutput('php://stderr');
|
if (empty(self::$stderr)) {
|
||||||
parent::__construct($error);
|
self::$stderr = new ConsoleOutput('php://stderr');
|
||||||
|
}
|
||||||
|
return self::$stderr;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -54,57 +56,37 @@ class ConsoleErrorHandler extends ErrorHandler {
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public static function handleException($exception) {
|
public static function handleException($exception) {
|
||||||
$error = new ConsoleErrorHandler($exception);
|
$stderr = self::getStderr();
|
||||||
$error->render();
|
$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
|
* @return void
|
||||||
*/
|
*/
|
||||||
protected function _getController($exception) {
|
public static function handleError($code, $description, $file = null, $line = null, $context = null) {
|
||||||
return 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
|
* undocumented function
|
||||||
* to prepare urls as they are not relevant for this.
|
|
||||||
*
|
*
|
||||||
* @param $error Exception Exception being handled.
|
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
protected function _cakeError($error) {
|
public function render() {
|
||||||
$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) {
|
|
||||||
$this->stderr->write(sprintf(
|
$this->stderr->write(sprintf(
|
||||||
__("<error>Error:</error> %s\n%s"),
|
__("<error>Error:</error> %s\n%s"),
|
||||||
$this->error->getMessage(),
|
$this->error->getMessage(),
|
||||||
|
|
|
@ -27,14 +27,36 @@ require_once CONSOLE_LIBS . 'console_error_handler.php';
|
||||||
class ConsoleErrorHandlerTest extends CakeTestCase {
|
class ConsoleErrorHandlerTest extends CakeTestCase {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Factory method for error handlers with stderr() mocked out.
|
* setup, create mocks
|
||||||
*
|
*
|
||||||
* @return Mock object
|
* @return Mock object
|
||||||
*/
|
*/
|
||||||
function getErrorHandler($exception) {
|
function setUp() {
|
||||||
$error = new ConsoleErrorHandler($exception);
|
parent::setUp();
|
||||||
$error->stderr = $this->getMock('ConsoleOutput', array(), array(), '', false);
|
ConsoleErrorHandler::$stderr = $this->getMock('ConsoleOutput', array(), array(), '', false);
|
||||||
return $error;
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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() {
|
function testCakeErrors() {
|
||||||
$exception = new MissingActionException('Missing action');
|
$exception = new MissingActionException('Missing action');
|
||||||
$error = $this->getErrorHandler($exception);
|
ConsoleErrorHandler::$stderr->expects($this->once())->method('write')
|
||||||
|
|
||||||
$error->stderr->expects($this->once())->method('write')
|
|
||||||
->with($this->stringContains('Missing action'));
|
->with($this->stringContains('Missing action'));
|
||||||
|
|
||||||
$error->render();
|
ConsoleErrorHandler::handleException($exception);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -59,12 +79,11 @@ class ConsoleErrorHandlerTest extends CakeTestCase {
|
||||||
*/
|
*/
|
||||||
function testNonCakeExceptions() {
|
function testNonCakeExceptions() {
|
||||||
$exception = new InvalidArgumentException('Too many parameters.');
|
$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.'));
|
->with($this->stringContains('Too many parameters.'));
|
||||||
|
|
||||||
$error->render();
|
ConsoleErrorHandler::handleException($exception);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -74,12 +93,11 @@ 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 = $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.'));
|
->with($this->stringContains('dont use me in cli.'));
|
||||||
|
|
||||||
$error->render();
|
ConsoleErrorHandler::handleException($exception);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -89,23 +107,11 @@ 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 = $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.'));
|
->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.');
|
|
||||||
}
|
|
||||||
}
|
}
|
Loading…
Reference in a new issue