|
| 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