ClosureTreeRepositoryTest.php 11 KB

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