TreeTest.php 13KB

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