NestedTreeRootRepositoryTest.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?php
  2. namespace Gedmo\Tree;
  3. use Doctrine\Common\EventManager;
  4. use Tool\BaseTestCaseORM;
  5. use Doctrine\Common\Util\Debug;
  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 NestedTreeRootRepositoryTest extends BaseTestCaseORM
  16. {
  17. const 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. $this->populate();
  25. }
  26. public function testRepository()
  27. {
  28. $repo = $this->em->getRepository(self::CATEGORY);
  29. $carrots = $repo->findOneByTitle('Carrots');
  30. $path = $repo->getPath($carrots);
  31. $this->assertEquals(3, count($path));
  32. $this->assertEquals('Food', $path[0]->getTitle());
  33. $this->assertEquals('Vegitables', $path[1]->getTitle());
  34. $this->assertEquals('Carrots', $path[2]->getTitle());
  35. $vegies = $repo->findOneByTitle('Vegitables');
  36. $childCount = $repo->childCount($vegies);
  37. $this->assertEquals(2, $childCount);
  38. $food = $repo->findOneByTitle('Food');
  39. $childCount = $repo->childCount($food, true);
  40. $this->assertEquals(2, $childCount);
  41. $childCount = $repo->childCount($food);
  42. $this->assertEquals(4, $childCount);
  43. $childCount = $repo->childCount();
  44. $this->assertEquals(6, $childCount);
  45. }
  46. public function testAdvancedRepositoryFunctions()
  47. {
  48. $this->populateMore();
  49. $repo = $this->em->getRepository(self::CATEGORY);
  50. // verification
  51. $this->assertTrue($repo->verify());
  52. $dql = 'UPDATE ' . self::CATEGORY . ' node';
  53. $dql .= ' SET node.lft = 1';
  54. $dql .= ' WHERE node.id = 4';
  55. $this->em->createQuery($dql)->getSingleScalarResult();
  56. $this->em->clear(); // must clear cached entities
  57. $errors = $repo->verify();
  58. $this->assertEquals(2, count($errors));
  59. $this->assertEquals('index [1], duplicate on tree root: 1', $errors[0]);
  60. $this->assertEquals('index [4], missing on tree root: 1', $errors[1]);
  61. $dql = 'UPDATE ' . self::CATEGORY . ' node';
  62. $dql .= ' SET node.lft = 4';
  63. $dql .= ' WHERE node.id = 4';
  64. $this->em->createQuery($dql)->getSingleScalarResult();
  65. //@todo implement
  66. /*$this->em->clear();
  67. $repo->recover();
  68. $this->em->clear();
  69. $this->assertTrue($repo->verify());*/
  70. $this->em->clear();
  71. $onions = $repo->findOneByTitle('Onions');
  72. $this->assertEquals(11, $onions->getLeft());
  73. $this->assertEquals(12, $onions->getRight());
  74. // move up
  75. $repo->moveUp($onions);
  76. $this->em->refresh($onions);
  77. $this->assertEquals(9, $onions->getLeft());
  78. $this->assertEquals(10, $onions->getRight());
  79. $repo->moveUp($onions, true);
  80. $this->em->refresh($onions);
  81. $this->assertEquals(5, $onions->getLeft());
  82. $this->assertEquals(6, $onions->getRight());
  83. // move down
  84. $repo->moveDown($onions, 2);
  85. $this->em->refresh($onions);
  86. $this->assertEquals(9, $onions->getLeft());
  87. $this->assertEquals(10, $onions->getRight());
  88. // reorder
  89. $this->em->clear();
  90. $node = $repo->findOneByTitle('Food');
  91. $repo->reorder($node, 'title', 'ASC', false);
  92. $this->em->clear();
  93. $node = $repo->findOneByTitle('Cabbages');
  94. $this->assertEquals(5, $node->getLeft());
  95. $this->assertEquals(6, $node->getRight());
  96. $node = $repo->findOneByTitle('Carrots');
  97. $this->assertEquals(7, $node->getLeft());
  98. $this->assertEquals(8, $node->getRight());
  99. $node = $repo->findOneByTitle('Onions');
  100. $this->assertEquals(9, $node->getLeft());
  101. $this->assertEquals(10, $node->getRight());
  102. $node = $repo->findOneByTitle('Potatoes');
  103. $this->assertEquals(11, $node->getLeft());
  104. $this->assertEquals(12, $node->getRight());
  105. // leafs
  106. $leafs = $repo->getLeafs($node);
  107. $this->assertEquals(5, count($leafs));
  108. $this->assertEquals('Fruits', $leafs[0]->getTitle());
  109. $this->assertEquals('Cabbages', $leafs[1]->getTitle());
  110. $this->assertEquals('Carrots', $leafs[2]->getTitle());
  111. $this->assertEquals('Onions', $leafs[3]->getTitle());
  112. $this->assertEquals('Potatoes', $leafs[4]->getTitle());
  113. // remove
  114. $this->em->clear();
  115. $node = $repo->findOneByTitle('Fruits');
  116. $id = $node->getId();
  117. $repo->removeFromTree($node);
  118. $this->assertTrue(is_null($repo->find($id)));
  119. $node = $repo->findOneByTitle('Vegitables');
  120. $id = $node->getId();
  121. $repo->removeFromTree($node);
  122. $this->assertTrue(is_null($repo->find($id)));
  123. $this->em->clear();
  124. $node = $repo->findOneByTitle('Cabbages');
  125. $this->assertEquals(1, $node->getRoot());
  126. $this->assertEquals(1, $node->getParent()->getId());
  127. }
  128. protected function getUsedEntityFixtures()
  129. {
  130. return array(
  131. self::CATEGORY
  132. );
  133. }
  134. private function populateMore()
  135. {
  136. $vegies = $this->em->getRepository(self::CATEGORY)
  137. ->findOneByTitle('Vegitables');
  138. $cabbages = new RootCategory();
  139. $cabbages->setParent($vegies);
  140. $cabbages->setTitle('Cabbages');
  141. $onions = new RootCategory();
  142. $onions->setParent($vegies);
  143. $onions->setTitle('Onions');
  144. $this->em->persist($cabbages);
  145. $this->em->persist($onions);
  146. $this->em->flush();
  147. $this->em->clear();
  148. }
  149. private function populate()
  150. {
  151. $root = new RootCategory();
  152. $root->setTitle("Food");
  153. $root2 = new RootCategory();
  154. $root2->setTitle("Sports");
  155. $child = new RootCategory();
  156. $child->setTitle("Fruits");
  157. $child->setParent($root);
  158. $child2 = new RootCategory();
  159. $child2->setTitle("Vegitables");
  160. $child2->setParent($root);
  161. $childsChild = new RootCategory();
  162. $childsChild->setTitle("Carrots");
  163. $childsChild->setParent($child2);
  164. $potatoes = new RootCategory();
  165. $potatoes->setTitle("Potatoes");
  166. $potatoes->setParent($child2);
  167. $this->em->persist($root);
  168. $this->em->persist($root2);
  169. $this->em->persist($child);
  170. $this->em->persist($child2);
  171. $this->em->persist($childsChild);
  172. $this->em->persist($potatoes);
  173. $this->em->flush();
  174. $this->em->clear();
  175. }
  176. }