Skip to content

Commit

Permalink
Don't silently ignore exceptions in mock destructor
Browse files Browse the repository at this point in the history
  • Loading branch information
HypeMC authored and sebastianbergmann committed Dec 31, 2023
1 parent 4918913 commit c61d971
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/Framework/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
20 changes: 20 additions & 0 deletions tests/_files/ExceptionInMockDestructor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* 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.');
}
}
22 changes: 22 additions & 0 deletions tests/_files/ExceptionInMockDestructorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* 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);
}
}
28 changes: 28 additions & 0 deletions tests/end-to-end/generic/exception-in-mock-destructor.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
--TEST--
phpunit ../../_files/ExceptionInMockDestructorTest.php
--FILE--
<?php declare(strict_types=1);
$_SERVER['argv'][] = '--do-not-cache-result';
$_SERVER['argv'][] = '--no-configuration';
$_SERVER['argv'][] = __DIR__ . '/../../_files/ExceptionInMockDestructorTest.php';

require_once __DIR__ . '/../../bootstrap.php';
(new PHPUnit\TextUI\Application)->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.

0 comments on commit c61d971

Please sign in to comment.