Skip to content

Commit 883f84d

Browse files
committed
adds testcase to reproduce #2582
1 parent ea1d375 commit 883f84d

File tree

2 files changed

+272
-0
lines changed

2 files changed

+272
-0
lines changed
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?php
2+
3+
namespace Gedmo\Tests\Tree\Fixture\Issue2582;
4+
5+
use Doctrine\Common\Collections\ArrayCollection;
6+
use Doctrine\Common\Collections\Collection;
7+
use Doctrine\ORM\Mapping as ORM;
8+
use Gedmo\Mapping\Annotation as Gedmo;
9+
10+
/**
11+
* @Gedmo\Tree(type="nested")
12+
*
13+
* @ORM\Table(name="ous")
14+
* @ORM\Index(name="idx_tree", columns={"lft", "rgt"})
15+
* @ORM\Entity())
16+
*/
17+
#[ORM\Table(name: 'ous')]
18+
#[ORM\Index(name: 'idx_tree', columns: ['lft', 'rgt'])]
19+
#[ORM\Entity]
20+
#[Gedmo\Tree(type: 'nested')]
21+
class OU
22+
{
23+
/**
24+
* @ORM\Column(name="id", type="guid")
25+
* @ORM\Id()
26+
*/
27+
#[ORM\Column('id', 'guid')]
28+
#[ORM\Id]
29+
private string $id;
30+
31+
/**
32+
* @Gedmo\TreeParent()
33+
*
34+
* @ORM\ManyToOne(targetEntity="\Gedmo\Tests\Tree\Fixture\Issue2582\OU", inversedBy="children")
35+
* @ORM\JoinColumn(name="parent", referencedColumnName="id", nullable=true, onDelete="CASCADE")
36+
*/
37+
#[ORM\ManyToOne(targetEntity: self::class, inversedBy: 'children')]
38+
#[ORM\JoinColumn(name: 'parent', referencedColumnName: 'id', nullable: true, onDelete: 'CASCADE')]
39+
#[Gedmo\TreeParent]
40+
private ?self $parent = null;
41+
42+
/**
43+
* @Gedmo\TreeLeft()
44+
*
45+
* @ORM\Column(name="lft", type="integer", options={"unsigned"=true})
46+
*/
47+
#[ORM\Column(name: 'lft', type: 'integer', options: ['unsigned' => true])]
48+
#[Gedmo\TreeLeft]
49+
private int $left = 1;
50+
51+
/**
52+
* @Gedmo\TreeLevel()
53+
*
54+
* @ORM\Column(name="lvl", type="integer", options={"unsigned"=true})
55+
*/
56+
#[ORM\Column(name: 'lvl', type: 'integer', options: ['unsigned' => true])]
57+
#[Gedmo\TreeLevel]
58+
private int $level = 0;
59+
60+
/**
61+
* @Gedmo\TreeRight()
62+
*
63+
* @ORM\Column(name="rgt", type="integer", options={"unsigned"=true})
64+
*/
65+
#[ORM\Column(name: 'rgt', type: 'integer', options: ['unsigned' => true])]
66+
#[Gedmo\TreeRight]
67+
private int $right = 2;
68+
69+
/**
70+
* @ORM\OneToMany(targetEntity="\Gedmo\Tests\Tree\Fixture\Issue2582\OU", mappedBy="parent")
71+
* @ORM\OrderBy({"left" = "ASC"})
72+
*/
73+
#[ORM\OneToMany(targetEntity: self::class, mappedBy: 'parent')]
74+
#[ORM\OrderBy(['left' => 'ASC'])]
75+
private Collection $children;
76+
77+
public function __construct(string $id, ?self $parent = null)
78+
{
79+
$this->id = $id;
80+
$this->children = new ArrayCollection();
81+
$this->parent = $parent;
82+
if ($parent) {
83+
$parent->children->add($this);
84+
}
85+
}
86+
87+
public function getId(): string
88+
{
89+
return $this->id;
90+
}
91+
92+
public function getParent(): ?OU
93+
{
94+
return $this->parent;
95+
}
96+
97+
public function getLeft(): int
98+
{
99+
return $this->left;
100+
}
101+
102+
public function getLevel(): int
103+
{
104+
return $this->level;
105+
}
106+
107+
public function getRight(): int
108+
{
109+
return $this->right;
110+
}
111+
112+
public function getChildren(): Collection
113+
{
114+
return $this->children;
115+
}
116+
}
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Doctrine Behavioral Extensions package.
5+
* (c) Gediminas Morkevicius <[email protected]> http://www.gediminasm.org
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
namespace Gedmo\Tests\Tree\Issue;
11+
12+
use Doctrine\Common\EventManager;
13+
use Gedmo\Tests\Tool\BaseTestCaseORM;
14+
use Gedmo\Tests\Tree\Fixture\Issue2582\OU;
15+
use Gedmo\Tree\TreeListener;
16+
17+
class Issue2582Test extends BaseTestCaseORM
18+
{
19+
private TreeListener $listener;
20+
21+
protected function setUp(): void
22+
{
23+
parent::setUp();
24+
25+
$this->listener = new TreeListener();
26+
27+
$evm = new EventManager();
28+
$evm->addEventSubscriber($this->listener);
29+
30+
$this->getDefaultMockSqliteEntityManager($evm);
31+
}
32+
33+
public function testInsertTwoRootsInOneFlush(): void
34+
{
35+
$ou1 = new OU('00000000-0000-0000-0000-000000000001', null);
36+
$ou11 = new OU('00000000-0000-0000-0000-000000000011', $ou1);
37+
$ou2 = new OU('00000000-0000-0000-0000-000000000002', null);
38+
$ou21 = new OU('00000000-0000-0000-0000-000000000021', $ou2);
39+
40+
41+
$this->em->persist($ou1);
42+
$this->em->persist($ou11);
43+
$this->em->persist($ou2);
44+
$this->em->persist($ou21);
45+
$this->em->flush();
46+
47+
$this->em->clear();
48+
49+
$expected = [
50+
['00000000-0000-0000-0000-000000000001', null, 1, 0, 4],
51+
['00000000-0000-0000-0000-000000000011', '00000000-0000-0000-0000-000000000001', 2, 1, 3],
52+
['00000000-0000-0000-0000-000000000002', null, 5, 0, 8],
53+
['00000000-0000-0000-0000-000000000021', '00000000-0000-0000-0000-000000000002', 6, 1, 7],
54+
];
55+
foreach ($this->fetchAllOUs() as $i => $a) {
56+
self::assertSame(
57+
$expected[$i],
58+
[
59+
$a->getId(),
60+
$a->getParent()?->getId(),
61+
$a->getLeft(),
62+
$a->getLevel(),
63+
$a->getRight(),
64+
],
65+
);
66+
}
67+
}
68+
69+
public function testInsertTwoRootsInOneFlushRootsFirst(): void
70+
{
71+
$ou1 = new OU('00000000-0000-0000-0000-000000000001', null);
72+
$ou11 = new OU('00000000-0000-0000-0000-000000000011', $ou1);
73+
$ou2 = new OU('00000000-0000-0000-0000-000000000002', null);
74+
$ou21 = new OU('00000000-0000-0000-0000-000000000021', $ou2);
75+
76+
77+
$this->em->persist($ou1);
78+
$this->em->persist($ou2);
79+
$this->em->persist($ou11);
80+
$this->em->persist($ou21);
81+
$this->em->flush();
82+
83+
$this->em->clear();
84+
85+
$expected = [
86+
['00000000-0000-0000-0000-000000000001', null, 1, 0, 4],
87+
['00000000-0000-0000-0000-000000000011', '00000000-0000-0000-0000-000000000001', 2, 1, 3],
88+
['00000000-0000-0000-0000-000000000002', null, 5, 0, 8],
89+
['00000000-0000-0000-0000-000000000021', '00000000-0000-0000-0000-000000000002', 6, 1, 7],
90+
];
91+
foreach ($this->fetchAllOUs() as $i => $a) {
92+
self::assertSame(
93+
$expected[$i],
94+
[
95+
$a->getId(),
96+
$a->getParent()?->getId(),
97+
$a->getLeft(),
98+
$a->getLevel(),
99+
$a->getRight(),
100+
],
101+
);
102+
}
103+
}
104+
105+
public function testInsertTwoRootsInTwoFlushes(): void
106+
{
107+
$ou1 = new OU('00000000-0000-0000-0000-000000000001', null);
108+
$ou11 = new OU('00000000-0000-0000-0000-000000000011', $ou1);
109+
$ou2 = new OU('00000000-0000-0000-0000-000000000002', null);
110+
$ou21 = new OU('00000000-0000-0000-0000-000000000021', $ou2);
111+
112+
113+
$this->em->persist($ou1);
114+
$this->em->persist($ou11);
115+
$this->em->flush();
116+
$this->em->persist($ou2);
117+
$this->em->persist($ou21);
118+
$this->em->flush();
119+
120+
$this->em->clear();
121+
122+
$expected = [
123+
['00000000-0000-0000-0000-000000000001', null, 1, 0, 4],
124+
['00000000-0000-0000-0000-000000000011', '00000000-0000-0000-0000-000000000001', 2, 1, 3],
125+
['00000000-0000-0000-0000-000000000002', null, 5, 0, 8],
126+
['00000000-0000-0000-0000-000000000021', '00000000-0000-0000-0000-000000000002', 6, 1, 7],
127+
];
128+
foreach ($this->fetchAllOUs() as $i => $a) {
129+
self::assertSame(
130+
$expected[$i],
131+
[
132+
$a->getId(),
133+
$a->getParent()?->getId(),
134+
$a->getLeft(),
135+
$a->getLevel(),
136+
$a->getRight(),
137+
],
138+
);
139+
}
140+
}
141+
142+
private function fetchAllOUs(): array
143+
{
144+
$categoryRepo = $this->em->getRepository(OU::class);
145+
return $categoryRepo
146+
->createQueryBuilder('ou')
147+
->orderBy('ou.left', 'ASC')
148+
->getQuery()
149+
->getResult();
150+
}
151+
152+
protected function getUsedEntityFixtures(): array
153+
{
154+
return [OU::class];
155+
}
156+
}

0 commit comments

Comments
 (0)