MaterializedPathORMRepositoryTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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 Gustavo Falco <comfortablynumb84@gmail.com>
  10. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  11. * @link http://www.gediminasm.org
  12. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  13. */
  14. class MaterializedPathORMRepositoryTest extends BaseTestCaseORM
  15. {
  16. const CATEGORY = "Tree\\Fixture\\MPCategory";
  17. /** @var $this->repo \Gedmo\Tree\Entity\Repository\MaterializedPathRepository */
  18. protected $repo;
  19. protected function setUp()
  20. {
  21. parent::setUp();
  22. $this->listener = new TreeListener;
  23. $evm = new EventManager;
  24. $evm->addEventSubscriber($this->listener);
  25. $this->getMockSqliteEntityManager($evm);
  26. $meta = $this->em->getClassMetadata(self::CATEGORY);
  27. $this->config = $this->listener->getConfiguration($this->em, $meta->name);
  28. $this->populate();
  29. $this->repo = $this->em->getRepository(self::CATEGORY);
  30. }
  31. /**
  32. * @test
  33. */
  34. function getRootNodes()
  35. {
  36. $result = $this->repo->getRootNodes('title');
  37. $this->assertCount(3, $result);
  38. $this->assertEquals('Drinks', $result[0]->getTitle());
  39. $this->assertEquals('Food', $result[1]->getTitle());
  40. $this->assertEquals('Sports', $result[2]->getTitle());
  41. }
  42. /**
  43. * @test
  44. */
  45. function getChildren()
  46. {
  47. $root = $this->repo->findOneByTitle('Food');
  48. // Get all children from the root, NOT including it
  49. $result = $this->repo->getChildren($root, false, 'title');
  50. $this->assertCount(4, $result);
  51. $this->assertEquals('Carrots', $result[0]->getTitle());
  52. $this->assertEquals('Fruits', $result[1]->getTitle());
  53. $this->assertEquals('Potatoes', $result[2]->getTitle());
  54. $this->assertEquals('Vegitables', $result[3]->getTitle());
  55. // Get all children from the root, including it
  56. $result = $this->repo->getChildren($root, false, 'title', 'asc', true);
  57. $this->assertCount(5, $result);
  58. $this->assertEquals('Carrots', $result[0]->getTitle());
  59. $this->assertEquals('Food', $result[1]->getTitle());
  60. $this->assertEquals('Fruits', $result[2]->getTitle());
  61. $this->assertEquals('Potatoes', $result[3]->getTitle());
  62. $this->assertEquals('Vegitables', $result[4]->getTitle());
  63. // Get direct children from the root, NOT including it
  64. $result = $this->repo->getChildren($root, true, 'title', 'asc');
  65. $this->assertCount(2, $result);
  66. $this->assertEquals('Fruits', $result[0]->getTitle());
  67. $this->assertEquals('Vegitables', $result[1]->getTitle());
  68. // Get direct children from the root, including it
  69. $result = $this->repo->getChildren($root, true, 'title', 'asc', true);
  70. $this->assertCount(3, $result);
  71. $this->assertEquals('Food', $result[0]->getTitle());
  72. $this->assertEquals('Fruits', $result[1]->getTitle());
  73. $this->assertEquals('Vegitables', $result[2]->getTitle());
  74. // Get ALL nodes
  75. $result = $this->repo->getChildren(null, false, 'title');
  76. $this->assertCount(9, $result);
  77. $this->assertEquals('Best Whisky', $result[0]->getTitle());
  78. $this->assertEquals('Carrots', $result[1]->getTitle());
  79. $this->assertEquals('Drinks', $result[2]->getTitle());
  80. $this->assertEquals('Food', $result[3]->getTitle());
  81. $this->assertEquals('Fruits', $result[4]->getTitle());
  82. $this->assertEquals('Potatoes', $result[5]->getTitle());
  83. $this->assertEquals('Sports', $result[6]->getTitle());
  84. $this->assertEquals('Vegitables', $result[7]->getTitle());
  85. $this->assertEquals('Whisky', $result[8]->getTitle());
  86. // Get ALL root nodes
  87. $result = $this->repo->getChildren(null, true, 'title');
  88. $this->assertCount(3, $result);
  89. $this->assertEquals('Drinks', $result[0]->getTitle());
  90. $this->assertEquals('Food', $result[1]->getTitle());
  91. $this->assertEquals('Sports', $result[2]->getTitle());
  92. }
  93. /**
  94. * @test
  95. */
  96. function getTree()
  97. {
  98. $tree = $this->repo->getTree();
  99. $this->assertCount(9, $tree);
  100. $this->assertEquals('Drinks', $tree[0]->getTitle());
  101. $this->assertEquals('Whisky', $tree[1]->getTitle());
  102. $this->assertEquals('Best Whisky', $tree[2]->getTitle());
  103. $this->assertEquals('Food', $tree[3]->getTitle());
  104. $this->assertEquals('Fruits', $tree[4]->getTitle());
  105. $this->assertEquals('Vegitables', $tree[5]->getTitle());
  106. $this->assertEquals('Carrots', $tree[6]->getTitle());
  107. $this->assertEquals('Potatoes', $tree[7]->getTitle());
  108. $this->assertEquals('Sports', $tree[8]->getTitle());
  109. // Get tree from a specific root node
  110. $roots = $this->repo->getRootNodes();
  111. $tree = $this->repo->getTree($roots[0]);
  112. $this->assertCount(3, $tree);
  113. $this->assertEquals('Drinks', $tree[0]->getTitle());
  114. $this->assertEquals('Whisky', $tree[1]->getTitle());
  115. $this->assertEquals('Best Whisky', $tree[2]->getTitle());
  116. }
  117. public function testChildrenHierarchyMethod()
  118. {
  119. $tree = $this->repo->childrenHierarchy();
  120. $this->assertEquals('Drinks', $tree[0]['title']);
  121. $this->assertEquals('Whisky', $tree[0]['__children'][0]['title']);
  122. $this->assertEquals('Best Whisky', $tree[0]['__children'][0]['__children'][0]['title']);
  123. $vegitablesChildren = $tree[1]['__children'][1]['__children'];
  124. $this->assertEquals('Food', $tree[1]['title']);
  125. $this->assertEquals('Fruits', $tree[1]['__children'][0]['title']);
  126. $this->assertEquals('Vegitables', $tree[1]['__children'][1]['title']);
  127. $this->assertEquals('Carrots', $vegitablesChildren[0]['title']);
  128. $this->assertEquals('Potatoes', $vegitablesChildren[1]['title']);
  129. $this->assertEquals('Sports', $tree[2]['title']);
  130. // Tree of one specific root, without the root node
  131. $roots = $this->repo->getRootNodes();
  132. $tree = $this->repo->childrenHierarchy($roots[0]);
  133. $this->assertEquals('Whisky', $tree[0]['title']);
  134. $this->assertEquals('Best Whisky', $tree[0]['__children'][0]['title']);
  135. // Tree of one specific root, with the root node
  136. $tree = $this->repo->childrenHierarchy($roots[0], false, array(), true);
  137. $this->assertEquals('Drinks', $tree[0]['title']);
  138. $this->assertEquals('Whisky', $tree[0]['__children'][0]['title']);
  139. $this->assertEquals('Best Whisky', $tree[0]['__children'][0]['__children'][0]['title']);
  140. // Tree of one specific root only with direct children, without the root node
  141. $roots = $this->repo->getRootNodes();
  142. $tree = $this->repo->childrenHierarchy($roots[1], true);
  143. $this->assertEquals(2, count($tree));
  144. $this->assertEquals('Fruits', $tree[0]['title']);
  145. $this->assertEquals('Vegitables', $tree[1]['title']);
  146. // Tree of one specific root only with direct children, with the root node
  147. $tree = $this->repo->childrenHierarchy($roots[1], true, array(), true);
  148. $this->assertEquals(1, count($tree));
  149. $this->assertEquals(2, count($tree[0]['__children']));
  150. $this->assertEquals('Food', $tree[0]['title']);
  151. $this->assertEquals('Fruits', $tree[0]['__children'][0]['title']);
  152. $this->assertEquals('Vegitables', $tree[0]['__children'][1]['title']);
  153. // HTML Tree of one specific root, without the root node
  154. $roots = $this->repo->getRootNodes();
  155. $tree = $this->repo->childrenHierarchy($roots[0], false, array('decorate' => true), false);
  156. $this->assertEquals('<ul><li>Whisky<ul><li>Best Whisky</li></ul></li></ul>', $tree);
  157. // HTML Tree of one specific root, with the root node
  158. $roots = $this->repo->getRootNodes();
  159. $tree = $this->repo->childrenHierarchy($roots[0], false, array('decorate' => true), true);
  160. $this->assertEquals('<ul><li>Drinks<ul><li>Whisky<ul><li>Best Whisky</li></ul></li></ul></li></ul>', $tree);
  161. }
  162. public function testChildCount()
  163. {
  164. // Count all
  165. $count = $this->repo->childCount();
  166. $this->assertEquals(9, $count);
  167. // Count all, but only direct ones
  168. $count = $this->repo->childCount(null, true);
  169. $this->assertEquals(3, $count);
  170. // Count food children
  171. $food = $this->repo->findOneByTitle('Food');
  172. $count = $this->repo->childCount($food);
  173. $this->assertEquals(4, $count);
  174. // Count food children, but only direct ones
  175. $count = $this->repo->childCount($food, true);
  176. $this->assertEquals(2, $count);
  177. }
  178. /**
  179. * @expectedException \Gedmo\Exception\InvalidArgumentException
  180. */
  181. public function testChildCount_ifAnObjectIsPassedWhichIsNotAnInstanceOfTheEntityClassThrowException()
  182. {
  183. $this->repo->childCount(new \DateTime());
  184. }
  185. /**
  186. * @expectedException \Gedmo\Exception\InvalidArgumentException
  187. */
  188. public function testChildCount_ifAnObjectIsPassedIsAnInstanceOfTheEntityClassButIsNotHandledByUnitOfWorkThrowException()
  189. {
  190. $this->repo->childCount($this->createCategory());
  191. }
  192. public function test_issue458()
  193. {
  194. $this->em->clear();
  195. $node = $this->repo->findOneByTitle('Fruits');
  196. $newNode = $this->createCategory();
  197. $parent = $node->getParent();
  198. $this->assertFalse($parent->__isInitialized());
  199. $newNode->setTitle('New Node');
  200. $newNode->setParent($parent);
  201. $this->em->persist($newNode);
  202. $this->em->flush();
  203. $this->assertRegexp('/Food\-\d+,New\sNode\-\d+/', $newNode->getPath());
  204. $this->assertEquals(2, $newNode->getLevel());
  205. }
  206. public function test_changeChildrenIndex()
  207. {
  208. $childrenIndex = 'myChildren';
  209. $this->repo->setChildrenIndex($childrenIndex);
  210. $tree = $this->repo->childrenHierarchy();
  211. $this->assertInternalType('array', $tree[0][$childrenIndex]);
  212. }
  213. protected function getUsedEntityFixtures()
  214. {
  215. return array(
  216. self::CATEGORY
  217. );
  218. }
  219. public function createCategory()
  220. {
  221. $class = self::CATEGORY;
  222. return new $class;
  223. }
  224. private function populate()
  225. {
  226. $root = $this->createCategory();
  227. $root->setTitle("Food");
  228. $root2 = $this->createCategory();
  229. $root2->setTitle("Sports");
  230. $child = $this->createCategory();
  231. $child->setTitle("Fruits");
  232. $child->setParent($root);
  233. $child2 = $this->createCategory();
  234. $child2->setTitle("Vegitables");
  235. $child2->setParent($root);
  236. $childsChild = $this->createCategory();
  237. $childsChild->setTitle("Carrots");
  238. $childsChild->setParent($child2);
  239. $potatoes = $this->createCategory();
  240. $potatoes->setTitle("Potatoes");
  241. $potatoes->setParent($child2);
  242. $drinks = $this->createCategory();
  243. $drinks->setTitle('Drinks');
  244. $whisky = $this->createCategory();
  245. $whisky->setTitle('Whisky');
  246. $whisky->setParent($drinks);
  247. $bestWhisky = $this->createCategory();
  248. $bestWhisky->setTitle('Best Whisky');
  249. $bestWhisky->setParent($whisky);
  250. $this->em->persist($root);
  251. $this->em->persist($root2);
  252. $this->em->persist($child);
  253. $this->em->persist($child2);
  254. $this->em->persist($childsChild);
  255. $this->em->persist($potatoes);
  256. $this->em->persist($drinks);
  257. $this->em->persist($whisky);
  258. $this->em->persist($bestWhisky);
  259. $this->em->flush();
  260. }
  261. }