MaterializedPathORMRepositoryTest.php 12KB

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