Browse Source

Ensure ConsoleErrorHandler exits with code 1 for exceptions with string codes.

Code was messed up during 2.6 => 3.0 merge.
ADmad 11 years ago
parent
commit
b31fb19dfd

+ 17 - 0
src/Console/ConsoleErrorHandler.php

@@ -51,6 +51,23 @@ class ConsoleErrorHandler extends BaseErrorHandler {
 	}
 
 /**
+ * Handle errors in the console environment. Writes errors to stderr,
+ * and logs messages if Configure::read('debug') is false.
+ *
+ * @param \Exception $exception Exception instance.
+ * @return void
+ * @throws Exception When renderer class not found
+ * @see http://php.net/manual/en/function.set-exception-handler.php
+ */
+	public function handleException(\Exception $exception) {
+		$this->_displayException($exception);
+		$this->_logException($exception);
+		$code = $exception->getCode();
+		$code = ($code && is_int($code)) ? $code : 1;
+		$this->_stop($code);
+	}
+
+/**
  * Prints an exception to stderr.
  *
  * @param \Exception $exception The exception to handle

+ 3 - 7
tests/TestCase/Console/ConsoleErrorHandlerTest.php

@@ -141,18 +141,14 @@ class ConsoleErrorHandlerTest extends TestCase {
  * @return void
  */
 	public function testNonIntegerExceptionCode() {
-		if (PHP_VERSION_ID < 50300) {
-			$this->markTestSkipped('ReflectionProperty::setAccessible() is available since 5.3');
-		}
+		$exception = new Error\Exception('Non-integer exception code');
 
-		$exception = new Exception('Non-integer exception code');
-
-		$class = new ReflectionClass('Exception');
+		$class = new \ReflectionClass('Exception');
 		$property = $class->getProperty('code');
 		$property->setAccessible(true);
 		$property->setValue($exception, '42S22');
 
-		ConsoleErrorHandler::$stderr->expects($this->once())->method('write')
+		$this->stderr->expects($this->once())->method('write')
 			->with($this->stringContains('Non-integer exception code'));
 
 		$this->Error->expects($this->once())