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\DataFixtures\DependentFixtureInterface; |
7
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
8
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
9
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\NamespaceHelper; |
10
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\EntityInterface; |
11
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Savers\EntitySaverFactory; |
12
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Savers\EntitySaverInterface; |
13
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Testing\EntityGenerator\TestEntityGenerator; |
14
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Testing\EntityGenerator\TestEntityGeneratorFactory; |
15
|
|
|
use Psr\Container\ContainerInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
19
|
|
|
*/ |
20
|
|
|
abstract class AbstractEntityFixtureLoader extends AbstractFixture |
21
|
|
|
{ |
22
|
|
|
public const BULK_AMOUNT_TO_GENERATE = 100; |
23
|
|
|
/** |
24
|
|
|
* @var TestEntityGenerator |
25
|
|
|
*/ |
26
|
|
|
protected $testEntityGenerator; |
27
|
|
|
/** |
28
|
|
|
* @var EntitySaverInterface |
29
|
|
|
*/ |
30
|
|
|
protected $saver; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var null|FixtureEntitiesModifierInterface |
34
|
|
|
*/ |
35
|
|
|
protected $modifier; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
protected $entityFqn; |
41
|
|
|
/** |
42
|
|
|
* @var NamespaceHelper |
43
|
|
|
*/ |
44
|
|
|
protected $namespaceHelper; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var TestEntityGeneratorFactory |
48
|
|
|
*/ |
49
|
|
|
private $testEntityGeneratorFactory; |
50
|
|
|
/** |
51
|
|
|
* @var ContainerInterface |
52
|
|
|
*/ |
53
|
|
|
private $container; |
54
|
|
|
|
55
|
|
|
private $usingReferences = true; |
56
|
|
|
|
57
|
|
|
public function __construct( |
58
|
|
|
TestEntityGeneratorFactory $testEntityGeneratorFactory, |
59
|
|
|
EntitySaverFactory $saverFactory, |
60
|
|
|
NamespaceHelper $namespaceHelper, |
61
|
|
|
ContainerInterface $container, |
62
|
|
|
?FixtureEntitiesModifierInterface $modifier = null |
63
|
|
|
) { |
64
|
|
|
$this->namespaceHelper = $namespaceHelper; |
65
|
|
|
$this->entityFqn = $this->getEntityFqn(); |
66
|
|
|
$this->saver = $saverFactory->getSaverForEntityFqn($this->entityFqn); |
67
|
|
|
if (null !== $modifier) { |
68
|
|
|
$this->setModifier($modifier); |
69
|
|
|
} |
70
|
|
|
$this->testEntityGeneratorFactory = $testEntityGeneratorFactory; |
71
|
|
|
$this->container = $container; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Get the fully qualified name of the Entity we are testing, |
76
|
|
|
* assumes EntityNameTest as the entity class short name |
77
|
|
|
* |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
|
|
protected function getEntityFqn(): string |
81
|
|
|
{ |
82
|
|
|
if (null === $this->entityFqn) { |
83
|
|
|
$this->entityFqn = $this->namespaceHelper->getEntityFqnFromFixtureFqn(static::class); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $this->entityFqn; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Use this method to inject your own modifier that will receive the array of generated entities and can then |
91
|
|
|
* update them as you see fit |
92
|
|
|
* |
93
|
|
|
* @param FixtureEntitiesModifierInterface $modifier |
94
|
|
|
*/ |
95
|
|
|
public function setModifier(FixtureEntitiesModifierInterface $modifier): void |
96
|
|
|
{ |
97
|
|
|
$this->modifier = $modifier; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Load data fixtures with the passed EntityManager |
102
|
|
|
* |
103
|
|
|
* @param ObjectManager $manager |
104
|
|
|
* |
105
|
|
|
* @throws \Doctrine\ORM\Mapping\MappingException |
106
|
|
|
* @throws \EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException |
107
|
|
|
* @throws \ErrorException |
108
|
|
|
* @throws \ReflectionException |
109
|
|
|
*/ |
110
|
|
|
public function load(ObjectManager $manager) |
111
|
|
|
{ |
112
|
|
|
if (!$manager instanceof EntityManagerInterface) { |
113
|
|
|
throw new \RuntimeException( |
114
|
|
|
'Expecting $manager to be EntityManagerInterface but got ' . \get_class($manager) |
115
|
|
|
); |
116
|
|
|
} |
117
|
|
|
$this->testEntityGenerator = $this->testEntityGeneratorFactory->createForEntityFqn($this->entityFqn, $manager); |
118
|
|
|
$this->testEntityGenerator->assertSameEntityManagerInstance($manager); |
119
|
|
|
$entities = $this->loadBulk(); |
120
|
|
|
$this->updateGenerated($entities); |
121
|
|
|
$this->saver->saveAll($entities); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return array|EntityInterface[] |
126
|
|
|
* @throws \Doctrine\ORM\Mapping\MappingException |
127
|
|
|
* @throws \ErrorException |
128
|
|
|
* @throws \ReflectionException |
129
|
|
|
*/ |
130
|
|
|
protected function loadBulk(): array |
131
|
|
|
{ |
132
|
|
|
$entities = $this->testEntityGenerator->generateEntities( |
133
|
|
|
static::BULK_AMOUNT_TO_GENERATE |
134
|
|
|
); |
135
|
|
|
foreach ($entities as $generated) { |
136
|
|
|
$this->testEntityGenerator->addAssociationEntities($generated); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return $entities; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
protected function updateGenerated(array &$entities) |
143
|
|
|
{ |
144
|
|
|
if (null === $this->modifier) { |
145
|
|
|
return; |
146
|
|
|
} |
147
|
|
|
$this->modifier->modifyEntities($entities); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param bool $usingReferences |
152
|
|
|
* |
153
|
|
|
* @return AbstractEntityFixtureLoader |
154
|
|
|
*/ |
155
|
|
|
public function setUsingReferences(bool $usingReferences): AbstractEntityFixtureLoader |
156
|
|
|
{ |
157
|
|
|
$this->usingReferences = $usingReferences; |
158
|
|
|
|
159
|
|
|
return $this; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function addReference($name, $object) |
163
|
|
|
{ |
164
|
|
|
if (false === $this->usingReferences) { |
165
|
|
|
return; |
166
|
|
|
} |
167
|
|
|
parent::addReference($name, $object); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Generally we should avoid using the container as a service locator, however for test assets it is acceptable if |
172
|
|
|
* really necessary |
173
|
|
|
* |
174
|
|
|
* @return ContainerInterface |
175
|
|
|
*/ |
176
|
|
|
protected function getContainer(): ContainerInterface |
177
|
|
|
{ |
178
|
|
|
return $this->container; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
|
182
|
|
|
} |
183
|
|
|
|