ClosureTreeRepositoryTest.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <?php
  2. namespace Gedmo\Tree;
  3. use Doctrine\Common\EventManager;
  4. use Tool\BaseTestCaseORM;
  5. use Tree\Fixture\Closure\Category;
  6. use Tree\Fixture\Closure\CategoryWithoutLevel;
  7. use Tree\Fixture\Closure\CategoryWithoutLevelClosure;
  8. /**
  9. * These are tests for Tree behavior
  10. *
  11. * @author Gustavo Adrian <comfortablynumb84@gmail.com>
  12. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  13. * @package Gedmo.Tree
  14. * @link http://www.gediminasm.org
  15. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  16. */
  17. class ClosureTreeRepositoryTest extends BaseTestCaseORM
  18. {
  19. const CATEGORY = "Tree\\Fixture\\Closure\\Category";
  20. const CLOSURE = "Tree\\Fixture\\Closure\\CategoryClosure";
  21. const CATEGORY_WITHOUT_LEVEL = "Tree\\Fixture\\Closure\\CategoryWithoutLevel";
  22. const CATEGORY_WITHOUT_LEVEL_CLOSURE = "Tree\\Fixture\\Closure\\CategoryWithoutLevelClosure";
  23. protected function setUp()
  24. {
  25. parent::setUp();
  26. $evm = new EventManager;
  27. $evm->addEventSubscriber(new TreeListener);
  28. $this->getMockSqliteEntityManager($evm);
  29. $this->populate();
  30. }
  31. public function testChildCount()
  32. {
  33. $repo = $this->em->getRepository(self::CATEGORY);
  34. $food = $repo->findOneByTitle('Food');
  35. $directCount = $repo->childCount($food, true);
  36. $this->assertEquals(3, $directCount);
  37. $fruits = $repo->findOneByTitle('Fruits');
  38. $count = $repo->childCount($fruits);
  39. $this->assertEquals(4, $count);
  40. $rootCount = $repo->childCount(null, true);
  41. $this->assertEquals(1, $rootCount);
  42. }
  43. public function testPath()
  44. {
  45. $repo = $this->em->getRepository(self::CATEGORY);
  46. $fruits = $repo->findOneByTitle('Fruits');
  47. $path = $repo->getPath($fruits);
  48. $this->assertCount(2, $path);
  49. $this->assertEquals('Food', $path[0]->getTitle());
  50. $this->assertEquals('Fruits', $path[1]->getTitle());
  51. $strawberries = $repo->findOneByTitle('Strawberries');
  52. $path = $repo->getPath($strawberries);
  53. $this->assertCount(4, $path);
  54. $this->assertEquals('Food', $path[0]->getTitle());
  55. $this->assertEquals('Fruits', $path[1]->getTitle());
  56. $this->assertEquals('Berries', $path[2]->getTitle());
  57. $this->assertEquals('Strawberries', $path[3]->getTitle());
  58. }
  59. public function testChildren()
  60. {
  61. $repo = $this->em->getRepository(self::CATEGORY);
  62. $fruits = $repo->findOneByTitle('Fruits');
  63. // direct children of node, sorted by title ascending order
  64. $children = $repo->children($fruits, true, 'title');
  65. $this->assertCount(3, $children);
  66. $this->assertEquals('Berries', $children[0]->getTitle());
  67. $this->assertEquals('Lemons', $children[1]->getTitle());
  68. $this->assertEquals('Oranges', $children[2]->getTitle());
  69. // all children of node
  70. $children = $repo->children($fruits);
  71. $this->assertCount(4, $children);
  72. $this->assertEquals('Oranges', $children[0]->getTitle());
  73. $this->assertEquals('Lemons', $children[1]->getTitle());
  74. $this->assertEquals('Berries', $children[2]->getTitle());
  75. $this->assertEquals('Strawberries', $children[3]->getTitle());
  76. // direct root nodes
  77. $children = $repo->children(null, true, 'title');
  78. $this->assertCount(1, $children);
  79. $this->assertEquals('Food', $children[0]->getTitle());
  80. // all tree
  81. $children = $repo->children();
  82. $this->assertCount(12, $children);
  83. }
  84. public function testSingleNodeRemoval()
  85. {
  86. $repo = $this->em->getRepository(self::CATEGORY);
  87. $fruits = $repo->findOneByTitle('Fruits');
  88. $repo->removeFromTree($fruits);
  89. // ensure in memory node integrity
  90. $this->em->flush();
  91. $food = $repo->findOneByTitle('Food');
  92. $children = $repo->children($food, true);
  93. $this->assertCount(5, $children);
  94. $berries = $repo->findOneByTitle('Berries');
  95. $this->assertEquals(1, $repo->childCount($berries, true));
  96. $lemons = $repo->findOneByTitle('Lemons');
  97. $this->assertEquals(0, $repo->childCount($lemons, true));
  98. $repo->removeFromTree($food);
  99. $vegitables = $repo->findOneByTitle('Vegitables');
  100. $this->assertEquals(2, $repo->childCount($vegitables, true));
  101. $this->assertNull($vegitables->getParent());
  102. $repo->removeFromTree($lemons);
  103. $this->assertCount(4, $repo->children(null, true));
  104. }
  105. public function testBuildTree()
  106. {
  107. $repo = $this->em->getRepository(self::CATEGORY);
  108. $roots = $repo->getRootNodes();
  109. $tree = $repo->childrenHierarchy(
  110. $roots[0],
  111. false,
  112. array('childSort' => array('field' => 'title', 'dir' => 'asc'))
  113. );
  114. $fruits = $tree[0]['__children'][0];
  115. $milk = $tree[0]['__children'][1];
  116. $vegitables = $tree[0]['__children'][2];
  117. $this->assertEquals('Food', $tree[0]['title']);
  118. $this->assertEquals('Fruits', $fruits['title']);
  119. $this->assertEquals('Berries', $fruits['__children'][0]['title']);
  120. $this->assertEquals('Strawberries', $fruits['__children'][0]['__children'][0]['title']);
  121. $this->assertEquals('Milk', $milk['title']);
  122. $this->assertEquals('Cheese', $milk['__children'][0]['title']);
  123. $this->assertEquals('Mould cheese', $milk['__children'][0]['__children'][0]['title']);
  124. $this->assertEquals('Vegitables', $vegitables['title']);
  125. $this->assertEquals('Cabbages', $vegitables['__children'][0]['title']);
  126. $this->assertEquals('Carrots', $vegitables['__children'][1]['title']);
  127. $food = $repo->findOneByTitle('Food');
  128. $vegitables = $repo->findOneByTitle('Vegitables');
  129. $boringFood = new Category();
  130. $boringFood->setTitle('Boring Food');
  131. $boringFood->setParent($food);
  132. $vegitables->setParent($boringFood);
  133. $this->em->persist($boringFood);
  134. $this->em->flush();
  135. $tree = $repo->childrenHierarchy(
  136. $roots[0],
  137. false,
  138. array('childSort' => array('field' => 'title', 'dir' => 'asc'))
  139. );
  140. $boringFood = $tree[0]['__children'][0];
  141. $fruits = $tree[0]['__children'][1];
  142. $milk = $tree[0]['__children'][2];
  143. $vegitables = $boringFood['__children'][0];
  144. $this->assertEquals('Food', $tree[0]['title']);
  145. $this->assertEquals('Fruits', $fruits['title']);
  146. $this->assertEquals('Berries', $fruits['__children'][0]['title']);
  147. $this->assertEquals('Strawberries', $fruits['__children'][0]['__children'][0]['title']);
  148. $this->assertEquals('Milk', $milk['title']);
  149. $this->assertEquals('Cheese', $milk['__children'][0]['title']);
  150. $this->assertEquals('Mould cheese', $milk['__children'][0]['__children'][0]['title']);
  151. $this->assertEquals('Boring Food', $boringFood['title']);
  152. $this->assertEquals('Vegitables', $boringFood['__children'][0]['title']);
  153. $this->assertEquals('Cabbages', $vegitables['__children'][0]['title']);
  154. $this->assertEquals('Carrots', $vegitables['__children'][1]['title']);
  155. }
  156. /**
  157. * @expectedException Gedmo\Exception\TreeLevelFieldNotFoundException
  158. */
  159. public function test_buildTreeArray_IfLevelFieldIsNotPresentThrowException()
  160. {
  161. $repo = $this->em->getRepository(self::CATEGORY_WITHOUT_LEVEL);
  162. $repo->buildTreeArray(array());
  163. }
  164. /**
  165. * @expectedException Gedmo\Exception\TreeLevelFieldNotFoundException
  166. */
  167. public function test_getNodesHierarchy_IfLevelFieldIsNotPresentThrowException()
  168. {
  169. $repo = $this->em->getRepository(self::CATEGORY_WITHOUT_LEVEL);
  170. $repo->getNodesHierarchy(new CategoryWithoutLevel(), true, array());
  171. }
  172. protected function getUsedEntityFixtures()
  173. {
  174. return array(
  175. self::CATEGORY,
  176. self::CLOSURE,
  177. self::CATEGORY_WITHOUT_LEVEL,
  178. self::CATEGORY_WITHOUT_LEVEL_CLOSURE
  179. );
  180. }
  181. private function populate()
  182. {
  183. $food = new Category;
  184. $food->setTitle("Food");
  185. $this->em->persist($food);
  186. $vegitables = new Category;
  187. $vegitables->setTitle('Vegitables');
  188. $vegitables->setParent($food);
  189. $this->em->persist($vegitables);
  190. $fruits = new Category;
  191. $fruits->setTitle('Fruits');
  192. $fruits->setParent($food);
  193. $this->em->persist($fruits);
  194. $oranges = new Category;
  195. $oranges->setTitle('Oranges');
  196. $oranges->setParent($fruits);
  197. $this->em->persist($oranges);
  198. $lemons = new Category;
  199. $lemons->setTitle('Lemons');
  200. $lemons->setParent($fruits);
  201. $this->em->persist($lemons);
  202. $berries = new Category;
  203. $berries->setTitle('Berries');
  204. $berries->setParent($fruits);
  205. $this->em->persist($berries);
  206. $strawberries = new Category;
  207. $strawberries->setTitle('Strawberries');
  208. $strawberries->setParent($berries);
  209. $this->em->persist($strawberries);
  210. $cabbages = new Category;
  211. $cabbages->setTitle('Cabbages');
  212. $cabbages->setParent($vegitables);
  213. $this->em->persist($cabbages);
  214. $carrots = new Category;
  215. $carrots->setTitle('Carrots');
  216. $carrots->setParent($vegitables);
  217. $this->em->persist($carrots);
  218. $milk = new Category;
  219. $milk->setTitle('Milk');
  220. $milk->setParent($food);
  221. $this->em->persist($milk);
  222. $cheese = new Category;
  223. $cheese->setTitle('Cheese');
  224. $cheese->setParent($milk);
  225. $this->em->persist($cheese);
  226. $mouldCheese = new Category;
  227. $mouldCheese->setTitle('Mould cheese');
  228. $mouldCheese->setParent($cheese);
  229. $this->em->persist($mouldCheese);
  230. $this->em->flush();
  231. $this->em->clear();
  232. }
  233. }