|
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
|
|
|
$this->container |
|
69
|
|
|
); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @test |
|
74
|
|
|
* @large |
|
75
|
|
|
*/ |
|
76
|
|
|
public function itLoadsAllTheFixturesWithRandomDataByDefault(): array |
|
77
|
|
|
{ |
|
78
|
|
|
$this->helper->setCacheKey(__CLASS__ . '_unmodified'); |
|
79
|
|
|
$fixture = $this->getUnmodifiedFixture(); |
|
80
|
|
|
$this->helper->addFixture($fixture); |
|
81
|
|
|
$this->helper->createDb(); |
|
82
|
|
|
$actual = $this->getRepositoryFactory() |
|
83
|
|
|
->getRepository($this->getCopiedFqn(self::ENTITY_WITHOUT_MODIFIER)) |
|
84
|
|
|
->findAll(); |
|
85
|
|
|
$actualCount = count($actual); |
|
86
|
|
|
self::assertSame(AbstractEntityFixtureLoader::BULK_AMOUNT_TO_GENERATE, $actualCount); |
|
87
|
|
|
|
|
88
|
|
|
return $actual; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
private function getUnmodifiedFixture(): AbstractEntityFixtureLoader |
|
92
|
|
|
{ |
|
93
|
|
|
return $this->getFixture($this->getCopiedFqn(self::ENTITY_WITHOUT_MODIFIER)); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
private function getFixture( |
|
97
|
|
|
string $entityFqn, |
|
98
|
|
|
?FixtureEntitiesModifierInterface $modifier = null |
|
99
|
|
|
): AbstractEntityFixtureLoader { |
|
100
|
|
|
return $this->helper->createFixtureInstanceForEntityFqn($entityFqn, $modifier); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @test |
|
105
|
|
|
* @large |
|
106
|
|
|
* @depends itLoadsAllTheFixturesWithRandomDataByDefault |
|
107
|
|
|
* |
|
108
|
|
|
* @param array $loadedFirstTime |
|
109
|
|
|
* |
|
110
|
|
|
* @return array |
|
111
|
|
|
* @throws \EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException |
|
112
|
|
|
* @throws \ReflectionException |
|
113
|
|
|
*/ |
|
114
|
|
|
public function itUsesTheCacheTheSecondTime(array $loadedFirstTime): array |
|
115
|
|
|
{ |
|
116
|
|
|
$this->getFileSystem() |
|
117
|
|
|
->mirror( |
|
118
|
|
|
$this->copiedWorkDir . |
|
119
|
|
|
'/../FixturesHelperTest_ItLoadsAllTheFixturesWithRandomDataByDefault_/cache', |
|
120
|
|
|
$this->copiedWorkDir . '/cache' |
|
121
|
|
|
); |
|
122
|
|
|
$this->helper->setCacheKey(__CLASS__ . '_unmodified'); |
|
123
|
|
|
$fixture = $this->getUnmodifiedFixture(); |
|
124
|
|
|
$this->helper->addFixture($fixture); |
|
125
|
|
|
$this->helper->createDb(); |
|
126
|
|
|
self::assertTrue($this->helper->isLoadedFromCache()); |
|
127
|
|
|
/** |
|
128
|
|
|
* @var EntityInterface[] $loadedSecondTime |
|
129
|
|
|
*/ |
|
130
|
|
|
$loadedSecondTime = $this->getRepositoryFactory() |
|
131
|
|
|
->getRepository($this->getCopiedFqn(self::ENTITY_WITHOUT_MODIFIER)) |
|
132
|
|
|
->findAll(); |
|
133
|
|
|
$actualCount = count($loadedSecondTime); |
|
134
|
|
|
$expectedCount = count($loadedFirstTime); |
|
135
|
|
|
self::assertSame($expectedCount, $actualCount); |
|
136
|
|
|
foreach ($loadedSecondTime as $key => $actualEntity) { |
|
137
|
|
|
$expectedEntity = $loadedFirstTime[$key]; |
|
138
|
|
|
$actualId = $actualEntity->getId(); |
|
139
|
|
|
$expectedId = $expectedEntity->getId(); |
|
140
|
|
|
$expectedText = $expectedEntity->getString(); |
|
141
|
|
|
$actualText = $actualEntity->getString(); |
|
|
|
|
|
|
142
|
|
|
self::assertEquals($expectedId, $actualId, 'Cached Entity ID does not match'); |
|
143
|
|
|
self::assertEquals($expectedText, $actualText, 'Cached Faker data does not match'); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
return $loadedSecondTime; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* @test |
|
151
|
|
|
* @large |
|
152
|
|
|
* @depends itUsesTheCacheTheSecondTime |
|
153
|
|
|
* |
|
154
|
|
|
* @param array $loadedSecondTime |
|
155
|
|
|
* |
|
156
|
|
|
* @throws \EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException |
|
157
|
|
|
* @throws \ReflectionException |
|
158
|
|
|
*/ |
|
159
|
|
|
public function itCanBeConfiguredNotToLoadFromTheCache(array $loadedSecondTime): void |
|
160
|
|
|
{ |
|
161
|
|
|
$this->getFileSystem() |
|
162
|
|
|
->mirror( |
|
163
|
|
|
$this->copiedWorkDir . |
|
164
|
|
|
'/../FixturesHelperTest_ItUsesTheCacheTheSecondTime_/cache', |
|
165
|
|
|
$this->copiedWorkDir . '/cache' |
|
166
|
|
|
); |
|
167
|
|
|
$this->helper->setCacheKey(__CLASS__ . '_unmodified'); |
|
168
|
|
|
$fixture = $this->getUnmodifiedFixture(); |
|
169
|
|
|
$this->helper->setLoadFromCache(false); |
|
170
|
|
|
$this->helper->addFixture($fixture); |
|
171
|
|
|
$this->helper->createDb(); |
|
172
|
|
|
self::assertFalse($this->helper->isLoadedFromCache()); |
|
173
|
|
|
/** |
|
174
|
|
|
* @var EntityInterface[] $loadedThirdTime |
|
175
|
|
|
*/ |
|
176
|
|
|
$loadedThirdTime = $this->getRepositoryFactory() |
|
177
|
|
|
->getRepository($this->getCopiedFqn(self::ENTITY_WITHOUT_MODIFIER)) |
|
178
|
|
|
->findAll(); |
|
179
|
|
|
$actualCount = count($loadedThirdTime); |
|
180
|
|
|
$expectedCount = count($loadedSecondTime); |
|
181
|
|
|
self::assertSame($expectedCount, $actualCount); |
|
182
|
|
|
foreach ($loadedThirdTime as $key => $actualEntity) { |
|
183
|
|
|
$loadedSecondTimeEntity = $loadedSecondTime[$key]; |
|
184
|
|
|
$actualId = $actualEntity->getId(); |
|
185
|
|
|
$secondTimeEntityId = $loadedSecondTimeEntity->getId(); |
|
186
|
|
|
$secondTimeText = $loadedSecondTimeEntity->getUniqueString(); |
|
187
|
|
|
$actualText = $actualEntity->getUniqueString(); |
|
|
|
|
|
|
188
|
|
|
self::assertNotEquals($secondTimeEntityId, $actualId, 'Cached Entity ID matches, this should not happen'); |
|
189
|
|
|
self::assertNotEquals($secondTimeText, $actualText, 'Cached Faker data matches, this should not happen'); |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* @test |
|
195
|
|
|
* @large |
|
196
|
|
|
*/ |
|
197
|
|
|
public function itCanTakeAModifierToCustomiseTheFixtures(): void |
|
198
|
|
|
{ |
|
199
|
|
|
$this->helper->setCacheKey(__CLASS__ . '_modified'); |
|
200
|
|
|
$fixture = $this->getModifiedFixture(); |
|
201
|
|
|
$this->helper->addFixture($fixture); |
|
202
|
|
|
$this->helper->createDb(); |
|
203
|
|
|
/** |
|
204
|
|
|
* @var EntityInterface[] $actual |
|
205
|
|
|
*/ |
|
206
|
|
|
$actual = $this->getRepositoryFactory() |
|
207
|
|
|
->getRepository($this->getCopiedFqn(self::ENTITY_WITH_MODIFIER)) |
|
208
|
|
|
->findAll(); |
|
209
|
|
|
$actualCount = count($actual); |
|
210
|
|
|
self::assertSame(AbstractEntityFixtureLoader::BULK_AMOUNT_TO_GENERATE + 1, $actualCount); |
|
211
|
|
|
$firstEntity = $actual[0]; |
|
212
|
|
|
$expectedString = 'This has been overridden'; |
|
213
|
|
|
$actualString = $firstEntity->getString(); |
|
214
|
|
|
self::assertSame($expectedString, $actualString); |
|
215
|
|
|
end($actual); |
|
216
|
|
|
$lastEntity = current($actual); |
|
217
|
|
|
$expectedString = 'This has been created'; |
|
218
|
|
|
$actualString = $lastEntity->getString(); |
|
219
|
|
|
self::assertSame($expectedString, $actualString); |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
private function getModifiedFixture(): AbstractEntityFixtureLoader |
|
223
|
|
|
{ |
|
224
|
|
|
return $this->getFixture( |
|
225
|
|
|
$this->getCopiedFqn(self::ENTITY_WITH_MODIFIER), |
|
226
|
|
|
$this->getFixtureModifier() |
|
227
|
|
|
); |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* @return FixtureEntitiesModifierInterface |
|
232
|
|
|
* @SuppressWarnings(PHPMD.ExcessiveMethodLength) |
|
233
|
|
|
*/ |
|
234
|
|
|
private function getFixtureModifier(): FixtureEntitiesModifierInterface |
|
235
|
|
|
{ |
|
236
|
|
|
|
|
237
|
|
|
return new class( |
|
238
|
|
|
$this->getCopiedFqn(self::ENTITY_WITH_MODIFIER), |
|
239
|
|
|
$this->getEntityFactory(), |
|
240
|
|
|
$this->getEntityDtoFactory(), |
|
241
|
|
|
$this->getUuidFactory() |
|
242
|
|
|
) |
|
243
|
|
|
implements FixtureEntitiesModifierInterface |
|
244
|
|
|
{ |
|
245
|
|
|
/** |
|
246
|
|
|
* @var string |
|
247
|
|
|
*/ |
|
248
|
|
|
protected $entityFqn; |
|
249
|
|
|
/** |
|
250
|
|
|
* @var EntityFactoryInterface |
|
251
|
|
|
*/ |
|
252
|
|
|
protected $factory; |
|
253
|
|
|
/** |
|
254
|
|
|
* @var array|EntityInterface[] |
|
255
|
|
|
*/ |
|
256
|
|
|
private $entities; |
|
257
|
|
|
/** |
|
258
|
|
|
* @var DtoFactory |
|
259
|
|
|
*/ |
|
260
|
|
|
private $dtoFactory; |
|
261
|
|
|
/** |
|
262
|
|
|
* @var UuidFactory |
|
263
|
|
|
*/ |
|
264
|
|
|
private $uuidFactory; |
|
265
|
|
|
|
|
266
|
|
|
public function __construct( |
|
267
|
|
|
string $entityFqn, |
|
268
|
|
|
EntityFactoryInterface $factory, |
|
269
|
|
|
DtoFactory $dtoFactory, |
|
270
|
|
|
UuidFactory $uuidFactory |
|
271
|
|
|
) { |
|
272
|
|
|
$this->entityFqn = $entityFqn; |
|
273
|
|
|
$this->factory = $factory; |
|
274
|
|
|
$this->dtoFactory = $dtoFactory; |
|
275
|
|
|
$this->uuidFactory = $uuidFactory; |
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
|
|
/** |
|
279
|
|
|
* Update the entities array by reference |
|
280
|
|
|
* |
|
281
|
|
|
* @param array $entities |
|
282
|
|
|
*/ |
|
283
|
|
|
public function modifyEntities(array &$entities): void |
|
284
|
|
|
{ |
|
285
|
|
|
$this->entities = &$entities; |
|
286
|
|
|
$this->updateFirstEntity(); |
|
287
|
|
|
$this->addAnotherEntity(); |
|
288
|
|
|
} |
|
289
|
|
|
|
|
290
|
|
|
private function updateFirstEntity(): void |
|
291
|
|
|
{ |
|
292
|
|
|
$firstEntity = current($this->entities); |
|
293
|
|
|
$firstEntity->update( |
|
294
|
|
|
new class($this->entityFqn, $firstEntity->getId()) |
|
295
|
|
|
implements DataTransferObjectInterface |
|
296
|
|
|
{ |
|
297
|
|
|
/** |
|
298
|
|
|
* @var string |
|
299
|
|
|
*/ |
|
300
|
|
|
private static $entityFqn; |
|
301
|
|
|
/** |
|
302
|
|
|
* @var UuidInterface |
|
303
|
|
|
*/ |
|
304
|
|
|
private $id; |
|
305
|
|
|
|
|
306
|
|
|
public function __construct(string $entityFqn, UuidInterface $id) |
|
307
|
|
|
{ |
|
308
|
|
|
self::$entityFqn = $entityFqn; |
|
309
|
|
|
$this->id = $id; |
|
310
|
|
|
} |
|
311
|
|
|
|
|
312
|
|
|
public function getString(): string |
|
313
|
|
|
{ |
|
314
|
|
|
return 'This has been overridden'; |
|
315
|
|
|
} |
|
316
|
|
|
|
|
317
|
|
|
public static function getEntityFqn(): string |
|
318
|
|
|
{ |
|
319
|
|
|
return self::$entityFqn; |
|
320
|
|
|
} |
|
321
|
|
|
|
|
322
|
|
|
public function getId(): UuidInterface |
|
323
|
|
|
{ |
|
324
|
|
|
return $this->id; |
|
325
|
|
|
} |
|
326
|
|
|
} |
|
327
|
|
|
); |
|
328
|
|
|
} |
|
329
|
|
|
|
|
330
|
|
|
private function addAnotherEntity(): void |
|
331
|
|
|
{ |
|
332
|
|
|
$entity = $this->factory->create( |
|
333
|
|
|
$this->entityFqn, |
|
334
|
|
|
new class($this->entityFqn, $this->uuidFactory) implements DataTransferObjectInterface |
|
335
|
|
|
{ |
|
336
|
|
|
/** |
|
337
|
|
|
* @var string |
|
338
|
|
|
*/ |
|
339
|
|
|
private static $entityFqn; |
|
340
|
|
|
/** |
|
341
|
|
|
* @var \Ramsey\Uuid\UuidInterface |
|
342
|
|
|
*/ |
|
343
|
|
|
private $id; |
|
344
|
|
|
|
|
345
|
|
|
public function __construct(string $entityFqn, UuidFactory $factory) |
|
346
|
|
|
{ |
|
347
|
|
|
self::$entityFqn = $entityFqn; |
|
348
|
|
|
$this->id = $factory->getOrderedTimeUuid(); |
|
349
|
|
|
} |
|
350
|
|
|
|
|
351
|
|
|
public function getString(): string |
|
352
|
|
|
{ |
|
353
|
|
|
return 'This has been created'; |
|
354
|
|
|
} |
|
355
|
|
|
|
|
356
|
|
|
public static function getEntityFqn(): string |
|
357
|
|
|
{ |
|
358
|
|
|
return self::$entityFqn; |
|
359
|
|
|
} |
|
360
|
|
|
|
|
361
|
|
|
public function getId(): UuidInterface |
|
362
|
|
|
{ |
|
363
|
|
|
return $this->id; |
|
364
|
|
|
} |
|
365
|
|
|
} |
|
366
|
|
|
); |
|
367
|
|
|
|
|
368
|
|
|
$this->entities[] = $entity; |
|
369
|
|
|
} |
|
370
|
|
|
}; |
|
371
|
|
|
} |
|
372
|
|
|
|
|
373
|
|
|
/** |
|
374
|
|
|
* @test |
|
375
|
|
|
* @large |
|
376
|
|
|
* @throws \EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException |
|
377
|
|
|
* @throws \ReflectionException |
|
378
|
|
|
*/ |
|
379
|
|
|
public function fixturesUseTheCorrectFakerDataProviders(): void |
|
380
|
|
|
{ |
|
381
|
|
|
$entityFqn = $this->getCopiedFqn(self::ENTITY_WITHOUT_MODIFIER); |
|
382
|
|
|
|
|
383
|
|
|
$this->helper->setCacheKey(__CLASS__ . '_faker'); |
|
384
|
|
|
$fixture = $this->getUnmodifiedFixture(); |
|
385
|
|
|
$this->helper->addFixture($fixture); |
|
386
|
|
|
$this->helper->createDb(); |
|
387
|
|
|
$actual = $this->getRepositoryFactory() |
|
388
|
|
|
->getRepository($entityFqn) |
|
389
|
|
|
->findAll(); |
|
390
|
|
|
/** |
|
391
|
|
|
* @var EntityInterface $entity |
|
392
|
|
|
*/ |
|
393
|
|
|
foreach ($actual as $entity) { |
|
394
|
|
|
self::assertContains($entity->getEnum(), EnumFieldInterface::ENUM_OPTIONS); |
|
395
|
|
|
} |
|
396
|
|
|
} |
|
397
|
|
|
} |
|
398
|
|
|
|
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.