ClosureTreeRepositoryTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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(2, $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. NOT including the root node
  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. // direct children of node, sorted by title ascending order. including the root node
  74. $children = $repo->children($fruits, true, 'title', 'asc', true);
  75. $this->assertCount(4, $children);
  76. $this->assertEquals('Berries', $children[0]->getTitle());
  77. $this->assertEquals('Fruits', $children[1]->getTitle());
  78. $this->assertEquals('Lemons', $children[2]->getTitle());
  79. $this->assertEquals('Oranges', $children[3]->getTitle());
  80. // all children of node, NOT including the root
  81. $children = $repo->children($fruits);
  82. $this->assertCount(4, $children);
  83. $this->assertEquals('Oranges', $children[0]->getTitle());
  84. $this->assertEquals('Lemons', $children[1]->getTitle());
  85. $this->assertEquals('Berries', $children[2]->getTitle());
  86. $this->assertEquals('Strawberries', $children[3]->getTitle());
  87. // all children of node, including the root
  88. $children = $repo->children($fruits, false, 'title', 'asc', true);
  89. $this->assertCount(5, $children);
  90. $this->assertEquals('Berries', $children[0]->getTitle());
  91. $this->assertEquals('Fruits', $children[1]->getTitle());
  92. $this->assertEquals('Lemons', $children[2]->getTitle());
  93. $this->assertEquals('Oranges', $children[3]->getTitle());
  94. $this->assertEquals('Strawberries', $children[4]->getTitle());
  95. // direct root nodes
  96. $children = $repo->children(null, true, 'title');
  97. $this->assertCount(2, $children);
  98. $this->assertEquals('Food', $children[0]->getTitle());
  99. $this->assertEquals('Sports', $children[1]->getTitle());
  100. // all tree
  101. $children = $repo->children();
  102. $this->assertCount(15, $children);
  103. }
  104. public function testSingleNodeRemoval()
  105. {
  106. $this->populate();
  107. $repo = $this->em->getRepository(self::CATEGORY);
  108. $fruits = $repo->findOneByTitle('Fruits');
  109. $repo->removeFromTree($fruits);
  110. // ensure in memory node integrity
  111. $this->em->flush();
  112. $food = $repo->findOneByTitle('Food');
  113. $children = $repo->children($food, true);
  114. $this->assertCount(5, $children);
  115. $berries = $repo->findOneByTitle('Berries');
  116. $this->assertEquals(1, $repo->childCount($berries, true));
  117. $lemons = $repo->findOneByTitle('Lemons');
  118. $this->assertEquals(0, $repo->childCount($lemons, true));
  119. $repo->removeFromTree($food);
  120. $vegitables = $repo->findOneByTitle('Vegitables');
  121. $this->assertEquals(2, $repo->childCount($vegitables, true));
  122. $this->assertNull($vegitables->getParent());
  123. $repo->removeFromTree($lemons);
  124. $this->assertCount(5, $repo->children(null, true));
  125. }
  126. public function testBuildTreeWithLevelProperty()
  127. {
  128. $this->populate();
  129. $this->buildTreeTests(self::CATEGORY);
  130. }
  131. public function testBuildTreeWithoutLevelProperty()
  132. {
  133. $this->populate(self::CATEGORY_WITHOUT_LEVEL);
  134. $this->buildTreeTests(self::CATEGORY_WITHOUT_LEVEL);
  135. }
  136. public function testHavingLevelPropertyAvoidsSubqueryInSelectInGetNodesHierarchy()
  137. {
  138. $this->populate();
  139. $repo = $this->em->getRepository(self::CATEGORY);
  140. $roots = $repo->getRootNodes();
  141. $meta = $this->em->getClassMetadata(self::CATEGORY);
  142. $config = $this->listener->getConfiguration($this->em, $meta->name);
  143. $qb = $repo->getNodesHierarchyQueryBuilder($roots[0], false, $config);
  144. $this->assertFalse(strpos($qb->getQuery()->getDql(), '(SELECT MAX('));
  145. }
  146. public function testoNotHavingLevelPropertyUsesASubqueryInSelectInGetNodesHierarchy()
  147. {
  148. $this->populate(self::CATEGORY_WITHOUT_LEVEL);
  149. $repo = $this->em->getRepository(self::CATEGORY_WITHOUT_LEVEL);
  150. $roots = $repo->getRootNodes();
  151. $meta = $this->em->getClassMetadata(self::CATEGORY_WITHOUT_LEVEL);
  152. $config = $this->listener->getConfiguration($this->em, $meta->name);
  153. $qb = $repo->getNodesHierarchyQueryBuilder($roots[0], false, $config);
  154. $this->assertTrue(((bool) strpos($qb->getQuery()->getDql(), '(SELECT MAX(')));
  155. }
  156. // Utility Methods
  157. protected function buildTreeTests($class)
  158. {
  159. $repo = $this->em->getRepository($class);
  160. $roots = $repo->getRootNodes();
  161. $tree = $repo->childrenHierarchy(
  162. $roots[0],
  163. false,
  164. array('childSort' => array('field' => 'title', 'dir' => 'asc'))
  165. );
  166. $fruits = $tree[0]['__children'][0];
  167. $milk = $tree[0]['__children'][1];
  168. $vegitables = $tree[0]['__children'][2];
  169. $this->assertEquals('Food', $tree[0]['title']);
  170. $this->assertEquals('Fruits', $fruits['title']);
  171. $this->assertEquals('Berries', $fruits['__children'][0]['title']);
  172. $this->assertEquals('Strawberries', $fruits['__children'][0]['__children'][0]['title']);
  173. $this->assertEquals('Milk', $milk['title']);
  174. $this->assertEquals('Cheese', $milk['__children'][0]['title']);
  175. $this->assertEquals('Mould cheese', $milk['__children'][0]['__children'][0]['title']);
  176. $this->assertEquals('Vegitables', $vegitables['title']);
  177. $this->assertEquals('Cabbages', $vegitables['__children'][0]['title']);
  178. $this->assertEquals('Carrots', $vegitables['__children'][1]['title']);
  179. $tree = $repo->childrenHierarchy(
  180. $roots[1],
  181. false,
  182. array('childSort' => array('field' => 'title', 'dir' => 'asc'))
  183. );
  184. $this->assertEquals('Sports', $tree[0]['title']);
  185. $this->assertEquals('Soccer', $tree[0]['__children'][0]['title']);
  186. $this->assertEquals('Indoor Soccer', $tree[0]['__children'][0]['__children'][0]['title']);
  187. $food = $repo->findOneByTitle('Food');
  188. $vegitables = $repo->findOneByTitle('Vegitables');
  189. $boringFood = new $class();
  190. $boringFood->setTitle('Boring Food');
  191. $boringFood->setParent($food);
  192. $vegitables->setParent($boringFood);
  193. $this->em->persist($boringFood);
  194. $this->em->flush();
  195. $tree = $repo->childrenHierarchy(
  196. $roots[0],
  197. false,
  198. array('childSort' => array('field' => 'title', 'dir' => 'asc'))
  199. );
  200. $boringFood = $tree[0]['__children'][0];
  201. $fruits = $tree[0]['__children'][1];
  202. $milk = $tree[0]['__children'][2];
  203. $vegitables = $boringFood['__children'][0];
  204. $this->assertEquals('Food', $tree[0]['title']);
  205. $this->assertEquals('Fruits', $fruits['title']);
  206. $this->assertEquals('Berries', $fruits['__children'][0]['title']);
  207. $this->assertEquals('Strawberries', $fruits['__children'][0]['__children'][0]['title']);
  208. $this->assertEquals('Milk', $milk['title']);
  209. $this->assertEquals('Cheese', $milk['__children'][0]['title']);
  210. $this->assertEquals('Mould cheese', $milk['__children'][0]['__children'][0]['title']);
  211. $this->assertEquals('Boring Food', $boringFood['title']);
  212. $this->assertEquals('Vegitables', $boringFood['__children'][0]['title']);
  213. $this->assertEquals('Cabbages', $vegitables['__children'][0]['title']);
  214. $this->assertEquals('Carrots', $vegitables['__children'][1]['title']);
  215. $tree = $repo->childrenHierarchy(
  216. $roots[1],
  217. false,
  218. array('childSort' => array('field' => 'title', 'dir' => 'asc'))
  219. );
  220. $this->assertEquals('Sports', $tree[0]['title']);
  221. $this->assertEquals('Soccer', $tree[0]['__children'][0]['title']);
  222. $this->assertEquals('Indoor Soccer', $tree[0]['__children'][0]['__children'][0]['title']);
  223. }
  224. protected function getUsedEntityFixtures()
  225. {
  226. return array(
  227. self::CATEGORY,
  228. self::CLOSURE,
  229. self::CATEGORY_WITHOUT_LEVEL,
  230. self::CATEGORY_WITHOUT_LEVEL_CLOSURE
  231. );
  232. }
  233. private function populate($class = self::CATEGORY)
  234. {
  235. $food = new $class;
  236. $food->setTitle("Food");
  237. $this->em->persist($food);
  238. $vegitables = new $class;
  239. $vegitables->setTitle('Vegitables');
  240. $vegitables->setParent($food);
  241. $this->em->persist($vegitables);
  242. $fruits = new $class;
  243. $fruits->setTitle('Fruits');
  244. $fruits->setParent($food);
  245. $this->em->persist($fruits);
  246. $oranges = new $class;
  247. $oranges->setTitle('Oranges');
  248. $oranges->setParent($fruits);
  249. $this->em->persist($oranges);
  250. $lemons = new $class;
  251. $lemons->setTitle('Lemons');
  252. $lemons->setParent($fruits);
  253. $this->em->persist($lemons);
  254. $berries = new $class;
  255. $berries->setTitle('Berries');
  256. $berries->setParent($fruits);
  257. $this->em->persist($berries);
  258. $strawberries = new $class;
  259. $strawberries->setTitle('Strawberries');
  260. $strawberries->setParent($berries);
  261. $this->em->persist($strawberries);
  262. $cabbages = new $class;
  263. $cabbages->setTitle('Cabbages');
  264. $cabbages->setParent($vegitables);
  265. $this->em->persist($cabbages);
  266. $carrots = new $class;
  267. $carrots->setTitle('Carrots');
  268. $carrots->setParent($vegitables);
  269. $this->em->persist($carrots);
  270. $milk = new $class;
  271. $milk->setTitle('Milk');
  272. $milk->setParent($food);
  273. $this->em->persist($milk);
  274. $cheese = new $class;
  275. $cheese->setTitle('Cheese');
  276. $cheese->setParent($milk);
  277. $this->em->persist($cheese);
  278. $mouldCheese = new $class;
  279. $mouldCheese->setTitle('Mould cheese');
  280. $mouldCheese->setParent($cheese);
  281. $this->em->persist($mouldCheese);
  282. $sports = new $class;
  283. $sports->setTitle('Sports');
  284. $this->em->persist($sports);
  285. $soccer = new $class;
  286. $soccer->setTitle('Soccer');
  287. $soccer->setParent($sports);
  288. $this->em->persist($soccer);
  289. $indoorSoccer = new $class;
  290. $indoorSoccer->setTitle('Indoor Soccer');
  291. $indoorSoccer->setParent($soccer);
  292. $this->em->persist($indoorSoccer);
  293. $this->em->flush();
  294. $this->em->clear();
  295. }
  296. }