Skip to content

Commit

Permalink
fix: restore PHPUnit error handler
Browse files Browse the repository at this point in the history
  • Loading branch information
nikophil committed Apr 10, 2024
1 parent ec2c895 commit de74551
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"bamarni/composer-bin-plugin": "^1.8",
"dama/doctrine-test-bundle": "^7.0|^8.0",
"doctrine/common": "^3.2",
"doctrine/collections": "^1.7|^2.0",
"doctrine/doctrine-bundle": "^2.10",
"doctrine/doctrine-migrations-bundle": "^2.2|^3.0",
"doctrine/mongodb-odm-bundle": "^4.6|^5.0",
Expand All @@ -43,7 +44,7 @@
},
"autoload": {
"psr-4": { "Zenstruck\\Foundry\\": "src/" },
"files": ["src/functions.php", "src/Persistence/functions.php"]
"files": ["src/functions.php", "src/Persistence/functions.php", "src/phpunit_helper.php"]
},
"autoload-dev": {
"psr-4": {
Expand Down
12 changes: 10 additions & 2 deletions src/Test/ResetDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Zenstruck\Foundry\Persistence\PersistenceManager;

use function Zenstruck\Foundry\restorePhpUnitErrorHandler;

/**
* @author Kevin Bond <kevinbond@gmail.com>
*/
Expand All @@ -34,7 +36,10 @@ public static function _resetDatabase(): void

PersistenceManager::resetDatabase(
static fn() => static::bootKernel(),
static fn() => static::ensureKernelShutdown(),
static function (): void {
static::ensureKernelShutdown();
restorePhpUnitErrorHandler();
},
);
}

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

PersistenceManager::resetSchema(
static fn() => static::bootKernel(),
static fn() => static::ensureKernelShutdown(),
static function (): void {
static::ensureKernelShutdown();
restorePhpUnitErrorHandler();
},
);
}
}
29 changes: 29 additions & 0 deletions src/phpunit_helper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace Zenstruck\Foundry;

/**
* When using ResetDatabase trait, we're booting the kernel,
* which registers the Symfony's error handler too soon.
* It is then impossible for PHPUnit to handle deprecations.
*
* This method tries to mitigate this problem by restoring the error handler.
*
* @see https://github.com/symfony/symfony/issues/53812
*
* @internal
*/
function restorePhpUnitErrorHandler(): void
{
while (true) {
$previousHandler = set_error_handler(static fn() => null); // @phpstan-ignore-line
restore_error_handler();
$isPhpUnitErrorHandler = ($previousHandler instanceof \PHPUnit\Runner\ErrorHandler); // @phpstan-ignore-line
if ($previousHandler === null || $isPhpUnitErrorHandler) {
break;
}
restore_error_handler();
}
}

0 comments on commit de74551

Please sign in to comment.