ClosureTreeTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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\Closure\Category;
  7. /**
  8. * These are tests for Tree behavior
  9. *
  10. * @author Gustavo Adrian <comfortablynumb84@gmail.com>
  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 ClosureTreeTest extends BaseTestCaseORM
  17. {
  18. const CATEGORY = "Tree\\Fixture\\Closure\\Category";
  19. const CLOSURE = "Tree\\Fixture\\Closure\\CategoryClosure";
  20. private $food;
  21. private $sports;
  22. private $fruits;
  23. private $vegitables;
  24. private $carrots;
  25. private $potatoes;
  26. protected function setUp()
  27. {
  28. parent::setUp();
  29. $evm = new EventManager;
  30. $evm->addEventSubscriber(new TreeListener);
  31. $this->getMockSqliteEntityManager($evm);
  32. $this->populate();
  33. }
  34. public function test_insertNodes_verifyClosurePaths()
  35. {
  36. // We check the inserted nodes fields from the closure table
  37. $repo = $this->em->getRepository(self::CLOSURE);
  38. $rows = $this->em->createQuery(sprintf('SELECT c FROM %s c ORDER BY c.ancestor ASC, c.descendant ASC, c.depth ASC', self::CLOSURE))
  39. ->getArrayResult();
  40. // Root self referencing row and descendants
  41. $this->assertEquals($rows[0]['ancestor'], $this->food->getId());
  42. $this->assertEquals($rows[0]['descendant'], $this->food->getId());
  43. $this->assertEquals($rows[0]['depth'], 0);
  44. $this->assertEquals($rows[1]['ancestor'], $this->food->getId());
  45. $this->assertEquals($rows[1]['descendant'], $this->fruits->getId());
  46. $this->assertEquals($rows[1]['depth'], 1);
  47. $this->assertEquals($rows[2]['ancestor'], $this->food->getId());
  48. $this->assertEquals($rows[2]['descendant'], $this->vegitables->getId());
  49. $this->assertEquals($rows[2]['depth'], 1);
  50. $this->assertEquals($rows[3]['ancestor'], $this->food->getId());
  51. $this->assertEquals($rows[3]['descendant'], $this->carrots->getId());
  52. $this->assertEquals($rows[3]['depth'], 2);
  53. $this->assertEquals($rows[4]['ancestor'], $this->food->getId());
  54. $this->assertEquals($rows[4]['descendant'], $this->potatoes->getId());
  55. $this->assertEquals($rows[4]['depth'], 2);
  56. // Sports self referencing row
  57. $this->assertEquals($rows[5]['ancestor'], $this->sports->getId());
  58. $this->assertEquals($rows[5]['descendant'], $this->sports->getId());
  59. $this->assertEquals($rows[5]['depth'], 0);
  60. // Fruits self referencing row
  61. $this->assertEquals($rows[6]['ancestor'], $this->fruits->getId());
  62. $this->assertEquals($rows[6]['descendant'], $this->fruits->getId());
  63. $this->assertEquals($rows[6]['depth'], 0);
  64. // Vegitables self referencing row and descendants
  65. $this->assertEquals($rows[7]['ancestor'], $this->vegitables->getId());
  66. $this->assertEquals($rows[7]['descendant'], $this->vegitables->getId());
  67. $this->assertEquals($rows[7]['depth'], 0);
  68. $this->assertEquals($rows[8]['ancestor'], $this->vegitables->getId());
  69. $this->assertEquals($rows[8]['descendant'], $this->carrots->getId());
  70. $this->assertEquals($rows[8]['depth'], 1);
  71. $this->assertEquals($rows[9]['ancestor'], $this->vegitables->getId());
  72. $this->assertEquals($rows[9]['descendant'], $this->potatoes->getId());
  73. $this->assertEquals($rows[9]['depth'], 1);
  74. // Carrots self referencing row
  75. $this->assertEquals($rows[10]['ancestor'], $this->carrots->getId());
  76. $this->assertEquals($rows[10]['descendant'], $this->carrots->getId());
  77. $this->assertEquals($rows[10]['depth'], 0);
  78. // Potatoes self referencing row
  79. $this->assertEquals($rows[11]['ancestor'], $this->potatoes->getId());
  80. $this->assertEquals($rows[11]['descendant'], $this->potatoes->getId());
  81. $this->assertEquals($rows[11]['depth'], 0);
  82. }
  83. public function test_childcount_afterInsertingNodesChildCountIsCalculated()
  84. {
  85. $this->em->refresh($this->food);
  86. $this->em->refresh($this->sports);
  87. $this->em->refresh($this->fruits);
  88. $this->em->refresh($this->vegitables);
  89. $this->em->refresh($this->carrots);
  90. $this->em->refresh($this->potatoes);
  91. $this->assertEquals($this->food->getChildCount(), 4);
  92. $this->assertEquals($this->sports->getChildCount(), 0);
  93. $this->assertEquals($this->fruits->getChildCount(), 0);
  94. $this->assertEquals($this->vegitables->getChildCount(), 2);
  95. $this->assertEquals($this->carrots->getChildCount(), 0);
  96. $this->assertEquals($this->potatoes->getChildCount(), 0);
  97. }
  98. public function test_updateNodes_moveASubtreeVerifyTreeClosurePathsAndVerifyChildCountField()
  99. {
  100. // We change a subtree's location
  101. $vegitables = $this->em->getRepository(self::CATEGORY)
  102. ->findOneByTitle('Vegitables');
  103. $sports = $this->em->getRepository(self::CATEGORY)
  104. ->findOneByTitle('Sports');
  105. $vegitables->setParent($sports);
  106. $this->em->persist($vegitables);
  107. $this->em->flush();
  108. // We then verify the closure paths
  109. $repo = $this->em->getRepository(self::CLOSURE);
  110. $rows = $this->em->createQuery(sprintf('SELECT c FROM %s c ORDER BY c.ancestor ASC, c.descendant ASC, c.depth ASC', self::CLOSURE))
  111. ->getArrayResult();
  112. // Food self referencing row and descendants
  113. $this->assertEquals($rows[0]['ancestor'], $this->food->getId());
  114. $this->assertEquals($rows[0]['descendant'], $this->food->getId());
  115. $this->assertEquals($rows[0]['depth'], 0);
  116. $this->assertEquals($rows[1]['ancestor'], $this->food->getId());
  117. $this->assertEquals($rows[1]['descendant'], $this->fruits->getId());
  118. $this->assertEquals($rows[1]['depth'], 1);
  119. // Sports self referencing row and descendants
  120. $this->assertEquals($rows[2]['ancestor'], $this->sports->getId());
  121. $this->assertEquals($rows[2]['descendant'], $this->sports->getId());
  122. $this->assertEquals($rows[2]['depth'], 0);
  123. $this->assertEquals($rows[3]['ancestor'], $this->sports->getId());
  124. $this->assertEquals($rows[3]['descendant'], $this->vegitables->getId());
  125. $this->assertEquals($rows[3]['depth'], 1);
  126. $this->assertEquals($rows[4]['ancestor'], $this->sports->getId());
  127. $this->assertEquals($rows[4]['descendant'], $this->carrots->getId());
  128. $this->assertEquals($rows[4]['depth'], 2);
  129. $this->assertEquals($rows[5]['ancestor'], $this->sports->getId());
  130. $this->assertEquals($rows[5]['descendant'], $this->potatoes->getId());
  131. $this->assertEquals($rows[5]['depth'], 2);
  132. // Fruits self referencing row
  133. $this->assertEquals($rows[6]['ancestor'], $this->fruits->getId());
  134. $this->assertEquals($rows[6]['descendant'], $this->fruits->getId());
  135. $this->assertEquals($rows[6]['depth'], 0);
  136. // Vegitables self referencing row and descendants
  137. $this->assertEquals($rows[7]['ancestor'], $this->vegitables->getId());
  138. $this->assertEquals($rows[7]['descendant'], $this->vegitables->getId());
  139. $this->assertEquals($rows[7]['depth'], 0);
  140. $this->assertEquals($rows[8]['ancestor'], $this->vegitables->getId());
  141. $this->assertEquals($rows[8]['descendant'], $this->carrots->getId());
  142. $this->assertEquals($rows[8]['depth'], 1);
  143. $this->assertEquals($rows[9]['ancestor'], $this->vegitables->getId());
  144. $this->assertEquals($rows[9]['descendant'], $this->potatoes->getId());
  145. $this->assertEquals($rows[9]['depth'], 1);
  146. // Carrots self referencing row
  147. $this->assertEquals($rows[10]['ancestor'], $this->carrots->getId());
  148. $this->assertEquals($rows[10]['descendant'], $this->carrots->getId());
  149. $this->assertEquals($rows[10]['depth'], 0);
  150. // Potatoes self referencing row
  151. $this->assertEquals($rows[11]['ancestor'], $this->potatoes->getId());
  152. $this->assertEquals($rows[11]['descendant'], $this->potatoes->getId());
  153. $this->assertEquals($rows[11]['depth'], 0);
  154. // We check the childCount field
  155. $this->em->refresh($this->food);
  156. $this->em->refresh($this->sports);
  157. $this->em->refresh($this->fruits);
  158. $this->em->refresh($this->vegitables);
  159. $this->em->refresh($this->carrots);
  160. $this->em->refresh($this->potatoes);
  161. $this->assertEquals($this->food->getChildCount(), 1);
  162. $this->assertEquals($this->sports->getChildCount(), 3);
  163. $this->assertEquals($this->fruits->getChildCount(), 0);
  164. $this->assertEquals($this->vegitables->getChildCount(), 2);
  165. $this->assertEquals($this->carrots->getChildCount(), 0);
  166. $this->assertEquals($this->potatoes->getChildCount(), 0);
  167. }
  168. public function test_removeNode_removesClosurePathsOfNodeAndVerifyTree()
  169. {
  170. // We remove a subtree
  171. $vegitables = $this->em->getRepository(self::CATEGORY)
  172. ->findOneByTitle('Vegitables');
  173. $this->em->remove($vegitables);
  174. $this->em->flush();
  175. // We then verify the closure paths
  176. $repo = $this->em->getRepository(self::CLOSURE);
  177. $rows = $this->em->createQuery(sprintf('SELECT c FROM %s c ORDER BY c.ancestor ASC, c.descendant ASC, c.depth ASC', self::CLOSURE))
  178. ->getArrayResult();
  179. // Food self referencing row and descendants
  180. $this->assertEquals($rows[0]['ancestor'], $this->food->getId());
  181. $this->assertEquals($rows[0]['descendant'], $this->food->getId());
  182. $this->assertEquals($rows[0]['depth'], 0);
  183. $this->assertEquals($rows[1]['ancestor'], $this->food->getId());
  184. $this->assertEquals($rows[1]['descendant'], $this->fruits->getId());
  185. $this->assertEquals($rows[1]['depth'], 1);
  186. // Sports self referencing row
  187. $this->assertEquals($rows[2]['ancestor'], $this->sports->getId());
  188. $this->assertEquals($rows[2]['descendant'], $this->sports->getId());
  189. $this->assertEquals($rows[2]['depth'], 0);
  190. // Fruits self referencing row
  191. $this->assertEquals($rows[3]['ancestor'], $this->fruits->getId());
  192. $this->assertEquals($rows[3]['descendant'], $this->fruits->getId());
  193. $this->assertEquals($rows[3]['depth'], 0);
  194. // We check the childCount field
  195. $this->em->refresh($this->food);
  196. $this->em->refresh($this->sports);
  197. $this->em->refresh($this->fruits);
  198. $this->assertEquals($this->food->getChildCount(), 1);
  199. $this->assertEquals($this->sports->getChildCount(), 0);
  200. $this->assertEquals($this->fruits->getChildCount(), 0);
  201. }
  202. protected function getUsedEntityFixtures()
  203. {
  204. return array(
  205. self::CATEGORY,
  206. self::CLOSURE
  207. );
  208. }
  209. private function populate()
  210. {
  211. $root = new Category();
  212. $root->setTitle("Food");
  213. $this->food = $root;
  214. $root2 = new Category();
  215. $root2->setTitle("Sports");
  216. $this->sports = $root2;
  217. $child = new Category();
  218. $child->setTitle("Fruits");
  219. $child->setParent($root);
  220. $this->fruits = $child;
  221. $child2 = new Category();
  222. $child2->setTitle("Vegitables");
  223. $child2->setParent($root);
  224. $this->vegitables = $child2;
  225. $childsChild = new Category();
  226. $childsChild->setTitle("Carrots");
  227. $childsChild->setParent($child2);
  228. $this->carrots = $childsChild;
  229. $potatoes = new Category();
  230. $potatoes->setTitle("Potatoes");
  231. $potatoes->setParent($child2);
  232. $this->potatoes = $potatoes;
  233. $this->em->persist($this->food);
  234. $this->em->persist($this->sports);
  235. $this->em->persist($this->fruits);
  236. $this->em->persist($this->vegitables);
  237. $this->em->persist($this->carrots);
  238. $this->em->persist($this->potatoes);
  239. $this->em->flush();
  240. }
  241. }