ClosureTreeRepositoryTest.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. namespace Gedmo\Tree;
  3. use Doctrine\Common\EventManager;
  4. use Tool\BaseTestCaseORM;
  5. use Tree\Fixture\Closure\Category;
  6. /**
  7. * These are tests for Tree behavior
  8. *
  9. * @author Gustavo Adrian <comfortablynumb84@gmail.com>
  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 ClosureTreeRepositoryTest extends BaseTestCaseORM
  16. {
  17. const CATEGORY = "Tree\\Fixture\\Closure\\Category";
  18. const CLOSURE = "Tree\\Fixture\\Closure\\CategoryClosure";
  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 testChildCount()
  28. {
  29. $repo = $this->em->getRepository(self::CATEGORY);
  30. $food = $repo->findOneByTitle('Food');
  31. $directCount = $repo->childCount($food, true);
  32. $this->assertEquals(3, $directCount);
  33. $fruits = $repo->findOneByTitle('Fruits');
  34. $count = $repo->childCount($fruits);
  35. $this->assertEquals(4, $count);
  36. $rootCount = $repo->childCount(null, true);
  37. $this->assertEquals(1, $rootCount);
  38. }
  39. public function testPath()
  40. {
  41. $repo = $this->em->getRepository(self::CATEGORY);
  42. $fruits = $repo->findOneByTitle('Fruits');
  43. $path = $repo->getPath($fruits);
  44. $this->assertEquals(2, count($path));
  45. $this->assertEquals('Food', $path[0]->getTitle());
  46. $this->assertEquals('Fruits', $path[1]->getTitle());
  47. $strawberries = $repo->findOneByTitle('Strawberries');
  48. $path = $repo->getPath($strawberries);
  49. $this->assertEquals(4, count($path));
  50. $this->assertEquals('Food', $path[0]->getTitle());
  51. $this->assertEquals('Fruits', $path[1]->getTitle());
  52. $this->assertEquals('Berries', $path[2]->getTitle());
  53. $this->assertEquals('Strawberries', $path[3]->getTitle());
  54. }
  55. public function testChildren()
  56. {
  57. $repo = $this->em->getRepository(self::CATEGORY);
  58. $fruits = $repo->findOneByTitle('Fruits');
  59. // direct children of node, sorted by title ascending order
  60. $children = $repo->children($fruits, true, 'title');
  61. $this->assertEquals(3, count($children));
  62. $this->assertEquals('Berries', $children[0]->getTitle());
  63. $this->assertEquals('Lemons', $children[1]->getTitle());
  64. $this->assertEquals('Oranges', $children[2]->getTitle());
  65. // all children of node
  66. $children = $repo->children($fruits);
  67. $this->assertEquals(4, count($children));
  68. $this->assertEquals('Oranges', $children[0]->getTitle());
  69. $this->assertEquals('Lemons', $children[1]->getTitle());
  70. $this->assertEquals('Berries', $children[2]->getTitle());
  71. $this->assertEquals('Strawberries', $children[3]->getTitle());
  72. // direct root nodes
  73. $children = $repo->children(null, true, 'title');
  74. $this->assertEquals(1, count($children));
  75. $this->assertEquals('Food', $children[0]->getTitle());
  76. // all tree
  77. $children = $repo->children();
  78. $this->assertEquals(12, count($children));
  79. }
  80. public function testSingleNodeRemoval()
  81. {
  82. $repo = $this->em->getRepository(self::CATEGORY);
  83. $fruits = $repo->findOneByTitle('Fruits');
  84. $repo->removeFromTree($fruits);
  85. // ensure in memory node integrity
  86. $this->em->flush();
  87. $food = $repo->findOneByTitle('Food');
  88. $children = $repo->children($food, true);
  89. $this->assertEquals(5, count($children));
  90. $berries = $repo->findOneByTitle('Berries');
  91. $this->assertEquals(1, $repo->childCount($berries, true));
  92. $lemons = $repo->findOneByTitle('Lemons');
  93. $this->assertEquals(0, $repo->childCount($lemons, true));
  94. $repo->removeFromTree($food);
  95. $vegitables = $repo->findOneByTitle('Vegitables');
  96. $this->assertEquals(2, $repo->childCount($vegitables, true));
  97. $this->assertEquals(null, $vegitables->getParent());
  98. $repo->removeFromTree($lemons);
  99. $this->assertEquals(4, count($repo->children(null, true)));
  100. }
  101. protected function getUsedEntityFixtures()
  102. {
  103. return array(
  104. self::CATEGORY,
  105. self::CLOSURE
  106. );
  107. }
  108. private function populate()
  109. {
  110. $food = new Category;
  111. $food->setTitle("Food");
  112. $this->em->persist($food);
  113. $fruits = new Category;
  114. $fruits->setTitle('Fruits');
  115. $fruits->setParent($food);
  116. $this->em->persist($fruits);
  117. $oranges = new Category;
  118. $oranges->setTitle('Oranges');
  119. $oranges->setParent($fruits);
  120. $this->em->persist($oranges);
  121. $lemons = new Category;
  122. $lemons->setTitle('Lemons');
  123. $lemons->setParent($fruits);
  124. $this->em->persist($lemons);
  125. $berries = new Category;
  126. $berries->setTitle('Berries');
  127. $berries->setParent($fruits);
  128. $this->em->persist($berries);
  129. $strawberries = new Category;
  130. $strawberries->setTitle('Strawberries');
  131. $strawberries->setParent($berries);
  132. $this->em->persist($strawberries);
  133. $vegitables = new Category;
  134. $vegitables->setTitle('Vegitables');
  135. $vegitables->setParent($food);
  136. $this->em->persist($vegitables);
  137. $cabbages = new Category;
  138. $cabbages->setTitle('Cabbages');
  139. $cabbages->setParent($vegitables);
  140. $this->em->persist($cabbages);
  141. $carrots = new Category;
  142. $carrots->setTitle('Carrots');
  143. $carrots->setParent($vegitables);
  144. $this->em->persist($carrots);
  145. $milk = new Category;
  146. $milk->setTitle('Milk');
  147. $milk->setParent($food);
  148. $this->em->persist($milk);
  149. $cheese = new Category;
  150. $cheese->setTitle('Cheese');
  151. $cheese->setParent($milk);
  152. $this->em->persist($cheese);
  153. $mouldCheese = new Category;
  154. $mouldCheese->setTitle('Mould cheese');
  155. $mouldCheese->setParent($cheese);
  156. $this->em->persist($mouldCheese);
  157. $this->em->flush();
  158. }
  159. }