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