Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Exception handling fix #88

Closed
wants to merge 1 commit into from

7 participants

Jordi Boggiano Lukas Kahwe Smith Sebastian Bergmann Daniel Convissor Michael M Slusarz Christian Brunotte Matthew Caruana Galizia
Jordi Boggiano

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.

Jordi Boggiano 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
Sebastian Bergmann

Closed by 4ad409a.

Victor Karamzin visor referenced this pull request from a commit
Sebastian Bergmann Close #88. 54d1cad
Jason Ardell ardell referenced this pull request from a commit in apinstein/phpunit
Sebastian Bergmann Close #88. 4ad409a
Daniel 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.

Michael M 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.

Sebastian Bergmann

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.

Christian Brunotte

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

Sebastian Bergmann

No.

Matthew Caruana Galizia

@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.

Greg Lamb greglamb referenced this pull request from a commit in greglamb/phpunit
Sebastian Bergmann 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. Jordi Boggiano

    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.
1  PHPUnit/Framework/TestCase.php
View
@@ -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) &&
15 Tests/Framework/TestCaseTest.php
View
@@ -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');
18 Tests/_files/TriggerWarningTestCase.php
View
@@ -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.