ClosureTreeRepositoryTest.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. }
  30. public function testChildCount()
  31. {
  32. $this->populate();
  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. $this->populate();
  46. $repo = $this->em->getRepository(self::CATEGORY);
  47. $fruits = $repo->findOneByTitle('Fruits');
  48. $path = $repo->getPath($fruits);
  49. $this->assertCount(2, $path);
  50. $this->assertEquals('Food', $path[0]->getTitle());
  51. $this->assertEquals('Fruits', $path[1]->getTitle());
  52. $strawberries = $repo->findOneByTitle('Strawberries');
  53. $path = $repo->getPath($strawberries);
  54. $this->assertCount(4, $path);
  55. $this->assertEquals('Food', $path[0]->getTitle());
  56. $this->assertEquals('Fruits', $path[1]->getTitle());
  57. $this->assertEquals('Berries', $path[2]->getTitle());
  58. $this->assertEquals('Strawberries', $path[3]->getTitle());
  59. }
  60. public function testChildren()
  61. {
  62. $this->populate();
  63. $repo = $this->em->getRepository(self::CATEGORY);
  64. $fruits = $repo->findOneByTitle('Fruits');
  65. // direct children of node, sorted by title ascending order
  66. $children = $repo->children($fruits, true, 'title');
  67. $this->assertCount(3, $children);
  68. $this->assertEquals('Berries', $children[0]->getTitle());
  69. $this->assertEquals('Lemons', $children[1]->getTitle());
  70. $this->assertEquals('Oranges', $children[2]->getTitle());
  71. // all children of node
  72. $children = $repo->children($fruits);
  73. $this->assertCount(4, $children);
  74. $this->assertEquals('Oranges', $children[0]->getTitle());
  75. $this->assertEquals('Lemons', $children[1]->getTitle());
  76. $this->assertEquals('Berries', $children[2]->getTitle());
  77. $this->assertEquals('Strawberries', $children[3]->getTitle());
  78. // direct root nodes
  79. $children = $repo->children(null, true, 'title');
  80. $this->assertCount(1, $children);
  81. $this->assertEquals('Food', $children[0]->getTitle());
  82. // all tree
  83. $children = $repo->children();
  84. $this->assertCount(12, $children);
  85. }
  86. public function testSingleNodeRemoval()
  87. {
  88. $this->populate();
  89. $repo = $this->em->getRepository(self::CATEGORY);
  90. $fruits = $repo->findOneByTitle('Fruits');
  91. $repo->removeFromTree($fruits);
  92. // ensure in memory node integrity
  93. $this->em->flush();
  94. $food = $repo->findOneByTitle('Food');
  95. $children = $repo->children($food, true);
  96. $this->assertCount(5, $children);
  97. $berries = $repo->findOneByTitle('Berries');
  98. $this->assertEquals(1, $repo->childCount($berries, true));
  99. $lemons = $repo->findOneByTitle('Lemons');
  100. $this->assertEquals(0, $repo->childCount($lemons, true));
  101. $repo->removeFromTree($food);
  102. $vegitables = $repo->findOneByTitle('Vegitables');
  103. $this->assertEquals(2, $repo->childCount($vegitables, true));
  104. $this->assertNull($vegitables->getParent());
  105. $repo->removeFromTree($lemons);
  106. $this->assertCount(4, $repo->children(null, true));
  107. }
  108. public function testBuildTreeWithLevelProperty()
  109. {
  110. $this->populate();
  111. $this->buildTreeTests(self::CATEGORY);
  112. }
  113. public function testBuildTreeWithoutLevelProperty()
  114. {
  115. $this->populate(self::CATEGORY_WITHOUT_LEVEL);
  116. $this->buildTreeTests(self::CATEGORY_WITHOUT_LEVEL);
  117. }
  118. protected function buildTreeTests($class)
  119. {
  120. $repo = $this->em->getRepository($class);
  121. $roots = $repo->getRootNodes();
  122. $tree = $repo->childrenHierarchy(
  123. $roots[0],
  124. false,
  125. array('childSort' => array('field' => 'title', 'dir' => 'asc'))
  126. );
  127. $fruits = $tree[0]['__children'][0];
  128. $milk = $tree[0]['__children'][1];
  129. $vegitables = $tree[0]['__children'][2];
  130. $this->assertEquals('Food', $tree[0]['title']);
  131. $this->assertEquals('Fruits', $fruits['title']);
  132. $this->assertEquals('Berries', $fruits['__children'][0]['title']);
  133. $this->assertEquals('Strawberries', $fruits['__children'][0]['__children'][0]['title']);
  134. $this->assertEquals('Milk', $milk['title']);
  135. $this->assertEquals('Cheese', $milk['__children'][0]['title']);
  136. $this->assertEquals('Mould cheese', $milk['__children'][0]['__children'][0]['title']);
  137. $this->assertEquals('Vegitables', $vegitables['title']);
  138. $this->assertEquals('Cabbages', $vegitables['__children'][0]['title']);
  139. $this->assertEquals('Carrots', $vegitables['__children'][1]['title']);
  140. $food = $repo->findOneByTitle('Food');
  141. $vegitables = $repo->findOneByTitle('Vegitables');
  142. $boringFood = new $class();
  143. $boringFood->setTitle('Boring Food');
  144. $boringFood->setParent($food);
  145. $vegitables->setParent($boringFood);
  146. $this->em->persist($boringFood);
  147. $this->em->flush();
  148. $tree = $repo->childrenHierarchy(
  149. $roots[0],
  150. false,
  151. array('childSort' => array('field' => 'title', 'dir' => 'asc'))
  152. );
  153. $boringFood = $tree[0]['__children'][0];
  154. $fruits = $tree[0]['__children'][1];
  155. $milk = $tree[0]['__children'][2];
  156. $vegitables = $boringFood['__children'][0];
  157. $this->assertEquals('Food', $tree[0]['title']);
  158. $this->assertEquals('Fruits', $fruits['title']);
  159. $this->assertEquals('Berries', $fruits['__children'][0]['title']);
  160. $this->assertEquals('Strawberries', $fruits['__children'][0]['__children'][0]['title']);
  161. $this->assertEquals('Milk', $milk['title']);
  162. $this->assertEquals('Cheese', $milk['__children'][0]['title']);
  163. $this->assertEquals('Mould cheese', $milk['__children'][0]['__children'][0]['title']);
  164. $this->assertEquals('Boring Food', $boringFood['title']);
  165. $this->assertEquals('Vegitables', $boringFood['__children'][0]['title']);
  166. $this->assertEquals('Cabbages', $vegitables['__children'][0]['title']);
  167. $this->assertEquals('Carrots', $vegitables['__children'][1]['title']);
  168. }
  169. protected function getUsedEntityFixtures()
  170. {
  171. return array(
  172. self::CATEGORY,
  173. self::CLOSURE,
  174. self::CATEGORY_WITHOUT_LEVEL,
  175. self::CATEGORY_WITHOUT_LEVEL_CLOSURE
  176. );
  177. }
  178. private function populate($class = self::CATEGORY)
  179. {
  180. $food = new $class;
  181. $food->setTitle("Food");
  182. $this->em->persist($food);
  183. $vegitables = new $class;
  184. $vegitables->setTitle('Vegitables');
  185. $vegitables->setParent($food);
  186. $this->em->persist($vegitables);
  187. $fruits = new $class;
  188. $fruits->setTitle('Fruits');
  189. $fruits->setParent($food);
  190. $this->em->persist($fruits);
  191. $oranges = new $class;
  192. $oranges->setTitle('Oranges');
  193. $oranges->setParent($fruits);
  194. $this->em->persist($oranges);
  195. $lemons = new $class;
  196. $lemons->setTitle('Lemons');
  197. $lemons->setParent($fruits);
  198. $this->em->persist($lemons);
  199. $berries = new $class;
  200. $berries->setTitle('Berries');
  201. $berries->setParent($fruits);
  202. $this->em->persist($berries);
  203. $strawberries = new $class;
  204. $strawberries->setTitle('Strawberries');
  205. $strawberries->setParent($berries);
  206. $this->em->persist($strawberries);
  207. $cabbages = new $class;
  208. $cabbages->setTitle('Cabbages');
  209. $cabbages->setParent($vegitables);
  210. $this->em->persist($cabbages);
  211. $carrots = new $class;
  212. $carrots->setTitle('Carrots');
  213. $carrots->setParent($vegitables);
  214. $this->em->persist($carrots);
  215. $milk = new $class;
  216. $milk->setTitle('Milk');
  217. $milk->setParent($food);
  218. $this->em->persist($milk);
  219. $cheese = new $class;
  220. $cheese->setTitle('Cheese');
  221. $cheese->setParent($milk);
  222. $this->em->persist($cheese);
  223. $mouldCheese = new $class;
  224. $mouldCheese->setTitle('Mould cheese');
  225. $mouldCheese->setParent($cheese);
  226. $this->em->persist($mouldCheese);
  227. $this->em->flush();
  228. $this->em->clear();
  229. }
  230. }