PageRenderTime 29ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/Gedmo/Tree/NestedTreeRootTest.php

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