ClosureTreeRepositoryTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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. $sortOption = array('childSort' => array('field' => 'title', 'dir' => 'asc'));
  161. $testClosure = function(ClosureTreeRepositoryTest $phpUnit, array $tree, $includeNode = false, $whichTree = 'both', $includeNewNode = false) {
  162. if ($whichTree === 'both' || $whichTree === 'first') {
  163. $boringFood = $includeNewNode ? ($includeNode ? $tree[0]['__children'][0] : $tree[0]) : null;
  164. $fruitsIndex = $includeNewNode ? 1 : 0;
  165. $milkIndex = $includeNewNode ? 2 : 1;
  166. $fruits = $includeNode ? $tree[0]['__children'][$fruitsIndex] : $tree[$fruitsIndex];
  167. $milk = $includeNode ? $tree[0]['__children'][$milkIndex] : $tree[$milkIndex];
  168. $vegitables = $includeNewNode ? $boringFood['__children'][0] : ($includeNode ? $tree[0]['__children'][2] : $tree[2]);
  169. if ($includeNode) {
  170. $phpUnit->assertEquals('Food', $tree[0]['title']);
  171. }
  172. $phpUnit->assertEquals('Fruits', $fruits['title']);
  173. $phpUnit->assertEquals('Berries', $fruits['__children'][0]['title']);
  174. $phpUnit->assertEquals('Strawberries', $fruits['__children'][0]['__children'][0]['title']);
  175. $phpUnit->assertEquals('Milk', $milk['title']);
  176. $phpUnit->assertEquals('Cheese', $milk['__children'][0]['title']);
  177. $phpUnit->assertEquals('Mould cheese', $milk['__children'][0]['__children'][0]['title']);
  178. if ($boringFood) {
  179. $phpUnit->assertEquals('Boring Food', $boringFood['title']);
  180. }
  181. $phpUnit->assertEquals('Vegitables', $vegitables['title']);
  182. $phpUnit->assertEquals('Cabbages', $vegitables['__children'][0]['title']);
  183. $phpUnit->assertEquals('Carrots', $vegitables['__children'][1]['title']);
  184. }
  185. if ($whichTree === 'both' || $whichTree === 'second') {
  186. $root = $whichTree === 'both' ? $tree[1] : $tree[0];
  187. $soccer = $includeNode ? $root['__children'][0] : $root;
  188. if ($includeNode) {
  189. $phpUnit->assertEquals('Sports', $root['title']);
  190. }
  191. $phpUnit->assertEquals('Soccer', $soccer['title']);
  192. $phpUnit->assertEquals('Indoor Soccer', $soccer['__children'][0]['title']);
  193. }
  194. };
  195. // All trees
  196. $tree = $repo->childrenHierarchy(null, false, $sortOption);
  197. $testClosure($this, $tree, true, 'both');
  198. $roots = $repo->getRootNodes();
  199. // First root tree, including root node
  200. $tree = $repo->childrenHierarchy(
  201. $roots[0],
  202. false,
  203. $sortOption,
  204. true
  205. );
  206. $testClosure($this, $tree, true, 'first');
  207. // First root tree, not including root node
  208. $tree = $repo->childrenHierarchy(
  209. $roots[0],
  210. false,
  211. $sortOption
  212. );
  213. $testClosure($this, $tree, false, 'first');
  214. // Second root tree, including root node
  215. $tree = $repo->childrenHierarchy(
  216. $roots[1],
  217. false,
  218. $sortOption,
  219. true
  220. );
  221. $testClosure($this, $tree, true, 'second');
  222. // Second root tree, not including root node
  223. $tree = $repo->childrenHierarchy(
  224. $roots[1],
  225. false,
  226. $sortOption
  227. );
  228. $testClosure($this, $tree, false, 'second');
  229. $food = $repo->findOneByTitle('Food');
  230. $vegitables = $repo->findOneByTitle('Vegitables');
  231. $boringFood = new $class();
  232. $boringFood->setTitle('Boring Food');
  233. $boringFood->setParent($food);
  234. $vegitables->setParent($boringFood);
  235. $this->em->persist($boringFood);
  236. $this->em->flush();
  237. // First root tree, after inserting a new node in the middle. This includes the root node
  238. $tree = $repo->childrenHierarchy(
  239. $roots[0],
  240. false,
  241. $sortOption,
  242. true
  243. );
  244. $testClosure($this, $tree, true, 'first', true);
  245. // First root tree, after inserting a new node in the middle. This not includes the root node
  246. $tree = $repo->childrenHierarchy(
  247. $roots[0],
  248. false,
  249. $sortOption
  250. );
  251. $testClosure($this, $tree, false, 'first', true);
  252. // Second root tree, after inserting a new node in the middle. This includes the root node
  253. $tree = $repo->childrenHierarchy(
  254. $roots[1],
  255. false,
  256. $sortOption,
  257. true
  258. );
  259. $testClosure($this, $tree, true, 'second', true);
  260. // Second root tree, after inserting a new node in the middle. This not includes the root node
  261. $tree = $repo->childrenHierarchy(
  262. $roots[1],
  263. false,
  264. $sortOption
  265. );
  266. $testClosure($this, $tree, false, 'second', false);
  267. // Test a subtree, including node
  268. $node = $repo->findOneByTitle('Fruits');
  269. $tree = $repo->childrenHierarchy(
  270. $node,
  271. false,
  272. $sortOption,
  273. true
  274. );
  275. $this->assertEquals('Fruits', $tree[0]['title']);
  276. $this->assertEquals('Berries', $tree[0]['__children'][0]['title']);
  277. $this->assertEquals('Strawberries', $tree[0]['__children'][0]['__children'][0]['title']);
  278. $node = $repo->findOneByTitle('Fruits');
  279. $tree = $repo->childrenHierarchy(
  280. $node,
  281. false,
  282. $sortOption
  283. );
  284. $this->assertEquals('Berries', $tree[0]['title']);
  285. $this->assertEquals('Strawberries', $tree[0]['__children'][0]['title']);
  286. // First Tree Direct Nodes, including root node
  287. $tree = $repo->childrenHierarchy(
  288. $roots[0],
  289. true,
  290. $sortOption,
  291. true
  292. );
  293. $food = $tree[0];
  294. $this->assertEquals('Food', $food['title']);
  295. $this->assertEquals(3, count($food['__children']));
  296. $this->assertEquals('Boring Food', $food['__children'][0]['title']);
  297. $this->assertEquals('Fruits', $food['__children'][1]['title']);
  298. $this->assertEquals('Milk', $food['__children'][2]['title']);
  299. // First Tree Direct Nodes, not including root node
  300. $tree = $repo->childrenHierarchy(
  301. $roots[0],
  302. true,
  303. $sortOption
  304. );
  305. $this->assertEquals(3, count($tree));
  306. $this->assertEquals('Boring Food', $tree[0]['title']);
  307. $this->assertEquals('Fruits', $tree[1]['title']);
  308. $this->assertEquals('Milk', $tree[2]['title']);
  309. // Helper Closures
  310. $getTree = function($includeNode) use ($repo, $roots, $sortOption) {
  311. return $repo->childrenHierarchy(
  312. $roots[0],
  313. true,
  314. array_merge($sortOption, array('decorate' => true)),
  315. $includeNode
  316. );
  317. };
  318. $getTreeHtml = function($includeNode) {
  319. $baseHtml = '<li>Boring Food<ul><li>Vegitables<ul><li>Cabbages</li><li>Carrots</li></ul></li></ul></li><li>Fruits<ul><li>Berries<ul><li>Strawberries</li></ul></li><li>Lemons</li><li>Oranges</li></ul></li><li>Milk<ul><li>Cheese<ul><li>Mould cheese</li></ul></li></ul></li></ul>';
  320. return $includeNode ? '<ul><li>Food<ul>'.$baseHtml.'</li></ul>' : '<ul>'.$baseHtml;
  321. };
  322. // First Tree - Including Root Node - Html test
  323. $this->assertEquals($getTreeHtml(true), $getTree(true));
  324. // First Tree - Not including Root Node - Html test
  325. $this->assertEquals($getTreeHtml(false), $getTree(false));
  326. }
  327. protected function getUsedEntityFixtures()
  328. {
  329. return array(
  330. self::CATEGORY,
  331. self::CLOSURE,
  332. self::CATEGORY_WITHOUT_LEVEL,
  333. self::CATEGORY_WITHOUT_LEVEL_CLOSURE
  334. );
  335. }
  336. private function populate($class = self::CATEGORY)
  337. {
  338. $food = new $class;
  339. $food->setTitle("Food");
  340. $this->em->persist($food);
  341. $vegitables = new $class;
  342. $vegitables->setTitle('Vegitables');
  343. $vegitables->setParent($food);
  344. $this->em->persist($vegitables);
  345. $fruits = new $class;
  346. $fruits->setTitle('Fruits');
  347. $fruits->setParent($food);
  348. $this->em->persist($fruits);
  349. $oranges = new $class;
  350. $oranges->setTitle('Oranges');
  351. $oranges->setParent($fruits);
  352. $this->em->persist($oranges);
  353. $lemons = new $class;
  354. $lemons->setTitle('Lemons');
  355. $lemons->setParent($fruits);
  356. $this->em->persist($lemons);
  357. $berries = new $class;
  358. $berries->setTitle('Berries');
  359. $berries->setParent($fruits);
  360. $this->em->persist($berries);
  361. $strawberries = new $class;
  362. $strawberries->setTitle('Strawberries');
  363. $strawberries->setParent($berries);
  364. $this->em->persist($strawberries);
  365. $cabbages = new $class;
  366. $cabbages->setTitle('Cabbages');
  367. $cabbages->setParent($vegitables);
  368. $this->em->persist($cabbages);
  369. $carrots = new $class;
  370. $carrots->setTitle('Carrots');
  371. $carrots->setParent($vegitables);
  372. $this->em->persist($carrots);
  373. $milk = new $class;
  374. $milk->setTitle('Milk');
  375. $milk->setParent($food);
  376. $this->em->persist($milk);
  377. $cheese = new $class;
  378. $cheese->setTitle('Cheese');
  379. $cheese->setParent($milk);
  380. $this->em->persist($cheese);
  381. $mouldCheese = new $class;
  382. $mouldCheese->setTitle('Mould cheese');
  383. $mouldCheese->setParent($cheese);
  384. $this->em->persist($mouldCheese);
  385. $sports = new $class;
  386. $sports->setTitle('Sports');
  387. $this->em->persist($sports);
  388. $soccer = new $class;
  389. $soccer->setTitle('Soccer');
  390. $soccer->setParent($sports);
  391. $this->em->persist($soccer);
  392. $indoorSoccer = new $class;
  393. $indoorSoccer->setTitle('Indoor Soccer');
  394. $indoorSoccer->setParent($soccer);
  395. $this->em->persist($indoorSoccer);
  396. $this->em->flush();
  397. $this->em->clear();
  398. }
  399. }