1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Zenstruck\Foundry\Test; |
4
|
|
|
|
5
|
|
|
use Doctrine\Persistence\ManagerRegistry; |
6
|
|
|
use Faker; |
7
|
|
|
use Psr\Container\ContainerInterface; |
8
|
|
|
use Psr\Container\NotFoundExceptionInterface; |
9
|
|
|
use Zenstruck\Foundry\ChainManagerRegistry; |
10
|
|
|
use Zenstruck\Foundry\Configuration; |
11
|
|
|
use Zenstruck\Foundry\Factory; |
12
|
|
|
use Zenstruck\Foundry\StoryManager; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @author Kevin Bond <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
final class TestState |
18
|
|
|
{ |
19
|
|
|
/** @var callable|null */ |
20
|
|
|
private static $instantiator; |
21
|
|
|
|
22
|
|
|
/** @var Faker\Generator|null */ |
23
|
|
|
private static $faker; |
24
|
|
|
|
25
|
|
|
/** @var bool|null */ |
26
|
|
|
private static $defaultProxyAutoRefresh; |
27
|
|
|
|
28
|
|
|
/** @var callable[] */ |
29
|
|
|
private static $globalStates = []; |
30
|
|
|
|
31
|
|
|
public static function setInstantiator(callable $instantiator): void |
32
|
|
|
{ |
33
|
|
|
self::$instantiator = $instantiator; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public static function setFaker(Faker\Generator $faker): void |
37
|
|
|
{ |
38
|
|
|
self::$faker = $faker; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public static function enableDefaultProxyAutoRefresh(): void |
42
|
|
|
{ |
43
|
|
|
self::$defaultProxyAutoRefresh = true; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public static function disableDefaultProxyAutoRefresh(): void |
47
|
|
|
{ |
48
|
|
|
self::$defaultProxyAutoRefresh = false; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @deprecated Use TestState::enableDefaultProxyAutoRefresh() |
53
|
|
|
*/ |
54
|
|
|
public static function alwaysAutoRefreshProxies(): void |
55
|
|
|
{ |
56
|
|
|
trigger_deprecation('zenstruck\foundry', '1.9', 'TestState::alwaysAutoRefreshProxies() is deprecated, use TestState::enableDefaultProxyAutoRefresh().'); |
57
|
964 |
|
|
58
|
|
|
self::enableDefaultProxyAutoRefresh(); |
59
|
964 |
|
} |
60
|
|
|
|
61
|
964 |
|
/** |
62
|
|
|
* @deprecated Foundry now auto-detects if the bundle is installed |
63
|
|
|
*/ |
64
|
|
|
public static function withoutBundle(): void |
65
|
964 |
|
{ |
66
|
|
|
trigger_deprecation('zenstruck\foundry', '1.4.0', 'TestState::withoutBundle() is deprecated, the bundle is now auto-detected.'); |
67
|
|
|
} |
68
|
|
|
|
69
|
964 |
|
public static function addGlobalState(callable $callback): void |
70
|
|
|
{ |
71
|
|
|
self::$globalStates[] = $callback; |
72
|
|
|
} |
73
|
964 |
|
|
74
|
964 |
|
public static function bootFoundry(?Configuration $configuration = null): void |
75
|
|
|
{ |
76
|
964 |
|
$configuration = $configuration ?? new Configuration(); |
77
|
|
|
|
78
|
964 |
|
if (self::$instantiator) { |
79
|
964 |
|
$configuration->setInstantiator(self::$instantiator); |
80
|
964 |
|
} |
81
|
|
|
|
82
|
|
|
if (self::$faker) { |
83
|
|
|
$configuration->setFaker(self::$faker); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if (true === self::$defaultProxyAutoRefresh) { |
87
|
|
|
$configuration->enableDefaultProxyAutoRefresh(); |
88
|
|
|
} elseif (false === self::$defaultProxyAutoRefresh) { |
89
|
|
|
$configuration->disableDefaultProxyAutoRefresh(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
Factory::boot($configuration); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public static function shutdownFoundry(): void |
96
|
|
|
{ |
97
|
634 |
|
Factory::shutdown(); |
98
|
|
|
StoryManager::reset(); |
99
|
634 |
|
} |
100
|
256 |
|
|
101
|
|
|
/** |
102
|
256 |
|
* @deprecated use TestState::bootFoundry() |
103
|
|
|
*/ |
104
|
|
|
public static function bootFactory(Configuration $configuration): Configuration |
105
|
378 |
|
{ |
106
|
|
|
trigger_deprecation('zenstruck\foundry', '1.4.0', 'TestState::bootFactory() is deprecated, use TestState::bootFoundry().'); |
107
|
|
|
|
108
|
378 |
|
self::bootFoundry($configuration); |
109
|
|
|
|
110
|
|
|
return Factory::configuration(); |
111
|
|
|
} |
112
|
|
|
|
113
|
378 |
|
/** |
114
|
378 |
|
* @internal |
115
|
|
|
*/ |
116
|
|
|
public static function bootFromContainer(ContainerInterface $container): void |
117
|
|
|
{ |
118
|
|
|
if ($container->has(Configuration::class)) { |
119
|
381 |
|
self::bootFoundry($container->get(Configuration::class)); |
120
|
|
|
|
121
|
381 |
|
return; |
122
|
|
|
} |
123
|
381 |
|
|
124
|
381 |
|
$configuration = new Configuration(); |
125
|
|
|
|
126
|
|
|
try { |
127
|
381 |
|
$configuration->setManagerRegistry(self::initializeChainManagerRegistry($container)); |
128
|
381 |
|
} catch (NotFoundExceptionInterface $e) { |
129
|
|
|
throw new \LogicException('Could not boot Foundry, is the DoctrineBundle installed/configured?', 0, $e); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
self::bootFoundry($configuration); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @internal |
137
|
|
|
*/ |
138
|
|
|
public static function initializeChainManagerRegistry(ContainerInterface $container): ChainManagerRegistry |
139
|
|
|
{ |
140
|
|
|
/** @var list<ManagerRegistry> $managerRegistries */ |
141
|
|
|
$managerRegistries = []; |
142
|
|
|
|
143
|
|
|
if ($container->has('doctrine')) { |
144
|
|
|
$managerRegistries[] = $container->get('doctrine'); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
if ($container->has('doctrine_mongodb')) { |
148
|
|
|
$managerRegistries[] = $container->get('doctrine_mongodb'); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
if (0 === \count($managerRegistries)) { |
152
|
|
|
throw new \LogicException('Neither doctrine/orm nor doctrine/mongodb-odm are present.'); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return new ChainManagerRegistry($managerRegistries); |
|
|
|
|
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @internal |
160
|
|
|
*/ |
161
|
|
|
public static function flushGlobalState(): void |
162
|
|
|
{ |
163
|
|
|
StoryManager::globalReset(); |
164
|
|
|
|
165
|
|
|
foreach (self::$globalStates as $callback) { |
166
|
|
|
$callback(); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
StoryManager::setGlobalState(); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|