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
|
|
|
public function __construct( |
68
|
|
|
TestEntityGeneratorFactory $testEntityGeneratorFactory, |
69
|
|
|
EntitySaverFactory $saverFactory, |
70
|
|
|
NamespaceHelper $namespaceHelper, |
71
|
|
|
EntityManagerInterface $entityManager, |
72
|
|
|
ContainerInterface $container, |
73
|
|
|
?FixtureEntitiesModifierInterface $modifier = null |
74
|
|
|
) { |
75
|
|
|
$this->namespaceHelper = $namespaceHelper; |
76
|
|
|
$this->entityFqn = $this->getEntityFqn(); |
77
|
|
|
$this->saver = $saverFactory->getSaverForEntityFqn($this->entityFqn); |
78
|
|
|
if (null !== $modifier) { |
79
|
|
|
$this->setModifier($modifier); |
80
|
|
|
} |
81
|
|
|
$this->testEntityGeneratorFactory = $testEntityGeneratorFactory; |
82
|
|
|
$this->container = $container; |
83
|
|
|
$this->assertReferencePrefixOverridden(); |
84
|
|
|
$this->entityManager = $entityManager; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Get the fully qualified name of the Entity we are testing, |
89
|
|
|
* assumes EntityNameTest as the entity class short name |
90
|
|
|
* |
91
|
|
|
* @return string |
92
|
|
|
*/ |
93
|
|
|
protected function getEntityFqn(): string |
94
|
|
|
{ |
95
|
|
|
if (null === $this->entityFqn) { |
96
|
|
|
$this->entityFqn = $this->namespaceHelper->getEntityFqnFromFixtureFqn(static::class); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $this->entityFqn; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Use this method to inject your own modifier that will receive the array of generated entities and can then |
104
|
|
|
* update them as you see fit |
105
|
|
|
* |
106
|
|
|
* @param FixtureEntitiesModifierInterface $modifier |
107
|
|
|
*/ |
108
|
|
|
public function setModifier(FixtureEntitiesModifierInterface $modifier): void |
109
|
|
|
{ |
110
|
|
|
$this->modifier = $modifier; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
private function assertReferencePrefixOverridden(): void |
114
|
|
|
{ |
115
|
|
|
if (static::REFERENCE_PREFIX === self::REFERENCE_PREFIX) { |
|
|
|
|
116
|
|
|
throw new \LogicException('You must override the REFERENCE_PREFIX constant in your Fixture'); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Load data fixtures with the passed EntityManager |
122
|
|
|
* |
123
|
|
|
* @param ObjectManager $manager |
124
|
|
|
* |
125
|
|
|
* @throws \Doctrine\ORM\Mapping\MappingException |
126
|
|
|
* @throws \EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException |
127
|
|
|
* @throws \ErrorException |
128
|
|
|
* @throws \ReflectionException |
129
|
|
|
*/ |
130
|
|
|
public function load(ObjectManager $manager) |
131
|
|
|
{ |
132
|
|
|
if (!$manager instanceof EntityManagerInterface) { |
133
|
|
|
throw new \RuntimeException( |
134
|
|
|
'Expecting $manager to be EntityManagerInterface but got ' . \get_class($manager) |
135
|
|
|
); |
136
|
|
|
} |
137
|
|
|
$this->testEntityGenerator = $this->testEntityGeneratorFactory->createForEntityFqn($this->entityFqn, $manager); |
138
|
|
|
$this->testEntityGenerator->assertSameEntityManagerInstance($manager); |
139
|
|
|
$entities = $this->loadBulk(); |
140
|
|
|
if (count($entities) !== static::BULK_AMOUNT_TO_GENERATE) { |
141
|
|
|
throw new \RuntimeException( |
142
|
|
|
'generated ' . count($entities) . |
143
|
|
|
' but the constant ' . get_class($this) . '::BULK_AMOUNT_TO_GENERATE is ' . |
144
|
|
|
static::BULK_AMOUNT_TO_GENERATE |
145
|
|
|
); |
146
|
|
|
} |
147
|
|
|
$this->updateGenerated($entities); |
148
|
|
|
$this->saver->saveAll($entities); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @return array|EntityInterface[] |
153
|
|
|
* @throws \Doctrine\ORM\Mapping\MappingException |
154
|
|
|
* @throws \ErrorException |
155
|
|
|
* @throws \ReflectionException |
156
|
|
|
*/ |
157
|
|
|
protected function loadBulk(): array |
158
|
|
|
{ |
159
|
|
|
$entities = $this->testEntityGenerator->generateEntities( |
160
|
|
|
static::BULK_AMOUNT_TO_GENERATE |
161
|
|
|
); |
162
|
|
|
$num = 0; |
163
|
|
|
foreach ($entities as $generated) { |
164
|
|
|
$this->addReference(static::REFERENCE_PREFIX . $num++, $generated); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
return $entities; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
public function addReference($name, $object) |
171
|
|
|
{ |
172
|
|
|
if (false === $this->usingReferences) { |
173
|
|
|
return; |
174
|
|
|
} |
175
|
|
|
parent::addReference($name, $object); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
protected function updateGenerated(array &$entities) |
179
|
|
|
{ |
180
|
|
|
if (null === $this->modifier) { |
181
|
|
|
return; |
182
|
|
|
} |
183
|
|
|
$this->modifier->modifyEntities($entities); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
public function setReferenceRepository(\Doctrine\Common\DataFixtures\ReferenceRepository $referenceRepository) |
187
|
|
|
{ |
188
|
|
|
$this->setUsingReferences(true); |
189
|
|
|
parent::setReferenceRepository($referenceRepository); // TODO: Change the autogenerated stub |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @param bool $usingReferences |
194
|
|
|
* |
195
|
|
|
* @return AbstractEntityFixtureLoader |
196
|
|
|
*/ |
197
|
|
|
public function setUsingReferences(bool $usingReferences): AbstractEntityFixtureLoader |
198
|
|
|
{ |
199
|
|
|
$this->usingReferences = $usingReferences; |
200
|
|
|
|
201
|
|
|
return $this; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
public function getReference($name): EntityInterface |
205
|
|
|
{ |
206
|
|
|
$reference = parent::getReference($name); |
207
|
|
|
$this->entityManager->initializeObject($reference); |
208
|
|
|
if ($reference instanceof EntityInterface) { |
209
|
|
|
return $reference; |
210
|
|
|
} |
211
|
|
|
throw new \RuntimeException('Failed initialising refernce into Entity'); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Generally we should avoid using the container as a service locator, however for test assets it is acceptable if |
216
|
|
|
* really necessary |
217
|
|
|
* |
218
|
|
|
* @return ContainerInterface |
219
|
|
|
*/ |
220
|
|
|
protected function getContainer(): ContainerInterface |
221
|
|
|
{ |
222
|
|
|
return $this->container; |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
|