NestedTreeRootTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. * @link http://www.gediminasm.org
  12. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  13. */
  14. class NestedTreeRootTest extends BaseTestCaseORM
  15. {
  16. const CATEGORY = "Tree\\Fixture\\RootCategory";
  17. protected function setUp()
  18. {
  19. parent::setUp();
  20. $evm = new EventManager;
  21. $evm->addEventSubscriber(new TreeListener);
  22. $this->getMockSqliteEntityManager($evm);
  23. $this->populate();
  24. }
  25. /**
  26. * @test
  27. */
  28. function shouldRemoveAndSynchronize()
  29. {
  30. $repo = $this->em->getRepository(self::CATEGORY);
  31. $vegies = $repo->findOneByTitle('Vegitables');
  32. $this->em->remove($vegies);
  33. $this->em->flush();
  34. $food = $repo->findOneByTitle('Food');
  35. $this->assertEquals(1, $food->getLeft());
  36. $this->assertEquals(4, $food->getRight());
  37. $vegies = new RootCategory;
  38. $vegies->setTitle('Vegies');
  39. $repo->persistAsFirstChildOf($vegies, $food);
  40. $this->em->flush();
  41. $this->assertEquals(1, $food->getLeft());
  42. $this->assertEquals(6, $food->getRight());
  43. $this->assertEquals(2, $vegies->getLeft());
  44. $this->assertEquals(3, $vegies->getRight());
  45. }
  46. /*public function testHeavyLoad()
  47. {
  48. $start = microtime(true);
  49. $dumpTime = function($start, $msg) {
  50. $took = microtime(true) - $start;
  51. $minutes = intval($took / 60); $seconds = $took % 60;
  52. echo sprintf("%s --> %02d:%02d", $msg, $minutes, $seconds) . PHP_EOL;
  53. };
  54. $repo = $this->em->getRepository(self::CATEGORY);
  55. $parent = null;
  56. $num = 800;
  57. for($i = 0; $i < 500; $i++) {
  58. $cat = new RootCategory;
  59. $cat->setParent($parent);
  60. $cat->setTitle('cat'.$i);
  61. $this->em->persist($cat);
  62. // siblings
  63. $rnd = rand(0, 3);
  64. for ($j = 0; $j < $rnd; $j++) {
  65. $siblingCat = new RootCategory;
  66. $siblingCat->setTitle('cat'.$i.$j);
  67. $siblingCat->setParent($cat);
  68. $this->em->persist($siblingCat);
  69. }
  70. $num += $rnd;
  71. $parent = $cat;
  72. }
  73. $this->em->flush();
  74. $dumpTime($start, $num.' - inserts took:');
  75. $start = microtime(true);
  76. // test moving
  77. $target = $repo->findOneByTitle('cat300');
  78. $dest = $repo->findOneByTitle('cat2000');
  79. $target->setParent($dest);
  80. $target2 = $repo->findOneByTitle('cat450');
  81. $dest2 = $repo->findOneByTitle('cat2500');
  82. $target2->setParent($dest2);
  83. $this->em->flush();
  84. $dumpTime($start, 'moving took:');
  85. }*/
  86. public function testTheTree()
  87. {
  88. $repo = $this->em->getRepository(self::CATEGORY);
  89. $node = $repo->findOneByTitle('Food');
  90. $this->assertEquals(1, $node->getRoot());
  91. $this->assertEquals(1, $node->getLeft());
  92. $this->assertEquals(0, $node->getLevel());
  93. $this->assertEquals(10, $node->getRight());
  94. $node = $repo->findOneByTitle('Sports');
  95. $this->assertEquals(2, $node->getRoot());
  96. $this->assertEquals(1, $node->getLeft());
  97. $this->assertEquals(0, $node->getLevel());
  98. $this->assertEquals(2, $node->getRight());
  99. $node = $repo->findOneByTitle('Fruits');
  100. $this->assertEquals(1, $node->getRoot());
  101. $this->assertEquals(2, $node->getLeft());
  102. $this->assertEquals(1, $node->getLevel());
  103. $this->assertEquals(3, $node->getRight());
  104. $node = $repo->findOneByTitle('Vegitables');
  105. $this->assertEquals(1, $node->getRoot());
  106. $this->assertEquals(4, $node->getLeft());
  107. $this->assertEquals(1, $node->getLevel());
  108. $this->assertEquals(9, $node->getRight());
  109. $node = $repo->findOneByTitle('Carrots');
  110. $this->assertEquals(1, $node->getRoot());
  111. $this->assertEquals(5, $node->getLeft());
  112. $this->assertEquals(2, $node->getLevel());
  113. $this->assertEquals(6, $node->getRight());
  114. $node = $repo->findOneByTitle('Potatoes');
  115. $this->assertEquals(1, $node->getRoot());
  116. $this->assertEquals(7, $node->getLeft());
  117. $this->assertEquals(2, $node->getLevel());
  118. $this->assertEquals(8, $node->getRight());
  119. }
  120. public function testSetParentToNull()
  121. {
  122. $repo = $this->em->getRepository(self::CATEGORY);
  123. $node = $repo->findOneByTitle('Vegitables');
  124. $node->setParent(null);
  125. $this->em->persist($node);
  126. $this->em->flush();
  127. $this->em->clear();
  128. $node = $repo->findOneByTitle('Vegitables');
  129. $this->assertEquals(4, $node->getRoot());
  130. $this->assertEquals(1, $node->getLeft());
  131. $this->assertEquals(6, $node->getRight());
  132. $this->assertEquals(0, $node->getLevel());
  133. }
  134. public function testTreeUpdateShiftToNextBranch()
  135. {
  136. $repo = $this->em->getRepository(self::CATEGORY);
  137. $sport = $repo->findOneByTitle('Sports');
  138. $food = $repo->findOneByTitle('Food');
  139. $sport->setParent($food);
  140. $this->em->persist($sport);
  141. $this->em->flush();
  142. $this->em->clear();
  143. $node = $repo->findOneByTitle('Food');
  144. $this->assertEquals(1, $node->getLeft());
  145. $this->assertEquals(12, $node->getRight());
  146. $node = $repo->findOneByTitle('Sports');
  147. $this->assertEquals(1, $node->getRoot());
  148. $this->assertEquals(2, $node->getLeft());
  149. $this->assertEquals(1, $node->getLevel());
  150. $this->assertEquals(3, $node->getRight());
  151. $node = $repo->findOneByTitle('Vegitables');
  152. $this->assertEquals(6, $node->getLeft());
  153. $this->assertEquals(11, $node->getRight());
  154. }
  155. public function testTreeUpdateShiftToRoot()
  156. {
  157. $repo = $this->em->getRepository(self::CATEGORY);
  158. $vegies = $repo->findOneByTitle('Vegitables');
  159. $vegies->setParent(null);
  160. $this->em->persist($vegies);
  161. $this->em->flush();
  162. $this->em->clear();
  163. $node = $repo->findOneByTitle('Food');
  164. $this->assertEquals(1, $node->getLeft());
  165. $this->assertEquals(4, $node->getRight());
  166. $node = $repo->findOneByTitle('Vegitables');
  167. $this->assertEquals(4, $node->getRoot());
  168. $this->assertEquals(1, $node->getLeft());
  169. $this->assertEquals(0, $node->getLevel());
  170. $this->assertEquals(6, $node->getRight());
  171. $node = $repo->findOneByTitle('Potatoes');
  172. $this->assertEquals(4, $node->getRoot());
  173. $this->assertEquals(4, $node->getLeft());
  174. $this->assertEquals(1, $node->getLevel());
  175. $this->assertEquals(5, $node->getRight());
  176. }
  177. public function testTreeUpdateShiftToOtherParent()
  178. {
  179. $repo = $this->em->getRepository(self::CATEGORY);
  180. $carrots = $repo->findOneByTitle('Carrots');
  181. $food = $repo->findOneByTitle('Food');
  182. $carrots->setParent($food);
  183. $this->em->persist($carrots);
  184. $this->em->flush();
  185. $this->em->clear();
  186. $node = $repo->findOneByTitle('Food');
  187. $this->assertEquals(1, $node->getLeft());
  188. $this->assertEquals(10, $node->getRight());
  189. $node = $repo->findOneByTitle('Carrots');
  190. $this->assertEquals(1, $node->getRoot());
  191. $this->assertEquals(2, $node->getLeft());
  192. $this->assertEquals(1, $node->getLevel());
  193. $this->assertEquals(3, $node->getRight());
  194. $node = $repo->findOneByTitle('Potatoes');
  195. $this->assertEquals(1, $node->getRoot());
  196. $this->assertEquals(7, $node->getLeft());
  197. $this->assertEquals(2, $node->getLevel());
  198. $this->assertEquals(8, $node->getRight());
  199. }
  200. /**
  201. * @expectedException UnexpectedValueException
  202. */
  203. public function testTreeUpdateShiftToChildParent()
  204. {
  205. $repo = $this->em->getRepository(self::CATEGORY);
  206. $vegies = $repo->findOneByTitle('Vegitables');
  207. $food = $repo->findOneByTitle('Food');
  208. $food->setParent($vegies);
  209. $this->em->persist($food);
  210. $this->em->flush();
  211. $this->em->clear();
  212. }
  213. public function testTwoUpdateOperations()
  214. {
  215. $repo = $this->em->getRepository(self::CATEGORY);
  216. $sport = $repo->findOneByTitle('Sports');
  217. $food = $repo->findOneByTitle('Food');
  218. $sport->setParent($food);
  219. $vegies = $repo->findOneByTitle('Vegitables');
  220. $vegies->setParent(null);
  221. $this->em->persist($vegies);
  222. $this->em->persist($sport);
  223. $this->em->flush();
  224. $this->em->clear();
  225. $node = $repo->findOneByTitle('Carrots');
  226. $this->assertEquals(4, $node->getRoot());
  227. $this->assertEquals(2, $node->getLeft());
  228. $this->assertEquals(1, $node->getLevel());
  229. $this->assertEquals(3, $node->getRight());
  230. $node = $repo->findOneByTitle('Vegitables');
  231. $this->assertEquals(4, $node->getRoot());
  232. $this->assertEquals(1, $node->getLeft());
  233. $this->assertEquals(0, $node->getLevel());
  234. $this->assertEquals(6, $node->getRight());
  235. $node = $repo->findOneByTitle('Sports');
  236. $this->assertEquals(1, $node->getRoot());
  237. $this->assertEquals(2, $node->getLeft());
  238. $this->assertEquals(1, $node->getLevel());
  239. $this->assertEquals(3, $node->getRight());
  240. }
  241. public function testRemoval()
  242. {
  243. $repo = $this->em->getRepository(self::CATEGORY);
  244. $vegies = $repo->findOneByTitle('Vegitables');
  245. $this->em->remove($vegies);
  246. $this->em->flush();
  247. $this->em->clear();
  248. $node = $repo->findOneByTitle('Food');
  249. $this->assertEquals(1, $node->getLeft());
  250. $this->assertEquals(4, $node->getRight());
  251. }
  252. protected function getUsedEntityFixtures()
  253. {
  254. return array(
  255. self::CATEGORY
  256. );
  257. }
  258. private function populate()
  259. {
  260. $root = new RootCategory();
  261. $root->setTitle("Food");
  262. $root2 = new RootCategory();
  263. $root2->setTitle("Sports");
  264. $child = new RootCategory();
  265. $child->setTitle("Fruits");
  266. $child->setParent($root);
  267. $child2 = new RootCategory();
  268. $child2->setTitle("Vegitables");
  269. $child2->setParent($root);
  270. $childsChild = new RootCategory();
  271. $childsChild->setTitle("Carrots");
  272. $childsChild->setParent($child2);
  273. $potatoes = new RootCategory();
  274. $potatoes->setTitle("Potatoes");
  275. $potatoes->setParent($child2);
  276. $this->em->persist($root);
  277. $this->em->persist($root2);
  278. $this->em->persist($child);
  279. $this->em->persist($child2);
  280. $this->em->persist($childsChild);
  281. $this->em->persist($potatoes);
  282. $this->em->flush();
  283. $this->em->clear();
  284. }
  285. }