NestedTreeRootRepositoryTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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. $childOpen = function (&$node) {
  146. return '<li class="depth'.$node['level'].'">';
  147. };
  148. $decoratedHtmlTree = $repo->childrenHierarchy(
  149. $food,
  150. false,
  151. compact('decorate', 'rootOpen', 'childOpen')
  152. );
  153. $this->assertEquals(
  154. '<ul class="group"><li class="depth1">Fruits</li><li class="depth1">Vegitables<ul class="group"><li class="depth2">Carrots</li><li class="depth2">Potatoes</li></ul></li></ul>',
  155. $decoratedHtmlTree
  156. );
  157. }
  158. /**
  159. * @test
  160. */
  161. function shouldSupportChildrenHierarchyByBuildTreeFunction()
  162. {
  163. $repo = $this->em->getRepository(self::CATEGORY);
  164. $q = $this->em
  165. ->createQueryBuilder()
  166. ->select('node')
  167. ->from(self::CATEGORY, 'node')
  168. ->orderBy('node.root, node.lft', 'ASC')
  169. ->where('node.root = 1')
  170. ->getQuery()
  171. ;
  172. $tree = $repo->buildTree($q->getArrayResult());
  173. $this->assertCount(1, $tree);
  174. $this->assertCount(2, $tree[0]['__children']);
  175. $nodes = array();
  176. $options = array('decorate' => true);
  177. $this->assertEquals('', $repo->buildTree($nodes, $options), 'should give empty string when there are no nodes given');
  178. }
  179. /**
  180. * @test
  181. */
  182. public function shouldRemoveRootNodeFromTree()
  183. {
  184. $repo = $this->em->getRepository(self::CATEGORY);
  185. $this->populateMore();
  186. $food = $repo->findOneByTitle('Food');
  187. $repo->removeFromTree($food);
  188. $this->em->clear();
  189. $food = $repo->findOneByTitle('Food');
  190. $this->assertNull($food);
  191. $node = $repo->findOneByTitle('Fruits');
  192. $this->assertEquals(1, $node->getLeft());
  193. $this->assertEquals(2, $node->getRight());
  194. $this->assertEquals(3, $node->getRoot());
  195. $this->assertNull($node->getParent());
  196. $node = $repo->findOneByTitle('Vegitables');
  197. $this->assertEquals(1, $node->getLeft());
  198. $this->assertEquals(10, $node->getRight());
  199. $this->assertEquals(4, $node->getRoot());
  200. $this->assertNull($node->getParent());
  201. }
  202. /**
  203. * @test
  204. */
  205. public function shouldHandleBasicRepositoryMethods()
  206. {
  207. $repo = $this->em->getRepository(self::CATEGORY);
  208. $carrots = $repo->findOneByTitle('Carrots');
  209. $path = $repo->getPath($carrots);
  210. $this->assertCount(3, $path);
  211. $this->assertEquals('Food', $path[0]->getTitle());
  212. $this->assertEquals('Vegitables', $path[1]->getTitle());
  213. $this->assertEquals('Carrots', $path[2]->getTitle());
  214. $vegies = $repo->findOneByTitle('Vegitables');
  215. $childCount = $repo->childCount($vegies);
  216. $this->assertEquals(2, $childCount);
  217. $food = $repo->findOneByTitle('Food');
  218. $childCount = $repo->childCount($food, true);
  219. $this->assertEquals(2, $childCount);
  220. $childCount = $repo->childCount($food);
  221. $this->assertEquals(4, $childCount);
  222. $childCount = $repo->childCount();
  223. $this->assertEquals(6, $childCount);
  224. $childCount = $repo->childCount(null, true);
  225. $this->assertEquals(2, $childCount);
  226. }
  227. /**
  228. * @test
  229. */
  230. public function shouldHandleAdvancedRepositoryFunctions()
  231. {
  232. $this->populateMore();
  233. $repo = $this->em->getRepository(self::CATEGORY);
  234. // verification
  235. $this->assertTrue($repo->verify());
  236. $dql = 'UPDATE ' . self::CATEGORY . ' node';
  237. $dql .= ' SET node.lft = 1';
  238. $dql .= ' WHERE node.id = 4';
  239. $this->em->createQuery($dql)->getSingleScalarResult();
  240. $this->em->clear(); // must clear cached entities
  241. $errors = $repo->verify();
  242. $this->assertCount(2, $errors);
  243. $this->assertEquals('index [1], duplicate on tree root: 1', $errors[0]);
  244. $this->assertEquals('index [4], missing on tree root: 1', $errors[1]);
  245. $dql = 'UPDATE ' . self::CATEGORY . ' node';
  246. $dql .= ' SET node.lft = 4';
  247. $dql .= ' WHERE node.id = 4';
  248. $this->em->createQuery($dql)->getSingleScalarResult();
  249. //@todo implement
  250. //$this->em->clear();
  251. //$repo->recover();
  252. //$this->em->clear();
  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. protected function getUsedEntityFixtures()
  339. {
  340. return array(
  341. self::CATEGORY
  342. );
  343. }
  344. private function populateMore()
  345. {
  346. $vegies = $this->em->getRepository(self::CATEGORY)
  347. ->findOneByTitle('Vegitables');
  348. $cabbages = new RootCategory();
  349. $cabbages->setParent($vegies);
  350. $cabbages->setTitle('Cabbages');
  351. $onions = new RootCategory();
  352. $onions->setParent($vegies);
  353. $onions->setTitle('Onions');
  354. $this->em->persist($cabbages);
  355. $this->em->persist($onions);
  356. $this->em->flush();
  357. }
  358. private function populate()
  359. {
  360. $root = new RootCategory();
  361. $root->setTitle("Food");
  362. $root2 = new RootCategory();
  363. $root2->setTitle("Sports");
  364. $child = new RootCategory();
  365. $child->setTitle("Fruits");
  366. $child->setParent($root);
  367. $child2 = new RootCategory();
  368. $child2->setTitle("Vegitables");
  369. $child2->setParent($root);
  370. $childsChild = new RootCategory();
  371. $childsChild->setTitle("Carrots");
  372. $childsChild->setParent($child2);
  373. $potatoes = new RootCategory();
  374. $potatoes->setTitle("Potatoes");
  375. $potatoes->setParent($child2);
  376. $this->em->persist($root);
  377. $this->em->persist($root2);
  378. $this->em->persist($child);
  379. $this->em->persist($child2);
  380. $this->em->persist($childsChild);
  381. $this->em->persist($potatoes);
  382. $this->em->flush();
  383. }
  384. }