NestedTreeRootTest.php 11KB

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