Skip to content

Commit 9174dc6

Browse files
authoredApr 18, 2024··
fix: restore PHPUnit error handler (#587)
1 parent 4156302 commit 9174dc6

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed
 

‎composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"bamarni/composer-bin-plugin": "^1.8",
2929
"dama/doctrine-test-bundle": "^7.0|^8.0",
3030
"doctrine/common": "^3.2",
31+
"doctrine/collections": "^1.7|^2.0",
3132
"doctrine/doctrine-bundle": "^2.10",
3233
"doctrine/doctrine-migrations-bundle": "^2.2|^3.0",
3334
"doctrine/mongodb-odm-bundle": "^4.6|^5.0",
@@ -43,7 +44,7 @@
4344
},
4445
"autoload": {
4546
"psr-4": { "Zenstruck\\Foundry\\": "src/" },
46-
"files": ["src/functions.php", "src/Persistence/functions.php"]
47+
"files": ["src/functions.php", "src/Persistence/functions.php", "src/phpunit_helper.php"]
4748
},
4849
"autoload-dev": {
4950
"psr-4": {

‎src/Test/ResetDatabase.php

+10-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
1717
use Zenstruck\Foundry\Persistence\PersistenceManager;
1818

19+
use function Zenstruck\Foundry\restorePhpUnitErrorHandler;
20+
1921
/**
2022
* @author Kevin Bond <kevinbond@gmail.com>
2123
*/
@@ -34,7 +36,10 @@ public static function _resetDatabase(): void
3436

3537
PersistenceManager::resetDatabase(
3638
static fn() => static::bootKernel(),
37-
static fn() => static::ensureKernelShutdown(),
39+
static function (): void {
40+
static::ensureKernelShutdown();
41+
restorePhpUnitErrorHandler();
42+
},
3843
);
3944
}
4045

@@ -51,7 +56,10 @@ public static function _resetSchema(): void
5156

5257
PersistenceManager::resetSchema(
5358
static fn() => static::bootKernel(),
54-
static fn() => static::ensureKernelShutdown(),
59+
static function (): void {
60+
static::ensureKernelShutdown();
61+
restorePhpUnitErrorHandler();
62+
},
5563
);
5664
}
5765
}

‎src/phpunit_helper.php

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Zenstruck\Foundry;
6+
7+
/**
8+
* When using ResetDatabase trait, we're booting the kernel,
9+
* which registers the Symfony's error handler too soon.
10+
* It is then impossible for PHPUnit to handle deprecations.
11+
*
12+
* This method tries to mitigate this problem by restoring the error handler.
13+
*
14+
* @see https://github.com/symfony/symfony/issues/53812
15+
*
16+
* @internal
17+
*/
18+
function restorePhpUnitErrorHandler(): void
19+
{
20+
while (true) {
21+
$previousHandler = set_error_handler(static fn() => null); // @phpstan-ignore-line
22+
restore_error_handler();
23+
$isPhpUnitErrorHandler = ($previousHandler instanceof \PHPUnit\Runner\ErrorHandler); // @phpstan-ignore-line
24+
if ($previousHandler === null || $isPhpUnitErrorHandler) {
25+
break;
26+
}
27+
restore_error_handler();
28+
}
29+
}

0 commit comments

Comments
 (0)
Please sign in to comment.