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