Skip to content

Commit ad489d2

Browse files
committed
Failing test: Entity insertions must not happen table-wise
Add tests for entity insertion and deletion that require writes to different tables in an interleaved fashion, and that have to re-visit a particular table. #### Background In doctrine#10531, I've given an example where it is necessary to compute the commit order on the entity (instead of table) level. Taking a closer look at the UoW to see how this could be achieved, I noticed that the current, table-level commit order manifests itself also in the API between the UoW and `EntityPersister`s. #### Current situation The UoW computes the commit order on the table level. All entity insertions for a particular table are passed through `EntityPersister::addInsert()` and finally written through `EntityPersister::executeInserts()`. #### Suggested change The test in this PR contains a carefully constructed set of four entities. Two of them are of the same class (are written to the same table), but require other entities to be processed first. In order to be able to insert this set of entities, the ORM must be able to perform inserts for a given table repeatedly, interleaved with writing other entities to their respective tables.
1 parent d196acb commit ad489d2

File tree

1 file changed

+184
-0
lines changed

1 file changed

+184
-0
lines changed
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
<?php
2+
3+
namespace Doctrine\Tests\ORM\Functional\Ticket;
4+
5+
use Doctrine\ORM\Mapping as ORM;
6+
use Doctrine\Tests\OrmFunctionalTestCase;
7+
8+
class GH10532Test extends OrmFunctionalTestCase
9+
{
10+
protected function setUp(): void
11+
{
12+
parent::setUp();
13+
14+
$this->createSchemaForModels(
15+
GH10532A::class,
16+
GH10532B::class,
17+
GH10532C::class,
18+
GH10532X::class
19+
);
20+
}
21+
22+
public function tearDown(): void
23+
{
24+
$conn = static::$sharedConn;
25+
$conn->executeStatement('DELETE FROM gh10532_c');
26+
$conn->executeStatement('DELETE FROM gh10532_b');
27+
$conn->executeStatement('DELETE FROM gh10532_a');
28+
$conn->executeStatement('DELETE FROM gh10532_x');
29+
}
30+
31+
public function testInserts(): void
32+
{
33+
// Dependencies are $a1 -> $b -> $a2 -> $c
34+
35+
$a1 = new GH10532A();
36+
$b = new GH10532B();
37+
$a2 = new GH10532A();
38+
$c = new GH10532C();
39+
40+
$a1->x = $b;
41+
$b->a = $a2;
42+
$a2->x = $c;
43+
44+
/*
45+
* The following would force a working commit order, but that's not what
46+
* we want (the ORM shall sort this out internally).
47+
*
48+
* $this->_em->persist($c);
49+
* $this->_em->persist($a2);
50+
* $this->_em->flush();
51+
* $this->_em->persist($b);
52+
* $this->_em->persist($a1);
53+
* $this->_em->flush();
54+
*/
55+
56+
$this->_em->persist($a1);
57+
$this->_em->persist($a2);
58+
$this->_em->persist($b);
59+
$this->_em->persist($c);
60+
$this->_em->flush();
61+
62+
self::assertNotNull($a1->id);
63+
self::assertNotNull($b->id);
64+
self::assertNotNull($a2->id);
65+
self::assertNotNull($c->id);
66+
}
67+
68+
public function testDeletes(): void
69+
{
70+
// Dependencies are $a1 -> $b -> $a2 -> $c
71+
72+
$this->expectNotToPerformAssertions();
73+
$con = $this->_em->getConnection();
74+
75+
// The "c" entity
76+
$con->insert('gh10532_x', ['id' => 1, 'discr' => 'C']);
77+
$con->insert('gh10532_c', ['id' => 1]);
78+
$c = $this->_em->find(GH10532C::class, 1);
79+
80+
// The "a2" entity
81+
$con->insert('gh10532_a', ['id' => 2, 'gh10532x_id' => 1]);
82+
$a2 = $this->_em->find(GH10532A::class, 2);
83+
84+
// The "b" entity
85+
$con->insert('gh10532_x', ['id' => 3, 'discr' => 'B']);
86+
$con->insert('gh10532_b', ['id' => 3, 'gh10532a_id' => 2]);
87+
$b = $this->_em->find(GH10532B::class, 3);
88+
89+
// The "a1" entity
90+
$con->insert('gh10532_a', ['id' => 4, 'gh10532x_id' => 3]);
91+
$a1 = $this->_em->find(GH10532A::class, 4);
92+
93+
/*
94+
* The following would make the deletions happen in an order
95+
* where the not-nullable foreign key constraints would not be
96+
* violated. But, we want the ORM to be able to sort this out
97+
* internally.
98+
*
99+
* $this->_em->remove($a1);
100+
* $this->_em->flush();
101+
* $this->_em->remove($b);
102+
* $this->_em->flush();
103+
* $this->_em->remove($a2);
104+
* $this->_em->remove($c);
105+
* $this->_em->flush();
106+
*/
107+
108+
$this->_em->remove($a1);
109+
$this->_em->remove($a2);
110+
$this->_em->remove($b);
111+
$this->_em->remove($c);
112+
113+
$this->_em->flush();
114+
}
115+
}
116+
117+
/**
118+
* @ORM\Entity
119+
* @ORM\Table(name="gh10532_x")
120+
* @ORM\DiscriminatorColumn(name="discr", type="string")
121+
* @ORM\DiscriminatorMap({ "B": "GH10532B", "C": "GH10532C" })
122+
* @ORM\InheritanceType("JOINED")
123+
*
124+
* We are using JTI here, since STI would relax the not-nullable constraint for the "parent"
125+
* column. Causes another error, but not the constraint violation I'd like to point out.
126+
*/
127+
abstract class GH10532X
128+
{
129+
/**
130+
* @ORM\Id
131+
* @ORM\GeneratedValue(strategy="AUTO")
132+
* @ORM\Column(type="integer")
133+
*
134+
* @var int
135+
*/
136+
public $id;
137+
}
138+
139+
/**
140+
* @ORM\Entity
141+
* @ORM\Table(name="gh10532_b")
142+
*/
143+
class GH10532B extends GH10532X
144+
{
145+
/**
146+
* @ORM\ManyToOne(targetEntity="GH10532A")
147+
* @ORM\JoinColumn(nullable=false, name="gh10532a_id")
148+
*
149+
* @var GH10532A
150+
*/
151+
public $a;
152+
}
153+
154+
/**
155+
* @ORM\Entity
156+
* @ORM\Table(name="gh10532_c")
157+
*/
158+
class GH10532C extends GH10532X
159+
{
160+
}
161+
162+
/**
163+
* @ORM\Entity
164+
* @ORM\Table(name="gh10532_a")
165+
*/
166+
class GH10532A
167+
{
168+
/**
169+
* @ORM\Id
170+
* @ORM\GeneratedValue(strategy="AUTO")
171+
* @ORM\Column(type="integer")
172+
*
173+
* @var int
174+
*/
175+
public $id;
176+
177+
/**
178+
* @ORM\ManyToOne(targetEntity="GH10532X")
179+
* @ORM\JoinColumn(nullable=false, name="gh10532x_id")
180+
*
181+
* @var GH10532X
182+
*/
183+
public $x;
184+
}

0 commit comments

Comments
 (0)