NestedTreeRootRepositoryTest.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. namespace Gedmo\Tree;
  3. use Doctrine\Common\EventManager;
  4. use Tool\BaseTestCaseORM;
  5. use Doctrine\Common\Util\Debug;
  6. use Tree\Fixture\RootCategory;
  7. /**
  8. * These are tests for Tree behavior
  9. *
  10. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  11. * @package Gedmo.Tree
  12. * @link http://www.gediminasm.org
  13. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  14. */
  15. class NestedTreeRootRepositoryTest extends BaseTestCaseORM
  16. {
  17. const CATEGORY = "Tree\\Fixture\\RootCategory";
  18. protected function setUp()
  19. {
  20. parent::setUp();
  21. $evm = new EventManager;
  22. $evm->addEventSubscriber(new TreeListener);
  23. $this->getMockSqliteEntityManager($evm);
  24. $this->populate();
  25. }
  26. public function testRootRemoval()
  27. {
  28. $repo = $this->em->getRepository(self::CATEGORY);
  29. $this->populateMore();
  30. $food = $repo->findOneByTitle('Food');
  31. $repo->removeFromTree($food);
  32. $this->em->clear();
  33. $food = $repo->findOneByTitle('Food');
  34. $this->assertTrue(is_null($food));
  35. $node = $repo->findOneByTitle('Fruits');
  36. $this->assertEquals(1, $node->getLeft());
  37. $this->assertEquals(2, $node->getRight());
  38. $this->assertEquals(3, $node->getRoot());
  39. $this->assertTrue(is_null($node->getParent()));
  40. $node = $repo->findOneByTitle('Vegitables');
  41. $this->assertEquals(1, $node->getLeft());
  42. $this->assertEquals(10, $node->getRight());
  43. $this->assertEquals(4, $node->getRoot());
  44. $this->assertTrue(is_null($node->getParent()));
  45. }
  46. public function testRepository()
  47. {
  48. $repo = $this->em->getRepository(self::CATEGORY);
  49. $carrots = $repo->findOneByTitle('Carrots');
  50. $path = $repo->getPath($carrots);
  51. $this->assertEquals(3, count($path));
  52. $this->assertEquals('Food', $path[0]->getTitle());
  53. $this->assertEquals('Vegitables', $path[1]->getTitle());
  54. $this->assertEquals('Carrots', $path[2]->getTitle());
  55. $vegies = $repo->findOneByTitle('Vegitables');
  56. $childCount = $repo->childCount($vegies);
  57. $this->assertEquals(2, $childCount);
  58. $food = $repo->findOneByTitle('Food');
  59. $childCount = $repo->childCount($food, true);
  60. $this->assertEquals(2, $childCount);
  61. $childCount = $repo->childCount($food);
  62. $this->assertEquals(4, $childCount);
  63. $childCount = $repo->childCount();
  64. $this->assertEquals(6, $childCount);
  65. }
  66. public function testAdvancedRepositoryFunctions()
  67. {
  68. $this->populateMore();
  69. $repo = $this->em->getRepository(self::CATEGORY);
  70. // verification
  71. $this->assertTrue($repo->verify());
  72. $dql = 'UPDATE ' . self::CATEGORY . ' node';
  73. $dql .= ' SET node.lft = 1';
  74. $dql .= ' WHERE node.id = 4';
  75. $this->em->createQuery($dql)->getSingleScalarResult();
  76. $this->em->clear(); // must clear cached entities
  77. $errors = $repo->verify();
  78. $this->assertEquals(2, count($errors));
  79. $this->assertEquals('index [1], duplicate on tree root: 1', $errors[0]);
  80. $this->assertEquals('index [4], missing on tree root: 1', $errors[1]);
  81. $dql = 'UPDATE ' . self::CATEGORY . ' node';
  82. $dql .= ' SET node.lft = 4';
  83. $dql .= ' WHERE node.id = 4';
  84. $this->em->createQuery($dql)->getSingleScalarResult();
  85. //@todo implement
  86. //$this->em->clear();
  87. //$repo->recover();
  88. //$this->em->clear();
  89. //$this->assertTrue($repo->verify());
  90. $this->em->clear();
  91. $onions = $repo->findOneByTitle('Onions');
  92. $this->assertEquals(11, $onions->getLeft());
  93. $this->assertEquals(12, $onions->getRight());
  94. // move up
  95. $repo->moveUp($onions);
  96. $this->assertEquals(9, $onions->getLeft());
  97. $this->assertEquals(10, $onions->getRight());
  98. $repo->moveUp($onions, true);
  99. $this->assertEquals(5, $onions->getLeft());
  100. $this->assertEquals(6, $onions->getRight());
  101. // move down
  102. $repo->moveDown($onions, 2);
  103. $this->assertEquals(9, $onions->getLeft());
  104. $this->assertEquals(10, $onions->getRight());
  105. // reorder
  106. $node = $repo->findOneByTitle('Food');
  107. $repo->reorder($node, 'title', 'ASC', false);
  108. $node = $repo->findOneByTitle('Cabbages');
  109. $this->assertEquals(5, $node->getLeft());
  110. $this->assertEquals(6, $node->getRight());
  111. $node = $repo->findOneByTitle('Carrots');
  112. $this->assertEquals(7, $node->getLeft());
  113. $this->assertEquals(8, $node->getRight());
  114. $node = $repo->findOneByTitle('Onions');
  115. $this->assertEquals(9, $node->getLeft());
  116. $this->assertEquals(10, $node->getRight());
  117. $node = $repo->findOneByTitle('Potatoes');
  118. $this->assertEquals(11, $node->getLeft());
  119. $this->assertEquals(12, $node->getRight());
  120. // leafs
  121. $leafs = $repo->getLeafs($node);
  122. $this->assertEquals(5, count($leafs));
  123. $this->assertEquals('Fruits', $leafs[0]->getTitle());
  124. $this->assertEquals('Cabbages', $leafs[1]->getTitle());
  125. $this->assertEquals('Carrots', $leafs[2]->getTitle());
  126. $this->assertEquals('Onions', $leafs[3]->getTitle());
  127. $this->assertEquals('Potatoes', $leafs[4]->getTitle());
  128. // remove
  129. $node = $repo->findOneByTitle('Fruits');
  130. $id = $node->getId();
  131. $repo->removeFromTree($node);
  132. $this->assertTrue(is_null($repo->find($id)));
  133. $node = $repo->findOneByTitle('Vegitables');
  134. $id = $node->getId();
  135. $repo->removeFromTree($node);
  136. $this->assertTrue(is_null($repo->find($id)));
  137. $this->em->clear();
  138. $node = $repo->findOneByTitle('Cabbages');
  139. $this->assertEquals(1, $node->getRoot());
  140. $this->assertEquals(1, $node->getParent()->getId());
  141. }
  142. public function testRemoveFromTreeLeaf()
  143. {
  144. $this->populateMore();
  145. $repo = $this->em->getRepository(self::CATEGORY);
  146. $onions = $repo->findOneByTitle('Onions');
  147. $id = $onions->getId();
  148. $repo->removeFromTree($onions);
  149. $this->assertTrue(is_null($repo->find($id)));
  150. $this->em->clear();
  151. $vegies = $repo->findOneByTitle('Vegitables');
  152. $this->assertTrue($repo->verify());
  153. }
  154. protected function getUsedEntityFixtures()
  155. {
  156. return array(
  157. self::CATEGORY
  158. );
  159. }
  160. private function populateMore()
  161. {
  162. $vegies = $this->em->getRepository(self::CATEGORY)
  163. ->findOneByTitle('Vegitables');
  164. $cabbages = new RootCategory();
  165. $cabbages->setParent($vegies);
  166. $cabbages->setTitle('Cabbages');
  167. $onions = new RootCategory();
  168. $onions->setParent($vegies);
  169. $onions->setTitle('Onions');
  170. $this->em->persist($cabbages);
  171. $this->em->persist($onions);
  172. $this->em->flush();
  173. }
  174. private function populate()
  175. {
  176. $root = new RootCategory();
  177. $root->setTitle("Food");
  178. $root2 = new RootCategory();
  179. $root2->setTitle("Sports");
  180. $child = new RootCategory();
  181. $child->setTitle("Fruits");
  182. $child->setParent($root);
  183. $child2 = new RootCategory();
  184. $child2->setTitle("Vegitables");
  185. $child2->setParent($root);
  186. $childsChild = new RootCategory();
  187. $childsChild->setTitle("Carrots");
  188. $childsChild->setParent($child2);
  189. $potatoes = new RootCategory();
  190. $potatoes->setTitle("Potatoes");
  191. $potatoes->setParent($child2);
  192. $this->em->persist($root);
  193. $this->em->persist($root2);
  194. $this->em->persist($child);
  195. $this->em->persist($child2);
  196. $this->em->persist($childsChild);
  197. $this->em->persist($potatoes);
  198. $this->em->flush();
  199. }
  200. }