From aad875eea174a5cb7b13c14c78ea5b3eca83d464 Mon Sep 17 00:00:00 2001 From: Matthias Pigulla Date: Fri, 2 Jun 2023 17:44:45 +0000 Subject: [PATCH] Add tests to show #6499 has been fixed MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit @frikkle was the first to propose these tests in #6533. @rvanlaak followed up in #8703, making some adjustments. Co-authored-by: Gabe van der Weijde Co-authored-by: Richard van Laak Co-authored-by: Grégoire Paris --- .../GH6499OneToManyRelationshipTest.php | 155 ++++++++++++++++++ .../Ticket/GH6499OneToOneRelationshipTest.php | 78 +++++++++ .../ORM/Functional/Ticket/GH6499Test.php | 103 ++++++++++++ 3 files changed, 336 insertions(+) create mode 100644 tests/Doctrine/Tests/ORM/Functional/Ticket/GH6499OneToManyRelationshipTest.php create mode 100644 tests/Doctrine/Tests/ORM/Functional/Ticket/GH6499OneToOneRelationshipTest.php create mode 100644 tests/Doctrine/Tests/ORM/Functional/Ticket/GH6499Test.php diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/GH6499OneToManyRelationshipTest.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/GH6499OneToManyRelationshipTest.php new file mode 100644 index 00000000000..f638e79f013 --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/GH6499OneToManyRelationshipTest.php @@ -0,0 +1,155 @@ +createSchemaForModels(Application::class, Person::class, ApplicationPerson::class); + } + + /** + * Test for the bug described in issue #6499. + */ + public function testIssue(): void + { + $person = new Person(); + $this->_em->persist($person); + + $application = new Application(); + $this->_em->persist($application); + + $applicationPerson = new ApplicationPerson($person, $application); + + $this->_em->persist($applicationPerson); + $this->_em->flush(); + $this->_em->clear(); + + $personFromDatabase = $this->_em->find(Person::class, $person->id); + $applicationFromDatabase = $this->_em->find(Application::class, $application->id); + + self::assertEquals($personFromDatabase->id, $person->id, 'Issue #6499 will result in an integrity constraint violation before reaching this point.'); + self::assertFalse($personFromDatabase->getApplicationPeople()->isEmpty()); + + self::assertEquals($applicationFromDatabase->id, $application->id, 'Issue #6499 will result in an integrity constraint violation before reaching this point.'); + self::assertFalse($applicationFromDatabase->getApplicationPeople()->isEmpty()); + } +} + +/** + * @ORM\Entity + * @ORM\Table("GH6499OTM_application") + */ +class Application +{ + /** + * @ORM\Id + * @ORM\GeneratedValue + * @ORM\Column(type="integer") + * + * @var int + */ + public $id; + + /** + * @ORM\OneToMany(targetEntity=ApplicationPerson::class, mappedBy="application", orphanRemoval=true, cascade={"persist"}) + * @ORM\JoinColumn(nullable=false) + * + * @var Collection + */ + private $applicationPeople; + + public function __construct() + { + $this->applicationPeople = new ArrayCollection(); + } + + public function getApplicationPeople(): Collection + { + return $this->applicationPeople; + } +} +/** + * @ORM\Entity() + * @ORM\Table("GH6499OTM_person") + */ +class Person +{ + /** + * @ORM\Id + * @ORM\GeneratedValue + * @ORM\Column(type="integer") + * + * @var int + */ + public $id; + + /** + * @ORM\OneToMany(targetEntity=ApplicationPerson::class, mappedBy="person", orphanRemoval=true, cascade={"persist"}) + * @ORM\JoinColumn(nullable=false) + * + * @var Collection + */ + private $applicationPeople; + + public function __construct() + { + $this->applicationPeople = new ArrayCollection(); + } + + public function getApplicationPeople(): Collection + { + return $this->applicationPeople; + } +} + +/** + * @ORM\Entity() + * @ORM\Table("GH6499OTM_application_person") + */ +class ApplicationPerson +{ + /** + * @ORM\Id + * @ORM\GeneratedValue + * @ORM\Column(type="integer") + * + * @var int + */ + public $id; + + /** + * @ORM\ManyToOne(targetEntity=Application::class, inversedBy="applicationPeople", cascade={"persist"}) + * @ORM\JoinColumn(nullable=false) + * + * @var Application + */ + public $application; + + /** + * @ORM\ManyToOne(targetEntity=Person::class, inversedBy="applicationPeople", cascade={"persist"}) + * @ORM\JoinColumn(nullable=false) + * + * @var Person + */ + public $person; + + public function __construct(Person $person, Application $application) + { + $this->person = $person; + $this->application = $application; + } +} diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/GH6499OneToOneRelationshipTest.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/GH6499OneToOneRelationshipTest.php new file mode 100644 index 00000000000..2922c674733 --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/GH6499OneToOneRelationshipTest.php @@ -0,0 +1,78 @@ +createSchemaForModels(GH6499OTOA::class, GH6499OTOB::class); + } + + /** + * Test for the bug described in issue #6499. + */ + public function testIssue(): void + { + $a = new GH6499OTOA(); + + $this->_em->persist($a); + $this->_em->flush(); + $this->_em->clear(); + + self::assertEquals( + $this->_em->find(GH6499OTOA::class, $a->id)->b->id, + $a->b->id, + 'Issue #6499 will result in an integrity constraint violation before reaching this point.' + ); + } +} + +/** @ORM\Entity */ +class GH6499OTOA +{ + /** + * @ORM\Id + * @ORM\Column(type="integer") + * @ORM\GeneratedValue + * + * @var int + */ + public $id; + + /** + * @ORM\OneToOne(targetEntity="GH6499OTOB", cascade={"persist"}) + * @ORM\JoinColumn(nullable=false) + * + * @var GH6499OTOB + */ + public $b; + + public function __construct() + { + $this->b = new GH6499OTOB(); + } +} + +/** @ORM\Entity */ +class GH6499OTOB +{ + /** + * @ORM\Id + * @ORM\Column(type="integer") + * @ORM\GeneratedValue + * + * @var int + */ + public $id; +} diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/GH6499Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/GH6499Test.php new file mode 100644 index 00000000000..a6672801ca4 --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/GH6499Test.php @@ -0,0 +1,103 @@ +createSchemaForModels(GH6499A::class, GH6499B::class); + } + + public function testIssue(): void + { + $b = new GH6499B(); + $a = new GH6499A(); + + $this->_em->persist($a); + + $a->b = $b; + + $this->_em->persist($b); + + $this->_em->flush(); + + self::assertIsInt($a->id); + self::assertIsInt($b->id); + } + + public function testIssueReversed(): void + { + $b = new GH6499B(); + $a = new GH6499A(); + + $a->b = $b; + + $this->_em->persist($b); + $this->_em->persist($a); + + $this->_em->flush(); + + self::assertIsInt($a->id); + self::assertIsInt($b->id); + } +} + +/** + * @ORM\Entity + */ +class GH6499A +{ + /** + * @ORM\Id + * @ORM\Column(type="integer") + * @ORM\GeneratedValue + * + * @var int + */ + public $id; + + /** + * @ORM\JoinColumn(nullable=false) + * @ORM\OneToOne(targetEntity=GH6499B::class) + * + * @var GH6499B + */ + public $b; +} + +/** + * @ORM\Entity + */ +class GH6499B +{ + /** + * @ORM\Id + * @ORM\Column(type="integer") + * @ORM\GeneratedValue + * + * @var int + */ + public $id; + + /** + * @ORM\ManyToOne(targetEntity=GH6499A::class) + * + * @var GH6499A + */ + private $a; +}