|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Testing\Fixtures; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\DataFixtures\AbstractFixture; |
|
6
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
|
7
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
8
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\NamespaceHelper; |
|
9
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\EntityInterface; |
|
10
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Savers\EntitySaverFactory; |
|
11
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Savers\EntitySaverInterface; |
|
12
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Testing\EntityGenerator\TestEntityGenerator; |
|
13
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Testing\EntityGenerator\TestEntityGeneratorFactory; |
|
14
|
|
|
use Psr\Container\ContainerInterface; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
|
18
|
|
|
*/ |
|
19
|
|
|
abstract class AbstractEntityFixtureLoader extends AbstractFixture |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* If you override the loadBulk method, please ensure you update this number to reflect the number of Entities you |
|
23
|
|
|
* are actually generating |
|
24
|
|
|
*/ |
|
25
|
|
|
public const BULK_AMOUNT_TO_GENERATE = 100; |
|
26
|
|
|
|
|
27
|
|
|
public const REFERENCE_PREFIX = 'OVERRIDE ME'; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var TestEntityGenerator |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $testEntityGenerator; |
|
33
|
|
|
/** |
|
34
|
|
|
* @var EntitySaverInterface |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $saver; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var null|FixtureEntitiesModifierInterface |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $modifier; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var string |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $entityFqn; |
|
47
|
|
|
/** |
|
48
|
|
|
* @var NamespaceHelper |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $namespaceHelper; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var TestEntityGeneratorFactory |
|
54
|
|
|
*/ |
|
55
|
|
|
private $testEntityGeneratorFactory; |
|
56
|
|
|
/** |
|
57
|
|
|
* @var ContainerInterface |
|
58
|
|
|
*/ |
|
59
|
|
|
private $container; |
|
60
|
|
|
|
|
61
|
|
|
private $usingReferences = false; |
|
62
|
|
|
/** |
|
63
|
|
|
* @var EntityManagerInterface |
|
64
|
|
|
*/ |
|
65
|
|
|
private $entityManager; |
|
66
|
|
|
/** |
|
67
|
|
|
* @var bool |
|
68
|
|
|
*/ |
|
69
|
|
|
protected $generateCustomFixtures = false; |
|
70
|
|
|
/** |
|
71
|
|
|
* @var int |
|
72
|
|
|
*/ |
|
73
|
|
|
protected $numberToGenerate; |
|
74
|
|
|
/** |
|
75
|
|
|
* @var array |
|
76
|
|
|
*/ |
|
77
|
|
|
protected $customData; |
|
78
|
|
|
|
|
79
|
4 |
|
public function __construct( |
|
80
|
|
|
TestEntityGeneratorFactory $testEntityGeneratorFactory, |
|
81
|
|
|
EntitySaverFactory $saverFactory, |
|
82
|
|
|
NamespaceHelper $namespaceHelper, |
|
83
|
|
|
EntityManagerInterface $entityManager, |
|
84
|
|
|
ContainerInterface $container, |
|
85
|
|
|
?FixtureEntitiesModifierInterface $modifier = null |
|
86
|
|
|
) { |
|
87
|
4 |
|
$this->namespaceHelper = $namespaceHelper; |
|
88
|
4 |
|
$this->entityFqn = $this->getEntityFqn(); |
|
89
|
4 |
|
$this->saver = $saverFactory->getSaverForEntityFqn($this->entityFqn); |
|
90
|
4 |
|
if (null !== $modifier) { |
|
91
|
1 |
|
$this->setModifier($modifier); |
|
92
|
|
|
} |
|
93
|
4 |
|
$this->testEntityGeneratorFactory = $testEntityGeneratorFactory; |
|
94
|
4 |
|
$this->container = $container; |
|
95
|
4 |
|
$this->assertReferencePrefixOverridden(); |
|
96
|
4 |
|
$this->entityManager = $entityManager; |
|
97
|
4 |
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Get the fully qualified name of the Entity we are testing, |
|
101
|
|
|
* assumes EntityNameTest as the entity class short name |
|
102
|
|
|
* |
|
103
|
|
|
* @return string |
|
104
|
|
|
*/ |
|
105
|
4 |
|
protected function getEntityFqn(): string |
|
106
|
|
|
{ |
|
107
|
4 |
|
if (null === $this->entityFqn) { |
|
108
|
4 |
|
$this->entityFqn = $this->namespaceHelper->getEntityFqnFromFixtureFqn(static::class); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
4 |
|
return $this->entityFqn; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Use this method to inject your own modifier that will receive the array of generated entities and can then |
|
116
|
|
|
* update them as you see fit |
|
117
|
|
|
* |
|
118
|
|
|
* @param FixtureEntitiesModifierInterface $modifier |
|
119
|
|
|
*/ |
|
120
|
1 |
|
public function setModifier(FixtureEntitiesModifierInterface $modifier): void |
|
121
|
|
|
{ |
|
122
|
1 |
|
$this->modifier = $modifier; |
|
123
|
1 |
|
} |
|
124
|
|
|
|
|
125
|
4 |
|
private function assertReferencePrefixOverridden(): void |
|
126
|
|
|
{ |
|
127
|
4 |
|
if (static::REFERENCE_PREFIX === self::REFERENCE_PREFIX) { |
|
|
|
|
|
|
128
|
|
|
throw new \LogicException('You must override the REFERENCE_PREFIX constant in your Fixture'); |
|
129
|
|
|
} |
|
130
|
4 |
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Load data fixtures with the passed EntityManager |
|
134
|
|
|
* |
|
135
|
|
|
* @param ObjectManager $manager |
|
136
|
|
|
* |
|
137
|
|
|
* @throws \Doctrine\ORM\Mapping\MappingException |
|
138
|
|
|
* @throws \EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException |
|
139
|
|
|
* @throws \ErrorException |
|
140
|
|
|
* @throws \ReflectionException |
|
141
|
|
|
*/ |
|
142
|
3 |
|
public function load(ObjectManager $manager) |
|
143
|
|
|
{ |
|
144
|
3 |
|
if (!$manager instanceof EntityManagerInterface) { |
|
145
|
|
|
throw new \RuntimeException( |
|
146
|
|
|
'Expecting $manager to be EntityManagerInterface but got ' . \get_class($manager) |
|
147
|
|
|
); |
|
148
|
|
|
} |
|
149
|
3 |
|
$this->testEntityGenerator = $this->testEntityGeneratorFactory->createForEntityFqn($this->entityFqn, $manager); |
|
150
|
3 |
|
$this->testEntityGenerator->assertSameEntityManagerInstance($manager); |
|
151
|
3 |
|
$entities = $this->loadBulk(); |
|
152
|
3 |
|
$this->validateNumberGenerated($entities); |
|
153
|
3 |
|
$this->updateGenerated($entities); |
|
154
|
3 |
|
$this->saver->saveAll($entities); |
|
155
|
3 |
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* This method can be used to generate ad hoc fixture with specified data. To use it pass in an array of arrays, |
|
159
|
|
|
* with each child array keyed with the properties that you want to set, e.g. |
|
160
|
|
|
* [ |
|
161
|
|
|
* [ |
|
162
|
|
|
* 'propertyOne' => true, |
|
163
|
|
|
* 'propertyTwo' => DifferentEntity, |
|
164
|
|
|
* ... |
|
165
|
|
|
* ], |
|
166
|
|
|
* ... |
|
167
|
|
|
* ] |
|
168
|
|
|
* |
|
169
|
|
|
* The entity will be created as normal, with Faker data used to populate each field and then the data in the array |
|
170
|
|
|
* will be used to override the properties |
|
171
|
|
|
* |
|
172
|
|
|
* @param array $customData |
|
173
|
|
|
*/ |
|
174
|
|
|
public function setCustomData(array $customData): void |
|
175
|
|
|
{ |
|
176
|
|
|
$this->numberToGenerate = count($customData); |
|
177
|
|
|
$this->customData = $customData; |
|
178
|
|
|
$this->generateCustomFixtures = true; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* This method will be used to check that the number of entities generated matches the number expected. If you are |
|
183
|
|
|
* generating custom fixtures then this will check the number generated matches the number of custom array items |
|
184
|
|
|
* passed in, otherwise it will check the constant defined in the class |
|
185
|
|
|
* |
|
186
|
|
|
* @param array $entities |
|
187
|
|
|
*/ |
|
188
|
3 |
|
protected function validateNumberGenerated(array $entities): void |
|
189
|
|
|
{ |
|
190
|
3 |
|
$expected = $this->generateCustomFixtures === true ? $this->numberToGenerate : static::BULK_AMOUNT_TO_GENERATE; |
|
191
|
3 |
|
if (count($entities) !== $expected) { |
|
192
|
|
|
throw new \RuntimeException( |
|
193
|
|
|
'generated ' . count($entities) . |
|
194
|
|
|
' but the constant ' . get_class($this) . '::BULK_AMOUNT_TO_GENERATE is ' . $expected |
|
195
|
|
|
); |
|
196
|
|
|
} |
|
197
|
3 |
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* @return array|EntityInterface[] |
|
201
|
|
|
* @throws \Doctrine\ORM\Mapping\MappingException |
|
202
|
|
|
* @throws \ErrorException |
|
203
|
|
|
* @throws \ReflectionException |
|
204
|
|
|
*/ |
|
205
|
3 |
|
protected function loadBulk(): array |
|
206
|
|
|
{ |
|
207
|
3 |
|
if ($this->generateCustomFixtures === true) { |
|
208
|
|
|
return $this->generateCustomFixtures(); |
|
209
|
|
|
} |
|
210
|
3 |
|
$entities = $this->testEntityGenerator->generateEntities( |
|
211
|
3 |
|
static::BULK_AMOUNT_TO_GENERATE |
|
212
|
|
|
); |
|
213
|
3 |
|
$num = 0; |
|
214
|
3 |
|
foreach ($entities as $generated) { |
|
215
|
3 |
|
$this->addReference(static::REFERENCE_PREFIX . $num++, $generated); |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
3 |
|
return $entities; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* This loops over the custom array and passes the data to to a function used to create and update the entity |
|
223
|
|
|
* |
|
224
|
|
|
* @return array |
|
225
|
|
|
*/ |
|
226
|
|
|
protected function generateCustomFixtures(): array |
|
227
|
|
|
{ |
|
228
|
|
|
$customFixtures = []; |
|
229
|
|
|
for ($numberToGenerate = 0; $numberToGenerate < $this->numberToGenerate; $numberToGenerate++) { |
|
230
|
|
|
$customFixtures[] = $this->generateCustomFixture($this->customData[$numberToGenerate], $numberToGenerate); |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
return $customFixtures; |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* This is used to create the custom entity. It can be overwritten if you want to use customise it further, e.g. |
|
238
|
|
|
* using the same vaule for each entity. The method is passed the array of custom data and the number, zero indexed, |
|
239
|
|
|
* of the entity being generated |
|
240
|
|
|
* |
|
241
|
|
|
* @param array $customData |
|
242
|
|
|
* @param int $fixtureNumber |
|
243
|
|
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter) - We don't need the fixture number in this method, but it may be |
|
244
|
|
|
* useful if the method is overwritten |
|
245
|
|
|
* |
|
246
|
|
|
* @return EntityInterface |
|
247
|
|
|
*/ |
|
248
|
|
|
protected function generateCustomFixture(array $customData, int $fixtureNumber): EntityInterface |
|
|
|
|
|
|
249
|
|
|
{ |
|
250
|
|
|
return $this->testEntityGenerator->create($customData); |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
3 |
|
public function addReference($name, $object) |
|
254
|
|
|
{ |
|
255
|
3 |
|
if (false === $this->usingReferences) { |
|
256
|
|
|
return; |
|
257
|
|
|
} |
|
258
|
3 |
|
parent::addReference($name, $object); |
|
259
|
3 |
|
} |
|
260
|
|
|
|
|
261
|
3 |
|
protected function updateGenerated(array &$entities) |
|
262
|
|
|
{ |
|
263
|
3 |
|
if (null === $this->modifier) { |
|
264
|
3 |
|
return; |
|
265
|
|
|
} |
|
266
|
1 |
|
$this->modifier->modifyEntities($entities); |
|
267
|
1 |
|
} |
|
268
|
|
|
|
|
269
|
3 |
|
public function setReferenceRepository(\Doctrine\Common\DataFixtures\ReferenceRepository $referenceRepository) |
|
270
|
|
|
{ |
|
271
|
3 |
|
$this->setUsingReferences(true); |
|
272
|
3 |
|
parent::setReferenceRepository($referenceRepository); // TODO: Change the autogenerated stub |
|
273
|
3 |
|
} |
|
274
|
|
|
|
|
275
|
|
|
/** |
|
276
|
|
|
* @param bool $usingReferences |
|
277
|
|
|
* |
|
278
|
|
|
* @return AbstractEntityFixtureLoader |
|
279
|
|
|
*/ |
|
280
|
3 |
|
public function setUsingReferences(bool $usingReferences): AbstractEntityFixtureLoader |
|
281
|
|
|
{ |
|
282
|
3 |
|
$this->usingReferences = $usingReferences; |
|
283
|
|
|
|
|
284
|
3 |
|
return $this; |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
3 |
|
public function getReference($name): EntityInterface |
|
288
|
|
|
{ |
|
289
|
3 |
|
$reference = parent::getReference($name); |
|
290
|
3 |
|
$this->entityManager->initializeObject($reference); |
|
291
|
3 |
|
if ($reference instanceof EntityInterface) { |
|
292
|
3 |
|
return $reference; |
|
293
|
|
|
} |
|
294
|
|
|
throw new \RuntimeException('Failed initialising refernce into Entity'); |
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
|
|
/** |
|
298
|
|
|
* Generally we should avoid using the container as a service locator, however for test assets it is acceptable if |
|
299
|
|
|
* really necessary |
|
300
|
|
|
* |
|
301
|
|
|
* @return ContainerInterface |
|
302
|
|
|
*/ |
|
303
|
|
|
protected function getContainer(): ContainerInterface |
|
304
|
|
|
{ |
|
305
|
|
|
return $this->container; |
|
306
|
|
|
} |
|
307
|
|
|
} |
|
308
|
|
|
|