diff --git a/src/Framework/TestSuite.php b/src/Framework/TestSuite.php index 843a7a20e35..7ab4366f61e 100644 --- a/src/Framework/TestSuite.php +++ b/src/Framework/TestSuite.php @@ -703,10 +703,10 @@ public function run(TestResult $result = null): TestResult \call_user_func([$this->name, $beforeClassMethod]); } } - } catch (SkippedTestSuiteError $e) { + } catch (SkippedTestSuiteError $error) { foreach ($this->tests() as $test) { $result->startTest($test); - $result->addFailure($test, $e, 0); + $result->addFailure($test, $error, 0); $result->endTest($test, 0); } @@ -756,7 +756,12 @@ public function run(TestResult $result = null): TestResult } } } catch (Throwable $t) { - \trigger_error($t->__toString(), \E_USER_WARNING); + $error = new SyntheticError($t->getMessage(), 0, $t->getFile(), $t->getLine(), $t->getTrace()); + $test = new \Failure('tearDownAfterClass'); + + $result->startTest($test); + $result->addFailure($test, $error, 0); + $result->endTest($test, 0); } $this->tearDown(); diff --git a/tests/_files/ExceptionInTearDownAfterClassTest.php b/tests/_files/ExceptionInTearDownAfterClassTest.php new file mode 100644 index 00000000000..092b02d9bd6 --- /dev/null +++ b/tests/_files/ExceptionInTearDownAfterClassTest.php @@ -0,0 +1,62 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +use PHPUnit\Framework\TestCase; + +class ExceptionInTearDownAfterClassTest extends TestCase +{ + public $setUp = false; + + public $assertPreConditions = false; + + public $assertPostConditions = false; + + public $tearDown = false; + + public $tearDownAfterClass = false; + + public $testSomething = false; + + public static function tearDownAfterClass(): void + { + throw new Exception('throw Exception in tearDownAfterClass()'); + } + + protected function setUp(): void + { + $this->setUp = true; + } + + protected function tearDown(): void + { + $this->tearDown = true; + } + + public function testOne(): void + { + $this->testSomething = true; + $this->assertTrue(true); + } + + public function testTwo(): void + { + $this->testSomething = $this->testSomething && true; + $this->assertTrue(true); + } + + protected function assertPreConditions(): void + { + $this->assertPreConditions = true; + } + + protected function assertPostConditions(): void + { + $this->assertPostConditions = true; + } +} diff --git a/tests/_files/ExceptionInTearDownTest.php b/tests/_files/ExceptionInTearDownTest.php index 517cfd30cf7..44cf1e6c39f 100644 --- a/tests/_files/ExceptionInTearDownTest.php +++ b/tests/_files/ExceptionInTearDownTest.php @@ -30,7 +30,7 @@ protected function tearDown(): void { $this->tearDown = true; - throw new Exception("throw Exception in tearDown()"); + throw new Exception('throw Exception in tearDown()'); } public function testSomething(): void diff --git a/tests/unit/Framework/TestSuiteTest.php b/tests/unit/Framework/TestSuiteTest.php index 4368be72b2b..71dd1349014 100644 --- a/tests/unit/Framework/TestSuiteTest.php +++ b/tests/unit/Framework/TestSuiteTest.php @@ -227,4 +227,17 @@ public function testCreateTestForConstructorlessTestClass(): void ->willReturn(__CLASS__); TestSuite::createTest($reflection, 'TestForConstructorlessTestClass'); } + + public function testTearDownAfterClassInTestSuite(): void + { + $suite = new TestSuite(\ExceptionInTearDownAfterClassTest::class); + $suite->run($this->result); + + $this->assertSame(3, $this->result->count()); + $this->assertCount(1, $this->result->failures()); + + /** @var TestFailure $failure */ + $failure = $this->result->failures()[0]; + $this->assertSame('throw Exception in tearDownAfterClass()', $failure->thrownException()->getMessage()); + } }