RepositoryTest.php 16KB

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