Created
November 3, 2012 14:26
-
-
Save cmbuckley/4007523 to your computer and use it in GitHub Desktop.
Erroneous exceptions in PHPUnit for http://stackoverflow.com/q/13198392
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
include 'fixture.php'; | |
class AnnotationTest extends PHPUnit_Framework_TestCase { | |
/** | |
* @expectedException MyException | |
*/ | |
public function testExceptionThrown() { | |
$fixture = new Fixture(); | |
$fixture->methodThatThrows(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Fixture { | |
public function __construct() { | |
// an exception is erroneously being thrown here | |
throw new MyException('This is a bug that will be masked by the annotation'); | |
} | |
public function methodThatThrows() { | |
throw new MyException('This is the expected exception'); | |
} | |
} | |
class MyException extends Exception {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
include 'fixture.php'; | |
class MethodTest extends PHPUnit_Framework_TestCase { | |
public function testExceptionThrown() { | |
$fixture = new Fixture(); | |
// with the method approach, I can control exactly when the Exception is expected | |
$this->setExpectedException('MyException'); | |
$fixture->methodThatThrows(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ phpunit annotation.php | |
PHPUnit 3.6.10 by Sebastian Bergmann. | |
. | |
OK (1 test, 1 assertion) | |
$ phpunit method.php | |
PHPUnit 3.6.10 by Sebastian Bergmann. | |
E | |
There was 1 error: | |
1) AnnotationTest::testExceptionThrown | |
MyException: This is a bug that will be masked by the annotation | |
fixture.php:6 | |
method.php:8 | |
FAILURES! | |
Tests: 1, Assertions: 0, Errors: 1. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment