Skip to content

Commit

Permalink
fix: Ensure the database is not populated several times in PHP 8.2 (#43)
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
theofidry and TravisMarkvh authored Sep 19, 2023
1 parent 4df65fd commit 841ac01
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
33 changes: 33 additions & 0 deletions src/PhpUnit/RefreshDatabaseState.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/*
* This file is part of the Hautelook\AliceBundle package.
*
* (c) Baldur Rensch <[email protected]>
*
* 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()
{
}
}
7 changes: 3 additions & 4 deletions src/PhpUnit/RefreshDatabaseTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 841ac01

Please sign in to comment.