From 1a89a3cb9d01594c6028b0ad6cbf14843155e2b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Renan=20Gon=C3=A7alves?= Date: Thu, 26 Jun 2014 13:12:28 +0200 Subject: [PATCH] Exiting with 1 when Exception::getCode() returns non-integer values. From php.net/exception.getcode > Returns the exception code as integer in Exception but possibly as other type in Exception descendants (for example as string in PDOException). --- lib/Cake/Console/ConsoleErrorHandler.php | 4 ++- .../Case/Console/ConsoleErrorHandlerTest.php | 27 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/lib/Cake/Console/ConsoleErrorHandler.php b/lib/Cake/Console/ConsoleErrorHandler.php index aa5c464f9..13f95af7a 100644 --- a/lib/Cake/Console/ConsoleErrorHandler.php +++ b/lib/Cake/Console/ConsoleErrorHandler.php @@ -58,7 +58,9 @@ class ConsoleErrorHandler { $exception->getMessage(), $exception->getTraceAsString() )); - return $this->_stop($exception->getCode() ? $exception->getCode() : 1); + $code = $exception->getCode(); + $code = ($code && is_integer($code)) ? $code : 1; + return $this->_stop($code); } /** diff --git a/lib/Cake/Test/Case/Console/ConsoleErrorHandlerTest.php b/lib/Cake/Test/Case/Console/ConsoleErrorHandlerTest.php index 7d640a862..83cae8095 100644 --- a/lib/Cake/Test/Case/Console/ConsoleErrorHandlerTest.php +++ b/lib/Cake/Test/Case/Console/ConsoleErrorHandlerTest.php @@ -147,4 +147,31 @@ class ConsoleErrorHandlerTest extends CakeTestCase { $this->Error->handleException($exception); } +/** + * test a exception with non-integer code + * + * @return void + */ + public function testNonIntegerExceptionCode() { + if (PHP_VERSION_ID < 50300) { + $this->markTestSkipped('ReflectionProperty::setAccessible() is available since 5.3'); + } + + $exception = new Exception('Non-integer exception code'); + + $class = new ReflectionClass('Exception'); + $property = $class->getProperty('code'); + $property->setAccessible(true); + $property->setValue($exception, '42S22'); + + ConsoleErrorHandler::$stderr->expects($this->once())->method('write') + ->with($this->stringContains('Non-integer exception code')); + + $this->Error->expects($this->once()) + ->method('_stop') + ->with(1); + + $this->Error->handleException($exception); + } + }