NestedTreePositionTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. <?php
  2. namespace Gedmo\Tree;
  3. use Doctrine\Common\EventManager;
  4. use Tool\BaseTestCaseORM;
  5. use Tree\Fixture\Category;
  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 NestedTreePositionTest extends BaseTestCaseORM
  16. {
  17. const CATEGORY = "Tree\\Fixture\\Category";
  18. const ROOT_CATEGORY = "Tree\\Fixture\\RootCategory";
  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 testTreeChildPositionMove2()
  27. {
  28. $this->populate();
  29. $repo = $this->em->getRepository(self::ROOT_CATEGORY);
  30. $oranges = $repo->findOneByTitle('Oranges');
  31. $meat = $repo->findOneByTitle('Meat');
  32. $this->assertEquals(2, $oranges->getLevel());
  33. $this->assertEquals(7, $oranges->getLeft());
  34. $this->assertEquals(8, $oranges->getRight());
  35. $repo->persistAsNextSiblingOf($meat, $oranges);
  36. $this->em->flush();
  37. $oranges = $repo->findOneByTitle('Oranges');
  38. $meat = $repo->findOneByTitle('Meat');
  39. $this->assertEquals(7, $oranges->getLeft());
  40. $this->assertEquals(8, $oranges->getRight());
  41. //Normal test that pass
  42. $this->assertEquals(9, $meat->getLeft());
  43. $this->assertEquals(10, $meat->getRight());
  44. // Raw query to show the issue #108 with wrong left value by Doctrine
  45. $dql = 'SELECT c FROM ' . self::ROOT_CATEGORY . ' c';
  46. $dql .= ' WHERE c.id = 5'; //5 == meat
  47. $meat_array = $this->em->createQuery($dql)->getScalarResult();
  48. $this->assertEquals(9, $meat_array[0]['c_lft']);
  49. $this->assertEquals(10, $meat_array[0]['c_rgt']);
  50. $this->assertEquals(2, $meat_array[0]['c_level']);
  51. }
  52. public function testTreeChildPositionMove3()
  53. {
  54. $this->populate();
  55. $repo = $this->em->getRepository(self::ROOT_CATEGORY);
  56. $oranges = $repo->findOneByTitle('Oranges');
  57. $milk = $repo->findOneByTitle('Milk');
  58. $this->assertEquals(2, $oranges->getLevel());
  59. $this->assertEquals(7, $oranges->getLeft());
  60. $this->assertEquals(8, $oranges->getRight());
  61. $repo->persistAsNextSiblingOf($milk, $oranges);
  62. $this->em->flush();
  63. $this->assertEquals(7, $oranges->getLeft());
  64. $this->assertEquals(8, $oranges->getRight());
  65. //Normal test that pass
  66. $this->assertEquals(9, $milk->getLeft());
  67. $this->assertEquals(10, $milk->getRight());
  68. // Raw query to show the issue #108 with wrong left value by Doctrine
  69. $dql = 'SELECT c FROM ' . self::ROOT_CATEGORY . ' c';
  70. $dql .= ' WHERE c.id = 4 '; //4 == Milk
  71. $milk_array = $this->em->createQuery($dql)->getScalarResult();
  72. $this->assertEquals(9, $milk_array[0]['c_lft']);
  73. $this->assertEquals(10, $milk_array[0]['c_rgt']);
  74. $this->assertEquals(2, $milk_array[0]['c_level']);
  75. }
  76. public function testPositionedUpdates()
  77. {
  78. $this->populate();
  79. $repo = $this->em->getRepository(self::ROOT_CATEGORY);
  80. $citrons = $repo->findOneByTitle('Citrons');
  81. $vegitables = $repo->findOneByTitle('Vegitables');
  82. $repo->persistAsNextSiblingOf($vegitables, $citrons);
  83. $this->em->flush();
  84. $this->assertEquals(5, $vegitables->getLeft());
  85. $this->assertEquals(6, $vegitables->getRight());
  86. $this->assertEquals(2, $vegitables->getParent()->getId());
  87. $fruits = $repo->findOneByTitle('Fruits');
  88. $this->assertEquals(2, $fruits->getLeft());
  89. $this->assertEquals(9, $fruits->getRight());
  90. $milk = $repo->findOneByTitle('Milk');
  91. $repo->persistAsFirstChildOf($milk, $fruits);
  92. $this->em->flush();
  93. $this->assertEquals(3, $milk->getLeft());
  94. $this->assertEquals(4, $milk->getRight());
  95. $this->assertEquals(2, $fruits->getLeft());
  96. $this->assertEquals(11, $fruits->getRight());
  97. }
  98. public function testTreeChildPositionMove()
  99. {
  100. $this->populate();
  101. $repo = $this->em->getRepository(self::ROOT_CATEGORY);
  102. $oranges = $repo->findOneByTitle('Oranges');
  103. $fruits = $repo->findOneByTitle('Fruits');
  104. $this->assertEquals(2, $oranges->getLevel());
  105. $repo->persistAsNextSiblingOf($oranges, $fruits);
  106. $this->em->flush();
  107. $this->assertEquals(1, $oranges->getLevel());
  108. $this->assertEquals(1, count($repo->children($fruits, true)));
  109. $vegies = $repo->findOneByTitle('Vegitables');
  110. $this->assertEquals(2, $vegies->getLeft());
  111. $repo->persistAsNextSiblingOf($vegies, $fruits);
  112. $this->em->flush();
  113. $this->assertEquals(6, $vegies->getLeft());
  114. $this->em->flush();
  115. $this->assertEquals(6, $vegies->getLeft());
  116. }
  117. public function testOnRootCategory()
  118. {
  119. // need to check if this does not produce errors
  120. $repo = $this->em->getRepository(self::ROOT_CATEGORY);
  121. $fruits = new RootCategory;
  122. $fruits->setTitle('Fruits');
  123. $vegitables = new RootCategory;
  124. $vegitables->setTitle('Vegitables');
  125. $milk = new RootCategory;
  126. $milk->setTitle('Milk');
  127. $meat = new RootCategory;
  128. $meat->setTitle('Meat');
  129. $repo
  130. ->persistAsFirstChild($fruits)
  131. ->persistAsFirstChild($vegitables)
  132. ->persistAsLastChild($milk)
  133. ->persistAsLastChild($meat);
  134. $cookies = new RootCategory;
  135. $cookies->setTitle('Cookies');
  136. $drinks = new RootCategory;
  137. $drinks->setTitle('Drinks');
  138. $repo
  139. ->persistAsNextSibling($cookies)
  140. ->persistAsPrevSibling($drinks);
  141. $this->em->flush();
  142. $dql = 'SELECT COUNT(c) FROM ' . self::ROOT_CATEGORY . ' c';
  143. $dql .= ' WHERE c.lft = 1 AND c.rgt = 2 AND c.parent IS NULL AND c.level = 0';
  144. $count = $this->em->createQuery($dql)->getSingleScalarResult();
  145. $this->assertEquals(6, $count);
  146. $repo = $this->em->getRepository(self::CATEGORY);
  147. $fruits = new Category;
  148. $fruits->setTitle('Fruits');
  149. $vegitables = new Category;
  150. $vegitables->setTitle('Vegitables');
  151. $milk = new Category;
  152. $milk->setTitle('Milk');
  153. $meat = new Category;
  154. $meat->setTitle('Meat');
  155. $repo
  156. ->persistAsFirstChild($fruits)
  157. ->persistAsFirstChild($vegitables)
  158. ->persistAsLastChild($milk)
  159. ->persistAsLastChild($meat);
  160. $cookies = new Category;
  161. $cookies->setTitle('Cookies');
  162. $drinks = new Category;
  163. $drinks->setTitle('Drinks');
  164. $repo
  165. ->persistAsNextSibling($cookies)
  166. ->persistAsPrevSibling($drinks);
  167. $this->em->flush();
  168. $dql = 'SELECT COUNT(c) FROM ' . self::CATEGORY . ' c';
  169. $dql .= ' WHERE c.parentId IS NULL AND c.level = 0';
  170. $dql .= ' AND c.lft BETWEEN 1 AND 11';
  171. $count = $this->em->createQuery($dql)->getSingleScalarResult();
  172. $this->assertEquals(6, $count);
  173. }
  174. public function testRootTreePositionedInserts()
  175. {
  176. $repo = $this->em->getRepository(self::ROOT_CATEGORY);
  177. // test child positioned inserts
  178. $food = new RootCategory;
  179. $food->setTitle('Food');
  180. $fruits = new RootCategory;
  181. $fruits->setTitle('Fruits');
  182. $vegitables = new RootCategory;
  183. $vegitables->setTitle('Vegitables');
  184. $milk = new RootCategory;
  185. $milk->setTitle('Milk');
  186. $meat = new RootCategory;
  187. $meat->setTitle('Meat');
  188. $repo
  189. ->persistAsFirstChild($food)
  190. ->persistAsFirstChildOf($fruits, $food)
  191. ->persistAsFirstChildOf($vegitables, $food)
  192. ->persistAsLastChildOf($milk, $food)
  193. ->persistAsLastChildOf($meat, $food);
  194. $this->em->flush();
  195. $this->assertEquals(4, $fruits->getLeft());
  196. $this->assertEquals(5, $fruits->getRight());
  197. $this->assertEquals(2, $vegitables->getLeft());
  198. $this->assertEquals(3, $vegitables->getRight());
  199. $this->assertEquals(6, $milk->getLeft());
  200. $this->assertEquals(7, $milk->getRight());
  201. $this->assertEquals(8, $meat->getLeft());
  202. $this->assertEquals(9, $meat->getRight());
  203. // test sibling positioned inserts
  204. $cookies = new RootCategory;
  205. $cookies->setTitle('Cookies');
  206. $drinks = new RootCategory;
  207. $drinks->setTitle('Drinks');
  208. $repo
  209. ->persistAsNextSiblingOf($cookies, $milk)
  210. ->persistAsPrevSiblingOf($drinks, $milk);
  211. $this->em->flush();
  212. $this->assertEquals(6, $drinks->getLeft());
  213. $this->assertEquals(7, $drinks->getRight());
  214. $this->assertEquals(10, $cookies->getLeft());
  215. $this->assertEquals(11, $cookies->getRight());
  216. $this->assertTrue($repo->verify());
  217. }
  218. public function testSimpleTreePositionedInserts()
  219. {
  220. $repo = $this->em->getRepository(self::CATEGORY);
  221. // test child positioned inserts
  222. $food = new Category;
  223. $food->setTitle('Food');
  224. $repo->persistAsFirstChild($food);
  225. $fruits = new Category;
  226. $fruits->setTitle('Fruits');
  227. $fruits->setParent($food);
  228. $repo->persistAsFirstChild($fruits);
  229. $vegitables = new Category;
  230. $vegitables->setTitle('Vegitables');
  231. $vegitables->setParent($food);
  232. $repo->persistAsFirstChild($vegitables);
  233. $milk = new Category;
  234. $milk->setTitle('Milk');
  235. $milk->setParent($food);
  236. $repo->persistAsLastChild($milk);
  237. $meat = new Category;
  238. $meat->setTitle('Meat');
  239. $meat->setParent($food);
  240. $repo->persistAsLastChild($meat);
  241. $this->em->flush();
  242. $this->assertEquals(4, $fruits->getLeft());
  243. $this->assertEquals(5, $fruits->getRight());
  244. $this->assertEquals(2, $vegitables->getLeft());
  245. $this->assertEquals(3, $vegitables->getRight());
  246. $this->assertEquals(6, $milk->getLeft());
  247. $this->assertEquals(7, $milk->getRight());
  248. $this->assertEquals(8, $meat->getLeft());
  249. $this->assertEquals(9, $meat->getRight());
  250. // test sibling positioned inserts
  251. $cookies = new Category;
  252. $cookies->setTitle('Cookies');
  253. $cookies->setParent($milk);
  254. $repo->persistAsNextSibling($cookies);
  255. $drinks = new Category;
  256. $drinks->setTitle('Drinks');
  257. $drinks->setParent($milk);
  258. $repo->persistAsPrevSibling($drinks);
  259. $this->em->flush();
  260. $this->assertEquals(6, $drinks->getLeft());
  261. $this->assertEquals(7, $drinks->getRight());
  262. $this->assertEquals(10, $cookies->getLeft());
  263. $this->assertEquals(11, $cookies->getRight());
  264. $this->assertTrue($repo->verify());
  265. }
  266. private function populate()
  267. {
  268. $repo = $this->em->getRepository(self::ROOT_CATEGORY);
  269. $food = new RootCategory;
  270. $food->setTitle('Food');
  271. $fruits = new RootCategory;
  272. $fruits->setTitle('Fruits');
  273. $vegitables = new RootCategory;
  274. $vegitables->setTitle('Vegitables');
  275. $milk = new RootCategory;
  276. $milk->setTitle('Milk');
  277. $meat = new RootCategory;
  278. $meat->setTitle('Meat');
  279. $oranges = new RootCategory;
  280. $oranges->setTitle('Oranges');
  281. $citrons = new RootCategory;
  282. $citrons->setTitle('Citrons');
  283. $repo
  284. ->persistAsFirstChild($food)
  285. ->persistAsFirstChildOf($fruits, $food)
  286. ->persistAsFirstChildOf($vegitables, $food)
  287. ->persistAsLastChildOf($milk, $food)
  288. ->persistAsLastChildOf($meat, $food)
  289. ->persistAsFirstChildOf($oranges, $fruits)
  290. ->persistAsFirstChildOf($citrons, $fruits);
  291. $this->em->flush();
  292. }
  293. protected function getUsedEntityFixtures()
  294. {
  295. return array(
  296. self::CATEGORY,
  297. self::ROOT_CATEGORY
  298. );
  299. }
  300. }