NestedTreePositionTest.php 13KB

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