forked from zenstruck/foundry
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResetFakerTestTrait.php
More file actions
63 lines (52 loc) · 2 KB
/
ResetFakerTestTrait.php
File metadata and controls
63 lines (52 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
declare(strict_types=1);
/*
* This file is part of the zenstruck/foundry package.
*
* (c) Kevin Bond <kevinbond@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Zenstruck\Foundry\Tests\Integration\Faker;
use PHPUnit\Framework\Attributes\AfterClass;
use PHPUnit\Framework\Attributes\Before;
use PHPUnit\Framework\Attributes\BeforeClass;
use Zenstruck\Foundry\FakerAdapter;
if (\method_exists(Before::class, '__construct')) { // @phpstan-ignore function.alreadyNarrowedType
trait ResetFakerTestTrait
{
private static ?string $savedServerSeed = null;
private static ?string $savedEnvSeed = null;
private static ?string $savedGetEnvSeed = null;
private static ?int $currentSeed = null;
#[BeforeClass(10)]
public static function __saveAndResetFakerSeed(): void
{
self::$savedServerSeed = $_SERVER['FOUNDRY_FAKER_SEED'] ?? null;
self::$savedEnvSeed = $_ENV['FOUNDRY_FAKER_SEED'] ?? null;
self::$savedGetEnvSeed = \getenv('FOUNDRY_FAKER_SEED') ?: null;
$_SERVER['FOUNDRY_FAKER_SEED'] = null;
$_ENV['FOUNDRY_FAKER_SEED'] = null;
\putenv('FOUNDRY_FAKER_SEED');
FakerAdapter::resetFakerSeed();
}
#[AfterClass(-10)]
public static function __restoreFakerSeed(): void
{
$_SERVER['FOUNDRY_FAKER_SEED'] = self::$savedServerSeed;
$_ENV['FOUNDRY_FAKER_SEED'] = self::$savedEnvSeed;
if (null === self::$savedGetEnvSeed) {
\putenv('FOUNDRY_FAKER_SEED');
} else {
\putenv('FOUNDRY_FAKER_SEED='.self::$savedGetEnvSeed);
}
$savedValue = self::$savedServerSeed ?? self::$savedEnvSeed ?? self::$savedGetEnvSeed;
FakerAdapter::resetFakerSeed($savedValue ? (int) $savedValue : null);
}
}
} else {
trait ResetFakerTestTrait
{
}
}