1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Zenstruck\Foundry; |
4
|
|
|
|
5
|
|
|
use Doctrine\Persistence\ManagerRegistry; |
6
|
|
|
use Doctrine\Persistence\ObjectManager; |
7
|
|
|
use Faker; |
8
|
|
|
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @internal |
12
|
|
|
* |
13
|
|
|
* @author Kevin Bond <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
final class Configuration |
16
|
|
|
{ |
17
|
|
|
/** @var ManagerRegistry|null */ |
18
|
|
|
private $managerRegistry; |
19
|
|
|
|
20
|
|
|
/** @var StoryManager */ |
21
|
|
|
private $stories; |
22
|
|
|
|
23
|
|
|
/** @var ModelFactoryManager */ |
24
|
|
|
private $factories; |
25
|
|
|
|
26
|
|
|
/** @var Faker\Generator */ |
27
|
|
|
private $faker; |
28
|
|
|
|
29
|
|
|
/** @var callable */ |
30
|
|
|
private $instantiator; |
31
|
|
|
|
32
|
|
|
/** @var bool|null */ |
33
|
|
|
private $defaultProxyAutoRefresh; |
34
|
|
|
|
35
|
968 |
|
/** @var bool */ |
36
|
|
|
private $flushEnabled = true; |
37
|
968 |
|
|
38
|
968 |
|
public function __construct() |
39
|
968 |
|
{ |
40
|
968 |
|
$this->stories = new StoryManager([]); |
41
|
968 |
|
$this->factories = new ModelFactoryManager([]); |
42
|
|
|
$this->faker = Faker\Factory::create(); |
43
|
405 |
|
$this->instantiator = new Instantiator(); |
44
|
|
|
} |
45
|
405 |
|
|
46
|
|
|
public function stories(): StoryManager |
47
|
|
|
{ |
48
|
607 |
|
return $this->stories; |
49
|
|
|
} |
50
|
607 |
|
|
51
|
|
|
public function factories(): ModelFactoryManager |
52
|
|
|
{ |
53
|
964 |
|
return $this->factories; |
54
|
|
|
} |
55
|
964 |
|
|
56
|
|
|
public function faker(): Faker\Generator |
57
|
|
|
{ |
58
|
793 |
|
return $this->faker; |
59
|
|
|
} |
60
|
793 |
|
|
61
|
|
|
public function instantiator(): callable |
62
|
|
|
{ |
63
|
857 |
|
return $this->instantiator; |
64
|
|
|
} |
65
|
857 |
|
|
66
|
|
|
public function defaultProxyAutoRefresh(): bool |
67
|
|
|
{ |
68
|
708 |
|
if (!$this->hasManagerRegistry()) { |
69
|
|
|
return false; |
70
|
708 |
|
} |
71
|
|
|
|
72
|
708 |
|
if (null === $this->defaultProxyAutoRefresh) { |
73
|
|
|
trigger_deprecation('zenstruck\foundry', '1.9', 'Not explicitly configuring the default proxy auto-refresh is deprecated and will default to "true" in 2.0. Use "zenstruck_foundry.auto_refresh_proxies" in the bundle config or TestState::enableDefaultProxyAutoRefresh()/disableDefaultProxyAutoRefresh().'); |
74
|
|
|
|
75
|
270 |
|
$this->defaultProxyAutoRefresh = false; |
76
|
|
|
} |
77
|
270 |
|
|
78
|
|
|
return $this->defaultProxyAutoRefresh; |
79
|
270 |
|
} |
80
|
|
|
|
81
|
|
|
public function setManagerRegistry(ManagerRegistry $managerRegistry): self |
82
|
260 |
|
{ |
83
|
|
|
$this->managerRegistry = $managerRegistry; |
84
|
260 |
|
|
85
|
|
|
return $this; |
86
|
260 |
|
} |
87
|
|
|
|
88
|
|
|
public function setInstantiator(callable $instantiator): self |
89
|
260 |
|
{ |
90
|
|
|
$this->instantiator = $instantiator; |
91
|
260 |
|
|
92
|
|
|
return $this; |
93
|
260 |
|
} |
94
|
|
|
|
95
|
|
|
public function setStoryManager(StoryManager $manager): self |
96
|
270 |
|
{ |
97
|
|
|
$this->stories = $manager; |
98
|
270 |
|
|
99
|
|
|
return $this; |
100
|
270 |
|
} |
101
|
|
|
|
102
|
|
|
public function setModelFactoryManager(ModelFactoryManager $manager): self |
103
|
|
|
{ |
104
|
|
|
$this->factories = $manager; |
105
|
|
|
|
106
|
|
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function setFaker(Faker\Generator $faker): self |
110
|
|
|
{ |
111
|
|
|
$this->faker = $faker; |
112
|
|
|
|
113
|
|
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function enableDefaultProxyAutoRefresh(): self |
117
|
|
|
{ |
118
|
|
|
$this->defaultProxyAutoRefresh = true; |
119
|
360 |
|
|
120
|
|
|
return $this; |
121
|
360 |
|
} |
122
|
|
|
|
123
|
|
|
public function disableDefaultProxyAutoRefresh(): self |
124
|
|
|
{ |
125
|
360 |
|
$this->defaultProxyAutoRefresh = false; |
126
|
10 |
|
|
127
|
|
|
return $this; |
128
|
|
|
} |
129
|
360 |
|
|
130
|
|
|
public function isFlushingEnabled(): bool |
131
|
|
|
{ |
132
|
|
|
return $this->flushEnabled; |
133
|
|
|
} |
134
|
|
|
|
135
|
645 |
|
public function delayFlush(callable $callback): void |
136
|
|
|
{ |
137
|
645 |
|
$this->flushEnabled = false; |
138
|
|
|
|
139
|
645 |
|
$callback(); |
140
|
|
|
|
141
|
|
|
foreach ($this->managerRegistry()->getManagers() as $manager) { |
142
|
|
|
$manager->flush(); |
143
|
635 |
|
} |
144
|
|
|
|
145
|
|
|
$this->flushEnabled = true; |
146
|
|
|
} |
147
|
855 |
|
|
148
|
|
|
/** |
149
|
855 |
|
* @param object|string $objectOrClass |
150
|
|
|
* |
151
|
|
|
* @psalm-suppress InvalidReturnType |
152
|
695 |
|
* @psalm-suppress InvalidReturnStatement |
153
|
|
|
* @template TObject of object |
154
|
695 |
|
* @psalm-param Proxy<TObject>|TObject|class-string<TObject> $objectOrClass |
155
|
|
|
* @psalm-return RepositoryProxy<TObject> |
156
|
10 |
|
*/ |
157
|
|
|
public function repositoryFor($objectOrClass): RepositoryProxy |
158
|
|
|
{ |
159
|
685 |
|
if ($objectOrClass instanceof Proxy) { |
160
|
|
|
$objectOrClass = $objectOrClass->object(); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
if (!\is_string($objectOrClass)) { |
164
|
|
|
$objectOrClass = \get_class($objectOrClass); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
return new RepositoryProxy($this->managerRegistry()->getRepository($objectOrClass)); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @param object|string $objectOrClass |
172
|
|
|
*/ |
173
|
|
|
public function objectManagerFor($objectOrClass): ObjectManager |
174
|
|
|
{ |
175
|
|
|
$class = \is_string($objectOrClass) ? $objectOrClass : \get_class($objectOrClass); |
176
|
|
|
|
177
|
|
|
if (!$objectManager = $this->managerRegistry()->getManagerForClass($class)) { |
178
|
|
|
throw new \RuntimeException(\sprintf('No object manager registered for "%s".', $class)); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
return $objectManager; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** @psalm-assert !null $this->managerRegistry */ |
185
|
|
|
public function hasManagerRegistry(): bool |
186
|
|
|
{ |
187
|
|
|
return null !== $this->managerRegistry; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
private function managerRegistry(): ManagerRegistry |
191
|
|
|
{ |
192
|
|
|
if (!$this->hasManagerRegistry()) { |
193
|
|
|
/** @psalm-suppress MissingDependency */ |
194
|
|
|
throw new \RuntimeException('Foundry was booted without doctrine. Ensure your TestCase extends '.KernelTestCase::class); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
return $this->managerRegistry; |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|