Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Deferred error handling #672

Closed
wants to merge 1 commit into from

3 participants

@Domini

When you write a functional test on a method that acquires a resource, does something that leads to a trigger_error(), and then releases the resource, PHPUnit_Util_ErrorHandler will convert the error to exception and throw it right where it happened, so the resource won't be released. If you want to assert whether error was triggered, you use setExpectedException(). But in this case the resource won't be released and test won't be isolated enough.

I've implemented setExpectedExceptionDeferred() to continue execution in case of trigger_error().

@sun

Very belated reply:

When … a functional test … acquires a resource, does something that leads to a trigger_error(), … the resource won't be released.

tearDown() is still invoked for each test (at least in 4.x; can't tell for 3.x, but it's recommended to update anyway).

Normally you should be able to clean up all state and resources in tearDown(), even in case the test threw an error/exception. Did you try that?

@whatthejeff
Collaborator

Sorry for the very delayed response, @Domini. Thanks for your work on this, but I agree with @sun. The solution for this problem is to cleanup in tearDown().

@Domini

Cleanup in tearDown() in cases where deferred error handling was necessary is quite more complex.

Currently PHP supports try {} finally {} so I guess this should be used (i. e. code being tested should handle its side effects whatever happens without relying on 3rd parties), not tearDown() or anything.

Anyway thanks for the late reply.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Sep 27, 2012
  1. @Domini

    Deferred error handling

    Domini authored
This page is out of date. Refresh to see the latest.
Showing with 61 additions and 1 deletion.
  1. +20 −0 PHPUnit/Framework/TestCase.php
  2. +41 −1 PHPUnit/Util/ErrorHandler.php
View
20 PHPUnit/Framework/TestCase.php
@@ -469,6 +469,17 @@ public function setExpectedException($exceptionName, $exceptionMessage = '', $ex
}
/**
+ * @param mixed $exceptionName
+ * @param string $exceptionMessage
+ * @param integer $exceptionCode
+ */
+ public function setExpectedExceptionDeferred($exceptionName, $exceptionMessage = '', $exceptionCode = NULL)
+ {
+ $this->setExpectedException($exceptionName, $exceptionMessage, $exceptionCode);
+ PHPUnit_Util_ErrorHandler::setDeferred();
+ }
+
+ /**
* @since Method available since Release 3.4.0
*/
protected function setExpectedExceptionFromAnnotation()
@@ -961,6 +972,7 @@ protected function runTest()
$this->fail($e->getMessage());
}
+ $e = null;
try {
$testResult = $method->invokeArgs(
$this, array_merge($this->data, $this->dependencyInput)
@@ -968,6 +980,14 @@ protected function runTest()
}
catch (Exception $e) {
+ }
+ if (!$e) {
+ $exceptionStack = PHPUnit_Util_ErrorHandler::getExceptionStack();
+ $e = reset($exceptionStack);
+ }
+ PHPUnit_Util_ErrorHandler::freeExceptionStack();
+ PHPUnit_Util_ErrorHandler::setDeferred(false);
+ if ($e) {
$checkException = FALSE;
if (is_string($this->expectedException)) {
View
42 PHPUnit/Util/ErrorHandler.php
@@ -65,6 +65,8 @@
class PHPUnit_Util_ErrorHandler
{
protected static $errorStack = array();
+ protected static $exceptionStack = array();
+ protected static $deferred = FALSE;
/**
* Returns the error stack.
@@ -77,6 +79,36 @@ public static function getErrorStack()
}
/**
+ * Returns the exception stack
+ *
+ * @return array
+ */
+ public static function getExceptionStack()
+ {
+ return self::$exceptionStack;
+ }
+
+ /**
+ * Frees the exception stack
+ *
+ * @return array
+ */
+ public static function freeExceptionStack()
+ {
+ self::$exceptionStack = array();
+ }
+
+ /**
+ * Defers throwing resulting exceptions
+ *
+ * @param bool $deferred
+ */
+ public static function setDeferred($deferred = TRUE)
+ {
+ self::$deferred = $deferred;
+ }
+
+ /**
* @param integer $errno
* @param string $errstr
* @param string $errfile
@@ -128,6 +160,14 @@ public static function handleError($errno, $errstr, $errfile, $errline)
$exception = 'PHPUnit_Framework_Error';
}
- throw new $exception($errstr, $errno, $errfile, $errline);
+ $Exception = new $exception($errstr, $errno, $errfile, $errline);
+ if (self::$deferred) {
+ self::$exceptionStack[] = $Exception;
+ return TRUE;
+ }
+
+ else {
+ throw $Exception;
+ }
}
}
Something went wrong with that request. Please try again.