Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 51f20a9

Browse files
committed
Add 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 330c0bc commit 51f20a9

1 file changed

Lines changed: 175 additions & 0 deletions

File tree

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

0 commit comments

Comments
 (0)