diff --git a/tests/Doctrine/Tests/Models/Issue9300/Issue9300Child.php b/tests/Doctrine/Tests/Models/Issue9300/Issue9300Child.php new file mode 100644 index 00000000000..eb22eb04ace --- /dev/null +++ b/tests/Doctrine/Tests/Models/Issue9300/Issue9300Child.php @@ -0,0 +1,44 @@ +parents = new ArrayCollection(); + } +} diff --git a/tests/Doctrine/Tests/Models/Issue9300/Issue9300Parent.php b/tests/Doctrine/Tests/Models/Issue9300/Issue9300Parent.php new file mode 100644 index 00000000000..73dc28df1ce --- /dev/null +++ b/tests/Doctrine/Tests/Models/Issue9300/Issue9300Parent.php @@ -0,0 +1,31 @@ +useModelSet('issue9300'); + parent::setUp(); + } + + /** + * @group #9300 + */ + public function testPersistedCollectionIsPresentInOriginalDataAfterFlush(): void + { + $parent = new Issue9300Parent(); + $child = new Issue9300Child(); + $child->parents->add($parent); + + $parent->name = 'abc'; + $child->name = 'abc'; + + $this->_em->persist($parent); + $this->_em->persist($child); + $this->_em->flush(); + + $parent->name = 'abcd'; + $child->name = 'abcd'; + + $this->_em->flush(); + + self::assertArrayHasKey('parents', $this->_em->getUnitOfWork()->getOriginalEntityData($child)); + } + + /** + * @group #9300 + */ + public function testPersistingCollectionAfterFlushWorksAsExpected(): void + { + $parentOne = new Issue9300Parent(); + $parentTwo = new Issue9300Parent(); + $childOne = new Issue9300Child(); + + $parentOne->name = 'abc'; + $parentTwo->name = 'abc'; + $childOne->name = 'abc'; + $childOne->parents = new ArrayCollection([$parentOne]); + + $this->_em->persist($parentOne); + $this->_em->persist($parentTwo); + $this->_em->persist($childOne); + $this->_em->flush(); + + // Recalculate change-set -> new original data + $childOne->name = 'abcd'; + $this->_em->flush(); + + $childOne->parents = new ArrayCollection([$parentTwo]); + + $this->_em->flush(); + $this->_em->clear(); + + $childOneFresh = $this->_em->find(Issue9300Child::class, $childOne->id); + self::assertCount(1, $childOneFresh->parents); + self::assertEquals($parentTwo->id, $childOneFresh->parents[0]->id); + } +} diff --git a/tests/Doctrine/Tests/ORM/UnitOfWorkTest.php b/tests/Doctrine/Tests/ORM/UnitOfWorkTest.php index df61cd27b59..f04068d3a35 100644 --- a/tests/Doctrine/Tests/ORM/UnitOfWorkTest.php +++ b/tests/Doctrine/Tests/ORM/UnitOfWorkTest.php @@ -828,75 +828,6 @@ public function testItThrowsWhenLookingUpIdentifierForUnknownEntity(): void $this->expectException(EntityNotFoundException::class); $this->_unitOfWork->getEntityIdentifier(new stdClass()); } - - /** - * @group #9300 - */ - public function testPersistedCollectionIsPresentInOriginalDataAfterComputeChangeset(): void - { - $entity = new EntityWithCollection(); - $entity->children->add(new ChildEntity()); - $entity->name = 'abc'; - - $this->_unitOfWork->persist($entity); - $this->_unitOfWork->commit(); - - $entity->name = 'abcd'; - $this->_unitOfWork->computeChangeSets(); - - self::assertArrayHasKey('children', $this->_unitOfWork->getOriginalEntityData($entity)); - } -} - -/** - * @Entity - */ -class EntityWithCollection -{ - /** - * @var Collection|ChildEntity[] - * @OneToMany(targetEntity="ChildEntity", mappedBy="parent", cascade={"persist"}) - */ - public $children; - - /** - * @var int - * @Id - * @Column(type="integer") - * @GeneratedValue - */ - public $id; - - /** - * @var string - * @Column(type="string") - */ - public $name; - - public function __construct() - { - $this->children = new ArrayCollection(); - } -} - -/** - * @Entity - */ -class ChildEntity -{ - /** - * @var int - * @Id - * @Column(type="integer") - * @GeneratedValue - */ - public $id; - - /** - * @var int - * @ManyToOne(targetEntity="EntityWithCollection", inversedBy="children") - */ - public $parent; } /** diff --git a/tests/Doctrine/Tests/OrmFunctionalTestCase.php b/tests/Doctrine/Tests/OrmFunctionalTestCase.php index d15dd58cc92..c907008cba3 100644 --- a/tests/Doctrine/Tests/OrmFunctionalTestCase.php +++ b/tests/Doctrine/Tests/OrmFunctionalTestCase.php @@ -335,6 +335,10 @@ abstract class OrmFunctionalTestCase extends OrmTestCase Models\Issue5989\Issue5989Employee::class, Models\Issue5989\Issue5989Manager::class, ], + 'issue9300' => [ + Models\Issue9300\Issue9300Child::class, + Models\Issue9300\Issue9300Parent::class, + ], ]; protected function useModelSet(string $setName): void