diff --git a/src/Framework/TestCase.php b/src/Framework/TestCase.php index 9f9c0b0dcdb..021336f305f 100644 --- a/src/Framework/TestCase.php +++ b/src/Framework/TestCase.php @@ -733,7 +733,14 @@ final public function runBare(): void } } - $this->mockObjects = []; + try { + $this->mockObjects = []; + } catch (Throwable $t) { + Event\Facade::emitter()->testErrored( + $this->valueObjectForEvents(), + Event\Code\ThrowableBuilder::from($t), + ); + } // Tear down the fixture. An exception raised in tearDown() will be // caught and passed on when no exception was raised before. diff --git a/tests/_files/ExceptionInMockDestructor.php b/tests/_files/ExceptionInMockDestructor.php new file mode 100644 index 00000000000..6be9ff15e1a --- /dev/null +++ b/tests/_files/ExceptionInMockDestructor.php @@ -0,0 +1,20 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace PHPUnit\TestFixture; + +use Exception; + +class ExceptionInMockDestructor +{ + public function __destruct() + { + throw new Exception('Some exception.'); + } +} diff --git a/tests/_files/ExceptionInMockDestructorTest.php b/tests/_files/ExceptionInMockDestructorTest.php new file mode 100644 index 00000000000..d12cc12b2cf --- /dev/null +++ b/tests/_files/ExceptionInMockDestructorTest.php @@ -0,0 +1,22 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace PHPUnit\TestFixture; + +use PHPUnit\Framework\TestCase; + +class ExceptionInMockDestructorTest extends TestCase +{ + public function testOne(): void + { + $mock = $this->createMock(ExceptionInMockDestructor::class); + + $this->assertInstanceOf(ExceptionInMockDestructor::class, $mock); + } +} diff --git a/tests/end-to-end/generic/exception-in-mock-destructor.phpt b/tests/end-to-end/generic/exception-in-mock-destructor.phpt new file mode 100644 index 00000000000..3a2e2e4f86f --- /dev/null +++ b/tests/end-to-end/generic/exception-in-mock-destructor.phpt @@ -0,0 +1,28 @@ +--TEST-- +phpunit ../../_files/ExceptionInMockDestructorTest.php +--FILE-- +run($_SERVER['argv']); +--EXPECTF-- +PHPUnit %s by Sebastian Bergmann and contributors. + +Runtime: %s + +E 1 / 1 (100%) + +Time: %s, Memory: %s + +There was 1 error: + +1) PHPUnit\TestFixture\ExceptionInMockDestructorTest::testOne +Exception: Some exception. + +%sExceptionInMockDestructor.php:%d + +ERRORS! +Tests: 1, Assertions: 1, Errors: 1.