1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\Tests\Large\Entity\Testing; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Cache\FilesystemCache; |
6
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Factory\EntityFactory; |
7
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\EntityInterface; |
8
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Savers\EntitySaverFactory; |
9
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Testing\Fixtures\AbstractEntityFixtureLoader; |
10
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Testing\Fixtures\FixtureEntitiesModifierInterface; |
11
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Testing\Fixtures\FixturesHelper; |
12
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Testing\TestEntityGenerator; |
13
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Validation\EntityValidatorFactory; |
14
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\MappingHelper; |
15
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Schema\Database; |
16
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Schema\Schema; |
17
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Tests\Assets\AbstractLargeTest; |
18
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Tests\Assets\AbstractTest; |
19
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Tests\Large\FullProjectBuildLargeTest; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @covers \EdmondsCommerce\DoctrineStaticMeta\Entity\Testing\Fixtures\AbstractEntityFixtureLoader |
23
|
|
|
* @covers \EdmondsCommerce\DoctrineStaticMeta\Entity\Testing\Fixtures\FixturesHelper |
24
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
25
|
|
|
*/ |
26
|
|
|
class FixturesTest extends AbstractLargeTest |
27
|
|
|
{ |
28
|
|
|
public const WORK_DIR = AbstractTest::VAR_PATH . |
29
|
|
|
self::TEST_TYPE_LARGE . |
30
|
|
|
'/TestEntityGeneratorLargeTest'; |
31
|
|
|
|
32
|
|
|
private const TEST_ENTITIES = FullProjectBuildLargeTest::TEST_ENTITIES; |
33
|
|
|
|
34
|
|
|
private const TEST_RELATIONS = FullProjectBuildLargeTest::TEST_RELATIONS; |
35
|
|
|
|
36
|
|
|
private const TEST_FIELD_FQN_BASE = FullProjectBuildLargeTest::TEST_FIELD_NAMESPACE_BASE . '\\Traits'; |
37
|
|
|
|
38
|
|
|
private const ENTITY_WITHOUT_MODIFIER = self::TEST_ENTITIES[0]; |
39
|
|
|
|
40
|
|
|
private const ENTITY_WITH_MODIFIER = self::TEST_ENTITIES[1]; |
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
|
|
|
$entityGenerator = $this->getEntityGenerator(); |
53
|
|
|
$fieldGenerator = $this->getFieldGenerator(); |
54
|
|
|
$relationsGenerator = $this->getRelationsGenerator(); |
55
|
|
|
$fields = []; |
56
|
|
|
foreach (MappingHelper::COMMON_TYPES as $type) { |
57
|
|
|
$fields[] = $fieldGenerator->generateField( |
58
|
|
|
self::TEST_FIELD_FQN_BASE . '\\' . ucwords($type), |
59
|
|
|
$type |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
foreach (self::TEST_ENTITIES as $entityFqn) { |
63
|
|
|
$entityGenerator->generateEntity($entityFqn); |
64
|
|
|
foreach ($fields as $fieldFqn) { |
65
|
|
|
$this->getFieldSetter()->setEntityHasField($entityFqn, $fieldFqn); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
foreach (self::TEST_RELATIONS as $relation) { |
69
|
|
|
$relationsGenerator->setEntityHasRelationToEntity(...$relation); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
self::$built = true; |
73
|
|
|
} |
74
|
|
|
$this->setupCopiedWorkDirAndCreateDatabase(); |
75
|
|
|
$this->helper = new FixturesHelper( |
76
|
|
|
$this->getEntityManager(), |
77
|
|
|
$this->container->get(Database::class), |
78
|
|
|
$this->container->get(Schema::class), |
79
|
|
|
$this->container->get(FilesystemCache::class) |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
private function getFixture( |
84
|
|
|
string $entityFqn, |
85
|
|
|
?FixtureEntitiesModifierInterface $modifier = null |
86
|
|
|
): AbstractEntityFixtureLoader { |
87
|
|
|
$fixtureFqn = $this->getNamespaceHelper()->getFixtureFqnFromEntityFqn($entityFqn); |
88
|
|
|
|
89
|
|
|
return new $fixtureFqn( |
90
|
|
|
new TestEntityGenerator( |
91
|
|
|
1.0, |
92
|
|
|
[], |
93
|
|
|
new \ts\Reflection\ReflectionClass( |
94
|
|
|
$entityFqn |
95
|
|
|
), |
96
|
|
|
$this->container->get(EntitySaverFactory::class), |
97
|
|
|
$this->container->get(EntityValidatorFactory::class) |
98
|
|
|
), |
99
|
|
|
$this->container->get(EntitySaverFactory::class), |
100
|
|
|
$modifier |
101
|
|
|
); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
private function getUnmodifiedFixture(): AbstractEntityFixtureLoader |
105
|
|
|
{ |
106
|
|
|
return $this->getFixture($this->getCopiedFqn(self::ENTITY_WITHOUT_MODIFIER)); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
private function getModifiedFixture(): AbstractEntityFixtureLoader |
110
|
|
|
{ |
111
|
|
|
return $this->getFixture( |
112
|
|
|
$this->getCopiedFqn(self::ENTITY_WITH_MODIFIER), |
113
|
|
|
$this->getFixtureModifier() |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
private function getFixtureModifier(): FixtureEntitiesModifierInterface |
118
|
|
|
{ |
119
|
|
|
return new class( |
120
|
|
|
$this->getCopiedFqn(self::ENTITY_WITH_MODIFIER), |
121
|
|
|
$this->getEntityFactory() |
122
|
|
|
) |
123
|
|
|
implements FixtureEntitiesModifierInterface |
124
|
|
|
{ |
125
|
|
|
/** |
126
|
|
|
* @var string |
127
|
|
|
*/ |
128
|
|
|
protected $entityFqn; |
129
|
|
|
/** |
130
|
|
|
* @var EntityFactory |
131
|
|
|
*/ |
132
|
|
|
protected $factory; |
133
|
|
|
/** |
134
|
|
|
* @var array|EntityInterface[] |
135
|
|
|
*/ |
136
|
|
|
private $entities; |
137
|
|
|
|
138
|
|
|
public function __construct(string $entityFqn, EntityFactory $factory) |
139
|
|
|
{ |
140
|
|
|
$this->entityFqn = $entityFqn; |
141
|
|
|
$this->factory = $factory; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Update the entities array by reference |
146
|
|
|
* |
147
|
|
|
* @param array $entities |
148
|
|
|
*/ |
149
|
|
|
public function modifyEntities(array &$entities): void |
150
|
|
|
{ |
151
|
|
|
$this->entities = &$entities; |
152
|
|
|
$this->updateFirstEntity(); |
153
|
|
|
$this->addAnotherEntity(); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
private function updateFirstEntity(): void |
157
|
|
|
{ |
158
|
|
|
$this->entities[0]->setString('This has been overridden'); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
private function addAnotherEntity(): void |
162
|
|
|
{ |
163
|
|
|
$entity = $this->factory->create($this->entityFqn); |
164
|
|
|
$entity->setString('This has been created'); |
|
|
|
|
165
|
|
|
$this->entities[] = $entity; |
166
|
|
|
} |
167
|
|
|
}; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @test |
172
|
|
|
* @large |
173
|
|
|
*/ |
174
|
|
|
public function itLoadsAllTheFixturesWithRandomDataByDefault(): array |
175
|
|
|
{ |
176
|
|
|
$this->helper->setCacheKey(__CLASS__ . '_unmodified'); |
177
|
|
|
$fixture = $this->getUnmodifiedFixture(); |
178
|
|
|
$this->helper->addFixture($fixture); |
179
|
|
|
$this->helper->createDb(); |
180
|
|
|
$actual = $this->getEntityManager() |
181
|
|
|
->getRepository($this->getCopiedFqn(self::ENTITY_WITHOUT_MODIFIER)) |
182
|
|
|
->findAll(); |
183
|
|
|
$actualCount = count($actual); |
184
|
|
|
self::assertSame(AbstractEntityFixtureLoader::BULK_AMOUNT_TO_GENERATE, $actualCount); |
185
|
|
|
|
186
|
|
|
return $actual; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @test |
191
|
|
|
* @large |
192
|
|
|
* @depends itLoadsAllTheFixturesWithRandomDataByDefault |
193
|
|
|
* |
194
|
|
|
* @param array $loadedFirstTime |
195
|
|
|
* |
196
|
|
|
* @throws \EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException |
197
|
|
|
* @throws \ReflectionException |
198
|
|
|
*/ |
199
|
|
|
public function itUsesTheCacheTheSecondTime(array $loadedFirstTime): void |
200
|
|
|
{ |
201
|
|
|
$this->helper->setCacheKey(__CLASS__ . '_unmodified'); |
202
|
|
|
$fixture = $this->getUnmodifiedFixture(); |
203
|
|
|
$this->helper->addFixture($fixture); |
204
|
|
|
$this->helper->createDb(); |
205
|
|
|
self::assertTrue($this->helper->isLoadedFromCache()); |
206
|
|
|
/** |
207
|
|
|
* @var EntityInterface[] $actual |
208
|
|
|
*/ |
209
|
|
|
$actual = $this->getEntityManager() |
210
|
|
|
->getRepository($this->getCopiedFqn(self::ENTITY_WITHOUT_MODIFIER)) |
211
|
|
|
->findAll(); |
212
|
|
|
$actualCount = count($actual); |
213
|
|
|
$expectedCount = count($loadedFirstTime); |
214
|
|
|
self::assertSame($expectedCount, $actualCount); |
215
|
|
|
foreach ($actual as $key => $actualEntity) { |
216
|
|
|
$expectedEntity = $loadedFirstTime[$key]; |
217
|
|
|
$actualId = $actualEntity->getId(); |
218
|
|
|
$expectedId = $expectedEntity->getId(); |
219
|
|
|
$expectedText = $expectedEntity->getString(); |
220
|
|
|
$actualText = $actualEntity->getString(); |
|
|
|
|
221
|
|
|
self::assertEquals($expectedId, $actualId, 'Cached Entity ID does not match'); |
222
|
|
|
self::assertEquals($expectedText, $actualText, 'Cached Faker data does not match'); |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* @test |
228
|
|
|
* @large |
229
|
|
|
*/ |
230
|
|
|
public function itCanTakeAModifierToCustomiseTheFixtures() |
231
|
|
|
{ |
232
|
|
|
$this->helper->setCacheKey(__CLASS__ . '_modified'); |
233
|
|
|
$fixture = $this->getModifiedFixture(); |
234
|
|
|
$this->helper->addFixture($fixture); |
235
|
|
|
$this->helper->createDb(); |
236
|
|
|
/** |
237
|
|
|
* @var EntityInterface[] $actual |
238
|
|
|
*/ |
239
|
|
|
$actual = $this->getEntityManager() |
240
|
|
|
->getRepository($this->getCopiedFqn(self::ENTITY_WITH_MODIFIER)) |
241
|
|
|
->findAll(); |
242
|
|
|
$actualCount = count($actual); |
243
|
|
|
self::assertSame(AbstractEntityFixtureLoader::BULK_AMOUNT_TO_GENERATE + 1, $actualCount); |
244
|
|
|
$firstEntity = $actual[0]; |
245
|
|
|
$expectedString = 'This has been overridden'; |
246
|
|
|
$actualString = $firstEntity->getString(); |
247
|
|
|
self::assertSame($expectedString, $actualString); |
248
|
|
|
end($actual); |
249
|
|
|
$lastEntity = current($actual); |
250
|
|
|
$expectedString = 'This has been created'; |
251
|
|
|
$actualString = $lastEntity->getString(); |
252
|
|
|
self::assertSame($expectedString, $actualString); |
253
|
|
|
} |
254
|
|
|
} |
255
|
|
|
|
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.