ClosureTreeRepositoryTest.php 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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. // Count all
  38. $count = $repo->childCount();
  39. $this->assertEquals(15, $count);
  40. // Count all, but only direct ones
  41. $count = $repo->childCount(null, true);
  42. $this->assertEquals(2, $count);
  43. // Count food children
  44. $food = $repo->findOneByTitle('Food');
  45. $count = $repo->childCount($food);
  46. $this->assertEquals(11, $count);
  47. // Count food children, but only direct ones
  48. $food = $repo->findOneByTitle('Food');
  49. $count = $repo->childCount($food, true);
  50. $this->assertEquals(3, $count);
  51. }
  52. public function testPath()
  53. {
  54. $this->populate();
  55. $repo = $this->em->getRepository(self::CATEGORY);
  56. $fruits = $repo->findOneByTitle('Fruits');
  57. $path = $repo->getPath($fruits);
  58. $this->assertCount(2, $path);
  59. $this->assertEquals('Food', $path[0]->getTitle());
  60. $this->assertEquals('Fruits', $path[1]->getTitle());
  61. $strawberries = $repo->findOneByTitle('Strawberries');
  62. $path = $repo->getPath($strawberries);
  63. $this->assertCount(4, $path);
  64. $this->assertEquals('Food', $path[0]->getTitle());
  65. $this->assertEquals('Fruits', $path[1]->getTitle());
  66. $this->assertEquals('Berries', $path[2]->getTitle());
  67. $this->assertEquals('Strawberries', $path[3]->getTitle());
  68. }
  69. public function testChildren()
  70. {
  71. $this->populate();
  72. $repo = $this->em->getRepository(self::CATEGORY);
  73. $fruits = $repo->findOneByTitle('Fruits');
  74. // direct children of node, sorted by title ascending order. NOT including the root node
  75. $children = $repo->children($fruits, true, 'title');
  76. $this->assertCount(3, $children);
  77. $this->assertEquals('Berries', $children[0]->getTitle());
  78. $this->assertEquals('Lemons', $children[1]->getTitle());
  79. $this->assertEquals('Oranges', $children[2]->getTitle());
  80. // direct children of node, sorted by title ascending order. including the root node
  81. $children = $repo->children($fruits, true, 'title', 'asc', true);
  82. $this->assertCount(4, $children);
  83. $this->assertEquals('Berries', $children[0]->getTitle());
  84. $this->assertEquals('Fruits', $children[1]->getTitle());
  85. $this->assertEquals('Lemons', $children[2]->getTitle());
  86. $this->assertEquals('Oranges', $children[3]->getTitle());
  87. // all children of node, NOT including the root
  88. $children = $repo->children($fruits);
  89. $this->assertCount(4, $children);
  90. $this->assertEquals('Oranges', $children[0]->getTitle());
  91. $this->assertEquals('Lemons', $children[1]->getTitle());
  92. $this->assertEquals('Berries', $children[2]->getTitle());
  93. $this->assertEquals('Strawberries', $children[3]->getTitle());
  94. // all children of node, including the root
  95. $children = $repo->children($fruits, false, 'title', 'asc', true);
  96. $this->assertCount(5, $children);
  97. $this->assertEquals('Berries', $children[0]->getTitle());
  98. $this->assertEquals('Fruits', $children[1]->getTitle());
  99. $this->assertEquals('Lemons', $children[2]->getTitle());
  100. $this->assertEquals('Oranges', $children[3]->getTitle());
  101. $this->assertEquals('Strawberries', $children[4]->getTitle());
  102. // direct root nodes
  103. $children = $repo->children(null, true, 'title');
  104. $this->assertCount(2, $children);
  105. $this->assertEquals('Food', $children[0]->getTitle());
  106. $this->assertEquals('Sports', $children[1]->getTitle());
  107. // all tree
  108. $children = $repo->children();
  109. $this->assertCount(15, $children);
  110. }
  111. public function testSingleNodeRemoval()
  112. {
  113. $this->populate();
  114. $repo = $this->em->getRepository(self::CATEGORY);
  115. $fruits = $repo->findOneByTitle('Fruits');
  116. $repo->removeFromTree($fruits);
  117. // ensure in memory node integrity
  118. $this->em->flush();
  119. $food = $repo->findOneByTitle('Food');
  120. $children = $repo->children($food, true);
  121. $this->assertCount(5, $children);
  122. $berries = $repo->findOneByTitle('Berries');
  123. $this->assertEquals(1, $repo->childCount($berries, true));
  124. $lemons = $repo->findOneByTitle('Lemons');
  125. $this->assertEquals(0, $repo->childCount($lemons, true));
  126. $repo->removeFromTree($food);
  127. $vegitables = $repo->findOneByTitle('Vegitables');
  128. $this->assertEquals(2, $repo->childCount($vegitables, true));
  129. $this->assertNull($vegitables->getParent());
  130. $repo->removeFromTree($lemons);
  131. $this->assertCount(5, $repo->children(null, true));
  132. }
  133. public function testBuildTreeWithLevelProperty()
  134. {
  135. $this->populate();
  136. $this->buildTreeTests(self::CATEGORY);
  137. }
  138. public function testBuildTreeWithoutLevelProperty()
  139. {
  140. $this->populate(self::CATEGORY_WITHOUT_LEVEL);
  141. $this->buildTreeTests(self::CATEGORY_WITHOUT_LEVEL);
  142. }
  143. public function testHavingLevelPropertyAvoidsSubqueryInSelectInGetNodesHierarchy()
  144. {
  145. $this->populate();
  146. $repo = $this->em->getRepository(self::CATEGORY);
  147. $roots = $repo->getRootNodes();
  148. $meta = $this->em->getClassMetadata(self::CATEGORY);
  149. $config = $this->listener->getConfiguration($this->em, $meta->name);
  150. $qb = $repo->getNodesHierarchyQueryBuilder($roots[0], false, $config);
  151. $this->assertFalse(strpos($qb->getQuery()->getDql(), '(SELECT MAX('));
  152. }
  153. public function testNotHavingLevelPropertyUsesASubqueryInSelectInGetNodesHierarchy()
  154. {
  155. $this->populate(self::CATEGORY_WITHOUT_LEVEL);
  156. $repo = $this->em->getRepository(self::CATEGORY_WITHOUT_LEVEL);
  157. $roots = $repo->getRootNodes();
  158. $meta = $this->em->getClassMetadata(self::CATEGORY_WITHOUT_LEVEL);
  159. $config = $this->listener->getConfiguration($this->em, $meta->name);
  160. $qb = $repo->getNodesHierarchyQueryBuilder($roots[0], false, $config);
  161. $this->assertTrue(((bool) strpos($qb->getQuery()->getDql(), '(SELECT MAX(')));
  162. }
  163. public function test_changeChildrenIndex()
  164. {
  165. $this->populate(self::CATEGORY);
  166. $childrenIndex = 'myChildren';
  167. $repo = $this->em->getRepository(self::CATEGORY);
  168. $repo->setChildrenIndex($childrenIndex);
  169. $tree = $repo->childrenHierarchy();
  170. $this->assertInternalType('array', $tree[0][$childrenIndex]);
  171. }
  172. // Utility Methods
  173. protected function buildTreeTests($class)
  174. {
  175. $repo = $this->em->getRepository($class);
  176. $sortOption = array('childSort' => array('field' => 'title', 'dir' => 'asc'));
  177. $testClosure = function(ClosureTreeRepositoryTest $phpUnit, array $tree, $includeNode = false, $whichTree = 'both', $includeNewNode = false) {
  178. if ($whichTree === 'both' || $whichTree === 'first') {
  179. $boringFood = $includeNewNode ? ($includeNode ? $tree[0]['__children'][0] : $tree[0]) : null;
  180. $fruitsIndex = $includeNewNode ? 1 : 0;
  181. $milkIndex = $includeNewNode ? 2 : 1;
  182. $fruits = $includeNode ? $tree[0]['__children'][$fruitsIndex] : $tree[$fruitsIndex];
  183. $milk = $includeNode ? $tree[0]['__children'][$milkIndex] : $tree[$milkIndex];
  184. $vegitables = $includeNewNode ? $boringFood['__children'][0] : ($includeNode ? $tree[0]['__children'][2] : $tree[2]);
  185. if ($includeNode) {
  186. $phpUnit->assertEquals('Food', $tree[0]['title']);
  187. }
  188. $phpUnit->assertEquals('Fruits', $fruits['title']);
  189. $phpUnit->assertEquals('Berries', $fruits['__children'][0]['title']);
  190. $phpUnit->assertEquals('Strawberries', $fruits['__children'][0]['__children'][0]['title']);
  191. $phpUnit->assertEquals('Milk', $milk['title']);
  192. $phpUnit->assertEquals('Cheese', $milk['__children'][0]['title']);
  193. $phpUnit->assertEquals('Mould cheese', $milk['__children'][0]['__children'][0]['title']);
  194. if ($boringFood) {
  195. $phpUnit->assertEquals('Boring Food', $boringFood['title']);
  196. }
  197. $phpUnit->assertEquals('Vegitables', $vegitables['title']);
  198. $phpUnit->assertEquals('Cabbages', $vegitables['__children'][0]['title']);
  199. $phpUnit->assertEquals('Carrots', $vegitables['__children'][1]['title']);
  200. }
  201. if ($whichTree === 'both' || $whichTree === 'second') {
  202. $root = $whichTree === 'both' ? $tree[1] : $tree[0];
  203. $soccer = $includeNode ? $root['__children'][0] : $root;
  204. if ($includeNode) {
  205. $phpUnit->assertEquals('Sports', $root['title']);
  206. }
  207. $phpUnit->assertEquals('Soccer', $soccer['title']);
  208. $phpUnit->assertEquals('Indoor Soccer', $soccer['__children'][0]['title']);
  209. }
  210. };
  211. // All trees
  212. $tree = $repo->childrenHierarchy(null, false, $sortOption);
  213. $testClosure($this, $tree, true, 'both');
  214. $roots = $repo->getRootNodes();
  215. // First root tree, including root node
  216. $tree = $repo->childrenHierarchy(
  217. $roots[0],
  218. false,
  219. $sortOption,
  220. true
  221. );
  222. $testClosure($this, $tree, true, 'first');
  223. // First root tree, not including root node
  224. $tree = $repo->childrenHierarchy(
  225. $roots[0],
  226. false,
  227. $sortOption
  228. );
  229. $testClosure($this, $tree, false, 'first');
  230. // Second root tree, including root node
  231. $tree = $repo->childrenHierarchy(
  232. $roots[1],
  233. false,
  234. $sortOption,
  235. true
  236. );
  237. $testClosure($this, $tree, true, 'second');
  238. // Second root tree, not including root node
  239. $tree = $repo->childrenHierarchy(
  240. $roots[1],
  241. false,
  242. $sortOption
  243. );
  244. $testClosure($this, $tree, false, 'second');
  245. $food = $repo->findOneByTitle('Food');
  246. $vegitables = $repo->findOneByTitle('Vegitables');
  247. $boringFood = new $class();
  248. $boringFood->setTitle('Boring Food');
  249. $boringFood->setParent($food);
  250. $vegitables->setParent($boringFood);
  251. $this->em->persist($boringFood);
  252. $this->em->flush();
  253. // First root tree, after inserting a new node in the middle. This includes the root node
  254. $tree = $repo->childrenHierarchy(
  255. $roots[0],
  256. false,
  257. $sortOption,
  258. true
  259. );
  260. $testClosure($this, $tree, true, 'first', true);
  261. // First root tree, after inserting a new node in the middle. This not includes the root node
  262. $tree = $repo->childrenHierarchy(
  263. $roots[0],
  264. false,
  265. $sortOption
  266. );
  267. $testClosure($this, $tree, false, 'first', true);
  268. // Second root tree, after inserting a new node in the middle. This includes the root node
  269. $tree = $repo->childrenHierarchy(
  270. $roots[1],
  271. false,
  272. $sortOption,
  273. true
  274. );
  275. $testClosure($this, $tree, true, 'second', true);
  276. // Second root tree, after inserting a new node in the middle. This not includes the root node
  277. $tree = $repo->childrenHierarchy(
  278. $roots[1],
  279. false,
  280. $sortOption
  281. );
  282. $testClosure($this, $tree, false, 'second', false);
  283. // Test a subtree, including node
  284. $node = $repo->findOneByTitle('Fruits');
  285. $tree = $repo->childrenHierarchy(
  286. $node,
  287. false,
  288. $sortOption,
  289. true
  290. );
  291. $this->assertEquals('Fruits', $tree[0]['title']);
  292. $this->assertEquals('Berries', $tree[0]['__children'][0]['title']);
  293. $this->assertEquals('Strawberries', $tree[0]['__children'][0]['__children'][0]['title']);
  294. $node = $repo->findOneByTitle('Fruits');
  295. $tree = $repo->childrenHierarchy(
  296. $node,
  297. false,
  298. $sortOption
  299. );
  300. $this->assertEquals('Berries', $tree[0]['title']);
  301. $this->assertEquals('Strawberries', $tree[0]['__children'][0]['title']);
  302. // First Tree Direct Nodes, including root node
  303. $tree = $repo->childrenHierarchy(
  304. $roots[0],
  305. true,
  306. $sortOption,
  307. true
  308. );
  309. $food = $tree[0];
  310. $this->assertEquals('Food', $food['title']);
  311. $this->assertEquals(3, count($food['__children']));
  312. $this->assertEquals('Boring Food', $food['__children'][0]['title']);
  313. $this->assertEquals('Fruits', $food['__children'][1]['title']);
  314. $this->assertEquals('Milk', $food['__children'][2]['title']);
  315. // First Tree Direct Nodes, not including root node
  316. $tree = $repo->childrenHierarchy(
  317. $roots[0],
  318. true,
  319. $sortOption
  320. );
  321. $this->assertEquals(3, count($tree));
  322. $this->assertEquals('Boring Food', $tree[0]['title']);
  323. $this->assertEquals('Fruits', $tree[1]['title']);
  324. $this->assertEquals('Milk', $tree[2]['title']);
  325. // Helper Closures
  326. $getTree = function($includeNode) use ($repo, $roots, $sortOption) {
  327. return $repo->childrenHierarchy(
  328. $roots[0],
  329. true,
  330. array_merge($sortOption, array('decorate' => true)),
  331. $includeNode
  332. );
  333. };
  334. $getTreeHtml = function($includeNode) {
  335. $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>';
  336. return $includeNode ? '<ul><li>Food<ul>'.$baseHtml.'</li></ul>' : '<ul>'.$baseHtml;
  337. };
  338. // First Tree - Including Root Node - Html test
  339. $this->assertEquals($getTreeHtml(true), $getTree(true));
  340. // First Tree - Not including Root Node - Html test
  341. $this->assertEquals($getTreeHtml(false), $getTree(false));
  342. }
  343. protected function getUsedEntityFixtures()
  344. {
  345. return array(
  346. self::CATEGORY,
  347. self::CLOSURE,
  348. self::CATEGORY_WITHOUT_LEVEL,
  349. self::CATEGORY_WITHOUT_LEVEL_CLOSURE
  350. );
  351. }
  352. private function populate($class = self::CATEGORY)
  353. {
  354. $food = new $class;
  355. $food->setTitle("Food");
  356. $this->em->persist($food);
  357. $vegitables = new $class;
  358. $vegitables->setTitle('Vegitables');
  359. $vegitables->setParent($food);
  360. $this->em->persist($vegitables);
  361. $fruits = new $class;
  362. $fruits->setTitle('Fruits');
  363. $fruits->setParent($food);
  364. $this->em->persist($fruits);
  365. $oranges = new $class;
  366. $oranges->setTitle('Oranges');
  367. $oranges->setParent($fruits);
  368. $this->em->persist($oranges);
  369. $lemons = new $class;
  370. $lemons->setTitle('Lemons');
  371. $lemons->setParent($fruits);
  372. $this->em->persist($lemons);
  373. $berries = new $class;
  374. $berries->setTitle('Berries');
  375. $berries->setParent($fruits);
  376. $this->em->persist($berries);
  377. $strawberries = new $class;
  378. $strawberries->setTitle('Strawberries');
  379. $strawberries->setParent($berries);
  380. $this->em->persist($strawberries);
  381. $cabbages = new $class;
  382. $cabbages->setTitle('Cabbages');
  383. $cabbages->setParent($vegitables);
  384. $this->em->persist($cabbages);
  385. $carrots = new $class;
  386. $carrots->setTitle('Carrots');
  387. $carrots->setParent($vegitables);
  388. $this->em->persist($carrots);
  389. $milk = new $class;
  390. $milk->setTitle('Milk');
  391. $milk->setParent($food);
  392. $this->em->persist($milk);
  393. $cheese = new $class;
  394. $cheese->setTitle('Cheese');
  395. $cheese->setParent($milk);
  396. $this->em->persist($cheese);
  397. $mouldCheese = new $class;
  398. $mouldCheese->setTitle('Mould cheese');
  399. $mouldCheese->setParent($cheese);
  400. $this->em->persist($mouldCheese);
  401. $sports = new $class;
  402. $sports->setTitle('Sports');
  403. $this->em->persist($sports);
  404. $soccer = new $class;
  405. $soccer->setTitle('Soccer');
  406. $soccer->setParent($sports);
  407. $this->em->persist($soccer);
  408. $indoorSoccer = new $class;
  409. $indoorSoccer->setTitle('Indoor Soccer');
  410. $indoorSoccer->setParent($soccer);
  411. $this->em->persist($indoorSoccer);
  412. $this->em->flush();
  413. $this->em->clear();
  414. }
  415. }