NestedTreeRootTest.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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 NestedTreeRootTest 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. public function testTheTree()
  27. {
  28. $repo = $this->em->getRepository(self::CATEGORY);
  29. $node = $repo->findOneByTitle('Food');
  30. $this->assertEquals(1, $node->getRoot());
  31. $this->assertEquals(1, $node->getLeft());
  32. $this->assertEquals(0, $node->getLevel());
  33. $this->assertEquals(10, $node->getRight());
  34. $node = $repo->findOneByTitle('Sports');
  35. $this->assertEquals(2, $node->getRoot());
  36. $this->assertEquals(1, $node->getLeft());
  37. $this->assertEquals(0, $node->getLevel());
  38. $this->assertEquals(2, $node->getRight());
  39. $node = $repo->findOneByTitle('Fruits');
  40. $this->assertEquals(1, $node->getRoot());
  41. $this->assertEquals(2, $node->getLeft());
  42. $this->assertEquals(1, $node->getLevel());
  43. $this->assertEquals(3, $node->getRight());
  44. $node = $repo->findOneByTitle('Vegitables');
  45. $this->assertEquals(1, $node->getRoot());
  46. $this->assertEquals(4, $node->getLeft());
  47. $this->assertEquals(1, $node->getLevel());
  48. $this->assertEquals(9, $node->getRight());
  49. $node = $repo->findOneByTitle('Carrots');
  50. $this->assertEquals(1, $node->getRoot());
  51. $this->assertEquals(5, $node->getLeft());
  52. $this->assertEquals(2, $node->getLevel());
  53. $this->assertEquals(6, $node->getRight());
  54. $node = $repo->findOneByTitle('Potatoes');
  55. $this->assertEquals(1, $node->getRoot());
  56. $this->assertEquals(7, $node->getLeft());
  57. $this->assertEquals(2, $node->getLevel());
  58. $this->assertEquals(8, $node->getRight());
  59. }
  60. public function testSetParentToNull()
  61. {
  62. $repo = $this->em->getRepository(self::CATEGORY);
  63. $node = $repo->findOneByTitle('Vegitables');
  64. $node->setParent(null);
  65. $this->em->persist($node);
  66. $this->em->flush();
  67. $this->em->clear();
  68. $node = $repo->findOneByTitle('Vegitables');
  69. $this->assertEquals(4, $node->getRoot());
  70. $this->assertEquals(1, $node->getLeft());
  71. $this->assertEquals(6, $node->getRight());
  72. $this->assertEquals(0, $node->getLevel());
  73. }
  74. public function testTreeUpdateShiftToNextBranch()
  75. {
  76. $repo = $this->em->getRepository(self::CATEGORY);
  77. $sport = $repo->findOneByTitle('Sports');
  78. $food = $repo->findOneByTitle('Food');
  79. $sport->setParent($food);
  80. $this->em->persist($sport);
  81. $this->em->flush();
  82. $this->em->clear();
  83. $node = $repo->findOneByTitle('Food');
  84. $this->assertEquals(1, $node->getLeft());
  85. $this->assertEquals(12, $node->getRight());
  86. $node = $repo->findOneByTitle('Sports');
  87. $this->assertEquals(1, $node->getRoot());
  88. $this->assertEquals(2, $node->getLeft());
  89. $this->assertEquals(1, $node->getLevel());
  90. $this->assertEquals(3, $node->getRight());
  91. $node = $repo->findOneByTitle('Vegitables');
  92. $this->assertEquals(6, $node->getLeft());
  93. $this->assertEquals(11, $node->getRight());
  94. }
  95. public function testTreeUpdateShiftToRoot()
  96. {
  97. $repo = $this->em->getRepository(self::CATEGORY);
  98. $vegies = $repo->findOneByTitle('Vegitables');
  99. $vegies->setParent(null);
  100. $this->em->persist($vegies);
  101. $this->em->flush();
  102. $this->em->clear();
  103. $node = $repo->findOneByTitle('Food');
  104. $this->assertEquals(1, $node->getLeft());
  105. $this->assertEquals(4, $node->getRight());
  106. $node = $repo->findOneByTitle('Vegitables');
  107. $this->assertEquals(4, $node->getRoot());
  108. $this->assertEquals(1, $node->getLeft());
  109. $this->assertEquals(0, $node->getLevel());
  110. $this->assertEquals(6, $node->getRight());
  111. $node = $repo->findOneByTitle('Potatoes');
  112. $this->assertEquals(4, $node->getRoot());
  113. $this->assertEquals(4, $node->getLeft());
  114. $this->assertEquals(1, $node->getLevel());
  115. $this->assertEquals(5, $node->getRight());
  116. }
  117. public function testTreeUpdateShiftToOtherParent()
  118. {
  119. $repo = $this->em->getRepository(self::CATEGORY);
  120. $carrots = $repo->findOneByTitle('Carrots');
  121. $food = $repo->findOneByTitle('Food');
  122. $carrots->setParent($food);
  123. $this->em->persist($carrots);
  124. $this->em->flush();
  125. $this->em->clear();
  126. $node = $repo->findOneByTitle('Food');
  127. $this->assertEquals(1, $node->getLeft());
  128. $this->assertEquals(10, $node->getRight());
  129. $node = $repo->findOneByTitle('Carrots');
  130. $this->assertEquals(1, $node->getRoot());
  131. $this->assertEquals(2, $node->getLeft());
  132. $this->assertEquals(1, $node->getLevel());
  133. $this->assertEquals(3, $node->getRight());
  134. $node = $repo->findOneByTitle('Potatoes');
  135. $this->assertEquals(1, $node->getRoot());
  136. $this->assertEquals(7, $node->getLeft());
  137. $this->assertEquals(2, $node->getLevel());
  138. $this->assertEquals(8, $node->getRight());
  139. }
  140. /**
  141. * @expectedException UnexpectedValueException
  142. */
  143. public function testTreeUpdateShiftToChildParent()
  144. {
  145. $repo = $this->em->getRepository(self::CATEGORY);
  146. $vegies = $repo->findOneByTitle('Vegitables');
  147. $food = $repo->findOneByTitle('Food');
  148. $food->setParent($vegies);
  149. $this->em->persist($food);
  150. $this->em->flush();
  151. $this->em->clear();
  152. }
  153. public function testTwoUpdateOperations()
  154. {
  155. $repo = $this->em->getRepository(self::CATEGORY);
  156. $sport = $repo->findOneByTitle('Sports');
  157. $food = $repo->findOneByTitle('Food');
  158. $sport->setParent($food);
  159. $vegies = $repo->findOneByTitle('Vegitables');
  160. $vegies->setParent(null);
  161. $this->em->persist($vegies);
  162. $this->em->persist($sport);
  163. $this->em->flush();
  164. $this->em->clear();
  165. $node = $repo->findOneByTitle('Carrots');
  166. $this->assertEquals(4, $node->getRoot());
  167. $this->assertEquals(2, $node->getLeft());
  168. $this->assertEquals(1, $node->getLevel());
  169. $this->assertEquals(3, $node->getRight());
  170. $node = $repo->findOneByTitle('Vegitables');
  171. $this->assertEquals(4, $node->getRoot());
  172. $this->assertEquals(1, $node->getLeft());
  173. $this->assertEquals(0, $node->getLevel());
  174. $this->assertEquals(6, $node->getRight());
  175. $node = $repo->findOneByTitle('Sports');
  176. $this->assertEquals(1, $node->getRoot());
  177. $this->assertEquals(2, $node->getLeft());
  178. $this->assertEquals(1, $node->getLevel());
  179. $this->assertEquals(3, $node->getRight());
  180. }
  181. public function testRemoval()
  182. {
  183. $repo = $this->em->getRepository(self::CATEGORY);
  184. $vegies = $repo->findOneByTitle('Vegitables');
  185. $this->em->remove($vegies);
  186. $this->em->flush();
  187. $this->em->clear();
  188. $node = $repo->findOneByTitle('Food');
  189. $this->assertEquals(1, $node->getLeft());
  190. $this->assertEquals(4, $node->getRight());
  191. }
  192. protected function getUsedEntityFixtures()
  193. {
  194. return array(
  195. self::CATEGORY
  196. );
  197. }
  198. private function populate()
  199. {
  200. $root = new RootCategory();
  201. $root->setTitle("Food");
  202. $root2 = new RootCategory();
  203. $root2->setTitle("Sports");
  204. $child = new RootCategory();
  205. $child->setTitle("Fruits");
  206. $child->setParent($root);
  207. $child2 = new RootCategory();
  208. $child2->setTitle("Vegitables");
  209. $child2->setParent($root);
  210. $childsChild = new RootCategory();
  211. $childsChild->setTitle("Carrots");
  212. $childsChild->setParent($child2);
  213. $potatoes = new RootCategory();
  214. $potatoes->setTitle("Potatoes");
  215. $potatoes->setParent($child2);
  216. $this->em->persist($root);
  217. $this->em->persist($root2);
  218. $this->em->persist($child);
  219. $this->em->persist($child2);
  220. $this->em->persist($childsChild);
  221. $this->em->persist($potatoes);
  222. $this->em->flush();
  223. $this->em->clear();
  224. }
  225. }