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