MaterializedPathODMMongoDBRepositoryTest.php 11 KB

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