NestedTreeRootRepositoryTest.php 12 KB

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