From 7d04e33dc9171b3a0c8f746effceea25ea0630a8 Mon Sep 17 00:00:00 2001 From: Matthias Pigulla Date: Thu, 21 Dec 2023 14:04:19 +0100 Subject: [PATCH] Add a test --- .../ORM/Functional/Ticket/GH11058Test.php | 122 ++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 tests/Doctrine/Tests/ORM/Functional/Ticket/GH11058Test.php diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/GH11058Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/GH11058Test.php new file mode 100644 index 00000000000..5141b5b67db --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/GH11058Test.php @@ -0,0 +1,122 @@ +setUpEntitySchema([ + GH11058Parent::class, + GH11058Child::class, + ]); + } + + public function testChildrenInsertedInOrderOfPersistCallsWhenParentPersistedLast(): void + { + [$parent, $child1, $child2] = $this->createParentWithTwoChildEntities(); + + $this->_em->persist($child1); + $this->_em->persist($child2); + $this->_em->persist($parent); + $this->_em->flush(); + + self::assertTrue($child1->id < $child2->id); + } + + public function testChildrenInsertedInOrderOfPersistCallsWhenParentPersistedFirst(): void + { + [$parent, $child1, $child2] = $this->createParentWithTwoChildEntities(); + + $this->_em->persist($parent); + $this->_em->persist($child1); + $this->_em->persist($child2); + $this->_em->flush(); + + self::assertTrue($child1->id < $child2->id); + } + + private function createParentWithTwoChildEntities(): array + { + $parent = new GH11058Parent(); + $child1 = new GH11058Child(); + $child2 = new GH11058Child(); + + $parent->addChild($child1); + $parent->addChild($child2); + + return [$parent, $child1, $child2]; + } +} + +/** + * @ORM\Entity() + */ +class GH11058Parent +{ + /** + * @ORM\Id + * @ORM\Column(type="integer") + * @ORM\GeneratedValue + * + * @var int + */ + public $id; + + /** + * @ORM\OneToMany(targetEntity="GH11058Child", mappedBy="parent") + * + * @var Collection + */ + public $children; + + public function __construct() + { + $this->children = new ArrayCollection(); + } + + public function addChild(GH11058Child $child): void + { + if (! $this->children->contains($child)) { + $this->children->add($child); + $child->setParent($this); + } + } +} + +/** + * @ORM\Entity() + */ +class GH11058Child +{ + /** + * @ORM\Id + * @ORM\Column(type="integer") + * @ORM\GeneratedValue + * + * @var int + */ + public $id; + + /** + * @ORM\ManyToOne(targetEntity="GH11058Parent", inversedBy="children") + * + * @var GH11058Parent + */ + public $parent; + + public function setParent(GH11058Parent $parent): void + { + $this->parent = $parent; + $parent->addChild($this); + } +}