|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * This file is part of the zenstruck/foundry package. |
| 7 | + * |
| 8 | + * (c) Kevin Bond <kevinbond@gmail.com> |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view the LICENSE |
| 11 | + * file that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace Zenstruck\Foundry\Tests\Integration\ResetDatabase; |
| 15 | + |
| 16 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 17 | +use PHPUnit\Framework\Attributes\IgnorePhpunitWarnings; |
| 18 | +use PHPUnit\Framework\Attributes\RequiresPhpunit; |
| 19 | +use PHPUnit\Framework\Attributes\Test; |
| 20 | +use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; |
| 21 | +use Zenstruck\Foundry\Tests\Fixture\DoctrineCascadeRelationship\ChangesEntityRelationshipCascadePersist; |
| 22 | +use Zenstruck\Foundry\Tests\Fixture\DoctrineCascadeRelationship\UsingRelationships; |
| 23 | +use Zenstruck\Foundry\Tests\Fixture\Entity\EdgeCases\RelationshipWithGlobalEntity; |
| 24 | +use Zenstruck\Foundry\Tests\Fixture\Entity\GlobalEntity; |
| 25 | +use Zenstruck\Foundry\Tests\Fixture\ResetDatabase\ResetDatabaseTestKernel; |
| 26 | +use Zenstruck\Foundry\Tests\Fixture\Stories\GlobalStory; |
| 27 | +use Zenstruck\Foundry\Tests\Integration\ORM\EdgeCasesRelationshipTest; |
| 28 | + |
| 29 | +use function Zenstruck\Foundry\Persistence\flush_after; |
| 30 | +use function Zenstruck\Foundry\Persistence\persistent_factory; |
| 31 | + |
| 32 | +abstract class OrmEdgeCaseTestCase extends KernelTestCase |
| 33 | +{ |
| 34 | + use ChangesEntityRelationshipCascadePersist; |
| 35 | + |
| 36 | + #[Test] |
| 37 | + #[DataProvider('provideCascadeRelationshipsCombinations')] |
| 38 | + #[UsingRelationships(RelationshipWithGlobalEntity\RelationshipWithGlobalEntity::class, ['globalEntity'])] |
| 39 | + #[RequiresPhpunit('>=11.4')] |
| 40 | + #[IgnorePhpunitWarnings(EdgeCasesRelationshipTest::DATA_PROVIDER_WARNING_REGEX)] |
| 41 | + public function it_can_use_flush_after_and_entity_from_global_state(): void |
| 42 | + { |
| 43 | + $relationshipWithGlobalEntityFactory = persistent_factory(RelationshipWithGlobalEntity\RelationshipWithGlobalEntity::class); |
| 44 | + $globalEntitiesCount = persistent_factory(GlobalEntity::class)::repository()->count(); |
| 45 | + |
| 46 | + flush_after(function() use ($relationshipWithGlobalEntityFactory) { |
| 47 | + $relationshipWithGlobalEntityFactory->create(['globalEntity' => GlobalStory::globalEntityProxy()]); |
| 48 | + $relationshipWithGlobalEntityFactory->create(['globalEntity' => GlobalStory::globalEntity()]); |
| 49 | + }); |
| 50 | + |
| 51 | + // assert no extra GlobalEntity have been created |
| 52 | + persistent_factory(GlobalEntity::class)::assert()->count($globalEntitiesCount); |
| 53 | + |
| 54 | + $relationshipWithGlobalEntityFactory::assert()->count(2); |
| 55 | + |
| 56 | + $entity = $relationshipWithGlobalEntityFactory::repository()->first(); |
| 57 | + self::assertSame(GlobalStory::globalEntity(), $entity?->getGlobalEntity()); |
| 58 | + |
| 59 | + $entity = $relationshipWithGlobalEntityFactory::repository()->last(); |
| 60 | + self::assertSame(GlobalStory::globalEntity(), $entity?->getGlobalEntity()); |
| 61 | + } |
| 62 | + |
| 63 | + protected static function getKernelClass(): string |
| 64 | + { |
| 65 | + return ResetDatabaseTestKernel::class; |
| 66 | + } |
| 67 | +} |
0 commit comments