1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Zenstruck\Foundry\Test; |
4
|
|
|
|
5
|
|
|
use Faker; |
6
|
|
|
use Psr\Container\ContainerInterface; |
7
|
|
|
use Psr\Container\NotFoundExceptionInterface; |
8
|
|
|
use Zenstruck\Foundry\Configuration; |
9
|
|
|
use Zenstruck\Foundry\Factory; |
10
|
|
|
use Zenstruck\Foundry\StoryManager; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @author Kevin Bond <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
final class TestState |
16
|
|
|
{ |
17
|
|
|
/** @var callable|null */ |
18
|
|
|
private static $instantiator; |
19
|
|
|
|
20
|
|
|
/** @var Faker\Generator|null */ |
21
|
|
|
private static $faker; |
22
|
|
|
|
23
|
|
|
/** @var bool */ |
24
|
|
|
private static $alwaysAutoRefreshProxies = false; |
25
|
|
|
|
26
|
|
|
/** @var callable[] */ |
27
|
|
|
private static $globalStates = []; |
28
|
|
|
|
29
|
|
|
public static function setInstantiator(callable $instantiator): void |
30
|
|
|
{ |
31
|
|
|
self::$instantiator = $instantiator; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public static function setFaker(Faker\Generator $faker): void |
35
|
|
|
{ |
36
|
|
|
self::$faker = $faker; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public static function alwaysAutoRefreshProxies(): void |
40
|
|
|
{ |
41
|
|
|
self::$alwaysAutoRefreshProxies = true; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @deprecated Foundry now auto-detects if the bundle is installed |
46
|
|
|
*/ |
47
|
|
|
public static function withoutBundle(): void |
48
|
|
|
{ |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public static function addGlobalState(callable $callback): void |
52
|
|
|
{ |
53
|
|
|
self::$globalStates[] = $callback; |
54
|
|
|
} |
55
|
|
|
|
56
|
731 |
|
public static function bootFoundry(?Configuration $configuration = null): void |
57
|
|
|
{ |
58
|
731 |
|
$configuration = $configuration ?? new Configuration(); |
59
|
|
|
|
60
|
731 |
|
if (self::$instantiator) { |
61
|
|
|
$configuration->setInstantiator(self::$instantiator); |
62
|
|
|
} |
63
|
|
|
|
64
|
731 |
|
if (self::$faker) { |
65
|
|
|
$configuration->setFaker(self::$faker); |
66
|
|
|
} |
67
|
|
|
|
68
|
731 |
|
if (self::$alwaysAutoRefreshProxies) { |
69
|
|
|
$configuration->alwaysAutoRefreshProxies(); |
70
|
|
|
} |
71
|
|
|
|
72
|
731 |
|
Factory::boot($configuration); |
73
|
731 |
|
} |
74
|
|
|
|
75
|
731 |
|
public static function shutdownFoundry(): void |
76
|
|
|
{ |
77
|
731 |
|
Factory::shutdown(); |
78
|
731 |
|
StoryManager::reset(); |
79
|
731 |
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @deprecated use TestState::bootFoundry() |
83
|
|
|
*/ |
84
|
|
|
public static function bootFactory(Configuration $configuration): Configuration |
85
|
|
|
{ |
86
|
|
|
self::bootFoundry($configuration); |
87
|
|
|
|
88
|
|
|
return Factory::configuration(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @internal |
93
|
|
|
*/ |
94
|
467 |
|
public static function bootFromContainer(ContainerInterface $container): void |
95
|
|
|
{ |
96
|
467 |
|
if ($container->has(Configuration::class)) { |
97
|
177 |
|
self::bootFoundry($container->get(Configuration::class)); |
98
|
|
|
|
99
|
177 |
|
return; |
100
|
|
|
} |
101
|
|
|
|
102
|
290 |
|
$configuration = new Configuration(); |
103
|
|
|
|
104
|
|
|
try { |
105
|
290 |
|
$configuration->setManagerRegistry($container->get('doctrine')); |
106
|
|
|
} catch (NotFoundExceptionInterface $e) { |
107
|
|
|
throw new \LogicException('Could not boot Foundry, is the DoctrineBundle installed/configured?', 0, $e); |
108
|
|
|
} |
109
|
|
|
|
110
|
290 |
|
self::bootFoundry($configuration); |
111
|
290 |
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @internal |
115
|
|
|
*/ |
116
|
234 |
|
public static function flushGlobalState(): void |
117
|
|
|
{ |
118
|
234 |
|
StoryManager::globalReset(); |
119
|
|
|
|
120
|
234 |
|
foreach (self::$globalStates as $callback) { |
121
|
234 |
|
$callback(); |
122
|
|
|
} |
123
|
|
|
|
124
|
234 |
|
StoryManager::setGlobalState(); |
125
|
234 |
|
} |
126
|
|
|
} |
127
|
|
|
|