|
15 | 15 |
|
16 | 16 | use Doctrine\ORM\EntityManagerInterface; |
17 | 17 | use PHPUnit\Framework\Attributes\DataProvider; |
| 18 | +use PHPUnit\Framework\Attributes\Depends; |
18 | 19 | use PHPUnit\Framework\Attributes\IgnorePhpunitWarnings; |
19 | 20 | use PHPUnit\Framework\Attributes\RequiresEnvironmentVariable; |
20 | 21 | use PHPUnit\Framework\Attributes\RequiresPhp; |
|
26 | 27 | use Zenstruck\Foundry\Tests\Fixture\DoctrineCascadeRelationship\ChangesEntityRelationshipCascadePersist; |
27 | 28 | use Zenstruck\Foundry\Tests\Fixture\DoctrineCascadeRelationship\UsingRelationships; |
28 | 29 | use Zenstruck\Foundry\Tests\Fixture\Entity\Contact; |
| 30 | +use Zenstruck\Foundry\Tests\Fixture\Entity\EdgeCases\EntityWithCloneMethod; |
29 | 31 | use Zenstruck\Foundry\Tests\Fixture\Factories\Entity\Address\AddressFactory; |
30 | 32 | use Zenstruck\Foundry\Tests\Fixture\Factories\Entity\Category\CategoryFactory; |
31 | 33 | use Zenstruck\Foundry\Tests\Fixture\Factories\Entity\Contact\ContactFactory; |
32 | 34 | use Zenstruck\Foundry\Tests\Fixture\Factories\Entity\GenericEntityFactory; |
33 | 35 | use Zenstruck\Foundry\Tests\Integration\Persistence\AutoRefreshTestCase; |
34 | 36 | use Zenstruck\Foundry\Tests\Integration\RequiresORM; |
35 | 37 |
|
| 38 | +use function Zenstruck\Foundry\Persistence\persistent_factory; |
36 | 39 | use function Zenstruck\Foundry\Persistence\refresh_all; |
37 | 40 |
|
38 | 41 | /** |
@@ -122,8 +125,26 @@ public function it_can_refresh_objects_with_relationships(): void |
122 | 125 | self::assertSame('city', $contact->getAddress()->getCity()); |
123 | 126 | } |
124 | 127 |
|
| 128 | + /** |
| 129 | + * The previous test creates entities with circular dependencies, |
| 130 | + * so the PersistedObjectsTracker still have references to them. |
| 131 | + * |
| 132 | + * Let's ensure we don't leave the test in an invalide state when the container is reset |
| 133 | + */ |
| 134 | + #[Test] |
| 135 | + #[Depends('it_can_refresh_objects_with_relationships')] |
| 136 | + public function assert_test_starts_with_a_non_booted_kernel(): void |
| 137 | + { |
| 138 | + self::assertSame(0, PersistedObjectsTracker::countObjects()); |
| 139 | + |
| 140 | + self::assertFalse(self::$booted); |
| 141 | + |
| 142 | + // ensure we can get a client without the error: |
| 143 | + // Booting the kernel before calling "WebTestCase::createClient()" is not supported |
| 144 | + self::createClient(); |
| 145 | + } |
| 146 | + |
125 | 147 | #[Test] |
126 | | - #[IgnorePhpunitWarnings(EdgeCasesRelationshipTest::DATA_PROVIDER_WARNING_REGEX)] |
127 | 148 | public function it_can_refresh_with_doctrine_proxies(): void |
128 | 149 | { |
129 | 150 | $contact = ContactFactory::createOne(); |
@@ -151,6 +172,22 @@ public function it_can_refresh_with_doctrine_proxies(): void |
151 | 172 | self::assertSame('foo', $category->getName()); |
152 | 173 | } |
153 | 174 |
|
| 175 | + #[Test] |
| 176 | + public function it_can_refresh_entity_which_removes_its_id_in_clone(): void |
| 177 | + { |
| 178 | + $entity = persistent_factory(EntityWithCloneMethod::class)->create(); |
| 179 | + $entityId = $entity->id; |
| 180 | + |
| 181 | + $this->objectManager()->getConnection()->executeQuery('UPDATE entity_with_clone_method SET prop = \'foo\' WHERE id = ?', [$entity->id]); |
| 182 | + |
| 183 | + self::ensureKernelShutdown(); |
| 184 | + |
| 185 | + self::assertTrue((new \ReflectionClass($entity))->isUninitializedLazyObject($entity)); |
| 186 | + |
| 187 | + self::assertSame('foo', $entity->prop); |
| 188 | + self::assertSame($entityId, $entity->id); |
| 189 | + } |
| 190 | + |
154 | 191 | protected static function factory(): PersistentObjectFactory |
155 | 192 | { |
156 | 193 | return GenericEntityFactory::new(); |
|
0 commit comments