MaterializedPathODMMongoDBRepositoryTest.php 11KB

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