NestedTreeRootRepositoryTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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. }
  63. /**
  64. * @test
  65. */
  66. function shouldSupportChildrenHierarchyAsHtml()
  67. {
  68. $repo = $this->em->getRepository(self::CATEGORY);
  69. $food = $repo->findOneByTitle('Food');
  70. $decorate = true;
  71. $defaultHtmlTree = $repo->childrenHierarchy($food, false, compact('decorate'));
  72. $this->assertEquals(
  73. '<ul><li>Fruits</li><li>Vegitables<ul><li>Carrots</li><li>Potatoes</li></ul></li></ul>',
  74. $defaultHtmlTree
  75. );
  76. // custom title
  77. $nodeDecorator = function($node) {
  78. return '<span>'.$node['title'].'</span>';
  79. };
  80. $decoratedHtmlTree = $repo->childrenHierarchy(
  81. $food,
  82. false,
  83. compact('decorate', 'nodeDecorator')
  84. );
  85. $this->assertEquals(
  86. '<ul><li><span>Fruits</span></li><li><span>Vegitables</span><ul><li><span>Carrots</span></li><li><span>Potatoes</span></li></ul></li></ul>',
  87. $decoratedHtmlTree
  88. );
  89. // cli friendly output
  90. $rootOpen = '';
  91. $rootClose = '';
  92. $childOpen = '';
  93. $childClose = '';
  94. $nodeDecorator = function($node) {
  95. return str_repeat('-', $node['level']).$node['title']."\n";
  96. };
  97. $decoratedCliTree = $repo->childrenHierarchy(
  98. $food,
  99. false,
  100. compact('decorate', 'nodeDecorator', 'rootOpen', 'rootClose', 'childOpen', 'childClose')
  101. );
  102. $this->assertEquals(
  103. "-Fruits\n-Vegitables\n--Carrots\n--Potatoes\n",
  104. $decoratedCliTree
  105. );
  106. $rootOpen = function () {return '<ul class="group">';};
  107. $childOpen = function (&$node) {
  108. return '<li class="depth'.$node['level'].'">';
  109. };
  110. $decoratedHtmlTree = $repo->childrenHierarchy(
  111. $food,
  112. false,
  113. compact('decorate', 'rootOpen', 'childOpen')
  114. );
  115. $this->assertEquals(
  116. '<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>',
  117. $decoratedHtmlTree
  118. );
  119. }
  120. /**
  121. * @test
  122. */
  123. function shouldSupportChildrenHierarchyByBuildTreeFunction()
  124. {
  125. $repo = $this->em->getRepository(self::CATEGORY);
  126. $q = $this->em
  127. ->createQueryBuilder()
  128. ->select('node')
  129. ->from(self::CATEGORY, 'node')
  130. ->orderBy('node.root, node.lft', 'ASC')
  131. ->where('node.root = 1')
  132. ->getQuery()
  133. ;
  134. $tree = $repo->buildTree($q->getArrayResult());
  135. $this->assertCount(1, $tree);
  136. $this->assertCount(2, $tree[0]['__children']);
  137. $nodes = array();
  138. $options = array('decorate' => true);
  139. $this->assertEquals('', $repo->buildTree($nodes, $options), 'should give empty string when there are no nodes given');
  140. }
  141. /**
  142. * @test
  143. */
  144. public function shouldRemoveRootNodeFromTree()
  145. {
  146. $repo = $this->em->getRepository(self::CATEGORY);
  147. $this->populateMore();
  148. $food = $repo->findOneByTitle('Food');
  149. $repo->removeFromTree($food);
  150. $this->em->clear();
  151. $food = $repo->findOneByTitle('Food');
  152. $this->assertNull($food);
  153. $node = $repo->findOneByTitle('Fruits');
  154. $this->assertEquals(1, $node->getLeft());
  155. $this->assertEquals(2, $node->getRight());
  156. $this->assertEquals(3, $node->getRoot());
  157. $this->assertNull($node->getParent());
  158. $node = $repo->findOneByTitle('Vegitables');
  159. $this->assertEquals(1, $node->getLeft());
  160. $this->assertEquals(10, $node->getRight());
  161. $this->assertEquals(4, $node->getRoot());
  162. $this->assertNull($node->getParent());
  163. }
  164. /**
  165. * @test
  166. */
  167. public function shouldHandleBasicRepositoryMethods()
  168. {
  169. $repo = $this->em->getRepository(self::CATEGORY);
  170. $carrots = $repo->findOneByTitle('Carrots');
  171. $path = $repo->getPath($carrots);
  172. $this->assertCount(3, $path);
  173. $this->assertEquals('Food', $path[0]->getTitle());
  174. $this->assertEquals('Vegitables', $path[1]->getTitle());
  175. $this->assertEquals('Carrots', $path[2]->getTitle());
  176. $vegies = $repo->findOneByTitle('Vegitables');
  177. $childCount = $repo->childCount($vegies);
  178. $this->assertEquals(2, $childCount);
  179. $food = $repo->findOneByTitle('Food');
  180. $childCount = $repo->childCount($food, true);
  181. $this->assertEquals(2, $childCount);
  182. $childCount = $repo->childCount($food);
  183. $this->assertEquals(4, $childCount);
  184. $childCount = $repo->childCount();
  185. $this->assertEquals(6, $childCount);
  186. $childCount = $repo->childCount(null, true);
  187. $this->assertEquals(2, $childCount);
  188. }
  189. /**
  190. * @test
  191. */
  192. public function shouldHandleAdvancedRepositoryFunctions()
  193. {
  194. $this->populateMore();
  195. $repo = $this->em->getRepository(self::CATEGORY);
  196. // verification
  197. $this->assertTrue($repo->verify());
  198. $dql = 'UPDATE ' . self::CATEGORY . ' node';
  199. $dql .= ' SET node.lft = 1';
  200. $dql .= ' WHERE node.id = 4';
  201. $this->em->createQuery($dql)->getSingleScalarResult();
  202. $this->em->clear(); // must clear cached entities
  203. $errors = $repo->verify();
  204. $this->assertCount(2, $errors);
  205. $this->assertEquals('index [1], duplicate on tree root: 1', $errors[0]);
  206. $this->assertEquals('index [4], missing on tree root: 1', $errors[1]);
  207. $dql = 'UPDATE ' . self::CATEGORY . ' node';
  208. $dql .= ' SET node.lft = 4';
  209. $dql .= ' WHERE node.id = 4';
  210. $this->em->createQuery($dql)->getSingleScalarResult();
  211. //@todo implement
  212. //$this->em->clear();
  213. //$repo->recover();
  214. //$this->em->clear();
  215. //$this->assertTrue($repo->verify());
  216. $this->em->clear();
  217. $onions = $repo->findOneByTitle('Onions');
  218. $this->assertEquals(11, $onions->getLeft());
  219. $this->assertEquals(12, $onions->getRight());
  220. // move up
  221. $repo->moveUp($onions);
  222. $this->assertEquals(9, $onions->getLeft());
  223. $this->assertEquals(10, $onions->getRight());
  224. $repo->moveUp($onions, true);
  225. $this->assertEquals(5, $onions->getLeft());
  226. $this->assertEquals(6, $onions->getRight());
  227. // move down
  228. $repo->moveDown($onions, 2);
  229. $this->assertEquals(9, $onions->getLeft());
  230. $this->assertEquals(10, $onions->getRight());
  231. // reorder
  232. $node = $repo->findOneByTitle('Food');
  233. $repo->reorder($node, 'title', 'ASC', false);
  234. $node = $repo->findOneByTitle('Cabbages');
  235. $this->assertEquals(5, $node->getLeft());
  236. $this->assertEquals(6, $node->getRight());
  237. $node = $repo->findOneByTitle('Carrots');
  238. $this->assertEquals(7, $node->getLeft());
  239. $this->assertEquals(8, $node->getRight());
  240. $node = $repo->findOneByTitle('Onions');
  241. $this->assertEquals(9, $node->getLeft());
  242. $this->assertEquals(10, $node->getRight());
  243. $node = $repo->findOneByTitle('Potatoes');
  244. $this->assertEquals(11, $node->getLeft());
  245. $this->assertEquals(12, $node->getRight());
  246. // leafs
  247. $leafs = $repo->getLeafs($node);
  248. $this->assertCount(5, $leafs);
  249. $this->assertEquals('Fruits', $leafs[0]->getTitle());
  250. $this->assertEquals('Cabbages', $leafs[1]->getTitle());
  251. $this->assertEquals('Carrots', $leafs[2]->getTitle());
  252. $this->assertEquals('Onions', $leafs[3]->getTitle());
  253. $this->assertEquals('Potatoes', $leafs[4]->getTitle());
  254. // remove
  255. $node = $repo->findOneByTitle('Fruits');
  256. $id = $node->getId();
  257. $repo->removeFromTree($node);
  258. $this->assertNull($repo->find($id));
  259. $node = $repo->findOneByTitle('Vegitables');
  260. $id = $node->getId();
  261. $repo->removeFromTree($node);
  262. $this->assertNull($repo->find($id));
  263. $this->em->clear();
  264. $node = $repo->findOneByTitle('Cabbages');
  265. $this->assertEquals(1, $node->getRoot());
  266. $this->assertEquals(1, $node->getParent()->getId());
  267. }
  268. /**
  269. * @test
  270. */
  271. public function shouldRemoveTreeLeafFromTree()
  272. {
  273. $this->populateMore();
  274. $repo = $this->em->getRepository(self::CATEGORY);
  275. $onions = $repo->findOneByTitle('Onions');
  276. $id = $onions->getId();
  277. $repo->removeFromTree($onions);
  278. $this->assertNull($repo->find($id));
  279. $this->em->clear();
  280. $vegies = $repo->findOneByTitle('Vegitables');
  281. $this->assertTrue($repo->verify());
  282. }
  283. /**
  284. * @test
  285. */
  286. public function getRootNodesTest()
  287. {
  288. $repo = $this->em->getRepository(self::CATEGORY);
  289. // Test getRootNodes without custom ordering
  290. $roots = $repo->getRootNodes();
  291. $this->assertEquals(2, count($roots));
  292. $this->assertEquals('Food', $roots[0]->getTitle());
  293. $this->assertEquals('Sports', $roots[1]->getTitle());
  294. // Test getRootNodes with custom ordering
  295. $roots = $repo->getRootNodes('title', 'desc');
  296. $this->assertEquals(2, count($roots));
  297. $this->assertEquals('Sports', $roots[0]->getTitle());
  298. $this->assertEquals('Food', $roots[1]->getTitle());
  299. }
  300. protected function getUsedEntityFixtures()
  301. {
  302. return array(
  303. self::CATEGORY
  304. );
  305. }
  306. private function populateMore()
  307. {
  308. $vegies = $this->em->getRepository(self::CATEGORY)
  309. ->findOneByTitle('Vegitables');
  310. $cabbages = new RootCategory();
  311. $cabbages->setParent($vegies);
  312. $cabbages->setTitle('Cabbages');
  313. $onions = new RootCategory();
  314. $onions->setParent($vegies);
  315. $onions->setTitle('Onions');
  316. $this->em->persist($cabbages);
  317. $this->em->persist($onions);
  318. $this->em->flush();
  319. }
  320. private function populate()
  321. {
  322. $root = new RootCategory();
  323. $root->setTitle("Food");
  324. $root2 = new RootCategory();
  325. $root2->setTitle("Sports");
  326. $child = new RootCategory();
  327. $child->setTitle("Fruits");
  328. $child->setParent($root);
  329. $child2 = new RootCategory();
  330. $child2->setTitle("Vegitables");
  331. $child2->setParent($root);
  332. $childsChild = new RootCategory();
  333. $childsChild->setTitle("Carrots");
  334. $childsChild->setParent($child2);
  335. $potatoes = new RootCategory();
  336. $potatoes->setTitle("Potatoes");
  337. $potatoes->setParent($child2);
  338. $this->em->persist($root);
  339. $this->em->persist($root2);
  340. $this->em->persist($child);
  341. $this->em->persist($child2);
  342. $this->em->persist($childsChild);
  343. $this->em->persist($potatoes);
  344. $this->em->flush();
  345. }
  346. }