Skip to content

Commit 841ac01

Browse files
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 <[email protected]>
1 parent 4df65fd commit 841ac01

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

Diff for: src/PhpUnit/RefreshDatabaseState.php

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Hautelook\AliceBundle package.
5+
*
6+
* (c) Baldur Rensch <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Hautelook\AliceBundle\PhpUnit;
15+
16+
final class RefreshDatabaseState
17+
{
18+
private static bool $dbPopulated = false;
19+
20+
public static function setDbPopulated(bool $populated): void
21+
{
22+
self::$dbPopulated = $populated;
23+
}
24+
25+
public static function isDbPopulated(): bool
26+
{
27+
return self::$dbPopulated;
28+
}
29+
30+
private function __construct()
31+
{
32+
}
33+
}

Diff for: src/PhpUnit/RefreshDatabaseTrait.php

+3-4
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,15 @@ trait RefreshDatabaseTrait
2525
{
2626
use BaseDatabaseTrait;
2727

28-
protected static bool $dbPopulated = false;
29-
3028
protected static function bootKernel(array $options = []): KernelInterface
3129
{
3230
static::ensureKernelTestCase();
3331
$kernel = parent::bootKernel($options);
3432

35-
if (!static::$dbPopulated) {
33+
if (!RefreshDatabaseState::isDbPopulated()) {
3634
static::populateDatabase();
37-
static::$dbPopulated = true;
35+
36+
RefreshDatabaseState::setDbPopulated(true);
3837
}
3938

4039
$container = static::$kernel->getContainer();

0 commit comments

Comments
 (0)