ClosureTreeRepositoryTest.php 18 KB

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