|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Gedmo\Tree; |
| 4 | + |
| 5 | +use Doctrine\Common\EventManager; |
| 6 | +use Tool\BaseTestCaseORM; |
| 7 | + |
| 8 | +/** |
| 9 | + * These are tests for Tree behavior |
| 10 | + * |
| 11 | + * @author Gustavo Falco <[email protected]> |
| 12 | + * @author Gediminas Morkevicius <[email protected]> |
| 13 | + * @link http://www.gediminasm.org |
| 14 | + * @license MIT License (http://www.opensource.org/licenses/mit-license.php) |
| 15 | + */ |
| 16 | +class MaterializedPathORMRootAssociationTest extends BaseTestCaseORM |
| 17 | +{ |
| 18 | + const CATEGORY = "Tree\\Fixture\\MPCategoryWithRootAssociation"; |
| 19 | + |
| 20 | + protected $config; |
| 21 | + protected $listener; |
| 22 | + |
| 23 | + protected function setUp() |
| 24 | + { |
| 25 | + parent::setUp(); |
| 26 | + |
| 27 | + $this->listener = new TreeListener(); |
| 28 | + |
| 29 | + $evm = new EventManager(); |
| 30 | + $evm->addEventSubscriber($this->listener); |
| 31 | + |
| 32 | + $this->getMockSqliteEntityManager($evm); |
| 33 | + |
| 34 | + $meta = $this->em->getClassMetadata(self::CATEGORY); |
| 35 | + $this->config = $this->listener->getConfiguration($this->em, $meta->name); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * @test |
| 40 | + */ |
| 41 | + public function insertUpdateAndRemove() |
| 42 | + { |
| 43 | + // Insert |
| 44 | + $category = $this->createCategory(); |
| 45 | + $category->setTitle('1'); |
| 46 | + $category2 = $this->createCategory(); |
| 47 | + $category2->setTitle('2'); |
| 48 | + $category3 = $this->createCategory(); |
| 49 | + $category3->setTitle('3'); |
| 50 | + $category4 = $this->createCategory(); |
| 51 | + $category4->setTitle('4'); |
| 52 | + |
| 53 | + $category2->setParent($category); |
| 54 | + $category3->setParent($category2); |
| 55 | + |
| 56 | + $this->em->persist($category4); |
| 57 | + $this->em->persist($category3); |
| 58 | + $this->em->persist($category2); |
| 59 | + $this->em->persist($category); |
| 60 | + $this->em->flush(); |
| 61 | + |
| 62 | + $this->em->refresh($category); |
| 63 | + $this->em->refresh($category2); |
| 64 | + $this->em->refresh($category3); |
| 65 | + $this->em->refresh($category4); |
| 66 | + |
| 67 | + $this->assertEquals($this->generatePath(array($category->getId())), $category->getPath()); |
| 68 | + $this->assertEquals($this->generatePath(array($category->getId(), $category2->getId())), $category2->getPath()); |
| 69 | + $this->assertEquals($this->generatePath(array($category->getId(), $category2->getId(), $category3->getId())), $category3->getPath()); |
| 70 | + $this->assertEquals($this->generatePath(array($category4->getId())), $category4->getPath()); |
| 71 | + $this->assertEquals(1, $category->getLevel()); |
| 72 | + $this->assertEquals(2, $category2->getLevel()); |
| 73 | + $this->assertEquals(3, $category3->getLevel()); |
| 74 | + $this->assertEquals(1, $category4->getLevel()); |
| 75 | + |
| 76 | + $this->assertEquals($category, $category->getTreeRootEntity()); |
| 77 | + $this->assertEquals($category, $category2->getTreeRootEntity()); |
| 78 | + $this->assertEquals($category, $category3->getTreeRootEntity()); |
| 79 | + $this->assertEquals($category4, $category4->getTreeRootEntity()); |
| 80 | + |
| 81 | + // Update |
| 82 | + $category2->setParent(null); |
| 83 | + |
| 84 | + $this->em->persist($category2); |
| 85 | + $this->em->flush(); |
| 86 | + |
| 87 | + $this->em->refresh($category); |
| 88 | + $this->em->refresh($category2); |
| 89 | + $this->em->refresh($category3); |
| 90 | + |
| 91 | + $this->assertEquals($this->generatePath(array($category->getId())), $category->getPath()); |
| 92 | + $this->assertEquals($this->generatePath(array($category2->getId())), $category2->getPath()); |
| 93 | + $this->assertEquals($this->generatePath(array($category2->getId(), $category3->getId())), $category3->getPath()); |
| 94 | + $this->assertEquals(1, $category->getLevel()); |
| 95 | + $this->assertEquals(1, $category2->getLevel()); |
| 96 | + $this->assertEquals(2, $category3->getLevel()); |
| 97 | + $this->assertEquals(1, $category4->getLevel()); |
| 98 | + |
| 99 | + $this->assertEquals($category, $category->getTreeRootEntity()); |
| 100 | + $this->assertEquals($category2, $category2->getTreeRootEntity()); |
| 101 | + $this->assertEquals($category2, $category3->getTreeRootEntity()); |
| 102 | + $this->assertEquals($category4, $category4->getTreeRootEntity()); |
| 103 | + |
| 104 | + // Remove |
| 105 | + $this->em->remove($category); |
| 106 | + $this->em->remove($category2); |
| 107 | + $this->em->flush(); |
| 108 | + |
| 109 | + $result = $this->em->createQueryBuilder()->select('c')->from(self::CATEGORY, 'c')->getQuery()->execute(); |
| 110 | + |
| 111 | + $firstResult = $result[0]; |
| 112 | + |
| 113 | + $this->assertCount(1, $result); |
| 114 | + $this->assertEquals('4', $firstResult->getTitle()); |
| 115 | + $this->assertEquals(1, $firstResult->getLevel()); |
| 116 | + $this->assertEquals($category4, $firstResult->getTreeRootEntity()); |
| 117 | + } |
| 118 | + |
| 119 | + public function createCategory() |
| 120 | + { |
| 121 | + $class = self::CATEGORY; |
| 122 | + |
| 123 | + return new $class(); |
| 124 | + } |
| 125 | + |
| 126 | + protected function getUsedEntityFixtures() |
| 127 | + { |
| 128 | + return array( |
| 129 | + self::CATEGORY, |
| 130 | + ); |
| 131 | + } |
| 132 | + |
| 133 | + public function generatePath(array $sources) |
| 134 | + { |
| 135 | + $path = ''; |
| 136 | + |
| 137 | + foreach ($sources as $id) { |
| 138 | + $path .= $id.$this->config['path_separator']; |
| 139 | + } |
| 140 | + |
| 141 | + return $path; |
| 142 | + } |
| 143 | +} |
0 commit comments