NestedTreeRootRepositoryTest.php 16KB

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