From 841ac01d0d4e1ea3381374360cdcc5d6b9c904cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20FIDRY?= <5175937+theofidry@users.noreply.github.com> Date: Tue, 19 Sep 2023 21:48:05 +0200 Subject: [PATCH] fix: Ensure the database is not populated several times in PHP 8.2 (#43) In PHP 8.2 static properties declared in traits behaves as if they were declared in the class using the trait, hence it will no longer be shared between classes using this trait. Closes #42. This PR is another take of #42 where the state is kept within an internal constant instead as a constant would mean introducing global state that cannot be mutated which I am not too found of. --------- Co-authored-by: TRAVIS | Mark --- src/PhpUnit/RefreshDatabaseState.php | 33 ++++++++++++++++++++++++++++ src/PhpUnit/RefreshDatabaseTrait.php | 7 +++--- 2 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 src/PhpUnit/RefreshDatabaseState.php diff --git a/src/PhpUnit/RefreshDatabaseState.php b/src/PhpUnit/RefreshDatabaseState.php new file mode 100644 index 0000000..a907d4b --- /dev/null +++ b/src/PhpUnit/RefreshDatabaseState.php @@ -0,0 +1,33 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Hautelook\AliceBundle\PhpUnit; + +final class RefreshDatabaseState +{ + private static bool $dbPopulated = false; + + public static function setDbPopulated(bool $populated): void + { + self::$dbPopulated = $populated; + } + + public static function isDbPopulated(): bool + { + return self::$dbPopulated; + } + + private function __construct() + { + } +} diff --git a/src/PhpUnit/RefreshDatabaseTrait.php b/src/PhpUnit/RefreshDatabaseTrait.php index cb99735..b1c34c8 100644 --- a/src/PhpUnit/RefreshDatabaseTrait.php +++ b/src/PhpUnit/RefreshDatabaseTrait.php @@ -25,16 +25,15 @@ trait RefreshDatabaseTrait { use BaseDatabaseTrait; - protected static bool $dbPopulated = false; - protected static function bootKernel(array $options = []): KernelInterface { static::ensureKernelTestCase(); $kernel = parent::bootKernel($options); - if (!static::$dbPopulated) { + if (!RefreshDatabaseState::isDbPopulated()) { static::populateDatabase(); - static::$dbPopulated = true; + + RefreshDatabaseState::setDbPopulated(true); } $container = static::$kernel->getContainer();