1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\Tests\Large\G\Entity\Testing\Fixtures; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Cache\FilesystemCache; |
6
|
|
|
use Doctrine\Common\DataFixtures\Loader; |
7
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\DataTransferObjects\DtoFactory; |
8
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Factory\EntityFactoryInterface; |
9
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Factories\UuidFactory; |
10
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Interfaces\String\EnumFieldInterface; |
11
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\DataTransferObjectInterface; |
12
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\EntityInterface; |
13
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Savers\EntitySaverFactory; |
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
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Tests\Assets\TestCodeGenerator; |
22
|
|
|
use Ramsey\Uuid\UuidInterface; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @covers \EdmondsCommerce\DoctrineStaticMeta\Entity\Testing\Fixtures\AbstractEntityFixtureLoader |
26
|
|
|
* @covers \EdmondsCommerce\DoctrineStaticMeta\Entity\Testing\Fixtures\FixturesHelper |
27
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
28
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
29
|
|
|
*/ |
30
|
|
|
class FixturesHelperTest extends AbstractLargeTest |
31
|
|
|
{ |
32
|
|
|
public const WORK_DIR = AbstractTest::VAR_PATH . |
33
|
|
|
self::TEST_TYPE_LARGE . |
34
|
|
|
'/FixturesTest'; |
35
|
|
|
|
36
|
|
|
private const ENTITY_WITHOUT_MODIFIER = self::TEST_ENTITIES_ROOT_NAMESPACE . |
37
|
|
|
TestCodeGenerator::TEST_ENTITY_ALL_ARCHETYPE_FIELDS; |
38
|
|
|
|
39
|
|
|
private const ENTITY_WITH_MODIFIER = self::TEST_ENTITIES_ROOT_NAMESPACE . |
40
|
|
|
TestCodeGenerator::TEST_ENTITY_ATTRIBUTES_ADDRESS; |
41
|
|
|
|
42
|
|
|
protected static $buildOnce = true; |
43
|
|
|
/** |
44
|
|
|
* @var FixturesHelper |
45
|
|
|
*/ |
46
|
|
|
private $helper; |
47
|
|
|
|
48
|
|
|
public function setup(): void |
49
|
|
|
{ |
50
|
|
|
parent::setUp(); |
51
|
|
|
if (false === self::$built) { |
52
|
|
|
$this->getTestCodeGenerator() |
53
|
|
|
->copyTo(self::WORK_DIR, self::TEST_PROJECT_ROOT_NAMESPACE); |
54
|
|
|
self::$built = true; |
55
|
|
|
} |
56
|
|
|
$this->setupCopiedWorkDirAndCreateDatabase(); |
57
|
|
|
$this->recreateDtos(); |
58
|
|
|
$cacheDir = $this->copiedWorkDir . '/cache'; |
59
|
|
|
mkdir($cacheDir, 0777, true); |
60
|
|
|
$this->helper = new FixturesHelper( |
|
|
|
|
61
|
|
|
$this->getEntityManager(), |
62
|
|
|
$this->container->get(Database::class), |
63
|
|
|
$this->container->get(Schema::class), |
64
|
|
|
new FilesystemCache($cacheDir), |
65
|
|
|
$this->container->get(EntitySaverFactory::class), |
66
|
|
|
$this->getNamespaceHelper(), |
67
|
|
|
$this->getTestEntityGeneratorFactory() |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @test |
73
|
|
|
* @large |
74
|
|
|
*/ |
75
|
|
|
public function itLoadsAllTheFixturesWithRandomDataByDefault(): array |
76
|
|
|
{ |
77
|
|
|
$this->helper->setCacheKey(__CLASS__ . '_unmodified'); |
78
|
|
|
$fixture = $this->getUnmodifiedFixture(); |
79
|
|
|
$this->helper->addFixture($fixture); |
80
|
|
|
$this->helper->createDb(); |
81
|
|
|
$actual = $this->getRepositoryFactory() |
82
|
|
|
->getRepository($this->getCopiedFqn(self::ENTITY_WITHOUT_MODIFIER)) |
83
|
|
|
->findAll(); |
84
|
|
|
$actualCount = count($actual); |
85
|
|
|
self::assertSame(AbstractEntityFixtureLoader::BULK_AMOUNT_TO_GENERATE, $actualCount); |
86
|
|
|
|
87
|
|
|
return $actual; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
private function getUnmodifiedFixture(): AbstractEntityFixtureLoader |
91
|
|
|
{ |
92
|
|
|
return $this->getFixture($this->getCopiedFqn(self::ENTITY_WITHOUT_MODIFIER)); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
private function getFixture( |
96
|
|
|
string $entityFqn, |
97
|
|
|
?FixtureEntitiesModifierInterface $modifier = null |
98
|
|
|
): AbstractEntityFixtureLoader { |
99
|
|
|
return $this->helper->createFixtureInstanceForEntityFqn($entityFqn, $modifier); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @test |
104
|
|
|
* @large |
105
|
|
|
* @depends itLoadsAllTheFixturesWithRandomDataByDefault |
106
|
|
|
* |
107
|
|
|
* @param array $loadedFirstTime |
108
|
|
|
* |
109
|
|
|
* @return array |
110
|
|
|
* @throws \EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException |
111
|
|
|
* @throws \ReflectionException |
112
|
|
|
*/ |
113
|
|
|
public function itUsesTheCacheTheSecondTime(array $loadedFirstTime): array |
114
|
|
|
{ |
115
|
|
|
$this->getFileSystem() |
116
|
|
|
->mirror( |
117
|
|
|
$this->copiedWorkDir . |
118
|
|
|
'/../FixturesHelperTest_ItLoadsAllTheFixturesWithRandomDataByDefault_/cache', |
119
|
|
|
$this->copiedWorkDir . '/cache' |
120
|
|
|
); |
121
|
|
|
$this->helper->setCacheKey(__CLASS__ . '_unmodified'); |
122
|
|
|
$fixture = $this->getUnmodifiedFixture(); |
123
|
|
|
$this->helper->addFixture($fixture); |
124
|
|
|
$this->helper->createDb(); |
125
|
|
|
self::assertTrue($this->helper->isLoadedFromCache()); |
126
|
|
|
/** |
127
|
|
|
* @var EntityInterface[] $loadedSecondTime |
128
|
|
|
*/ |
129
|
|
|
$loadedSecondTime = $this->getRepositoryFactory() |
130
|
|
|
->getRepository($this->getCopiedFqn(self::ENTITY_WITHOUT_MODIFIER)) |
131
|
|
|
->findAll(); |
132
|
|
|
$actualCount = count($loadedSecondTime); |
133
|
|
|
$expectedCount = count($loadedFirstTime); |
134
|
|
|
self::assertSame($expectedCount, $actualCount); |
135
|
|
|
foreach ($loadedSecondTime as $key => $actualEntity) { |
136
|
|
|
$expectedEntity = $loadedFirstTime[$key]; |
137
|
|
|
$actualId = $actualEntity->getId(); |
138
|
|
|
$expectedId = $expectedEntity->getId(); |
139
|
|
|
$expectedText = $expectedEntity->getString(); |
140
|
|
|
$actualText = $actualEntity->getString(); |
|
|
|
|
141
|
|
|
self::assertEquals($expectedId, $actualId, 'Cached Entity ID does not match'); |
142
|
|
|
self::assertEquals($expectedText, $actualText, 'Cached Faker data does not match'); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
return $loadedSecondTime; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @test |
150
|
|
|
* @large |
151
|
|
|
* @depends itUsesTheCacheTheSecondTime |
152
|
|
|
* |
153
|
|
|
* @param array $loadedSecondTime |
154
|
|
|
* |
155
|
|
|
* @throws \EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException |
156
|
|
|
* @throws \ReflectionException |
157
|
|
|
*/ |
158
|
|
|
public function itCanBeConfiguredNotToLoadFromTheCache(array $loadedSecondTime): void |
159
|
|
|
{ |
160
|
|
|
$this->getFileSystem() |
161
|
|
|
->mirror( |
162
|
|
|
$this->copiedWorkDir . |
163
|
|
|
'/../FixturesHelperTest_ItUsesTheCacheTheSecondTime_/cache', |
164
|
|
|
$this->copiedWorkDir . '/cache' |
165
|
|
|
); |
166
|
|
|
$this->helper->setCacheKey(__CLASS__ . '_unmodified'); |
167
|
|
|
$fixture = $this->getUnmodifiedFixture(); |
168
|
|
|
$this->helper->setLoadFromCache(false); |
169
|
|
|
$this->helper->addFixture($fixture); |
170
|
|
|
$this->helper->createDb(); |
171
|
|
|
self::assertFalse($this->helper->isLoadedFromCache()); |
172
|
|
|
/** |
173
|
|
|
* @var EntityInterface[] $loadedThirdTime |
174
|
|
|
*/ |
175
|
|
|
$loadedThirdTime = $this->getRepositoryFactory() |
176
|
|
|
->getRepository($this->getCopiedFqn(self::ENTITY_WITHOUT_MODIFIER)) |
177
|
|
|
->findAll(); |
178
|
|
|
$actualCount = count($loadedThirdTime); |
179
|
|
|
$expectedCount = count($loadedSecondTime); |
180
|
|
|
self::assertSame($expectedCount, $actualCount); |
181
|
|
|
foreach ($loadedThirdTime as $key => $actualEntity) { |
182
|
|
|
$loadedSecondTimeEntity = $loadedSecondTime[$key]; |
183
|
|
|
$actualId = $actualEntity->getId(); |
184
|
|
|
$secondTimeEntityId = $loadedSecondTimeEntity->getId(); |
185
|
|
|
$secondTimeText = $loadedSecondTimeEntity->getUniqueString(); |
186
|
|
|
$actualText = $actualEntity->getUniqueString(); |
|
|
|
|
187
|
|
|
self::assertNotEquals($secondTimeEntityId, $actualId, 'Cached Entity ID matches, this should not happen'); |
188
|
|
|
self::assertNotEquals($secondTimeText, $actualText, 'Cached Faker data matches, this should not happen'); |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @test |
194
|
|
|
* @large |
195
|
|
|
*/ |
196
|
|
|
public function itCanTakeAModifierToCustomiseTheFixtures(): void |
197
|
|
|
{ |
198
|
|
|
$this->helper->setCacheKey(__CLASS__ . '_modified'); |
199
|
|
|
$fixture = $this->getModifiedFixture(); |
200
|
|
|
$this->helper->addFixture($fixture); |
201
|
|
|
$this->helper->createDb(); |
202
|
|
|
/** |
203
|
|
|
* @var EntityInterface[] $actual |
204
|
|
|
*/ |
205
|
|
|
$actual = $this->getRepositoryFactory() |
206
|
|
|
->getRepository($this->getCopiedFqn(self::ENTITY_WITH_MODIFIER)) |
207
|
|
|
->findAll(); |
208
|
|
|
$actualCount = count($actual); |
209
|
|
|
self::assertSame(AbstractEntityFixtureLoader::BULK_AMOUNT_TO_GENERATE + 1, $actualCount); |
210
|
|
|
$firstEntity = $actual[0]; |
211
|
|
|
$expectedString = 'This has been overridden'; |
212
|
|
|
$actualString = $firstEntity->getString(); |
213
|
|
|
self::assertSame($expectedString, $actualString); |
214
|
|
|
end($actual); |
215
|
|
|
$lastEntity = current($actual); |
216
|
|
|
$expectedString = 'This has been created'; |
217
|
|
|
$actualString = $lastEntity->getString(); |
218
|
|
|
self::assertSame($expectedString, $actualString); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
private function getModifiedFixture(): AbstractEntityFixtureLoader |
222
|
|
|
{ |
223
|
|
|
return $this->getFixture( |
224
|
|
|
$this->getCopiedFqn(self::ENTITY_WITH_MODIFIER), |
225
|
|
|
$this->getFixtureModifier() |
226
|
|
|
); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* @return FixtureEntitiesModifierInterface |
231
|
|
|
* @SuppressWarnings(PHPMD.ExcessiveMethodLength) |
232
|
|
|
*/ |
233
|
|
|
private function getFixtureModifier(): FixtureEntitiesModifierInterface |
234
|
|
|
{ |
235
|
|
|
|
236
|
|
|
return new class( |
237
|
|
|
$this->getCopiedFqn(self::ENTITY_WITH_MODIFIER), |
238
|
|
|
$this->getEntityFactory(), |
239
|
|
|
$this->getEntityDtoFactory(), |
240
|
|
|
$this->getUuidFactory() |
241
|
|
|
) |
242
|
|
|
implements FixtureEntitiesModifierInterface |
243
|
|
|
{ |
244
|
|
|
/** |
245
|
|
|
* @var string |
246
|
|
|
*/ |
247
|
|
|
protected $entityFqn; |
248
|
|
|
/** |
249
|
|
|
* @var EntityFactoryInterface |
250
|
|
|
*/ |
251
|
|
|
protected $factory; |
252
|
|
|
/** |
253
|
|
|
* @var array|EntityInterface[] |
254
|
|
|
*/ |
255
|
|
|
private $entities; |
256
|
|
|
/** |
257
|
|
|
* @var DtoFactory |
258
|
|
|
*/ |
259
|
|
|
private $dtoFactory; |
260
|
|
|
/** |
261
|
|
|
* @var UuidFactory |
262
|
|
|
*/ |
263
|
|
|
private $uuidFactory; |
264
|
|
|
|
265
|
|
|
public function __construct( |
266
|
|
|
string $entityFqn, |
267
|
|
|
EntityFactoryInterface $factory, |
268
|
|
|
DtoFactory $dtoFactory, |
269
|
|
|
UuidFactory $uuidFactory |
270
|
|
|
) { |
271
|
|
|
$this->entityFqn = $entityFqn; |
272
|
|
|
$this->factory = $factory; |
273
|
|
|
$this->dtoFactory = $dtoFactory; |
274
|
|
|
$this->uuidFactory = $uuidFactory; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* Update the entities array by reference |
279
|
|
|
* |
280
|
|
|
* @param array $entities |
281
|
|
|
*/ |
282
|
|
|
public function modifyEntities(array &$entities): void |
283
|
|
|
{ |
284
|
|
|
$this->entities = &$entities; |
285
|
|
|
$this->updateFirstEntity(); |
286
|
|
|
$this->addAnotherEntity(); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
private function updateFirstEntity(): void |
290
|
|
|
{ |
291
|
|
|
$firstEntity = current($this->entities); |
292
|
|
|
$firstEntity->update( |
293
|
|
|
new class($this->entityFqn, $firstEntity->getId()) |
294
|
|
|
implements DataTransferObjectInterface |
295
|
|
|
{ |
296
|
|
|
/** |
297
|
|
|
* @var string |
298
|
|
|
*/ |
299
|
|
|
private static $entityFqn; |
300
|
|
|
/** |
301
|
|
|
* @var UuidInterface |
302
|
|
|
*/ |
303
|
|
|
private $id; |
304
|
|
|
|
305
|
|
|
public function __construct(string $entityFqn, UuidInterface $id) |
306
|
|
|
{ |
307
|
|
|
self::$entityFqn = $entityFqn; |
308
|
|
|
$this->id = $id; |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
public function getString(): string |
312
|
|
|
{ |
313
|
|
|
return 'This has been overridden'; |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
public static function getEntityFqn(): string |
317
|
|
|
{ |
318
|
|
|
return self::$entityFqn; |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
public function getId(): UuidInterface |
322
|
|
|
{ |
323
|
|
|
return $this->id; |
324
|
|
|
} |
325
|
|
|
} |
326
|
|
|
); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
private function addAnotherEntity(): void |
330
|
|
|
{ |
331
|
|
|
$entity = $this->factory->create( |
332
|
|
|
$this->entityFqn, |
333
|
|
|
new class($this->entityFqn, $this->uuidFactory) implements DataTransferObjectInterface |
334
|
|
|
{ |
335
|
|
|
/** |
336
|
|
|
* @var string |
337
|
|
|
*/ |
338
|
|
|
private static $entityFqn; |
339
|
|
|
/** |
340
|
|
|
* @var \Ramsey\Uuid\UuidInterface |
341
|
|
|
*/ |
342
|
|
|
private $id; |
343
|
|
|
|
344
|
|
|
public function __construct(string $entityFqn, UuidFactory $factory) |
345
|
|
|
{ |
346
|
|
|
self::$entityFqn = $entityFqn; |
347
|
|
|
$this->id = $factory->getOrderedTimeUuid(); |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
public function getString(): string |
351
|
|
|
{ |
352
|
|
|
return 'This has been created'; |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
public static function getEntityFqn(): string |
356
|
|
|
{ |
357
|
|
|
return self::$entityFqn; |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
public function getId(): UuidInterface |
361
|
|
|
{ |
362
|
|
|
return $this->id; |
363
|
|
|
} |
364
|
|
|
} |
365
|
|
|
); |
366
|
|
|
|
367
|
|
|
$this->entities[] = $entity; |
368
|
|
|
} |
369
|
|
|
}; |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
/** |
373
|
|
|
* @test |
374
|
|
|
* @large |
375
|
|
|
*/ |
376
|
|
|
public function theOrderOfFixtureLoadingCanBeSet(): void |
377
|
|
|
{ |
378
|
|
|
$loader = new Loader(); |
379
|
|
|
$fixture1 = $this->getModifiedFixture(); |
380
|
|
|
$loader->addFixture($fixture1); |
381
|
|
|
$fixture2 = $this->getUnmodifiedFixture(); |
382
|
|
|
$fixture2->setOrder(AbstractEntityFixtureLoader::ORDER_FIRST); |
383
|
|
|
$loader->addFixture($fixture2); |
384
|
|
|
$orderedFixtures = $loader->getFixtures(); |
385
|
|
|
self::assertSame($fixture2, current($orderedFixtures)); |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
/** |
389
|
|
|
* @test |
390
|
|
|
* @large |
391
|
|
|
* @throws \EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException |
392
|
|
|
* @throws \ReflectionException |
393
|
|
|
*/ |
394
|
|
|
public function fixturesUseTheCorrectFakerDataProviders(): void |
395
|
|
|
{ |
396
|
|
|
$entityFqn = $this->getCopiedFqn(self::ENTITY_WITHOUT_MODIFIER); |
397
|
|
|
|
398
|
|
|
$this->helper->setCacheKey(__CLASS__ . '_faker'); |
399
|
|
|
$fixture = $this->getUnmodifiedFixture(); |
400
|
|
|
$this->helper->addFixture($fixture); |
401
|
|
|
$this->helper->createDb(); |
402
|
|
|
$actual = $this->getRepositoryFactory() |
403
|
|
|
->getRepository($entityFqn) |
404
|
|
|
->findAll(); |
405
|
|
|
/** |
406
|
|
|
* @var EntityInterface $entity |
407
|
|
|
*/ |
408
|
|
|
foreach ($actual as $entity) { |
409
|
|
|
self::assertContains($entity->getEnum(), EnumFieldInterface::ENUM_OPTIONS); |
410
|
|
|
} |
411
|
|
|
} |
412
|
|
|
} |
413
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.