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 */ |
33
|
|
|
private $defaultProxyAutoRefresh = false; |
34
|
|
|
|
35
|
958 |
|
/** @var bool */ |
36
|
|
|
private $flushEnabled = true; |
37
|
958 |
|
|
38
|
958 |
|
public function __construct() |
39
|
958 |
|
{ |
40
|
958 |
|
$this->stories = new StoryManager([]); |
41
|
958 |
|
$this->factories = new ModelFactoryManager([]); |
42
|
|
|
$this->faker = Faker\Factory::create(); |
43
|
399 |
|
$this->instantiator = new Instantiator(); |
44
|
|
|
} |
45
|
399 |
|
|
46
|
|
|
public function stories(): StoryManager |
47
|
|
|
{ |
48
|
597 |
|
return $this->stories; |
49
|
|
|
} |
50
|
597 |
|
|
51
|
|
|
public function factories(): ModelFactoryManager |
52
|
|
|
{ |
53
|
954 |
|
return $this->factories; |
54
|
|
|
} |
55
|
954 |
|
|
56
|
|
|
public function faker(): Faker\Generator |
57
|
|
|
{ |
58
|
783 |
|
return $this->faker; |
59
|
|
|
} |
60
|
783 |
|
|
61
|
|
|
public function instantiator(): callable |
62
|
|
|
{ |
63
|
847 |
|
return $this->instantiator; |
64
|
|
|
} |
65
|
847 |
|
|
66
|
|
|
public function defaultProxyAutoRefresh(): bool |
67
|
|
|
{ |
68
|
698 |
|
return $this->defaultProxyAutoRefresh; |
69
|
|
|
} |
70
|
698 |
|
|
71
|
|
|
public function setManagerRegistry(ManagerRegistry $managerRegistry): self |
72
|
698 |
|
{ |
73
|
|
|
$this->managerRegistry = $managerRegistry; |
74
|
|
|
|
75
|
266 |
|
return $this; |
76
|
|
|
} |
77
|
266 |
|
|
78
|
|
|
public function setInstantiator(callable $instantiator): self |
79
|
266 |
|
{ |
80
|
|
|
$this->instantiator = $instantiator; |
81
|
|
|
|
82
|
256 |
|
return $this; |
83
|
|
|
} |
84
|
256 |
|
|
85
|
|
|
public function setStoryManager(StoryManager $manager): self |
86
|
256 |
|
{ |
87
|
|
|
$this->stories = $manager; |
88
|
|
|
|
89
|
256 |
|
return $this; |
90
|
|
|
} |
91
|
256 |
|
|
92
|
|
|
public function setModelFactoryManager(ModelFactoryManager $manager): self |
93
|
256 |
|
{ |
94
|
|
|
$this->factories = $manager; |
95
|
|
|
|
96
|
266 |
|
return $this; |
97
|
|
|
} |
98
|
266 |
|
|
99
|
|
|
public function setFaker(Faker\Generator $faker): self |
100
|
266 |
|
{ |
101
|
|
|
$this->faker = $faker; |
102
|
|
|
|
103
|
|
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function alwaysAutoRefreshProxies(): self |
107
|
|
|
{ |
108
|
|
|
$this->defaultProxyAutoRefresh = true; |
109
|
|
|
|
110
|
|
|
return $this; |
111
|
|
|
} |
112
|
|
|
|
113
|
350 |
|
public function isFlushingEnabled(): bool |
114
|
|
|
{ |
115
|
350 |
|
return $this->flushEnabled; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function delayFlush(callable $callback): void |
119
|
350 |
|
{ |
120
|
10 |
|
$this->flushEnabled = false; |
121
|
|
|
|
122
|
|
|
$callback(); |
123
|
350 |
|
|
124
|
|
|
foreach ($this->managerRegistry()->getManagers() as $manager) { |
125
|
|
|
$manager->flush(); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
$this->flushEnabled = true; |
129
|
635 |
|
} |
130
|
|
|
|
131
|
635 |
|
/** |
132
|
|
|
* @param object|string $objectOrClass |
133
|
635 |
|
*/ |
134
|
|
|
public function repositoryFor($objectOrClass): RepositoryProxy |
135
|
|
|
{ |
136
|
|
|
if ($objectOrClass instanceof Proxy) { |
137
|
625 |
|
$objectOrClass = $objectOrClass->object(); |
138
|
|
|
} |
139
|
|
|
|
140
|
845 |
|
if (!\is_string($objectOrClass)) { |
141
|
|
|
$objectOrClass = \get_class($objectOrClass); |
142
|
845 |
|
} |
143
|
|
|
|
144
|
|
|
return new RepositoryProxy($this->managerRegistry()->getRepository($objectOrClass)); |
145
|
685 |
|
} |
146
|
|
|
|
147
|
685 |
|
/** |
148
|
10 |
|
* @param object|string $objectOrClass |
149
|
|
|
*/ |
150
|
|
|
public function objectManagerFor($objectOrClass): ObjectManager |
151
|
675 |
|
{ |
152
|
|
|
$class = \is_string($objectOrClass) ? $objectOrClass : \get_class($objectOrClass); |
153
|
|
|
|
154
|
|
|
if (!$objectManager = $this->managerRegistry()->getManagerForClass($class)) { |
155
|
|
|
throw new \RuntimeException(\sprintf('No object manager registered for "%s".', $class)); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
return $objectManager; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function hasManagerRegistry(): bool |
162
|
|
|
{ |
163
|
|
|
return null !== $this->managerRegistry; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
private function managerRegistry(): ManagerRegistry |
167
|
|
|
{ |
168
|
|
|
if (!$this->hasManagerRegistry()) { |
169
|
|
|
throw new \RuntimeException('Foundry was booted without doctrine. Ensure your TestCase extends '.KernelTestCase::class); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
return $this->managerRegistry; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|