|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * This file is part of the Doctrine Behavioral Extensions package. |
| 7 | + * (c) Gediminas Morkevicius <[email protected]> http://www.gediminasm.org |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Gedmo\Tests\Tree; |
| 13 | + |
| 14 | +use Doctrine\Common\EventManager; |
| 15 | +use Gedmo\Tests\Tool\BaseTestCaseORM; |
| 16 | +use Gedmo\Tests\Tree\Fixture\MPCategoryUuid; |
| 17 | +use Gedmo\Tree\TreeListener; |
| 18 | +use Symfony\Component\Uid\UuidV4; |
| 19 | + |
| 20 | +/** |
| 21 | + * These are tests for Tree behavior when using Uuid as primary key and TreePathSource. |
| 22 | + * |
| 23 | + * @author Gustavo Falco <[email protected]> |
| 24 | + * @author Gediminas Morkevicius <[email protected]> |
| 25 | + * @author Andrea Bergamasco <[email protected]> |
| 26 | + */ |
| 27 | +final class MaterializedPathUuidORMTest extends BaseTestCaseORM |
| 28 | +{ |
| 29 | + /** |
| 30 | + * @var array<string, mixed> |
| 31 | + */ |
| 32 | + protected array $config; |
| 33 | + |
| 34 | + protected TreeListener $listener; |
| 35 | + |
| 36 | + protected function setUp(): void |
| 37 | + { |
| 38 | + parent::setUp(); |
| 39 | + |
| 40 | + $this->listener = new TreeListener(); |
| 41 | + |
| 42 | + $evm = new EventManager(); |
| 43 | + $evm->addEventSubscriber($this->listener); |
| 44 | + |
| 45 | + $this->getDefaultMockSqliteEntityManager($evm); |
| 46 | + |
| 47 | + $meta = $this->em->getClassMetadata(MPCategoryUuid::class); |
| 48 | + $this->config = $this->listener->getConfiguration($this->em, $meta->getName()); |
| 49 | + } |
| 50 | + |
| 51 | + public function testInsertUpdateAndRemove(): void |
| 52 | + { |
| 53 | + // Insert |
| 54 | + $category = $this->createCategory(); |
| 55 | + $category->setTitle('1'); |
| 56 | + static::assertNotNull($category->getId()); |
| 57 | + $category2 = $this->createCategory(); |
| 58 | + $category2->setTitle('2'); |
| 59 | + static::assertNotNull($category2->getId()); |
| 60 | + $category3 = $this->createCategory(); |
| 61 | + $category3->setTitle('3'); |
| 62 | + static::assertNotNull($category3->getId()); |
| 63 | + $category4 = $this->createCategory(); |
| 64 | + $category4->setTitle('4'); |
| 65 | + static::assertNotNull($category4->getId()); |
| 66 | + |
| 67 | + $category2->setParent($category); |
| 68 | + $category3->setParent($category2); |
| 69 | + |
| 70 | + $this->em->persist($category4); |
| 71 | + $this->em->persist($category3); |
| 72 | + $this->em->persist($category2); |
| 73 | + $this->em->persist($category); |
| 74 | + $this->em->flush(); |
| 75 | + |
| 76 | + $this->em->refresh($category); |
| 77 | + $this->em->refresh($category2); |
| 78 | + $this->em->refresh($category3); |
| 79 | + $this->em->refresh($category4); |
| 80 | + |
| 81 | + static::assertSame($this->generatePath([$category->getId()]), $category->getPath()); |
| 82 | + static::assertSame($this->generatePath([$category->getId(), $category2->getId()]), $category2->getPath()); |
| 83 | + static::assertSame($this->generatePath([$category->getId(), $category2->getId(), $category3->getId()]), $category3->getPath()); |
| 84 | + static::assertSame($this->generatePath([$category4->getId()]), $category4->getPath()); |
| 85 | + static::assertSame(1, $category->getLevel()); |
| 86 | + static::assertSame(2, $category2->getLevel()); |
| 87 | + static::assertSame(3, $category3->getLevel()); |
| 88 | + static::assertSame(1, $category4->getLevel()); |
| 89 | + |
| 90 | + static::assertSame($this->getTreeRootValueOfRootNode($category), $category->getTreeRootValue()); |
| 91 | + static::assertSame($this->getTreeRootValueOfRootNode($category2), $category2->getTreeRootValue()); |
| 92 | + static::assertSame($this->getTreeRootValueOfRootNode($category3), $category3->getTreeRootValue()); |
| 93 | + static::assertSame($this->getTreeRootValueOfRootNode($category4), $category4->getTreeRootValue()); |
| 94 | + |
| 95 | + // Update |
| 96 | + $category2->setParent(null); |
| 97 | + |
| 98 | + $this->em->persist($category2); |
| 99 | + $this->em->flush(); |
| 100 | + |
| 101 | + $this->em->refresh($category); |
| 102 | + $this->em->refresh($category2); |
| 103 | + $this->em->refresh($category3); |
| 104 | + |
| 105 | + static::assertSame($this->generatePath([$category->getId()]), $category->getPath()); |
| 106 | + static::assertSame($this->generatePath([$category2->getId()]), $category2->getPath()); |
| 107 | + static::assertSame($this->generatePath([$category2->getId(), $category3->getId()]), $category3->getPath()); |
| 108 | + static::assertSame(1, $category->getLevel()); |
| 109 | + static::assertSame(1, $category2->getLevel()); |
| 110 | + static::assertSame(2, $category3->getLevel()); |
| 111 | + static::assertSame(1, $category4->getLevel()); |
| 112 | + |
| 113 | + static::assertSame($this->getTreeRootValueOfRootNode($category), $category->getTreeRootValue()); |
| 114 | + static::assertSame($this->getTreeRootValueOfRootNode($category2), $category2->getTreeRootValue()); |
| 115 | + static::assertSame($this->getTreeRootValueOfRootNode($category3), $category3->getTreeRootValue()); |
| 116 | + static::assertSame($this->getTreeRootValueOfRootNode($category4), $category4->getTreeRootValue()); |
| 117 | + |
| 118 | + // Remove |
| 119 | + $this->em->remove($category); |
| 120 | + $this->em->remove($category2); |
| 121 | + $this->em->flush(); |
| 122 | + |
| 123 | + $result = $this->em->createQueryBuilder()->select('c')->from(MPCategoryUuid::class, 'c')->getQuery()->getResult(); |
| 124 | + |
| 125 | + $firstResult = $result[0]; |
| 126 | + |
| 127 | + static::assertCount(1, $result); |
| 128 | + static::assertSame('4', $firstResult->getTitle()); |
| 129 | + static::assertSame(1, $firstResult->getLevel()); |
| 130 | + static::assertSame($this->getTreeRootValueOfRootNode($firstResult), $firstResult->getTreeRootValue()); |
| 131 | + } |
| 132 | + |
| 133 | + protected function getUsedEntityFixtures(): array |
| 134 | + { |
| 135 | + return [ |
| 136 | + MPCategoryUuid::class, |
| 137 | + ]; |
| 138 | + } |
| 139 | + |
| 140 | + private function createCategory(): MPCategoryUuid |
| 141 | + { |
| 142 | + return new MPCategoryUuid(); |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * @param array<int|string, int|string|UuidV4|null> $sources |
| 147 | + */ |
| 148 | + private function generatePath(array $sources): string |
| 149 | + { |
| 150 | + $path = $this->config['path_starts_with_separator'] |
| 151 | + ? $this->config['path_separator'] |
| 152 | + : ''; |
| 153 | + |
| 154 | + $path .= implode($this->config['path_separator'], $sources); |
| 155 | + |
| 156 | + $path .= $this->config['path_ends_with_separator'] |
| 157 | + ? $this->config['path_separator'] |
| 158 | + : ''; |
| 159 | + |
| 160 | + return $path; |
| 161 | + } |
| 162 | + |
| 163 | + private function getTreeRootValueOfRootNode(MPCategoryUuid $category): string |
| 164 | + { |
| 165 | + while (null !== $category->getParent()) { |
| 166 | + $category = $category->getParent(); |
| 167 | + } |
| 168 | + |
| 169 | + return $category->getTreeRootValue(); |
| 170 | + } |
| 171 | +} |
0 commit comments