Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Exception handling fix #88

Closed
wants to merge 1 commit into from

7 participants

@Seldaek

See the commit description, this is a really annoying side effect which has led to bad code making it through the tests, and then failing in real conditions.

@Seldaek Seldaek setExpectedException should not catch exceptions coming from PHPUnit'…
…s error handler

Too often tests are believed to work because they catch an Exception, while they are in fact just catching a PHP warning/notice/error that has been converted to an Exception by PHPUnit
e9a352e
@sebastianbergmann

Closed by 4ad409a.

@visor visor referenced this pull request from a commit
@sebastianbergmann Close #88. 54d1cad
@ardell ardell referenced this pull request from a commit in apinstein/phpunit
@sebastianbergmann Close #88. 4ad409a
@convissor

This change is problematic. Now one can't write a simple test case for PHP core functions (like DateTime) that throw plain old Exceptions. Let alone, this change breaks BC, so now existing code out there that did $this->setExpectedException('Exception'); no longer works and we all have to rewrite the tests with try/catch blocks.

@slusarz

Agreed... this commit was not thought out very well. And it doesn't make any sense in relation to the problem it allegedly solves. When I explictly say I am expecting an Exception to be thrown, I'm expecting just that: an Exception to be thrown. This shouldn't catch an ErrorException, an Extended_Exception, or anything else. So I don't see the issue. (I am guessing PHPUnit is not throwing plain Exceptions when it catches things like syntax/runtime errors.)

Not to mention this is blatantly counterintuitive. And not mentioned anywhere in the documentation.

@sebastianbergmann

You are beating a dead horse. Expecting the generic Exception class is possible again in the master branch (which will become PHPUnit 3.7). That does not mean, however, that using the generic Exception class in your code is suddenly a good idea.

@lathspell

Any chance that this fix of the fix gets into the next 3.6.x release?

@sebastianbergmann

No.

@mattcg

@lathspell You could always use the Standard PHP Library Exceptions if you think writing exception classes is overkill. They're all allowed by PHPUnit, even pre-3.7.

@greglamb greglamb referenced this pull request from a commit in greglamb/phpunit
@sebastianbergmann Close #88. f9466a6
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 13, 2010
  1. @Seldaek

    setExpectedException should not catch exceptions coming from PHPUnit'…

    Seldaek authored
    …s error handler
    
    Too often tests are believed to work because they catch an Exception, while they are in fact just catching a PHP warning/notice/error that has been converted to an Exception by PHPUnit
This page is out of date. Refresh to see the latest.
View
1  PHPUnit/Framework/TestCase.php
@@ -740,6 +740,7 @@ protected function runTest()
catch (Exception $e) {
if (!$e instanceof PHPUnit_Framework_IncompleteTest &&
!$e instanceof PHPUnit_Framework_SkippedTest &&
+ !$e instanceof PHPUnit_Framework_Error &&
is_string($this->expectedException) &&
$e instanceof $this->expectedException) {
if (is_string($this->expectedExceptionMessage) &&
View
15 Tests/Framework/TestCaseTest.php
@@ -56,6 +56,7 @@
require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'Success.php';
require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'ThrowExceptionTestCase.php';
require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'ThrowNoExceptionTestCase.php';
+require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'TriggerWarningTestCase.php';
require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'WasRun.php';
$GLOBALS['a'] = 'a';
@@ -157,6 +158,20 @@ public function testExceptionInTest()
$this->assertTrue($test->tearDown);
}
+ public function testRealExceptionIsCaughtByExpectedException()
+ {
+ $test = new TriggerWarningTestCase('testRealExceptionIsCaught');
+ $result = $test->run();
+ $this->assertTrue($result->wasSuccessful(), 'Normal Exceptions should be caught by the setExpectedException method');
+ }
+
+ public function testExceptionFromWarningNotCaughtByExpectedException()
+ {
+ $test = new TriggerWarningTestCase('testWarningAsExceptionIsNotCaught');
+ $result = $test->run();
+ $this->assertFalse($result->wasSuccessful(), 'A warning as an exception shouldn\'t be caught by the setExpectedException method');
+ }
+
public function testExceptionInAssertPostConditions()
{
$test = new ExceptionInAssertPostConditionsTest('testSomething');
View
18 Tests/_files/TriggerWarningTestCase.php
@@ -0,0 +1,18 @@
+<?php
+class TriggerWarningTestCase extends PHPUnit_Framework_TestCase
+{
+ public function testRealExceptionIsCaught()
+ {
+ $this->setExpectedException('Exception');
+ throw new Exception('', 1);
+ }
+
+ /**
+ * @errorHandler enabled
+ */
+ public function testWarningAsExceptionIsNotCaught()
+ {
+ $this->setExpectedException('Exception');
+ trigger_error("Catch as exception", E_USER_NOTICE);
+ }
+}
Something went wrong with that request. Please try again.