RepositoryTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. <?php
  2. namespace Gedmo\Tree;
  3. use Doctrine\Common\EventManager;
  4. use Tool\BaseTestCaseORM;
  5. use Doctrine\Common\Util\Debug,
  6. Tree\Fixture\Category,
  7. 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 RepositoryTest 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. $this->populate();
  26. }
  27. public function testBasicFunctions()
  28. {
  29. $vegies = $this->em->getRepository(self::CATEGORY)
  30. ->findOneByTitle('Vegitables');
  31. $food = $this->em->getRepository(self::CATEGORY)
  32. ->findOneByTitle('Food');
  33. // test childCount
  34. $childCount = $this->em->getRepository(self::CATEGORY)
  35. ->childCount($vegies);
  36. $this->assertEquals(2, $childCount);
  37. $childCount = $this->em->getRepository(self::CATEGORY)
  38. ->childCount($food);
  39. $this->assertEquals(4, $childCount);
  40. $childCount = $this->em->getRepository(self::CATEGORY)
  41. ->childCount($food, true);
  42. $this->assertEquals(2, $childCount);
  43. $childCount = $this->em->getRepository(self::CATEGORY)
  44. ->childCount();
  45. $this->assertEquals(6, $childCount);
  46. // test children
  47. $children = $this->em->getRepository(self::CATEGORY)
  48. ->children($vegies);
  49. $this->assertCount(2, $children);
  50. $this->assertEquals('Carrots', $children[0]->getTitle());
  51. $this->assertEquals('Potatoes', $children[1]->getTitle());
  52. $children = $this->em->getRepository(self::CATEGORY)
  53. ->children($food);
  54. $this->assertCount(4, $children);
  55. $this->assertEquals('Fruits', $children[0]->getTitle());
  56. $this->assertEquals('Vegitables', $children[1]->getTitle());
  57. $this->assertEquals('Carrots', $children[2]->getTitle());
  58. $this->assertEquals('Potatoes', $children[3]->getTitle());
  59. $children = $this->em->getRepository(self::CATEGORY)
  60. ->children($food, true);
  61. $this->assertCount(2, $children);
  62. $this->assertEquals('Fruits', $children[0]->getTitle());
  63. $this->assertEquals('Vegitables', $children[1]->getTitle());
  64. $children = $this->em->getRepository(self::CATEGORY)
  65. ->children();
  66. $this->assertCount(6, $children);
  67. // path
  68. $path = $this->em->getRepository(self::CATEGORY)
  69. ->getPath($vegies);
  70. $this->assertCount(2, $path);
  71. $this->assertEquals('Food', $path[0]->getTitle());
  72. $this->assertEquals('Vegitables', $path[1]->getTitle());
  73. $carrots = $this->em->getRepository(self::CATEGORY)
  74. ->findOneByTitle('Carrots');
  75. $path = $this->em->getRepository(self::CATEGORY)
  76. ->getPath($carrots);
  77. $this->assertCount(3, $path);
  78. $this->assertEquals('Food', $path[0]->getTitle());
  79. $this->assertEquals('Vegitables', $path[1]->getTitle());
  80. $this->assertEquals('Carrots', $path[2]->getTitle());
  81. // leafs
  82. $leafs = $this->em->getRepository(self::CATEGORY)
  83. ->getLeafs();
  84. $this->assertCount(4, $leafs);
  85. $this->assertEquals('Fruits', $leafs[0]->getTitle());
  86. $this->assertEquals('Carrots', $leafs[1]->getTitle());
  87. $this->assertEquals('Potatoes', $leafs[2]->getTitle());
  88. $this->assertEquals('Sports', $leafs[3]->getTitle());
  89. }
  90. public function testAdvancedFunctions()
  91. {
  92. $this->populateMore();
  93. $onions = $this->em->getRepository(self::CATEGORY)
  94. ->findOneByTitle('Onions');
  95. $repo = $this->em->getRepository(self::CATEGORY);
  96. $meta = $this->em->getClassMetadata(self::CATEGORY);
  97. $left = $meta->getReflectionProperty('lft')->getValue($onions);
  98. $right = $meta->getReflectionProperty('rgt')->getValue($onions);
  99. $this->assertEquals(11, $left);
  100. $this->assertEquals(12, $right);
  101. // move up onions by one position
  102. $repo->moveUp($onions, 1);
  103. $left = $meta->getReflectionProperty('lft')->getValue($onions);
  104. $right = $meta->getReflectionProperty('rgt')->getValue($onions);
  105. $this->assertEquals(9, $left);
  106. $this->assertEquals(10, $right);
  107. // move down onions by one position
  108. $repo->moveDown($onions, 1);
  109. $left = $meta->getReflectionProperty('lft')->getValue($onions);
  110. $right = $meta->getReflectionProperty('rgt')->getValue($onions);
  111. $this->assertEquals(11, $left);
  112. $this->assertEquals(12, $right);
  113. // move to the up onions on this level
  114. $repo->moveUp($onions, true);
  115. $left = $meta->getReflectionProperty('lft')->getValue($onions);
  116. $right = $meta->getReflectionProperty('rgt')->getValue($onions);
  117. $this->assertEquals(5, $left);
  118. $this->assertEquals(6, $right);
  119. // test tree reordering
  120. // reorder tree by title
  121. $food = $repo->findOneByTitle('Food');
  122. $repo->reorder($food, 'title');
  123. $node = $this->em->getRepository(self::CATEGORY)
  124. ->findOneByTitle('Cabbages');
  125. $left = $meta->getReflectionProperty('lft')->getValue($node);
  126. $right = $meta->getReflectionProperty('rgt')->getValue($node);
  127. $this->assertEquals(5, $left);
  128. $this->assertEquals(6, $right);
  129. $node = $this->em->getRepository(self::CATEGORY)
  130. ->findOneByTitle('Carrots');
  131. $left = $meta->getReflectionProperty('lft')->getValue($node);
  132. $right = $meta->getReflectionProperty('rgt')->getValue($node);
  133. $this->assertEquals(7, $left);
  134. $this->assertEquals(8, $right);
  135. $node = $this->em->getRepository(self::CATEGORY)
  136. ->findOneByTitle('Onions');
  137. $left = $meta->getReflectionProperty('lft')->getValue($node);
  138. $right = $meta->getReflectionProperty('rgt')->getValue($node);
  139. $this->assertEquals(9, $left);
  140. $this->assertEquals(10, $right);
  141. $node = $this->em->getRepository(self::CATEGORY)
  142. ->findOneByTitle('Potatoes');
  143. $left = $meta->getReflectionProperty('lft')->getValue($node);
  144. $right = $meta->getReflectionProperty('rgt')->getValue($node);
  145. $this->assertEquals(11, $left);
  146. $this->assertEquals(12, $right);
  147. // test removal with reparenting
  148. $vegies = $this->em->getRepository(self::CATEGORY)
  149. ->findOneByTitle('Vegitables');
  150. $repo->removeFromTree($vegies);
  151. $this->em->clear(); // clear all cached nodes
  152. $vegies = $this->em->getRepository(self::CATEGORY)
  153. ->findOneByTitle('Vegitables');
  154. $this->assertNull($vegies);
  155. $node = $this->em->getRepository(self::CATEGORY)
  156. ->findOneByTitle('Fruits');
  157. $left = $meta->getReflectionProperty('lft')->getValue($node);
  158. $right = $meta->getReflectionProperty('rgt')->getValue($node);
  159. $this->assertEquals(2, $left);
  160. $this->assertEquals(3, $right);
  161. $this->assertEquals('Food', $node->getParent()->getTitle());
  162. $node = $this->em->getRepository(self::CATEGORY)
  163. ->findOneByTitle('Cabbages');
  164. $left = $meta->getReflectionProperty('lft')->getValue($node);
  165. $right = $meta->getReflectionProperty('rgt')->getValue($node);
  166. $this->assertEquals(4, $left);
  167. $this->assertEquals(5, $right);
  168. $this->assertEquals('Food', $node->getParent()->getTitle());
  169. }
  170. public function testRootRemoval()
  171. {
  172. $repo = $this->em->getRepository(self::CATEGORY);
  173. $meta = $this->em->getClassMetadata(self::CATEGORY);
  174. $this->populateMore();
  175. $food = $repo->findOneByTitle('Food');
  176. $repo->removeFromTree($food);
  177. $this->em->clear();
  178. $food = $repo->findOneByTitle('Food');
  179. $this->assertNull($food);
  180. $node = $repo->findOneByTitle('Fruits');
  181. $left = $meta->getReflectionProperty('lft')->getValue($node);
  182. $right = $meta->getReflectionProperty('rgt')->getValue($node);
  183. $this->assertEquals(1, $left);
  184. $this->assertEquals(2, $right);
  185. $this->assertNull($node->getParent());
  186. $node = $repo->findOneByTitle('Vegitables');
  187. $left = $meta->getReflectionProperty('lft')->getValue($node);
  188. $right = $meta->getReflectionProperty('rgt')->getValue($node);
  189. $this->assertEquals(3, $left);
  190. $this->assertEquals(12, $right);
  191. $this->assertNull($node->getParent());
  192. }
  193. public function testVerificationAndRecover()
  194. {
  195. $repo = $this->em->getRepository(self::CATEGORY);
  196. $meta = $this->em->getClassMetadata(self::CATEGORY);
  197. $this->populateMore();
  198. // test verification of tree
  199. $this->assertTrue($repo->verify());
  200. // now lets brake something
  201. $dql = 'UPDATE ' . self::CATEGORY . ' node';
  202. $dql .= ' SET node.lft = 1';
  203. $dql .= ' WHERE node.id = 8';
  204. $q = $this->em->createQuery($dql);
  205. $q->getSingleScalarResult();
  206. $this->em->clear(); // must clear cached entities
  207. // verify again
  208. $result = $repo->verify();
  209. $this->assertTrue(is_array($result));
  210. $this->assertArrayHasKey(0, $result);
  211. $this->assertArrayHasKey(1, $result);
  212. $this->assertArrayHasKey(2, $result);
  213. $duplicate = $result[0];
  214. $missing = $result[1];
  215. $invalidLeft = $result[2];
  216. $this->assertEquals('index [1], duplicate', $duplicate);
  217. $this->assertEquals('index [11], missing', $missing);
  218. $this->assertEquals('node [8] left is less than parent`s [4] left value', $invalidLeft);
  219. // test recover functionality
  220. $repo->recover();
  221. $this->em->flush();
  222. $this->assertTrue($repo->verify());
  223. }
  224. public function testMoveRootNode()
  225. {
  226. $repo = $this->em->getRepository(self::CATEGORY);
  227. $food = $repo->findOneByTitle('Food');
  228. $repo->moveDown($food, 1);
  229. $meta = $this->em->getClassMetadata(self::CATEGORY);
  230. $left = $meta->getReflectionProperty('lft')->getValue($food);
  231. $right = $meta->getReflectionProperty('rgt')->getValue($food);
  232. $this->assertEquals(3, $left);
  233. $this->assertEquals(12, $right);
  234. $this->assertNull($food->getParent());
  235. $this->assertTrue($repo->verify());
  236. }
  237. public function testIssue273()
  238. {
  239. $this->populateUuid();
  240. $vegies = $this->em->getRepository(self::CATEGORY_UUID)
  241. ->findOneByTitle('Vegitables');
  242. $food = $this->em->getRepository(self::CATEGORY_UUID)
  243. ->findOneByTitle('Food');
  244. // test childCount
  245. $childCount = $this->em->getRepository(self::CATEGORY_UUID)
  246. ->childCount($vegies);
  247. $this->assertEquals(2, $childCount);
  248. $childCount = $this->em->getRepository(self::CATEGORY_UUID)
  249. ->childCount($food);
  250. $this->assertEquals(4, $childCount);
  251. $childCount = $this->em->getRepository(self::CATEGORY_UUID)
  252. ->childCount($food, true);
  253. $this->assertEquals(2, $childCount);
  254. $childCount = $this->em->getRepository(self::CATEGORY_UUID)
  255. ->childCount();
  256. $this->assertEquals(6, $childCount);
  257. // test children
  258. $children = $this->em->getRepository(self::CATEGORY_UUID)
  259. ->children($vegies);
  260. $this->assertCount(2, $children);
  261. $this->assertEquals('Carrots', $children[0]->getTitle());
  262. $this->assertEquals('Potatoes', $children[1]->getTitle());
  263. $children = $this->em->getRepository(self::CATEGORY_UUID)
  264. ->children($food);
  265. $this->assertCount(4, $children);
  266. $this->assertEquals('Fruits', $children[0]->getTitle());
  267. $this->assertEquals('Vegitables', $children[1]->getTitle());
  268. $this->assertEquals('Carrots', $children[2]->getTitle());
  269. $this->assertEquals('Potatoes', $children[3]->getTitle());
  270. $children = $this->em->getRepository(self::CATEGORY_UUID)
  271. ->children($food, true);
  272. $this->assertCount(2, $children);
  273. $this->assertEquals('Fruits', $children[0]->getTitle());
  274. $this->assertEquals('Vegitables', $children[1]->getTitle());
  275. $children = $this->em->getRepository(self::CATEGORY_UUID)
  276. ->children();
  277. $this->assertCount(6, $children);
  278. // path
  279. $path = $this->em->getRepository(self::CATEGORY_UUID)
  280. ->getPath($vegies);
  281. $this->assertCount(2, $path);
  282. $this->assertEquals('Food', $path[0]->getTitle());
  283. $this->assertEquals('Vegitables', $path[1]->getTitle());
  284. $carrots = $this->em->getRepository(self::CATEGORY_UUID)
  285. ->findOneByTitle('Carrots');
  286. $path = $this->em->getRepository(self::CATEGORY_UUID)
  287. ->getPath($carrots);
  288. $this->assertCount(3, $path);
  289. $this->assertEquals('Food', $path[0]->getTitle());
  290. $this->assertEquals('Vegitables', $path[1]->getTitle());
  291. $this->assertEquals('Carrots', $path[2]->getTitle());
  292. // leafs
  293. $leafs = $this->em->getRepository(self::CATEGORY_UUID)
  294. ->getLeafs($path[0]);
  295. $this->assertCount(3, $leafs);
  296. $this->assertEquals('Fruits', $leafs[0]->getTitle());
  297. $this->assertEquals('Carrots', $leafs[1]->getTitle());
  298. $this->assertEquals('Potatoes', $leafs[2]->getTitle());
  299. }
  300. protected function getUsedEntityFixtures()
  301. {
  302. return array(
  303. self::CATEGORY,
  304. self::CATEGORY_UUID,
  305. );
  306. }
  307. private function populateMore()
  308. {
  309. $vegies = $this->em->getRepository(self::CATEGORY)
  310. ->findOneByTitle('Vegitables');
  311. $cabbages = new Category();
  312. $cabbages->setParent($vegies);
  313. $cabbages->setTitle('Cabbages');
  314. $onions = new Category();
  315. $onions->setParent($vegies);
  316. $onions->setTitle('Onions');
  317. $this->em->persist($cabbages);
  318. $this->em->persist($onions);
  319. $this->em->flush();
  320. $this->em->clear();
  321. }
  322. private function populate()
  323. {
  324. $root = new Category();
  325. $root->setTitle("Food");
  326. $root2 = new Category();
  327. $root2->setTitle("Sports");
  328. $child = new Category();
  329. $child->setTitle("Fruits");
  330. $child->setParent($root);
  331. $child2 = new Category();
  332. $child2->setTitle("Vegitables");
  333. $child2->setParent($root);
  334. $childsChild = new Category();
  335. $childsChild->setTitle("Carrots");
  336. $childsChild->setParent($child2);
  337. $potatoes = new Category();
  338. $potatoes->setTitle("Potatoes");
  339. $potatoes->setParent($child2);
  340. $this->em->persist($root);
  341. $this->em->persist($root2);
  342. $this->em->persist($child);
  343. $this->em->persist($child2);
  344. $this->em->persist($childsChild);
  345. $this->em->persist($potatoes);
  346. $this->em->flush();
  347. $this->em->clear();
  348. }
  349. private function populateUuid()
  350. {
  351. $root = new CategoryUuid();
  352. $root->setTitle("Food");
  353. $root2 = new CategoryUuid();
  354. $root2->setTitle("Sports");
  355. $child = new CategoryUuid();
  356. $child->setTitle("Fruits");
  357. $child->setParent($root);
  358. $child2 = new CategoryUuid();
  359. $child2->setTitle("Vegitables");
  360. $child2->setParent($root);
  361. $childsChild = new CategoryUuid();
  362. $childsChild->setTitle("Carrots");
  363. $childsChild->setParent($child2);
  364. $potatoes = new CategoryUuid();
  365. $potatoes->setTitle("Potatoes");
  366. $potatoes->setParent($child2);
  367. $this->em->persist($root);
  368. $this->em->persist($root2);
  369. $this->em->persist($child);
  370. $this->em->persist($child2);
  371. $this->em->persist($childsChild);
  372. $this->em->persist($potatoes);
  373. $this->em->flush();
  374. $this->em->clear();
  375. }
  376. }