|
4 | 4 |
|
5 | 5 | namespace Doctrine\Tests\ORM\Functional;
|
6 | 6 |
|
| 7 | +use Doctrine\Common\Collections\ArrayCollection; |
7 | 8 | use Doctrine\ORM\Event\PostPersistEventArgs;
|
8 | 9 | use Doctrine\ORM\Event\PostRemoveEventArgs;
|
9 | 10 | use Doctrine\ORM\Event\PreFlushEventArgs;
|
|
12 | 13 | use Doctrine\ORM\UnitOfWork;
|
13 | 14 | use Doctrine\Persistence\Event\LifecycleEventArgs;
|
14 | 15 | use Doctrine\Tests\Models\Company\CompanyContractListener;
|
| 16 | +use Doctrine\Tests\Models\Company\CompanyEmployee; |
15 | 17 | use Doctrine\Tests\Models\Company\CompanyFixContract;
|
16 | 18 | use Doctrine\Tests\Models\Company\CompanyPerson;
|
17 | 19 | use Doctrine\Tests\OrmFunctionalTestCase;
|
@@ -266,4 +268,44 @@ public function postRemove(PostRemoveEventArgs $args): void
|
266 | 268 |
|
267 | 269 | self::assertSame(2, $listener->invocationCount);
|
268 | 270 | }
|
| 271 | + |
| 272 | + public function testPostRemoveCalledAfterAllInMemoryCollectionsHaveBeenUpdated(): void |
| 273 | + { |
| 274 | + $contract = new CompanyFixContract(); |
| 275 | + $contract->setFixPrice(2000); |
| 276 | + |
| 277 | + $engineer = new CompanyEmployee(); |
| 278 | + $engineer->setName('J. Doe'); |
| 279 | + $engineer->setSalary(50); |
| 280 | + $engineer->setDepartment('tech'); |
| 281 | + |
| 282 | + $contract->addEngineer($engineer); |
| 283 | + $engineer->contracts = new ArrayCollection([$contract]); |
| 284 | + |
| 285 | + $this->_em->persist($contract); |
| 286 | + $this->_em->persist($engineer); |
| 287 | + $this->_em->flush(); |
| 288 | + |
| 289 | + $this->_em->getEventManager()->addEventListener([Events::postRemove], new class ($contract) { |
| 290 | + /** @var CompanyFixContract */ |
| 291 | + private $contract; |
| 292 | + |
| 293 | + public function __construct(CompanyFixContract $contract) |
| 294 | + { |
| 295 | + $this->contract = $contract; |
| 296 | + } |
| 297 | + |
| 298 | + public function postRemove(): void |
| 299 | + { |
| 300 | + Assert::assertEmpty($this->contract->getEngineers()); // Assert collection has been updated before event was dispatched |
| 301 | + Assert::assertTrue($this->contract->getEngineers()->isDirty()); // Collections are still dirty (is that relevant?) |
| 302 | + } |
| 303 | + }); |
| 304 | + |
| 305 | + $this->_em->remove($engineer); |
| 306 | + $this->_em->flush(); |
| 307 | + |
| 308 | + self::assertEmpty($contract->getEngineers()); |
| 309 | + self::assertFalse($contract->getEngineers()->isDirty()); |
| 310 | + } |
269 | 311 | }
|
0 commit comments