NestedTreePositionTest.php 13 KB

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