1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\Tests\Large\Entity\Testing\Fixtures; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Cache\FilesystemCache; |
6
|
|
|
use Doctrine\Common\DataFixtures\Loader; |
7
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\NamespaceHelper; |
8
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Factory\EntityFactory; |
9
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Interfaces\String\EnumFieldInterface; |
10
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Traits\String\EnumFieldTrait; |
11
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\EntityInterface; |
12
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Savers\EntitySaverFactory; |
13
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Testing\EntityGenerator\TestEntityGeneratorFactory; |
14
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Testing\Fixtures\AbstractEntityFixtureLoader; |
15
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Testing\Fixtures\FixtureEntitiesModifierInterface; |
16
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Testing\Fixtures\FixturesHelper; |
17
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Schema\Database; |
18
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Schema\Schema; |
19
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Tests\Assets\AbstractLargeTest; |
20
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Tests\Assets\AbstractTest; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @covers \EdmondsCommerce\DoctrineStaticMeta\Entity\Testing\Fixtures\AbstractEntityFixtureLoader |
24
|
|
|
* @covers \EdmondsCommerce\DoctrineStaticMeta\Entity\Testing\Fixtures\FixturesHelper |
25
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
26
|
|
|
*/ |
27
|
|
|
class FixtureLoaderAndHelperTest extends AbstractLargeTest |
28
|
|
|
{ |
29
|
|
|
public const TEST_PROJECT_ROOT_NAMESPACE = 'Fixtures\\Test'; |
30
|
|
|
|
31
|
|
|
public const WORK_DIR = AbstractTest::VAR_PATH . |
32
|
|
|
self::TEST_TYPE_LARGE . |
33
|
|
|
'/FixturesTest'; |
34
|
|
|
|
35
|
|
|
private const ENTITY_WITHOUT_MODIFIER = self::TEST_PROJECT_ROOT_NAMESPACE . '\\Entities\\Person'; |
36
|
|
|
|
37
|
|
|
private const ENTITY_WITH_MODIFIER = self::TEST_PROJECT_ROOT_NAMESPACE . '\\Entities\\Attributes\\Address'; |
38
|
|
|
|
39
|
|
|
protected static $buildOnce = true; |
40
|
|
|
/** |
41
|
|
|
* @var FixturesHelper |
42
|
|
|
*/ |
43
|
|
|
private $helper; |
44
|
|
|
|
45
|
|
|
public function setup(): void |
46
|
|
|
{ |
47
|
|
|
parent::setUp(); |
48
|
|
|
if (false === self::$built) { |
49
|
|
|
$this->getTestCodeGenerator() |
50
|
|
|
->copyTo(self::WORK_DIR, self::TEST_PROJECT_ROOT_NAMESPACE); |
51
|
|
|
$this->getFieldSetter() |
52
|
|
|
->setEntityHasField( |
53
|
|
|
self::ENTITY_WITHOUT_MODIFIER, |
54
|
|
|
EnumFieldTrait::class |
55
|
|
|
); |
56
|
|
|
self::$built = true; |
57
|
|
|
} |
58
|
|
|
$this->setupCopiedWorkDirAndCreateDatabase(); |
59
|
|
|
$cacheDir = $this->copiedWorkDir . '/cache'; |
60
|
|
|
mkdir($cacheDir, 0777, true); |
61
|
|
|
$this->helper = new FixturesHelper( |
62
|
|
|
$this->getEntityManager(), |
63
|
|
|
$this->container->get(Database::class), |
64
|
|
|
$this->container->get(Schema::class), |
65
|
|
|
new FilesystemCache($cacheDir) |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @test |
71
|
|
|
* @large |
72
|
|
|
*/ |
73
|
|
|
public function itLoadsAllTheFixturesWithRandomDataByDefault(): array |
74
|
|
|
{ |
75
|
|
|
$this->helper->setCacheKey(__CLASS__ . '_unmodified'); |
76
|
|
|
$fixture = $this->getUnmodifiedFixture(); |
77
|
|
|
$this->helper->addFixture($fixture); |
78
|
|
|
$this->helper->createDb(); |
79
|
|
|
$actual = $this->getEntityManager() |
80
|
|
|
->getRepository($this->getCopiedFqn(self::ENTITY_WITHOUT_MODIFIER)) |
81
|
|
|
->findAll(); |
82
|
|
|
$actualCount = count($actual); |
83
|
|
|
self::assertSame(AbstractEntityFixtureLoader::BULK_AMOUNT_TO_GENERATE, $actualCount); |
84
|
|
|
|
85
|
|
|
return $actual; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
private function getUnmodifiedFixture(): AbstractEntityFixtureLoader |
89
|
|
|
{ |
90
|
|
|
return $this->getFixture($this->getCopiedFqn(self::ENTITY_WITHOUT_MODIFIER)); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
private function getFixture( |
94
|
|
|
string $entityFqn, |
95
|
|
|
?FixtureEntitiesModifierInterface $modifier = null |
96
|
|
|
): AbstractEntityFixtureLoader { |
97
|
|
|
$fixtureFqn = $this->getNamespaceHelper()->getFixtureFqnFromEntityFqn($entityFqn); |
98
|
|
|
|
99
|
|
|
return new $fixtureFqn( |
100
|
|
|
$this->container->get(TestEntityGeneratorFactory::class), |
101
|
|
|
$this->container->get(EntitySaverFactory::class), |
102
|
|
|
$this->container->get(NamespaceHelper::class), |
103
|
|
|
$modifier |
104
|
|
|
); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @test |
109
|
|
|
* @large |
110
|
|
|
* @depends itLoadsAllTheFixturesWithRandomDataByDefault |
111
|
|
|
* |
112
|
|
|
* @param array $loadedFirstTime |
113
|
|
|
* |
114
|
|
|
* @return array |
115
|
|
|
*/ |
116
|
|
|
public function itUsesTheCacheTheSecondTime(array $loadedFirstTime): array |
117
|
|
|
{ |
118
|
|
|
$this->getFileSystem() |
119
|
|
|
->mirror( |
120
|
|
|
$this->copiedWorkDir . |
121
|
|
|
'/../FixtureLoaderAndHelperTest_ItLoadsAllTheFixturesWithRandomDataByDefault_/cache', |
122
|
|
|
$this->copiedWorkDir . '/cache' |
123
|
|
|
); |
124
|
|
|
$this->helper->setCacheKey(__CLASS__ . '_unmodified'); |
125
|
|
|
$fixture = $this->getUnmodifiedFixture(); |
126
|
|
|
$this->helper->addFixture($fixture); |
127
|
|
|
$this->helper->createDb(); |
128
|
|
|
self::assertTrue($this->helper->isLoadedFromCache()); |
129
|
|
|
/** |
130
|
|
|
* @var EntityInterface[] $loadedSecondTime |
131
|
|
|
*/ |
132
|
|
|
$loadedSecondTime = $this->getEntityManager() |
133
|
|
|
->getRepository($this->getCopiedFqn(self::ENTITY_WITHOUT_MODIFIER)) |
134
|
|
|
->findAll(); |
135
|
|
|
$actualCount = count($loadedSecondTime); |
136
|
|
|
$expectedCount = count($loadedFirstTime); |
137
|
|
|
self::assertSame($expectedCount, $actualCount); |
138
|
|
|
foreach ($loadedSecondTime as $key => $actualEntity) { |
139
|
|
|
$expectedEntity = $loadedFirstTime[$key]; |
140
|
|
|
$actualId = $actualEntity->getId(); |
141
|
|
|
$expectedId = $expectedEntity->getId(); |
142
|
|
|
$expectedText = $expectedEntity->getString(); |
143
|
|
|
$actualText = $actualEntity->getString(); |
|
|
|
|
144
|
|
|
self::assertEquals($expectedId, $actualId, 'Cached Entity ID does not match'); |
145
|
|
|
self::assertEquals($expectedText, $actualText, 'Cached Faker data does not match'); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return $loadedSecondTime; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @test |
153
|
|
|
* @large |
154
|
|
|
* @depends itUsesTheCacheTheSecondTime |
155
|
|
|
* |
156
|
|
|
* @param array $loadedSecondTime |
157
|
|
|
* |
158
|
|
|
* @throws \EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException |
159
|
|
|
* @throws \ReflectionException |
160
|
|
|
*/ |
161
|
|
|
public function itCanBeConfiguredNotToLoadFromTheCache(array $loadedSecondTime): void |
162
|
|
|
{ |
163
|
|
|
$this->getFileSystem() |
164
|
|
|
->mirror( |
165
|
|
|
$this->copiedWorkDir . |
166
|
|
|
'/../FixtureLoaderAndHelperTest_ItUsesTheCacheTheSecondTime_/cache', |
167
|
|
|
$this->copiedWorkDir . '/cache' |
168
|
|
|
); |
169
|
|
|
$this->helper->setCacheKey(__CLASS__ . '_unmodified'); |
170
|
|
|
$fixture = $this->getUnmodifiedFixture(); |
171
|
|
|
$this->helper->setLoadFromCache(false); |
172
|
|
|
$this->helper->addFixture($fixture); |
173
|
|
|
$this->helper->createDb(); |
174
|
|
|
self::assertFalse($this->helper->isLoadedFromCache()); |
175
|
|
|
/** |
176
|
|
|
* @var EntityInterface[] $loadedThirdTime |
177
|
|
|
*/ |
178
|
|
|
$loadedThirdTime = $this->getEntityManager() |
179
|
|
|
->getRepository($this->getCopiedFqn(self::ENTITY_WITHOUT_MODIFIER)) |
180
|
|
|
->findAll(); |
181
|
|
|
$actualCount = count($loadedThirdTime); |
182
|
|
|
$expectedCount = count($loadedSecondTime); |
183
|
|
|
self::assertSame($expectedCount, $actualCount); |
184
|
|
|
foreach ($loadedThirdTime as $key => $actualEntity) { |
185
|
|
|
$loadedSecondTimeEntity = $loadedSecondTime[$key]; |
186
|
|
|
$actualId = $actualEntity->getId(); |
187
|
|
|
$secondTimeEntityId = $loadedSecondTimeEntity->getId(); |
188
|
|
|
$secondTimeText = $loadedSecondTimeEntity->getString(); |
189
|
|
|
$actualText = $actualEntity->getString(); |
190
|
|
|
self::assertNotEquals($secondTimeEntityId, $actualId, 'Cached Entity ID matches, this should not happen'); |
191
|
|
|
self::assertNotEquals($secondTimeText, $actualText, 'Cached Faker data matches, this should not happen'); |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @test |
197
|
|
|
* @large |
198
|
|
|
*/ |
199
|
|
|
public function itCanTakeAModifierToCustomiseTheFixtures() |
200
|
|
|
{ |
201
|
|
|
$this->helper->setCacheKey(__CLASS__ . '_modified'); |
202
|
|
|
$fixture = $this->getModifiedFixture(); |
203
|
|
|
$this->helper->addFixture($fixture); |
204
|
|
|
$this->helper->createDb(); |
205
|
|
|
/** |
206
|
|
|
* @var EntityInterface[] $actual |
207
|
|
|
*/ |
208
|
|
|
$actual = $this->getEntityManager() |
209
|
|
|
->getRepository($this->getCopiedFqn(self::ENTITY_WITH_MODIFIER)) |
210
|
|
|
->findAll(); |
211
|
|
|
$actualCount = count($actual); |
212
|
|
|
self::assertSame(AbstractEntityFixtureLoader::BULK_AMOUNT_TO_GENERATE + 1, $actualCount); |
213
|
|
|
$firstEntity = $actual[0]; |
214
|
|
|
$expectedString = 'This has been overridden'; |
215
|
|
|
$actualString = $firstEntity->getString(); |
216
|
|
|
self::assertSame($expectedString, $actualString); |
217
|
|
|
end($actual); |
218
|
|
|
$lastEntity = current($actual); |
219
|
|
|
$expectedString = 'This has been created'; |
220
|
|
|
$actualString = $lastEntity->getString(); |
221
|
|
|
self::assertSame($expectedString, $actualString); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
private function getModifiedFixture(): AbstractEntityFixtureLoader |
225
|
|
|
{ |
226
|
|
|
return $this->getFixture( |
227
|
|
|
$this->getCopiedFqn(self::ENTITY_WITH_MODIFIER), |
228
|
|
|
$this->getFixtureModifier() |
229
|
|
|
); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
private function getFixtureModifier(): FixtureEntitiesModifierInterface |
233
|
|
|
{ |
234
|
|
|
return new class( |
235
|
|
|
$this->getCopiedFqn(self::ENTITY_WITH_MODIFIER), |
236
|
|
|
$this->getEntityFactory() |
237
|
|
|
) |
238
|
|
|
implements FixtureEntitiesModifierInterface |
239
|
|
|
{ |
240
|
|
|
/** |
241
|
|
|
* @var string |
242
|
|
|
*/ |
243
|
|
|
protected $entityFqn; |
244
|
|
|
/** |
245
|
|
|
* @var EntityFactory |
246
|
|
|
*/ |
247
|
|
|
protected $factory; |
248
|
|
|
/** |
249
|
|
|
* @var array|EntityInterface[] |
250
|
|
|
*/ |
251
|
|
|
private $entities; |
252
|
|
|
|
253
|
|
|
public function __construct(string $entityFqn, EntityFactory $factory) |
254
|
|
|
{ |
255
|
|
|
$this->entityFqn = $entityFqn; |
256
|
|
|
$this->factory = $factory; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Update the entities array by reference |
261
|
|
|
* |
262
|
|
|
* @param array $entities |
263
|
|
|
*/ |
264
|
|
|
public function modifyEntities(array &$entities): void |
265
|
|
|
{ |
266
|
|
|
$this->entities = &$entities; |
267
|
|
|
$this->updateFirstEntity(); |
268
|
|
|
$this->addAnotherEntity(); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
private function updateFirstEntity(): void |
272
|
|
|
{ |
273
|
|
|
$this->entities[0]->setString('This has been overridden'); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
private function addAnotherEntity(): void |
277
|
|
|
{ |
278
|
|
|
$entity = $this->factory->create($this->entityFqn); |
279
|
|
|
$entity->setString('This has been created'); |
|
|
|
|
280
|
|
|
$this->entities[] = $entity; |
281
|
|
|
} |
282
|
|
|
}; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* @test |
287
|
|
|
* @large |
288
|
|
|
*/ |
289
|
|
|
public function theOrderOfFixtureLoadingCanBeSet(): void |
290
|
|
|
{ |
291
|
|
|
$loader = new Loader(); |
292
|
|
|
$fixture1 = $this->getModifiedFixture(); |
293
|
|
|
$loader->addFixture($fixture1); |
294
|
|
|
$fixture2 = $this->getUnmodifiedFixture(); |
295
|
|
|
$fixture2->setOrder(AbstractEntityFixtureLoader::ORDER_FIRST); |
296
|
|
|
$loader->addFixture($fixture2); |
297
|
|
|
$orderedFixtures = $loader->getFixtures(); |
298
|
|
|
self::assertSame($fixture2, current($orderedFixtures)); |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* @test |
303
|
|
|
* @large |
304
|
|
|
* @throws \EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException |
305
|
|
|
* @throws \ReflectionException |
306
|
|
|
*/ |
307
|
|
|
public function fixturesUseTheCorrectFakerDataProviders(): void |
308
|
|
|
{ |
309
|
|
|
$entityFqn = $this->getCopiedFqn(self::ENTITY_WITHOUT_MODIFIER); |
310
|
|
|
|
311
|
|
|
$this->helper->setCacheKey(__CLASS__ . '_faker'); |
312
|
|
|
$fixture = $this->getUnmodifiedFixture(); |
313
|
|
|
$this->helper->addFixture($fixture); |
314
|
|
|
$this->helper->createDb(); |
315
|
|
|
$actual = $this->getEntityManager() |
316
|
|
|
->getRepository($entityFqn) |
317
|
|
|
->findAll(); |
318
|
|
|
/** |
319
|
|
|
* @var EntityInterface $entity |
320
|
|
|
*/ |
321
|
|
|
foreach ($actual as $entity) { |
322
|
|
|
self::assertContains($entity->getEnum(), EnumFieldInterface::ENUM_OPTIONS); |
323
|
|
|
} |
324
|
|
|
} |
325
|
|
|
} |
326
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.