|
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
|
692 |
|
|
|
35
|
|
|
public function __construct() |
|
36
|
692 |
|
{ |
|
37
|
692 |
|
$this->stories = new StoryManager([]); |
|
38
|
692 |
|
$this->factories = new ModelFactoryManager([]); |
|
39
|
692 |
|
$this->faker = Faker\Factory::create(); |
|
40
|
692 |
|
$this->instantiator = new Instantiator(); |
|
41
|
692 |
|
} |
|
42
|
|
|
|
|
43
|
239 |
|
public function stories(): StoryManager |
|
44
|
|
|
{ |
|
45
|
239 |
|
return $this->stories; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
417 |
|
public function factories(): ModelFactoryManager |
|
49
|
|
|
{ |
|
50
|
417 |
|
return $this->factories; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
497 |
|
public function faker(): Faker\Generator |
|
54
|
|
|
{ |
|
55
|
497 |
|
return $this->faker; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
556 |
|
public function instantiator(): callable |
|
59
|
|
|
{ |
|
60
|
556 |
|
return $this->instantiator; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
608 |
|
public function defaultProxyAutoRefresh(): bool |
|
64
|
|
|
{ |
|
65
|
608 |
|
return $this->defaultProxyAutoRefresh; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
489 |
|
public function setManagerRegistry(ManagerRegistry $managerRegistry): self |
|
69
|
|
|
{ |
|
70
|
489 |
|
$this->managerRegistry = $managerRegistry; |
|
71
|
|
|
|
|
72
|
489 |
|
return $this; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
179 |
|
public function setInstantiator(callable $instantiator): self |
|
76
|
|
|
{ |
|
77
|
179 |
|
$this->instantiator = $instantiator; |
|
78
|
|
|
|
|
79
|
179 |
|
return $this; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
179 |
|
public function setStoryManager(StoryManager $manager): self |
|
83
|
|
|
{ |
|
84
|
179 |
|
$this->stories = $manager; |
|
85
|
|
|
|
|
86
|
179 |
|
return $this; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function setModelFactoryManager(ModelFactoryManager $manager): self |
|
90
|
|
|
{ |
|
91
|
|
|
$this->factories = $manager; |
|
92
|
|
|
|
|
93
|
|
|
return $this; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function setFaker(Faker\Generator $faker): self |
|
97
|
|
|
{ |
|
98
|
|
|
$this->faker = $faker; |
|
99
|
248 |
|
|
|
100
|
|
|
return $this; |
|
101
|
248 |
|
} |
|
102
|
|
|
|
|
103
|
|
|
public function alwaysAutoRefreshProxies(): self |
|
104
|
|
|
{ |
|
105
|
248 |
|
$this->defaultProxyAutoRefresh = true; |
|
106
|
8 |
|
|
|
107
|
|
|
return $this; |
|
108
|
|
|
} |
|
109
|
248 |
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @param object|string $objectOrClass |
|
112
|
|
|
*/ |
|
113
|
|
|
public function repositoryFor($objectOrClass): RepositoryProxy |
|
114
|
|
|
{ |
|
115
|
436 |
|
if ($objectOrClass instanceof Proxy) { |
|
116
|
|
|
$objectOrClass = $objectOrClass->object(); |
|
117
|
436 |
|
} |
|
118
|
|
|
|
|
119
|
436 |
|
if (!\is_string($objectOrClass)) { |
|
120
|
|
|
$objectOrClass = \get_class($objectOrClass); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
436 |
|
return new RepositoryProxy($this->managerRegistry()->getRepository($objectOrClass)); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @param object|string $objectOrClass |
|
128
|
|
|
*/ |
|
129
|
|
|
public function objectManagerFor($objectOrClass): ObjectManager |
|
130
|
|
|
{ |
|
131
|
|
|
$class = \is_string($objectOrClass) ? $objectOrClass : \get_class($objectOrClass); |
|
132
|
|
|
|
|
133
|
|
|
if (!$objectManager = $this->managerRegistry()->getManagerForClass($class)) { |
|
134
|
|
|
throw new \RuntimeException(\sprintf('No object manager registered for "%s".', $class)); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
return $objectManager; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
private function managerRegistry(): ManagerRegistry |
|
141
|
|
|
{ |
|
142
|
|
|
if (!$this->managerRegistry) { |
|
143
|
|
|
throw new \RuntimeException('Foundry was booted without doctrine. Ensure your TestCase extends '.KernelTestCase::class); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
return $this->managerRegistry; |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|