NestedTreeRootRepositoryTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. <?php
  2. namespace Gedmo\Tree;
  3. use Doctrine\Common\EventManager;
  4. use Tool\BaseTestCaseORM;
  5. use Tree\Fixture\RootCategory;
  6. /**
  7. * These are tests for Tree behavior
  8. *
  9. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  10. * @link http://www.gediminasm.org
  11. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  12. */
  13. class NestedTreeRootRepositoryTest extends BaseTestCaseORM
  14. {
  15. const CATEGORY = "Tree\\Fixture\\RootCategory";
  16. protected function setUp()
  17. {
  18. parent::setUp();
  19. $evm = new EventManager;
  20. $evm->addEventSubscriber(new TreeListener);
  21. $this->getMockSqliteEntityManager($evm);
  22. $this->populate();
  23. }
  24. /**
  25. * Based on issue #342
  26. *
  27. * @test
  28. */
  29. function shouldBeAbleToShiftRootNode()
  30. {
  31. $repo = $this->em->getRepository(self::CATEGORY);
  32. $food = $repo->findOneByTitle('Food');
  33. $acme = new RootCategory;
  34. $acme->setTitle('Acme');
  35. $food->setParent($acme);
  36. $this->em->persist($acme);
  37. $this->em->persist($food);
  38. $this->em->flush();
  39. $this->assertNull($acme->getParent());
  40. $this->assertSame($acme, $food->getParent());
  41. $this->assertSame($acme->getId(), $acme->getRoot());
  42. $this->assertSame($acme->getId(), $food->getRoot());
  43. $this->assertSame(1, $acme->getLeft());
  44. $this->assertSame(12, $acme->getRight());
  45. $this->assertSame(2, $food->getLeft());
  46. $this->assertSame(11, $food->getRight());
  47. }
  48. /**
  49. * @test
  50. */
  51. function shouldSupportChildrenHierarchyAsArray()
  52. {
  53. $repo = $this->em->getRepository(self::CATEGORY);
  54. $result = $repo->childrenHierarchy();
  55. $this->assertCount(2, $result);
  56. $this->assertTrue(isset($result[0]['__children'][0]['__children']));
  57. $vegies = $repo->findOneByTitle('Vegitables');
  58. $result = $repo->childrenHierarchy($vegies);
  59. $this->assertCount(2, $result);
  60. $this->assertCount(0, $result[0]['__children']);
  61. // Complete Tree
  62. $roots = $repo->getRootNodes();
  63. $tree = $repo->childrenHierarchy();
  64. $this->assertEquals(2, count($tree)); // Count roots
  65. $this->assertEquals('Food', $tree[0]['title']);
  66. $this->assertEquals('Sports', $tree[1]['title']);
  67. $this->assertEquals('Fruits', $tree[0]['__children'][0]['title']);
  68. $this->assertEquals('Vegitables', $tree[0]['__children'][1]['title']);
  69. $this->assertEquals('Carrots', $tree[0]['__children'][1]['__children'][0]['title']);
  70. $this->assertEquals('Potatoes', $tree[0]['__children'][1]['__children'][1]['title']);
  71. // Tree of one specific root, without the root node
  72. $roots = $repo->getRootNodes();
  73. $tree = $repo->childrenHierarchy($roots[0]);
  74. $this->assertEquals(2, count($tree)); // Count roots
  75. $this->assertEquals('Fruits', $tree[0]['title']);
  76. $this->assertEquals('Vegitables', $tree[1]['title']);
  77. $this->assertEquals('Carrots', $tree[1]['__children'][0]['title']);
  78. $this->assertEquals('Potatoes', $tree[1]['__children'][1]['title']);
  79. // Tree of one specific root, with the root node
  80. $tree = $repo->childrenHierarchy($roots[0], false, array(), true);
  81. $this->assertEquals(1, count($tree)); // Count roots
  82. $this->assertEquals('Food', $tree[0]['title']);
  83. $this->assertEquals('Fruits', $tree[0]['__children'][0]['title']);
  84. $this->assertEquals('Vegitables', $tree[0]['__children'][1]['title']);
  85. $this->assertEquals('Carrots', $tree[0]['__children'][1]['__children'][0]['title']);
  86. $this->assertEquals('Potatoes', $tree[0]['__children'][1]['__children'][1]['title']);
  87. // Tree of one specific root only with direct children, without the root node
  88. $roots = $repo->getRootNodes();
  89. $tree = $repo->childrenHierarchy($roots[0], true);
  90. $this->assertEquals(2, count($tree));
  91. $this->assertEquals('Fruits', $tree[0]['title']);
  92. $this->assertEquals('Vegitables', $tree[1]['title']);
  93. // Tree of one specific root only with direct children, with the root node
  94. $tree = $repo->childrenHierarchy($roots[0], true, array(), true);
  95. $this->assertEquals(1, count($tree));
  96. $this->assertEquals('Food', $tree[0]['title']);
  97. $this->assertEquals('Fruits', $tree[0]['__children'][0]['title']);
  98. $this->assertEquals('Vegitables', $tree[0]['__children'][1]['title']);
  99. }
  100. /**
  101. * @test
  102. */
  103. function shouldSupportChildrenHierarchyAsHtml()
  104. {
  105. $repo = $this->em->getRepository(self::CATEGORY);
  106. $food = $repo->findOneByTitle('Food');
  107. $decorate = true;
  108. $defaultHtmlTree = $repo->childrenHierarchy($food, false, compact('decorate'));
  109. $this->assertEquals(
  110. '<ul><li>Fruits</li><li>Vegitables<ul><li>Carrots</li><li>Potatoes</li></ul></li></ul>',
  111. $defaultHtmlTree
  112. );
  113. // custom title
  114. $nodeDecorator = function($node) {
  115. return '<span>'.$node['title'].'</span>';
  116. };
  117. $decoratedHtmlTree = $repo->childrenHierarchy(
  118. $food,
  119. false,
  120. compact('decorate', 'nodeDecorator')
  121. );
  122. $this->assertEquals(
  123. '<ul><li><span>Fruits</span></li><li><span>Vegitables</span><ul><li><span>Carrots</span></li><li><span>Potatoes</span></li></ul></li></ul>',
  124. $decoratedHtmlTree
  125. );
  126. // cli friendly output
  127. $rootOpen = '';
  128. $rootClose = '';
  129. $childOpen = '';
  130. $childClose = '';
  131. $nodeDecorator = function($node) {
  132. return str_repeat('-', $node['level']).$node['title']."\n";
  133. };
  134. $decoratedCliTree = $repo->childrenHierarchy(
  135. $food,
  136. false,
  137. compact('decorate', 'nodeDecorator', 'rootOpen', 'rootClose', 'childOpen', 'childClose')
  138. );
  139. $this->assertEquals(
  140. "-Fruits\n-Vegitables\n--Carrots\n--Potatoes\n",
  141. $decoratedCliTree
  142. );
  143. $rootOpen = function () {return '<ul class="group">';};
  144. // check support of the closures in rootClose
  145. $rootClose = function () {return '</ul><!--rootCloseClosure-->';};
  146. $childOpen = function (&$node) {
  147. return '<li class="depth'.$node['level'].'">';
  148. };
  149. // check support of the closures in childClose
  150. $childClose = function(&$node) {
  151. return '</li><!--childCloseClosure-->';
  152. };
  153. $decoratedHtmlTree = $repo->childrenHierarchy(
  154. $food,
  155. false,
  156. compact('decorate', 'rootOpen', 'rootClose','childOpen','childClose')
  157. );
  158. $this->assertEquals(
  159. '<ul class="group"><li class="depth1">Fruits</li><!--childCloseClosure--><li class="depth1">Vegitables<ul class="group"><li class="depth2">Carrots</li><!--childCloseClosure--><li class="depth2">Potatoes</li><!--childCloseClosure--></ul><!--rootCloseClosure--></li><!--childCloseClosure--></ul><!--rootCloseClosure-->',
  160. $decoratedHtmlTree
  161. );
  162. }
  163. /**
  164. * @test
  165. */
  166. function shouldSupportChildrenHierarchyByBuildTreeFunction()
  167. {
  168. $repo = $this->em->getRepository(self::CATEGORY);
  169. $q = $this->em
  170. ->createQueryBuilder()
  171. ->select('node')
  172. ->from(self::CATEGORY, 'node')
  173. ->orderBy('node.root, node.lft', 'ASC')
  174. ->where('node.root = 1')
  175. ->getQuery()
  176. ;
  177. $tree = $repo->buildTree($q->getArrayResult());
  178. $this->assertCount(1, $tree);
  179. $this->assertCount(2, $tree[0]['__children']);
  180. $nodes = array();
  181. $options = array('decorate' => true);
  182. $this->assertEquals('', $repo->buildTree($nodes, $options), 'should give empty string when there are no nodes given');
  183. }
  184. /**
  185. * @test
  186. */
  187. public function shouldRemoveRootNodeFromTree()
  188. {
  189. $repo = $this->em->getRepository(self::CATEGORY);
  190. $this->populateMore();
  191. $food = $repo->findOneByTitle('Food');
  192. $repo->removeFromTree($food);
  193. $this->em->clear();
  194. $food = $repo->findOneByTitle('Food');
  195. $this->assertNull($food);
  196. $node = $repo->findOneByTitle('Fruits');
  197. $this->assertEquals(1, $node->getLeft());
  198. $this->assertEquals(2, $node->getRight());
  199. $this->assertEquals(3, $node->getRoot());
  200. $this->assertNull($node->getParent());
  201. $node = $repo->findOneByTitle('Vegitables');
  202. $this->assertEquals(1, $node->getLeft());
  203. $this->assertEquals(10, $node->getRight());
  204. $this->assertEquals(4, $node->getRoot());
  205. $this->assertNull($node->getParent());
  206. }
  207. /**
  208. * @test
  209. */
  210. public function shouldHandleBasicRepositoryMethods()
  211. {
  212. $repo = $this->em->getRepository(self::CATEGORY);
  213. $carrots = $repo->findOneByTitle('Carrots');
  214. $path = $repo->getPath($carrots);
  215. $this->assertCount(3, $path);
  216. $this->assertEquals('Food', $path[0]->getTitle());
  217. $this->assertEquals('Vegitables', $path[1]->getTitle());
  218. $this->assertEquals('Carrots', $path[2]->getTitle());
  219. $vegies = $repo->findOneByTitle('Vegitables');
  220. $childCount = $repo->childCount($vegies);
  221. $this->assertEquals(2, $childCount);
  222. $food = $repo->findOneByTitle('Food');
  223. $childCount = $repo->childCount($food, true);
  224. $this->assertEquals(2, $childCount);
  225. $childCount = $repo->childCount($food);
  226. $this->assertEquals(4, $childCount);
  227. $childCount = $repo->childCount();
  228. $this->assertEquals(6, $childCount);
  229. $childCount = $repo->childCount(null, true);
  230. $this->assertEquals(2, $childCount);
  231. }
  232. /**
  233. * @test
  234. */
  235. public function shouldHandleAdvancedRepositoryFunctions()
  236. {
  237. $this->populateMore();
  238. $repo = $this->em->getRepository(self::CATEGORY);
  239. // verification
  240. $this->assertTrue($repo->verify());
  241. $dql = 'UPDATE ' . self::CATEGORY . ' node';
  242. $dql .= ' SET node.lft = 5';
  243. $dql .= ' WHERE node.id = 4';
  244. $this->em->createQuery($dql)->getSingleScalarResult();
  245. $this->em->clear(); // must clear cached entities
  246. $errors = $repo->verify();
  247. $this->assertCount(2, $errors);
  248. $this->assertEquals('index [4], missing on tree root: 1', $errors[0]);
  249. $this->assertEquals('index [5], duplicate on tree root: 1', $errors[1]);
  250. // test recover functionality
  251. $repo->recover();
  252. $this->em->flush();
  253. $this->assertTrue($repo->verify());
  254. $this->em->clear();
  255. $onions = $repo->findOneByTitle('Onions');
  256. $this->assertEquals(11, $onions->getLeft());
  257. $this->assertEquals(12, $onions->getRight());
  258. // move up
  259. $repo->moveUp($onions);
  260. $this->assertEquals(9, $onions->getLeft());
  261. $this->assertEquals(10, $onions->getRight());
  262. $repo->moveUp($onions, true);
  263. $this->assertEquals(5, $onions->getLeft());
  264. $this->assertEquals(6, $onions->getRight());
  265. // move down
  266. $repo->moveDown($onions, 2);
  267. $this->assertEquals(9, $onions->getLeft());
  268. $this->assertEquals(10, $onions->getRight());
  269. // reorder
  270. $node = $repo->findOneByTitle('Food');
  271. $repo->reorder($node, 'title', 'ASC', false);
  272. $node = $repo->findOneByTitle('Cabbages');
  273. $this->assertEquals(5, $node->getLeft());
  274. $this->assertEquals(6, $node->getRight());
  275. $node = $repo->findOneByTitle('Carrots');
  276. $this->assertEquals(7, $node->getLeft());
  277. $this->assertEquals(8, $node->getRight());
  278. $node = $repo->findOneByTitle('Onions');
  279. $this->assertEquals(9, $node->getLeft());
  280. $this->assertEquals(10, $node->getRight());
  281. $node = $repo->findOneByTitle('Potatoes');
  282. $this->assertEquals(11, $node->getLeft());
  283. $this->assertEquals(12, $node->getRight());
  284. // leafs
  285. $leafs = $repo->getLeafs($node);
  286. $this->assertCount(5, $leafs);
  287. $this->assertEquals('Fruits', $leafs[0]->getTitle());
  288. $this->assertEquals('Cabbages', $leafs[1]->getTitle());
  289. $this->assertEquals('Carrots', $leafs[2]->getTitle());
  290. $this->assertEquals('Onions', $leafs[3]->getTitle());
  291. $this->assertEquals('Potatoes', $leafs[4]->getTitle());
  292. // remove
  293. $node = $repo->findOneByTitle('Fruits');
  294. $id = $node->getId();
  295. $repo->removeFromTree($node);
  296. $this->assertNull($repo->find($id));
  297. $node = $repo->findOneByTitle('Vegitables');
  298. $id = $node->getId();
  299. $repo->removeFromTree($node);
  300. $this->assertNull($repo->find($id));
  301. $this->em->clear();
  302. $node = $repo->findOneByTitle('Cabbages');
  303. $this->assertEquals(1, $node->getRoot());
  304. $this->assertEquals(1, $node->getParent()->getId());
  305. }
  306. /**
  307. * @test
  308. */
  309. public function shouldRemoveTreeLeafFromTree()
  310. {
  311. $this->populateMore();
  312. $repo = $this->em->getRepository(self::CATEGORY);
  313. $onions = $repo->findOneByTitle('Onions');
  314. $id = $onions->getId();
  315. $repo->removeFromTree($onions);
  316. $this->assertNull($repo->find($id));
  317. $this->em->clear();
  318. $vegies = $repo->findOneByTitle('Vegitables');
  319. $this->assertTrue($repo->verify());
  320. }
  321. /**
  322. * @test
  323. */
  324. public function getRootNodesTest()
  325. {
  326. $repo = $this->em->getRepository(self::CATEGORY);
  327. // Test getRootNodes without custom ordering
  328. $roots = $repo->getRootNodes();
  329. $this->assertEquals(2, count($roots));
  330. $this->assertEquals('Food', $roots[0]->getTitle());
  331. $this->assertEquals('Sports', $roots[1]->getTitle());
  332. // Test getRootNodes with custom ordering
  333. $roots = $repo->getRootNodes('title', 'desc');
  334. $this->assertEquals(2, count($roots));
  335. $this->assertEquals('Sports', $roots[0]->getTitle());
  336. $this->assertEquals('Food', $roots[1]->getTitle());
  337. }
  338. /**
  339. * @test
  340. */
  341. public function changeChildrenIndexTest()
  342. {
  343. $repo = $this->em->getRepository(self::CATEGORY);
  344. $childrenIndex = 'myChildren';
  345. $repo->setChildrenIndex($childrenIndex);
  346. $tree = $repo->childrenHierarchy();
  347. $this->assertInternalType('array', $tree[0][$childrenIndex]);
  348. }
  349. protected function getUsedEntityFixtures()
  350. {
  351. return array(
  352. self::CATEGORY
  353. );
  354. }
  355. private function populateMore()
  356. {
  357. $vegies = $this->em->getRepository(self::CATEGORY)
  358. ->findOneByTitle('Vegitables');
  359. $cabbages = new RootCategory();
  360. $cabbages->setParent($vegies);
  361. $cabbages->setTitle('Cabbages');
  362. $onions = new RootCategory();
  363. $onions->setParent($vegies);
  364. $onions->setTitle('Onions');
  365. $this->em->persist($cabbages);
  366. $this->em->persist($onions);
  367. $this->em->flush();
  368. }
  369. private function populate()
  370. {
  371. $root = new RootCategory();
  372. $root->setTitle("Food");
  373. $root2 = new RootCategory();
  374. $root2->setTitle("Sports");
  375. $child = new RootCategory();
  376. $child->setTitle("Fruits");
  377. $child->setParent($root);
  378. $child2 = new RootCategory();
  379. $child2->setTitle("Vegitables");
  380. $child2->setParent($root);
  381. $childsChild = new RootCategory();
  382. $childsChild->setTitle("Carrots");
  383. $childsChild->setParent($child2);
  384. $potatoes = new RootCategory();
  385. $potatoes->setTitle("Potatoes");
  386. $potatoes->setParent($child2);
  387. $this->em->persist($root);
  388. $this->em->persist($root2);
  389. $this->em->persist($child);
  390. $this->em->persist($child2);
  391. $this->em->persist($childsChild);
  392. $this->em->persist($potatoes);
  393. $this->em->flush();
  394. }
  395. }