Skip to content

Commit

Permalink
fix: restore error handler in Foundry's traits (#577)
Browse files Browse the repository at this point in the history
  • Loading branch information
nikophil authored Apr 3, 2024
1 parent d55c728 commit 7cfab99
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ parameters:
- var_dump
excludePaths:
- ./src/Bundle/Resources
- ./src/Test/KernelHelper.php

# phpstan runs with orm 3 - there are too many failures in these files which are compatible with orm 2
- ./src/Bundle/Maker/Factory/LegacyORMDefaultPropertiesGuesser.php
Expand Down
2 changes: 1 addition & 1 deletion src/Test/Factories.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static function _setUpFactories(): void
),
);

$kernel->shutdown();
KernelHelper::shutdownKernel($kernel);
}

/**
Expand Down
37 changes: 37 additions & 0 deletions src/Test/KernelHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace Zenstruck\Foundry\Test;

use Symfony\Component\HttpKernel\KernelInterface;

/**
* @internal
*/
final class KernelHelper
{
/**
* When using ResetDatabase or Factories traits, 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
*/
public static function shutdownKernel(KernelInterface $kernel): void
{
$kernel->shutdown();

while (true) {
$previousHandler = set_error_handler(static fn() => null);
restore_error_handler();
$isPhpUnitErrorHandler = ($previousHandler instanceof \PHPUnit\Runner\ErrorHandler);
if ($previousHandler === null || $isPhpUnitErrorHandler) {
break;
}
restore_error_handler();
}
}
}
4 changes: 2 additions & 2 deletions src/Test/ResetDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public static function _resetDatabase(): void
StaticDriver::setKeepStaticConnections(true);
}

$kernel->shutdown();
KernelHelper::shutdownKernel($kernel);
}

/**
Expand All @@ -80,7 +80,7 @@ public static function _resetSchema(): void

DatabaseResetter::resetSchema($kernel);

$kernel->shutdown();
KernelHelper::shutdownKernel($kernel);
}

private static function shouldReset(KernelInterface $kernel): bool
Expand Down

0 comments on commit 7cfab99

Please sign in to comment.