TreeTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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\Category;
  7. use Tree\Fixture\CategoryUuid;
  8. /**
  9. * These are tests for Tree behavior
  10. *
  11. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  12. * @link http://www.gediminasm.org
  13. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  14. */
  15. class TreeTest extends BaseTestCaseORM
  16. {
  17. const CATEGORY = "Tree\\Fixture\\Category";
  18. const CATEGORY_UUID = "Tree\\Fixture\\CategoryUuid";
  19. protected function setUp()
  20. {
  21. parent::setUp();
  22. $evm = new EventManager;
  23. $evm->addEventSubscriber(new TreeListener);
  24. $this->getMockSqliteEntityManager($evm);
  25. }
  26. public function testTheTree()
  27. {
  28. $meta = $this->em->getClassMetadata(self::CATEGORY);
  29. $root = new Category();
  30. $root->setTitle("Root");
  31. $this->assertTrue($root instanceof Node);
  32. $this->em->persist($root);
  33. $this->em->flush();
  34. $this->em->clear();
  35. $root = $this->em->getRepository(self::CATEGORY)->find(1);
  36. $left = $meta->getReflectionProperty('lft')->getValue($root);
  37. $right = $meta->getReflectionProperty('rgt')->getValue($root);
  38. $this->assertEquals(1, $left);
  39. $this->assertEquals(2, $right);
  40. $child = new Category();
  41. $child->setTitle("child");
  42. $child->setParent($root);
  43. $this->em->persist($child);
  44. $this->em->flush();
  45. $this->em->clear();
  46. $root = $this->em->getRepository(self::CATEGORY)->find(1);
  47. $left = $meta->getReflectionProperty('lft')->getValue($root);
  48. $right = $meta->getReflectionProperty('rgt')->getValue($root);
  49. $level = $meta->getReflectionProperty('level')->getValue($root);
  50. $this->assertEquals(1, $left);
  51. $this->assertEquals(4, $right);
  52. $this->assertEquals(0, $level);
  53. $child = $this->em->getRepository(self::CATEGORY)->find(2);
  54. $left = $meta->getReflectionProperty('lft')->getValue($child);
  55. $right = $meta->getReflectionProperty('rgt')->getValue($child);
  56. $level = $meta->getReflectionProperty('level')->getValue($child);
  57. $this->assertEquals(2, $left);
  58. $this->assertEquals(3, $right);
  59. $this->assertEquals(1, $level);
  60. $child2 = new Category();
  61. $child2->setTitle("child2");
  62. $child2->setParent($root);
  63. $this->em->persist($child2);
  64. $this->em->flush();
  65. $this->em->clear();
  66. $root = $this->em->getRepository(self::CATEGORY)->find(1);
  67. $left = $meta->getReflectionProperty('lft')->getValue($root);
  68. $right = $meta->getReflectionProperty('rgt')->getValue($root);
  69. $level = $meta->getReflectionProperty('level')->getValue($root);
  70. $this->assertEquals(1, $left);
  71. $this->assertEquals(6, $right);
  72. $this->assertEquals(0, $level);
  73. $child2 = $this->em->getRepository(self::CATEGORY)->find(3);
  74. $left = $meta->getReflectionProperty('lft')->getValue($child2);
  75. $right = $meta->getReflectionProperty('rgt')->getValue($child2);
  76. $level = $meta->getReflectionProperty('level')->getValue($child2);
  77. $this->assertEquals(4, $left);
  78. $this->assertEquals(5, $right);
  79. $this->assertEquals(1, $level);
  80. $childsChild = new Category();
  81. $childsChild->setTitle("childs2_child");
  82. $childsChild->setParent($child2);
  83. $this->em->persist($childsChild);
  84. $this->em->flush();
  85. $this->em->clear();
  86. $child2 = $this->em->getRepository(self::CATEGORY)->find(3);
  87. $left = $meta->getReflectionProperty('lft')->getValue($child2);
  88. $right = $meta->getReflectionProperty('rgt')->getValue($child2);
  89. $level = $meta->getReflectionProperty('level')->getValue($child2);
  90. $this->assertEquals(4, $left);
  91. $this->assertEquals(7, $right);
  92. $this->assertEquals(1, $level);
  93. $level = $meta->getReflectionProperty('level')->getValue($childsChild);
  94. $this->assertEquals(2, $level);
  95. // test updates to nodes, parent changes
  96. $childsChild = $this->em->getRepository(self::CATEGORY)->find(4);
  97. $child = $this->em->getRepository(self::CATEGORY)->find(2);
  98. $childsChild->setTitle('childs_child');
  99. $childsChild->setParent($child);
  100. $this->em->persist($childsChild);
  101. $this->em->flush();
  102. $this->em->clear();
  103. $child = $this->em->getRepository(self::CATEGORY)->find(2);
  104. $left = $meta->getReflectionProperty('lft')->getValue($child);
  105. $right = $meta->getReflectionProperty('rgt')->getValue($child);
  106. $level = $meta->getReflectionProperty('level')->getValue($child);
  107. $this->assertEquals(2, $left);
  108. $this->assertEquals(5, $right);
  109. $this->assertEquals(1, $level);
  110. // test deletion
  111. $this->em->remove($child);
  112. $this->em->flush();
  113. $this->em->clear();
  114. $root = $this->em->getRepository(self::CATEGORY)->find(1);
  115. $left = $meta->getReflectionProperty('lft')->getValue($root);
  116. $right = $meta->getReflectionProperty('rgt')->getValue($root);
  117. $this->assertEquals(1, $left);
  118. $this->assertEquals(4, $right);
  119. // test persisting in any time
  120. $yetAnotherChild = new Category();
  121. $this->em->persist($yetAnotherChild);
  122. $yetAnotherChild->setTitle("yetanotherchild");
  123. $yetAnotherChild->setParent($root);
  124. //$this->em->persist($yetAnotherChild);
  125. $this->em->flush();
  126. $this->em->clear();
  127. $left = $meta->getReflectionProperty('lft')->getValue($yetAnotherChild);
  128. $right = $meta->getReflectionProperty('rgt')->getValue($yetAnotherChild);
  129. $level = $meta->getReflectionProperty('level')->getValue($yetAnotherChild);
  130. $this->assertEquals(4, $left);
  131. $this->assertEquals(5, $right);
  132. $this->assertEquals(1, $level);
  133. }
  134. public function testIssue33()
  135. {
  136. $repo = $this->em->getRepository(self::CATEGORY);
  137. $root = new Category;
  138. $root->setTitle('root');
  139. $node1 = new Category;
  140. $node1->setTitle('node1');
  141. $node1->setParent($root);
  142. $node2 = new Category;
  143. $node2->setTitle('node2');
  144. $node2->setParent($root);
  145. $subNode = new Category;
  146. $subNode->setTitle('sub-node');
  147. $subNode->setParent($node2);
  148. $this->em->persist($root);
  149. $this->em->persist($node1);
  150. $this->em->persist($node2);
  151. $this->em->persist($subNode);
  152. $this->em->flush();
  153. $this->em->clear();
  154. $subNode = $repo->findOneByTitle('sub-node');
  155. $node1 = $repo->findOneByTitle('node1');
  156. $subNode->setParent($node1);
  157. $this->em->persist($subNode);
  158. $this->em->flush();
  159. $this->em->clear();
  160. $meta = $this->em->getClassMetadata(self::CATEGORY);
  161. $subNode = $repo->findOneByTitle('sub-node');
  162. $left = $meta->getReflectionProperty('lft')->getValue($subNode);
  163. $right = $meta->getReflectionProperty('rgt')->getValue($subNode);
  164. $this->assertEquals(3, $left);
  165. $this->assertEquals(4, $right);
  166. $node1 = $repo->findOneByTitle('node1');
  167. $left = $meta->getReflectionProperty('lft')->getValue($node1);
  168. $right = $meta->getReflectionProperty('rgt')->getValue($node1);
  169. $this->assertEquals(2, $left);
  170. $this->assertEquals(5, $right);
  171. }
  172. public function testIssue273()
  173. {
  174. $meta = $this->em->getClassMetadata(self::CATEGORY_UUID);
  175. $root = new CategoryUuid();
  176. $root->setTitle("Root");
  177. $this->assertTrue($root instanceof Node);
  178. $this->em->persist($root);
  179. $rootId = $root->getId();
  180. $this->em->flush();
  181. $this->em->clear();
  182. $root = $this->em->getRepository(self::CATEGORY_UUID)->find($rootId);
  183. $left = $meta->getReflectionProperty('lft')->getValue($root);
  184. $right = $meta->getReflectionProperty('rgt')->getValue($root);
  185. $this->assertEquals(1, $left);
  186. $this->assertEquals(2, $right);
  187. $child = new CategoryUuid();
  188. $child->setTitle("child");
  189. $child->setParent($root);
  190. $this->em->persist($child);
  191. $childId = $child->getId();
  192. $this->em->flush();
  193. $this->em->clear();
  194. $root = $this->em->getRepository(self::CATEGORY_UUID)->find($rootId);
  195. $left = $meta->getReflectionProperty('lft')->getValue($root);
  196. $right = $meta->getReflectionProperty('rgt')->getValue($root);
  197. $level = $meta->getReflectionProperty('level')->getValue($root);
  198. $this->assertEquals(1, $left);
  199. $this->assertEquals(4, $right);
  200. $this->assertEquals(0, $level);
  201. $child = $this->em->getRepository(self::CATEGORY_UUID)->find($childId);
  202. $left = $meta->getReflectionProperty('lft')->getValue($child);
  203. $right = $meta->getReflectionProperty('rgt')->getValue($child);
  204. $level = $meta->getReflectionProperty('level')->getValue($child);
  205. $this->assertEquals(2, $left);
  206. $this->assertEquals(3, $right);
  207. $this->assertEquals(1, $level);
  208. $child2 = new CategoryUuid();
  209. $child2->setTitle("child2");
  210. $child2->setParent($root);
  211. $this->em->persist($child2);
  212. $child2Id = $child2->getId();
  213. $this->em->flush();
  214. $this->em->clear();
  215. $root = $this->em->getRepository(self::CATEGORY_UUID)->find($rootId);
  216. $left = $meta->getReflectionProperty('lft')->getValue($root);
  217. $right = $meta->getReflectionProperty('rgt')->getValue($root);
  218. $level = $meta->getReflectionProperty('level')->getValue($root);
  219. $this->assertEquals(1, $left);
  220. $this->assertEquals(6, $right);
  221. $this->assertEquals(0, $level);
  222. $child2 = $this->em->getRepository(self::CATEGORY_UUID)->find($child2Id);
  223. $left = $meta->getReflectionProperty('lft')->getValue($child2);
  224. $right = $meta->getReflectionProperty('rgt')->getValue($child2);
  225. $level = $meta->getReflectionProperty('level')->getValue($child2);
  226. $this->assertEquals(4, $left);
  227. $this->assertEquals(5, $right);
  228. $this->assertEquals(1, $level);
  229. $childsChild = new CategoryUuid();
  230. $childsChild->setTitle("childs2_child");
  231. $childsChild->setParent($child2);
  232. $this->em->persist($childsChild);
  233. $childsChildId = $childsChild->getId();
  234. $this->em->flush();
  235. $this->em->clear();
  236. $child2 = $this->em->getRepository(self::CATEGORY_UUID)->find($child2Id);
  237. $left = $meta->getReflectionProperty('lft')->getValue($child2);
  238. $right = $meta->getReflectionProperty('rgt')->getValue($child2);
  239. $level = $meta->getReflectionProperty('level')->getValue($child2);
  240. $this->assertEquals(4, $left);
  241. $this->assertEquals(7, $right);
  242. $this->assertEquals(1, $level);
  243. $level = $meta->getReflectionProperty('level')->getValue($childsChild);
  244. $this->assertEquals(2, $level);
  245. // test updates to nodes, parent changes
  246. $childsChild = $this->em->getRepository(self::CATEGORY_UUID)->find($childsChildId);
  247. $child = $this->em->getRepository(self::CATEGORY_UUID)->find($childId);
  248. $childsChild->setTitle('childs_child');
  249. $childsChild->setParent($child);
  250. $this->em->persist($childsChild);
  251. $this->em->flush();
  252. $this->em->clear();
  253. $child = $this->em->getRepository(self::CATEGORY_UUID)->find($childId);
  254. $left = $meta->getReflectionProperty('lft')->getValue($child);
  255. $right = $meta->getReflectionProperty('rgt')->getValue($child);
  256. $level = $meta->getReflectionProperty('level')->getValue($child);
  257. $this->assertEquals(2, $left);
  258. $this->assertEquals(5, $right);
  259. $this->assertEquals(1, $level);
  260. // test deletion
  261. $this->em->remove($child);
  262. $this->em->flush();
  263. $this->em->clear();
  264. $root = $this->em->getRepository(self::CATEGORY_UUID)->find($rootId);
  265. $left = $meta->getReflectionProperty('lft')->getValue($root);
  266. $right = $meta->getReflectionProperty('rgt')->getValue($root);
  267. $this->assertEquals(1, $left);
  268. $this->assertEquals(4, $right);
  269. // test persisting in any time
  270. $yetAnotherChild = new CategoryUuid();
  271. $this->em->persist($yetAnotherChild);
  272. $yetAnotherChild->setTitle("yetanotherchild");
  273. $yetAnotherChild->setParent($root);
  274. //$this->em->persist($yetAnotherChild);
  275. $this->em->flush();
  276. $this->em->clear();
  277. $left = $meta->getReflectionProperty('lft')->getValue($yetAnotherChild);
  278. $right = $meta->getReflectionProperty('rgt')->getValue($yetAnotherChild);
  279. $level = $meta->getReflectionProperty('level')->getValue($yetAnotherChild);
  280. $this->assertEquals(4, $left);
  281. $this->assertEquals(5, $right);
  282. $this->assertEquals(1, $level);
  283. }
  284. protected function getUsedEntityFixtures()
  285. {
  286. return array(
  287. self::CATEGORY,
  288. self::CATEGORY_UUID,
  289. );
  290. }
  291. }