1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Zenstruck\Foundry\Test; |
4
|
|
|
|
5
|
|
|
use Faker; |
6
|
|
|
use Psr\Container\ContainerInterface; |
7
|
|
|
use Zenstruck\Foundry\Configuration; |
8
|
|
|
use Zenstruck\Foundry\Factory; |
9
|
|
|
use Zenstruck\Foundry\StoryManager; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @author Kevin Bond <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
final class TestState |
15
|
|
|
{ |
16
|
|
|
/** @var callable|null */ |
17
|
|
|
private static $instantiator; |
18
|
|
|
private static ?Faker\Generator $faker = null; |
19
|
|
|
private static bool $useBundle = true; |
20
|
|
|
private static array $globalStates = []; |
21
|
|
|
|
22
|
|
|
public static function setInstantiator(callable $instantiator): void |
23
|
|
|
{ |
24
|
|
|
self::$instantiator = $instantiator; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public static function setFaker(Faker\Generator $faker): void |
28
|
|
|
{ |
29
|
|
|
self::$faker = $faker; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public static function withoutBundle(): void |
33
|
|
|
{ |
34
|
|
|
self::$useBundle = false; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public static function addGlobalState(callable $callback): void |
38
|
|
|
{ |
39
|
|
|
self::$globalStates[] = $callback; |
40
|
|
|
} |
41
|
|
|
|
42
|
154 |
|
public static function bootFactory(Configuration $configuration): Configuration |
43
|
|
|
{ |
44
|
154 |
|
if (self::$instantiator) { |
45
|
|
|
$configuration->setInstantiator(self::$instantiator); |
46
|
|
|
} |
47
|
|
|
|
48
|
154 |
|
if (self::$faker) { |
49
|
|
|
$configuration->setFaker(self::$faker); |
50
|
|
|
} |
51
|
|
|
|
52
|
154 |
|
Factory::boot($configuration); |
53
|
|
|
|
54
|
154 |
|
return $configuration; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @internal |
59
|
|
|
*/ |
60
|
154 |
|
public static function bootFromContainer(ContainerInterface $container): Configuration |
61
|
|
|
{ |
62
|
154 |
|
if (self::$useBundle) { |
63
|
|
|
// todo improve/catch service not found exception - foundry-bundle not installed/configured... |
64
|
78 |
|
return self::bootFactory($container->get(Configuration::class)); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
// todo improve/catch service not found exception - doctrine-bundle not installed/configured... |
68
|
76 |
|
return self::bootFactory(new Configuration($container->get('doctrine'), new StoryManager([]))); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @internal |
73
|
|
|
*/ |
74
|
154 |
|
public static function flushGlobalState(): void |
75
|
|
|
{ |
76
|
154 |
|
StoryManager::globalReset(); |
77
|
|
|
|
78
|
154 |
|
foreach (self::$globalStates as $callback) { |
79
|
154 |
|
$callback(); |
80
|
|
|
} |
81
|
|
|
|
82
|
154 |
|
StoryManager::setGlobalState(); |
83
|
154 |
|
} |
84
|
|
|
} |
85
|
|
|
|