|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Doctrine\Tests\ORM\Functional\Ticket; |
| 6 | + |
| 7 | +use Doctrine\Common\Collections\ArrayCollection; |
| 8 | +use Doctrine\Tests\Models\Issue9300\Issue9300Child; |
| 9 | +use Doctrine\Tests\Models\Issue9300\Issue9300Parent; |
| 10 | +use Doctrine\Tests\OrmFunctionalTestCase; |
| 11 | + |
| 12 | +/** |
| 13 | + * @group 9300 |
| 14 | + */ |
| 15 | +class Issue9300Test extends OrmFunctionalTestCase |
| 16 | +{ |
| 17 | + protected function setUp(): void |
| 18 | + { |
| 19 | + $this->useModelSet('issue9300'); |
| 20 | + parent::setUp(); |
| 21 | + } |
| 22 | + |
| 23 | + /** |
| 24 | + * @group #9300 |
| 25 | + */ |
| 26 | + public function testPersistedCollectionIsPresentInOriginalDataAfterFlush(): void |
| 27 | + { |
| 28 | + $parent = new Issue9300Parent(); |
| 29 | + $child = new Issue9300Child(); |
| 30 | + $child->parents->add($parent); |
| 31 | + |
| 32 | + $parent->name = 'abc'; |
| 33 | + $child->name = 'abc'; |
| 34 | + |
| 35 | + $this->_em->persist($parent); |
| 36 | + $this->_em->persist($child); |
| 37 | + $this->_em->flush(); |
| 38 | + |
| 39 | + $parent->name = 'abcd'; |
| 40 | + $child->name = 'abcd'; |
| 41 | + |
| 42 | + $this->_em->flush(); |
| 43 | + |
| 44 | + self::assertArrayHasKey('parents', $this->_em->getUnitOfWork()->getOriginalEntityData($child)); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * @group #9300 |
| 49 | + */ |
| 50 | + public function testPersistingCollectionAfterFlushWorksAsExpected(): void |
| 51 | + { |
| 52 | + $parentOne = new Issue9300Parent(); |
| 53 | + $parentTwo = new Issue9300Parent(); |
| 54 | + $childOne = new Issue9300Child(); |
| 55 | + |
| 56 | + $parentOne->name = 'abc'; |
| 57 | + $parentTwo->name = 'abc'; |
| 58 | + $childOne->name = 'abc'; |
| 59 | + $childOne->parents = new ArrayCollection([$parentOne]); |
| 60 | + |
| 61 | + $this->_em->persist($parentOne); |
| 62 | + $this->_em->persist($parentTwo); |
| 63 | + $this->_em->persist($childOne); |
| 64 | + $this->_em->flush(); |
| 65 | + |
| 66 | + // Recalculate change-set -> new original data |
| 67 | + $childOne->name = 'abcd'; |
| 68 | + $this->_em->flush(); |
| 69 | + |
| 70 | + $childOne->parents = new ArrayCollection([$parentTwo]); |
| 71 | + |
| 72 | + $this->_em->flush(); |
| 73 | + $this->_em->clear(); |
| 74 | + |
| 75 | + $childOneFresh = $this->_em->find(Issue9300Child::class, $childOne->id); |
| 76 | + self::assertCount(1, $childOneFresh->parents); |
| 77 | + self::assertEquals($parentTwo->id, $childOneFresh->parents[0]->id); |
| 78 | + } |
| 79 | +} |
0 commit comments