NestedTreeRootRepositoryTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. }
  84. /**
  85. * @test
  86. */
  87. function shouldSupportChildrenHierarchyByBuildTreeFunction()
  88. {
  89. $repo = $this->em->getRepository(self::CATEGORY);
  90. $q = $this->em
  91. ->createQueryBuilder()
  92. ->select('node')
  93. ->from(self::CATEGORY, 'node')
  94. ->orderBy('node.root, node.lft', 'ASC')
  95. ->where('node.root = 1')
  96. ->getQuery()
  97. ;
  98. $tree = $repo->buildTree($q->getArrayResult());
  99. $this->assertEquals(1, count($tree));
  100. $this->assertEquals(2, count($tree[0]['__children']));
  101. }
  102. public function testRootRemoval()
  103. {
  104. $repo = $this->em->getRepository(self::CATEGORY);
  105. $this->populateMore();
  106. $food = $repo->findOneByTitle('Food');
  107. $repo->removeFromTree($food);
  108. $this->em->clear();
  109. $food = $repo->findOneByTitle('Food');
  110. $this->assertTrue(is_null($food));
  111. $node = $repo->findOneByTitle('Fruits');
  112. $this->assertEquals(1, $node->getLeft());
  113. $this->assertEquals(2, $node->getRight());
  114. $this->assertEquals(3, $node->getRoot());
  115. $this->assertTrue(is_null($node->getParent()));
  116. $node = $repo->findOneByTitle('Vegitables');
  117. $this->assertEquals(1, $node->getLeft());
  118. $this->assertEquals(10, $node->getRight());
  119. $this->assertEquals(4, $node->getRoot());
  120. $this->assertTrue(is_null($node->getParent()));
  121. }
  122. public function testRepository()
  123. {
  124. $repo = $this->em->getRepository(self::CATEGORY);
  125. $carrots = $repo->findOneByTitle('Carrots');
  126. $path = $repo->getPath($carrots);
  127. $this->assertEquals(3, count($path));
  128. $this->assertEquals('Food', $path[0]->getTitle());
  129. $this->assertEquals('Vegitables', $path[1]->getTitle());
  130. $this->assertEquals('Carrots', $path[2]->getTitle());
  131. $vegies = $repo->findOneByTitle('Vegitables');
  132. $childCount = $repo->childCount($vegies);
  133. $this->assertEquals(2, $childCount);
  134. $food = $repo->findOneByTitle('Food');
  135. $childCount = $repo->childCount($food, true);
  136. $this->assertEquals(2, $childCount);
  137. $childCount = $repo->childCount($food);
  138. $this->assertEquals(4, $childCount);
  139. $childCount = $repo->childCount();
  140. $this->assertEquals(6, $childCount);
  141. }
  142. public function testAdvancedRepositoryFunctions()
  143. {
  144. $this->populateMore();
  145. $repo = $this->em->getRepository(self::CATEGORY);
  146. // verification
  147. $this->assertTrue($repo->verify());
  148. $dql = 'UPDATE ' . self::CATEGORY . ' node';
  149. $dql .= ' SET node.lft = 1';
  150. $dql .= ' WHERE node.id = 4';
  151. $this->em->createQuery($dql)->getSingleScalarResult();
  152. $this->em->clear(); // must clear cached entities
  153. $errors = $repo->verify();
  154. $this->assertEquals(2, count($errors));
  155. $this->assertEquals('index [1], duplicate on tree root: 1', $errors[0]);
  156. $this->assertEquals('index [4], missing on tree root: 1', $errors[1]);
  157. $dql = 'UPDATE ' . self::CATEGORY . ' node';
  158. $dql .= ' SET node.lft = 4';
  159. $dql .= ' WHERE node.id = 4';
  160. $this->em->createQuery($dql)->getSingleScalarResult();
  161. //@todo implement
  162. //$this->em->clear();
  163. //$repo->recover();
  164. //$this->em->clear();
  165. //$this->assertTrue($repo->verify());
  166. $this->em->clear();
  167. $onions = $repo->findOneByTitle('Onions');
  168. $this->assertEquals(11, $onions->getLeft());
  169. $this->assertEquals(12, $onions->getRight());
  170. // move up
  171. $repo->moveUp($onions);
  172. $this->assertEquals(9, $onions->getLeft());
  173. $this->assertEquals(10, $onions->getRight());
  174. $repo->moveUp($onions, true);
  175. $this->assertEquals(5, $onions->getLeft());
  176. $this->assertEquals(6, $onions->getRight());
  177. // move down
  178. $repo->moveDown($onions, 2);
  179. $this->assertEquals(9, $onions->getLeft());
  180. $this->assertEquals(10, $onions->getRight());
  181. // reorder
  182. $node = $repo->findOneByTitle('Food');
  183. $repo->reorder($node, 'title', 'ASC', false);
  184. $node = $repo->findOneByTitle('Cabbages');
  185. $this->assertEquals(5, $node->getLeft());
  186. $this->assertEquals(6, $node->getRight());
  187. $node = $repo->findOneByTitle('Carrots');
  188. $this->assertEquals(7, $node->getLeft());
  189. $this->assertEquals(8, $node->getRight());
  190. $node = $repo->findOneByTitle('Onions');
  191. $this->assertEquals(9, $node->getLeft());
  192. $this->assertEquals(10, $node->getRight());
  193. $node = $repo->findOneByTitle('Potatoes');
  194. $this->assertEquals(11, $node->getLeft());
  195. $this->assertEquals(12, $node->getRight());
  196. // leafs
  197. $leafs = $repo->getLeafs($node);
  198. $this->assertEquals(5, count($leafs));
  199. $this->assertEquals('Fruits', $leafs[0]->getTitle());
  200. $this->assertEquals('Cabbages', $leafs[1]->getTitle());
  201. $this->assertEquals('Carrots', $leafs[2]->getTitle());
  202. $this->assertEquals('Onions', $leafs[3]->getTitle());
  203. $this->assertEquals('Potatoes', $leafs[4]->getTitle());
  204. // remove
  205. $node = $repo->findOneByTitle('Fruits');
  206. $id = $node->getId();
  207. $repo->removeFromTree($node);
  208. $this->assertTrue(is_null($repo->find($id)));
  209. $node = $repo->findOneByTitle('Vegitables');
  210. $id = $node->getId();
  211. $repo->removeFromTree($node);
  212. $this->assertTrue(is_null($repo->find($id)));
  213. $this->em->clear();
  214. $node = $repo->findOneByTitle('Cabbages');
  215. $this->assertEquals(1, $node->getRoot());
  216. $this->assertEquals(1, $node->getParent()->getId());
  217. }
  218. public function testRemoveFromTreeLeaf()
  219. {
  220. $this->populateMore();
  221. $repo = $this->em->getRepository(self::CATEGORY);
  222. $onions = $repo->findOneByTitle('Onions');
  223. $id = $onions->getId();
  224. $repo->removeFromTree($onions);
  225. $this->assertTrue(is_null($repo->find($id)));
  226. $this->em->clear();
  227. $vegies = $repo->findOneByTitle('Vegitables');
  228. $this->assertTrue($repo->verify());
  229. }
  230. protected function getUsedEntityFixtures()
  231. {
  232. return array(
  233. self::CATEGORY
  234. );
  235. }
  236. private function populateMore()
  237. {
  238. $vegies = $this->em->getRepository(self::CATEGORY)
  239. ->findOneByTitle('Vegitables');
  240. $cabbages = new RootCategory();
  241. $cabbages->setParent($vegies);
  242. $cabbages->setTitle('Cabbages');
  243. $onions = new RootCategory();
  244. $onions->setParent($vegies);
  245. $onions->setTitle('Onions');
  246. $this->em->persist($cabbages);
  247. $this->em->persist($onions);
  248. $this->em->flush();
  249. }
  250. private function populate()
  251. {
  252. $root = new RootCategory();
  253. $root->setTitle("Food");
  254. $root2 = new RootCategory();
  255. $root2->setTitle("Sports");
  256. $child = new RootCategory();
  257. $child->setTitle("Fruits");
  258. $child->setParent($root);
  259. $child2 = new RootCategory();
  260. $child2->setTitle("Vegitables");
  261. $child2->setParent($root);
  262. $childsChild = new RootCategory();
  263. $childsChild->setTitle("Carrots");
  264. $childsChild->setParent($child2);
  265. $potatoes = new RootCategory();
  266. $potatoes->setTitle("Potatoes");
  267. $potatoes->setParent($child2);
  268. $this->em->persist($root);
  269. $this->em->persist($root2);
  270. $this->em->persist($child);
  271. $this->em->persist($child2);
  272. $this->em->persist($childsChild);
  273. $this->em->persist($potatoes);
  274. $this->em->flush();
  275. }
  276. }