NestedTreeRootRepositoryTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. * @test
  28. */
  29. function shouldSupportChildrenHierarchyAsArray()
  30. {
  31. $repo = $this->em->getRepository(self::CATEGORY);
  32. $result = $repo->childrenHierarchy();
  33. $this->assertEquals(2, count($result));
  34. $this->assertTrue(isset($result[0]['__children'][0]['__children']));
  35. $vegies = $repo->findOneByTitle('Vegitables');
  36. $result = $repo->childrenHierarchy($vegies);
  37. $this->assertEquals(2, count($result));
  38. $this->assertEquals(0, count($result[0]['__children']));
  39. }
  40. /**
  41. * @test
  42. */
  43. function shouldSupportChildrenHierarchyAsHtml()
  44. {
  45. $repo = $this->em->getRepository(self::CATEGORY);
  46. $food = $repo->findOneByTitle('Food');
  47. $decorate = true;
  48. $defaultHtmlTree = $repo->childrenHierarchy($food, false, compact('decorate'));
  49. $this->assertEquals(
  50. '<ul><li>Fruits</li><li>Vegitables<ul><li>Carrots</li><li>Potatoes</li></ul></li></ul>',
  51. $defaultHtmlTree
  52. );
  53. // custom title
  54. $nodeDecorator = function($node) {
  55. return '<span>'.$node['title'].'</span>';
  56. };
  57. $decoratedHtmlTree = $repo->childrenHierarchy(
  58. $food,
  59. false,
  60. compact('decorate', 'nodeDecorator')
  61. );
  62. $this->assertEquals(
  63. '<ul><li><span>Fruits</span></li><li><span>Vegitables</span><ul><li><span>Carrots</span></li><li><span>Potatoes</span></li></ul></li></ul>',
  64. $decoratedHtmlTree
  65. );
  66. // cli friendly output
  67. $rootOpen = '';
  68. $rootClose = '';
  69. $childOpen = '';
  70. $childClose = '';
  71. $nodeDecorator = function($node) {
  72. return str_repeat('-', $node['level']).$node['title'].PHP_EOL;
  73. };
  74. $decoratedCliTree = $repo->childrenHierarchy(
  75. $food,
  76. false,
  77. compact('decorate', 'nodeDecorator', 'rootOpen', 'rootClose', 'childOpen', 'childClose')
  78. );
  79. $this->assertEquals(
  80. "-Fruits\n-Vegitables\n--Carrots\n--Potatoes\n",
  81. $decoratedCliTree
  82. );
  83. $rootOpen = function () {return '<ul class="group">';};
  84. $childOpen = function (&$node) {
  85. return '<li class="depth'.$node['level'].'">';
  86. };
  87. $decoratedHtmlTree = $repo->childrenHierarchy(
  88. $food,
  89. false,
  90. compact('decorate', 'rootOpen', 'childOpen')
  91. );
  92. $this->assertEquals(
  93. '<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>',
  94. $decoratedHtmlTree
  95. );
  96. }
  97. /**
  98. * @test
  99. */
  100. function shouldSupportChildrenHierarchyByBuildTreeFunction()
  101. {
  102. $repo = $this->em->getRepository(self::CATEGORY);
  103. $q = $this->em
  104. ->createQueryBuilder()
  105. ->select('node')
  106. ->from(self::CATEGORY, 'node')
  107. ->orderBy('node.root, node.lft', 'ASC')
  108. ->where('node.root = 1')
  109. ->getQuery()
  110. ;
  111. $tree = $repo->buildTree($q->getArrayResult());
  112. $this->assertEquals(1, count($tree));
  113. $this->assertEquals(2, count($tree[0]['__children']));
  114. }
  115. public function testRootRemoval()
  116. {
  117. $repo = $this->em->getRepository(self::CATEGORY);
  118. $this->populateMore();
  119. $food = $repo->findOneByTitle('Food');
  120. $repo->removeFromTree($food);
  121. $this->em->clear();
  122. $food = $repo->findOneByTitle('Food');
  123. $this->assertTrue(is_null($food));
  124. $node = $repo->findOneByTitle('Fruits');
  125. $this->assertEquals(1, $node->getLeft());
  126. $this->assertEquals(2, $node->getRight());
  127. $this->assertEquals(3, $node->getRoot());
  128. $this->assertTrue(is_null($node->getParent()));
  129. $node = $repo->findOneByTitle('Vegitables');
  130. $this->assertEquals(1, $node->getLeft());
  131. $this->assertEquals(10, $node->getRight());
  132. $this->assertEquals(4, $node->getRoot());
  133. $this->assertTrue(is_null($node->getParent()));
  134. }
  135. public function testRepository()
  136. {
  137. $repo = $this->em->getRepository(self::CATEGORY);
  138. $carrots = $repo->findOneByTitle('Carrots');
  139. $path = $repo->getPath($carrots);
  140. $this->assertEquals(3, count($path));
  141. $this->assertEquals('Food', $path[0]->getTitle());
  142. $this->assertEquals('Vegitables', $path[1]->getTitle());
  143. $this->assertEquals('Carrots', $path[2]->getTitle());
  144. $vegies = $repo->findOneByTitle('Vegitables');
  145. $childCount = $repo->childCount($vegies);
  146. $this->assertEquals(2, $childCount);
  147. $food = $repo->findOneByTitle('Food');
  148. $childCount = $repo->childCount($food, true);
  149. $this->assertEquals(2, $childCount);
  150. $childCount = $repo->childCount($food);
  151. $this->assertEquals(4, $childCount);
  152. $childCount = $repo->childCount();
  153. $this->assertEquals(6, $childCount);
  154. }
  155. public function testAdvancedRepositoryFunctions()
  156. {
  157. $this->populateMore();
  158. $repo = $this->em->getRepository(self::CATEGORY);
  159. // verification
  160. $this->assertTrue($repo->verify());
  161. $dql = 'UPDATE ' . self::CATEGORY . ' node';
  162. $dql .= ' SET node.lft = 1';
  163. $dql .= ' WHERE node.id = 4';
  164. $this->em->createQuery($dql)->getSingleScalarResult();
  165. $this->em->clear(); // must clear cached entities
  166. $errors = $repo->verify();
  167. $this->assertEquals(2, count($errors));
  168. $this->assertEquals('index [1], duplicate on tree root: 1', $errors[0]);
  169. $this->assertEquals('index [4], missing on tree root: 1', $errors[1]);
  170. $dql = 'UPDATE ' . self::CATEGORY . ' node';
  171. $dql .= ' SET node.lft = 4';
  172. $dql .= ' WHERE node.id = 4';
  173. $this->em->createQuery($dql)->getSingleScalarResult();
  174. //@todo implement
  175. //$this->em->clear();
  176. //$repo->recover();
  177. //$this->em->clear();
  178. //$this->assertTrue($repo->verify());
  179. $this->em->clear();
  180. $onions = $repo->findOneByTitle('Onions');
  181. $this->assertEquals(11, $onions->getLeft());
  182. $this->assertEquals(12, $onions->getRight());
  183. // move up
  184. $repo->moveUp($onions);
  185. $this->assertEquals(9, $onions->getLeft());
  186. $this->assertEquals(10, $onions->getRight());
  187. $repo->moveUp($onions, true);
  188. $this->assertEquals(5, $onions->getLeft());
  189. $this->assertEquals(6, $onions->getRight());
  190. // move down
  191. $repo->moveDown($onions, 2);
  192. $this->assertEquals(9, $onions->getLeft());
  193. $this->assertEquals(10, $onions->getRight());
  194. // reorder
  195. $node = $repo->findOneByTitle('Food');
  196. $repo->reorder($node, 'title', 'ASC', false);
  197. $node = $repo->findOneByTitle('Cabbages');
  198. $this->assertEquals(5, $node->getLeft());
  199. $this->assertEquals(6, $node->getRight());
  200. $node = $repo->findOneByTitle('Carrots');
  201. $this->assertEquals(7, $node->getLeft());
  202. $this->assertEquals(8, $node->getRight());
  203. $node = $repo->findOneByTitle('Onions');
  204. $this->assertEquals(9, $node->getLeft());
  205. $this->assertEquals(10, $node->getRight());
  206. $node = $repo->findOneByTitle('Potatoes');
  207. $this->assertEquals(11, $node->getLeft());
  208. $this->assertEquals(12, $node->getRight());
  209. // leafs
  210. $leafs = $repo->getLeafs($node);
  211. $this->assertEquals(5, count($leafs));
  212. $this->assertEquals('Fruits', $leafs[0]->getTitle());
  213. $this->assertEquals('Cabbages', $leafs[1]->getTitle());
  214. $this->assertEquals('Carrots', $leafs[2]->getTitle());
  215. $this->assertEquals('Onions', $leafs[3]->getTitle());
  216. $this->assertEquals('Potatoes', $leafs[4]->getTitle());
  217. // remove
  218. $node = $repo->findOneByTitle('Fruits');
  219. $id = $node->getId();
  220. $repo->removeFromTree($node);
  221. $this->assertTrue(is_null($repo->find($id)));
  222. $node = $repo->findOneByTitle('Vegitables');
  223. $id = $node->getId();
  224. $repo->removeFromTree($node);
  225. $this->assertTrue(is_null($repo->find($id)));
  226. $this->em->clear();
  227. $node = $repo->findOneByTitle('Cabbages');
  228. $this->assertEquals(1, $node->getRoot());
  229. $this->assertEquals(1, $node->getParent()->getId());
  230. }
  231. public function testRemoveFromTreeLeaf()
  232. {
  233. $this->populateMore();
  234. $repo = $this->em->getRepository(self::CATEGORY);
  235. $onions = $repo->findOneByTitle('Onions');
  236. $id = $onions->getId();
  237. $repo->removeFromTree($onions);
  238. $this->assertTrue(is_null($repo->find($id)));
  239. $this->em->clear();
  240. $vegies = $repo->findOneByTitle('Vegitables');
  241. $this->assertTrue($repo->verify());
  242. }
  243. protected function getUsedEntityFixtures()
  244. {
  245. return array(
  246. self::CATEGORY
  247. );
  248. }
  249. private function populateMore()
  250. {
  251. $vegies = $this->em->getRepository(self::CATEGORY)
  252. ->findOneByTitle('Vegitables');
  253. $cabbages = new RootCategory();
  254. $cabbages->setParent($vegies);
  255. $cabbages->setTitle('Cabbages');
  256. $onions = new RootCategory();
  257. $onions->setParent($vegies);
  258. $onions->setTitle('Onions');
  259. $this->em->persist($cabbages);
  260. $this->em->persist($onions);
  261. $this->em->flush();
  262. }
  263. private function populate()
  264. {
  265. $root = new RootCategory();
  266. $root->setTitle("Food");
  267. $root2 = new RootCategory();
  268. $root2->setTitle("Sports");
  269. $child = new RootCategory();
  270. $child->setTitle("Fruits");
  271. $child->setParent($root);
  272. $child2 = new RootCategory();
  273. $child2->setTitle("Vegitables");
  274. $child2->setParent($root);
  275. $childsChild = new RootCategory();
  276. $childsChild->setTitle("Carrots");
  277. $childsChild->setParent($child2);
  278. $potatoes = new RootCategory();
  279. $potatoes->setTitle("Potatoes");
  280. $potatoes->setParent($child2);
  281. $this->em->persist($root);
  282. $this->em->persist($root2);
  283. $this->em->persist($child);
  284. $this->em->persist($child2);
  285. $this->em->persist($childsChild);
  286. $this->em->persist($potatoes);
  287. $this->em->flush();
  288. }
  289. }