From 1de78f774a6e63b5252703ab75e1040f6ff8521c Mon Sep 17 00:00:00 2001 From: Richard van Laak Date: Wed, 30 Jun 2021 15:23:35 +0200 Subject: [PATCH] add OneToMany test to demonstrate #6499 persistence ordering bug on non-nullable cascading relationship --- .../DDC6499OneToManyRelationshipTest.php | 168 ++++++++++++++++++ ...hp => DDC6499OneToOneRelationshipTest.php} | 5 +- 2 files changed, 169 insertions(+), 4 deletions(-) create mode 100644 tests/Doctrine/Tests/ORM/Functional/Ticket/DDC6499OneToManyRelationshipTest.php rename tests/Doctrine/Tests/ORM/Functional/Ticket/{DDC6499Test.php => DDC6499OneToOneRelationshipTest.php} (96%) diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC6499OneToManyRelationshipTest.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC6499OneToManyRelationshipTest.php new file mode 100644 index 00000000000..001f355d82f --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC6499OneToManyRelationshipTest.php @@ -0,0 +1,168 @@ +_schemaTool->createSchema( + [ + $this->_em->getClassMetadata(Application::class), + $this->_em->getClassMetadata(Person::class), + $this->_em->getClassMetadata(ApplicationPerson::class), + ] + ); + } + + protected function tearDown(): void + { + parent::tearDown(); + + $this->_schemaTool->dropSchema( + [ + $this->_em->getClassMetadata(Application::class), + $this->_em->getClassMetadata(Person::class), + $this->_em->getClassMetadata(ApplicationPerson::class), + ] + ); + } + + /** + * Test for the bug described in issue #6499. + */ + public function testIssue(): void + { + if (! $this->_em->getConnection()->getDatabasePlatform()->supportsForeignKeyConstraints()) { + self::markTestSkipped('Platform does not support foreign keys.'); + } + + $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 a Integrity constraint violation before reaching this point.'); + self::assertFalse($personFromDatabase->getApplicationPeople()->isEmpty()); + + self::assertEquals($applicationFromDatabase->id, $application->id, 'Issue #6499 will result in a Integrity constraint violation before reaching this point.'); + self::assertFalse($applicationFromDatabase->getApplicationPeople()->isEmpty()); + } +} + +/** + * @Entity() + * @Table("ddc6499_application") + */ +class Application +{ + /** + * @Id + * @GeneratedValue + * @Column(type="integer") + * @var int + */ + public $id; + + /** + * @OneToMany(targetEntity=ApplicationPerson::class, mappedBy="application", orphanRemoval=true, cascade={"persist"}) + * @JoinColumn(nullable=false) + * @var Collection + */ + private $applicationPeople; + + public function __construct() + { + $this->applicationPeople = new ArrayCollection(); + } + + public function getApplicationPeople(): Collection + { + return $this->applicationPeople; + } +} +/** + * @Entity() + * @Table("ddc6499_person") + */ +class Person +{ + /** + * @Id + * @GeneratedValue + * @Column(type="integer") + * @var int + */ + public $id; + + /** + * @OneToMany(targetEntity=ApplicationPerson::class, mappedBy="person", orphanRemoval=true, cascade={"persist"}) + * @JoinColumn(nullable=false) + * @var Collection + */ + private $applicationPeople; + + public function __construct() + { + $this->applicationPeople = new ArrayCollection(); + } + + public function getApplicationPeople(): Collection + { + return $this->applicationPeople; + } +} + +/** + * @Entity() + * @Table("ddc6499_application_person") + */ +class ApplicationPerson +{ + /** + * @Id + * @GeneratedValue + * @Column(type="integer") + * @var int + */ + public $id; + + /** + * @ManyToOne(targetEntity=Application::class, inversedBy="applicationPeople", cascade={"persist"}) + * @JoinColumn(nullable=false) + * @var Application + */ + public $application; + + /** + * @ManyToOne(targetEntity=Person::class, inversedBy="applicationPeople", cascade={"persist"}) + * @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/DDC6499Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC6499OneToOneRelationshipTest.php similarity index 96% rename from tests/Doctrine/Tests/ORM/Functional/Ticket/DDC6499Test.php rename to tests/Doctrine/Tests/ORM/Functional/Ticket/DDC6499OneToOneRelationshipTest.php index 19c8e10ee3a..d79df0792d0 100644 --- a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC6499Test.php +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC6499OneToOneRelationshipTest.php @@ -7,7 +7,7 @@ /** * @group DDC-6499 */ -class DDC6499Test extends OrmFunctionalTestCase +class DDC6499OneToOneRelationshipTest extends OrmFunctionalTestCase { protected function setUp(): void { @@ -59,7 +59,6 @@ class DDC6499A * @Id * @Column(type="integer") * @GeneratedValue - * * @var int */ public $id; @@ -67,7 +66,6 @@ class DDC6499A /** * @OneToOne(targetEntity="DDC6499B", cascade={"persist"}) * @JoinColumn(nullable=false) - * * @var DDC6499B */ public $b; @@ -85,7 +83,6 @@ class DDC6499B * @Id * @Column(type="integer") * @GeneratedValue - * * @var int */ public $id;