|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Doctrine\Tests\ORM\Functional; |
| 6 | + |
| 7 | +use Doctrine\ORM\Mapping as ORM; |
| 8 | +use Doctrine\Tests\OrmFunctionalTestCase; |
| 9 | + |
| 10 | +use function uniqid; |
| 11 | + |
| 12 | +/** |
| 13 | + * @group GH7877 |
| 14 | + */ |
| 15 | +class GH7877Test extends OrmFunctionalTestCase |
| 16 | +{ |
| 17 | + protected function setUp(): void |
| 18 | + { |
| 19 | + parent::setUp(); |
| 20 | + |
| 21 | + $this->createSchemaForModels( |
| 22 | + GH7877ApplicationGeneratedIdEntity::class, |
| 23 | + GH7877EntityWithNullableAssociation::class |
| 24 | + ); |
| 25 | + } |
| 26 | + |
| 27 | + public function testSelfReferenceWithApplicationGeneratedIdMayBeNotNullable(): void |
| 28 | + { |
| 29 | + $entity = new GH7877ApplicationGeneratedIdEntity(); |
| 30 | + $entity->parent = $entity; |
| 31 | + |
| 32 | + $this->expectNotToPerformAssertions(); |
| 33 | + |
| 34 | + $this->_em->persist($entity); |
| 35 | + $this->_em->flush(); |
| 36 | + } |
| 37 | + |
| 38 | + public function testCrossReferenceWithApplicationGeneratedIdMayBeNotNullable(): void |
| 39 | + { |
| 40 | + $entity1 = new GH7877ApplicationGeneratedIdEntity(); |
| 41 | + $entity1->parent = $entity1; |
| 42 | + $entity2 = new GH7877ApplicationGeneratedIdEntity(); |
| 43 | + $entity2->parent = $entity1; |
| 44 | + |
| 45 | + $this->expectNotToPerformAssertions(); |
| 46 | + |
| 47 | + // As long as we do not have entity-level commit order computation |
| 48 | + // (see https://github.com/doctrine/orm/pull/10547), |
| 49 | + // this only works when the UoW processes $entity1 before $entity2, |
| 50 | + // so that the foreign key constraint E2 -> E1 can be satisfied. |
| 51 | + |
| 52 | + $this->_em->persist($entity1); |
| 53 | + $this->_em->persist($entity2); |
| 54 | + $this->_em->flush(); |
| 55 | + } |
| 56 | + |
| 57 | + public function testNullableForeignKeysMakeInsertOrderLessRelevant(): void |
| 58 | + { |
| 59 | + $entity1 = new GH7877EntityWithNullableAssociation(); |
| 60 | + $entity1->parent = $entity1; |
| 61 | + $entity2 = new GH7877EntityWithNullableAssociation(); |
| 62 | + $entity2->parent = $entity1; |
| 63 | + |
| 64 | + $this->expectNotToPerformAssertions(); |
| 65 | + |
| 66 | + // In contrast to the previous test, this case demonstrates that with NULLable |
| 67 | + // associations, even without entity-level commit order computation |
| 68 | + // (see https://github.com/doctrine/orm/pull/10547), we can get away with an |
| 69 | + // insertion order of E2 before E1. That is because the UoW will schedule an extra |
| 70 | + // update that saves the day - the foreign key reference will established only after |
| 71 | + // all insertions have been performed. |
| 72 | + |
| 73 | + $this->_em->persist($entity2); |
| 74 | + $this->_em->persist($entity1); |
| 75 | + $this->_em->flush(); |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +/** |
| 80 | + * @ORM\Entity |
| 81 | + */ |
| 82 | +class GH7877ApplicationGeneratedIdEntity |
| 83 | +{ |
| 84 | + /** |
| 85 | + * @ORM\Id |
| 86 | + * @ORM\Column(type="string") |
| 87 | + * @ORM\GeneratedValue(strategy="NONE") |
| 88 | + * |
| 89 | + * @var string |
| 90 | + */ |
| 91 | + public $id; |
| 92 | + |
| 93 | + /** |
| 94 | + * (!) Note this uses "nullable=false" |
| 95 | + * |
| 96 | + * @ORM\ManyToOne(targetEntity="GH7877ApplicationGeneratedIdEntity") |
| 97 | + * @ORM\JoinColumn(name="parent_id", referencedColumnName="id", nullable=false) |
| 98 | + * |
| 99 | + * @var self |
| 100 | + */ |
| 101 | + public $parent; |
| 102 | + |
| 103 | + public function __construct() |
| 104 | + { |
| 105 | + $this->id = uniqid(); |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +/** |
| 110 | + * @ORM\Entity |
| 111 | + */ |
| 112 | +class GH7877EntityWithNullableAssociation |
| 113 | +{ |
| 114 | + /** |
| 115 | + * @ORM\Id |
| 116 | + * @ORM\Column(type="string") |
| 117 | + * @ORM\GeneratedValue(strategy="NONE") |
| 118 | + * |
| 119 | + * @var string |
| 120 | + */ |
| 121 | + public $id; |
| 122 | + |
| 123 | + /** |
| 124 | + * @ORM\ManyToOne(targetEntity="GH7877EntityWithNullableAssociation") |
| 125 | + * @ORM\JoinColumn(name="parent_id", referencedColumnName="id", nullable=true) |
| 126 | + * |
| 127 | + * @var self |
| 128 | + */ |
| 129 | + public $parent; |
| 130 | + |
| 131 | + public function __construct() |
| 132 | + { |
| 133 | + $this->id = uniqid(); |
| 134 | + } |
| 135 | +} |
0 commit comments