NestedTreeRootRepositoryTest.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 testChildrenHierarchyAsArray()
  27. {
  28. $repo = $this->em->getRepository(self::CATEGORY);
  29. $result = $repo->childrenHierarchy();
  30. $this->assertEquals(2, count($result));
  31. $this->assertTrue(isset($result[0]['children'][0]['children']));
  32. $vegies = $repo->findOneByTitle('Vegitables');
  33. $result = $repo->childrenHierarchy($vegies);
  34. $this->assertEquals(2, count($result));
  35. $this->assertEquals(0, count($result[0]['children']));
  36. }
  37. public function testChildrenHierarchyAsHtml()
  38. {
  39. $repo = $this->em->getRepository(self::CATEGORY);
  40. $food = $repo->findOneByTitle('Food');
  41. $result = $repo->childrenHierarchy($food, false, true);
  42. $this->assertTrue(is_string($result));
  43. }
  44. public function testRootRemoval()
  45. {
  46. $repo = $this->em->getRepository(self::CATEGORY);
  47. $this->populateMore();
  48. $food = $repo->findOneByTitle('Food');
  49. $repo->removeFromTree($food);
  50. $this->em->clear();
  51. $food = $repo->findOneByTitle('Food');
  52. $this->assertTrue(is_null($food));
  53. $node = $repo->findOneByTitle('Fruits');
  54. $this->assertEquals(1, $node->getLeft());
  55. $this->assertEquals(2, $node->getRight());
  56. $this->assertEquals(3, $node->getRoot());
  57. $this->assertTrue(is_null($node->getParent()));
  58. $node = $repo->findOneByTitle('Vegitables');
  59. $this->assertEquals(1, $node->getLeft());
  60. $this->assertEquals(10, $node->getRight());
  61. $this->assertEquals(4, $node->getRoot());
  62. $this->assertTrue(is_null($node->getParent()));
  63. }
  64. public function testRepository()
  65. {
  66. $repo = $this->em->getRepository(self::CATEGORY);
  67. $carrots = $repo->findOneByTitle('Carrots');
  68. $path = $repo->getPath($carrots);
  69. $this->assertEquals(3, count($path));
  70. $this->assertEquals('Food', $path[0]->getTitle());
  71. $this->assertEquals('Vegitables', $path[1]->getTitle());
  72. $this->assertEquals('Carrots', $path[2]->getTitle());
  73. $vegies = $repo->findOneByTitle('Vegitables');
  74. $childCount = $repo->childCount($vegies);
  75. $this->assertEquals(2, $childCount);
  76. $food = $repo->findOneByTitle('Food');
  77. $childCount = $repo->childCount($food, true);
  78. $this->assertEquals(2, $childCount);
  79. $childCount = $repo->childCount($food);
  80. $this->assertEquals(4, $childCount);
  81. $childCount = $repo->childCount();
  82. $this->assertEquals(6, $childCount);
  83. }
  84. public function testAdvancedRepositoryFunctions()
  85. {
  86. $this->populateMore();
  87. $repo = $this->em->getRepository(self::CATEGORY);
  88. // verification
  89. $this->assertTrue($repo->verify());
  90. $dql = 'UPDATE ' . self::CATEGORY . ' node';
  91. $dql .= ' SET node.lft = 1';
  92. $dql .= ' WHERE node.id = 4';
  93. $this->em->createQuery($dql)->getSingleScalarResult();
  94. $this->em->clear(); // must clear cached entities
  95. $errors = $repo->verify();
  96. $this->assertEquals(2, count($errors));
  97. $this->assertEquals('index [1], duplicate on tree root: 1', $errors[0]);
  98. $this->assertEquals('index [4], missing on tree root: 1', $errors[1]);
  99. $dql = 'UPDATE ' . self::CATEGORY . ' node';
  100. $dql .= ' SET node.lft = 4';
  101. $dql .= ' WHERE node.id = 4';
  102. $this->em->createQuery($dql)->getSingleScalarResult();
  103. //@todo implement
  104. //$this->em->clear();
  105. //$repo->recover();
  106. //$this->em->clear();
  107. //$this->assertTrue($repo->verify());
  108. $this->em->clear();
  109. $onions = $repo->findOneByTitle('Onions');
  110. $this->assertEquals(11, $onions->getLeft());
  111. $this->assertEquals(12, $onions->getRight());
  112. // move up
  113. $repo->moveUp($onions);
  114. $this->assertEquals(9, $onions->getLeft());
  115. $this->assertEquals(10, $onions->getRight());
  116. $repo->moveUp($onions, true);
  117. $this->assertEquals(5, $onions->getLeft());
  118. $this->assertEquals(6, $onions->getRight());
  119. // move down
  120. $repo->moveDown($onions, 2);
  121. $this->assertEquals(9, $onions->getLeft());
  122. $this->assertEquals(10, $onions->getRight());
  123. // reorder
  124. $node = $repo->findOneByTitle('Food');
  125. $repo->reorder($node, 'title', 'ASC', false);
  126. $node = $repo->findOneByTitle('Cabbages');
  127. $this->assertEquals(5, $node->getLeft());
  128. $this->assertEquals(6, $node->getRight());
  129. $node = $repo->findOneByTitle('Carrots');
  130. $this->assertEquals(7, $node->getLeft());
  131. $this->assertEquals(8, $node->getRight());
  132. $node = $repo->findOneByTitle('Onions');
  133. $this->assertEquals(9, $node->getLeft());
  134. $this->assertEquals(10, $node->getRight());
  135. $node = $repo->findOneByTitle('Potatoes');
  136. $this->assertEquals(11, $node->getLeft());
  137. $this->assertEquals(12, $node->getRight());
  138. // leafs
  139. $leafs = $repo->getLeafs($node);
  140. $this->assertEquals(5, count($leafs));
  141. $this->assertEquals('Fruits', $leafs[0]->getTitle());
  142. $this->assertEquals('Cabbages', $leafs[1]->getTitle());
  143. $this->assertEquals('Carrots', $leafs[2]->getTitle());
  144. $this->assertEquals('Onions', $leafs[3]->getTitle());
  145. $this->assertEquals('Potatoes', $leafs[4]->getTitle());
  146. // remove
  147. $node = $repo->findOneByTitle('Fruits');
  148. $id = $node->getId();
  149. $repo->removeFromTree($node);
  150. $this->assertTrue(is_null($repo->find($id)));
  151. $node = $repo->findOneByTitle('Vegitables');
  152. $id = $node->getId();
  153. $repo->removeFromTree($node);
  154. $this->assertTrue(is_null($repo->find($id)));
  155. $this->em->clear();
  156. $node = $repo->findOneByTitle('Cabbages');
  157. $this->assertEquals(1, $node->getRoot());
  158. $this->assertEquals(1, $node->getParent()->getId());
  159. }
  160. public function testRemoveFromTreeLeaf()
  161. {
  162. $this->populateMore();
  163. $repo = $this->em->getRepository(self::CATEGORY);
  164. $onions = $repo->findOneByTitle('Onions');
  165. $id = $onions->getId();
  166. $repo->removeFromTree($onions);
  167. $this->assertTrue(is_null($repo->find($id)));
  168. $this->em->clear();
  169. $vegies = $repo->findOneByTitle('Vegitables');
  170. $this->assertTrue($repo->verify());
  171. }
  172. protected function getUsedEntityFixtures()
  173. {
  174. return array(
  175. self::CATEGORY
  176. );
  177. }
  178. private function populateMore()
  179. {
  180. $vegies = $this->em->getRepository(self::CATEGORY)
  181. ->findOneByTitle('Vegitables');
  182. $cabbages = new RootCategory();
  183. $cabbages->setParent($vegies);
  184. $cabbages->setTitle('Cabbages');
  185. $onions = new RootCategory();
  186. $onions->setParent($vegies);
  187. $onions->setTitle('Onions');
  188. $this->em->persist($cabbages);
  189. $this->em->persist($onions);
  190. $this->em->flush();
  191. }
  192. private function populate()
  193. {
  194. $root = new RootCategory();
  195. $root->setTitle("Food");
  196. $root2 = new RootCategory();
  197. $root2->setTitle("Sports");
  198. $child = new RootCategory();
  199. $child->setTitle("Fruits");
  200. $child->setParent($root);
  201. $child2 = new RootCategory();
  202. $child2->setTitle("Vegitables");
  203. $child2->setParent($root);
  204. $childsChild = new RootCategory();
  205. $childsChild->setTitle("Carrots");
  206. $childsChild->setParent($child2);
  207. $potatoes = new RootCategory();
  208. $potatoes->setTitle("Potatoes");
  209. $potatoes->setParent($child2);
  210. $this->em->persist($root);
  211. $this->em->persist($root2);
  212. $this->em->persist($child);
  213. $this->em->persist($child2);
  214. $this->em->persist($childsChild);
  215. $this->em->persist($potatoes);
  216. $this->em->flush();
  217. }
  218. }