ClosureTreeTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. use Tree\Fixture\Closure\News;
  8. use Tree\Fixture\Closure\CategoryClosure;
  9. use Tree\Fixture\Closure\CategoryWithoutLevel;
  10. use Tree\Fixture\Closure\CategoryWithoutLevelClosure;
  11. /**
  12. * These are tests for Tree behavior
  13. *
  14. * @author Gustavo Adrian <comfortablynumb84@gmail.com>
  15. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  16. * @link http://www.gediminasm.org
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. class ClosureTreeTest extends BaseTestCaseORM
  20. {
  21. const CATEGORY = "Tree\\Fixture\\Closure\\Category";
  22. const CLOSURE = "Tree\\Fixture\\Closure\\CategoryClosure";
  23. const PERSON = "Tree\\Fixture\\Closure\\Person";
  24. const USER = "Tree\\Fixture\\Closure\\User";
  25. const PERSON_CLOSURE = "Tree\\Fixture\\Closure\\PersonClosure";
  26. const NEWS = "Tree\\Fixture\\Closure\\News";
  27. const CATEGORY_WITHOUT_LEVEL = "Tree\\Fixture\\Closure\\CategoryWithoutLevel";
  28. const CATEGORY_WITHOUT_LEVEL_CLOSURE = "Tree\\Fixture\\Closure\\CategoryWithoutLevelClosure";
  29. protected $listener;
  30. protected function setUp()
  31. {
  32. parent::setUp();
  33. $this->listener = new TreeListener;
  34. $evm = new EventManager;
  35. $evm->addEventSubscriber($this->listener);
  36. $this->getMockSqliteEntityManager($evm);
  37. $this->populate();
  38. }
  39. /*public function testHeavyLoad()
  40. {
  41. $start = microtime(true);
  42. $dumpTime = function($start, $msg) {
  43. $took = microtime(true) - $start;
  44. $minutes = intval($took / 60); $seconds = $took % 60;
  45. echo sprintf("%s --> %02d:%02d", $msg, $minutes, $seconds) . PHP_EOL;
  46. };
  47. $repo = $this->em->getRepository(self::CATEGORY);
  48. $parent = null;
  49. $num = 800;
  50. for($i = 0; $i < 500; $i++) {
  51. $cat = new Category;
  52. $cat->setParent($parent);
  53. $cat->setTitle('cat'.$i);
  54. $this->em->persist($cat);
  55. // siblings
  56. $rnd = rand(0, 3);
  57. for ($j = 0; $j < $rnd; $j++) {
  58. $siblingCat = new Category;
  59. $siblingCat->setTitle('cat'.$i.$j);
  60. $siblingCat->setParent($cat);
  61. $this->em->persist($siblingCat);
  62. }
  63. $num += $rnd;
  64. $parent = $cat;
  65. }
  66. $this->em->flush();
  67. $dumpTime($start, $num.' - inserts took:');
  68. $start = microtime(true);
  69. // test moving
  70. $target = $repo->findOneByTitle('cat300');
  71. $dest = $repo->findOneByTitle('cat2000');
  72. $target->setParent($dest);
  73. $target2 = $repo->findOneByTitle('cat450');
  74. $dest2 = $repo->findOneByTitle('cat2500');
  75. $target2->setParent($dest2);
  76. $this->em->flush();
  77. $dumpTime($start, 'moving took:');
  78. }*/
  79. public function testClosureTree()
  80. {
  81. $repo = $this->em->getRepository(self::CATEGORY);
  82. $closureRepo = $this->em->getRepository(self::CLOSURE);
  83. $food = $repo->findOneByTitle('Food');
  84. $dql = 'SELECT c FROM '.self::CLOSURE.' c';
  85. $dql .= ' WHERE c.ancestor = :ancestor';
  86. $query = $this->em->createQuery($dql);
  87. $query->setParameter('ancestor', $food);
  88. $foodClosures = $query->getResult();
  89. $this->assertCount(12, $foodClosures);
  90. foreach ($foodClosures as $closure) {
  91. $descendant = $closure->getDescendant();
  92. if ($descendant === $food) {
  93. $this->assertEquals(0, $closure->getDepth());
  94. continue;
  95. }
  96. $descendantTitle = $descendant->getTitle();
  97. $query->setParameter('ancestor', $descendant);
  98. $descendantClosures = $query->getResult();
  99. switch ($descendantTitle) {
  100. case 'Fruits':
  101. $this->assertCount(5, $descendantClosures);
  102. $this->assertEquals(1, $closure->getDepth());
  103. break;
  104. case 'Oranges':
  105. $this->assertCount(1, $descendantClosures);
  106. $this->assertEquals(2, $closure->getDepth());
  107. break;
  108. case 'Berries':
  109. $this->assertCount(2, $descendantClosures);
  110. $this->assertEquals(2, $closure->getDepth());
  111. break;
  112. case 'Vegitables':
  113. $this->assertCount(3, $descendantClosures);
  114. $this->assertEquals(1, $closure->getDepth());
  115. break;
  116. case 'Milk':
  117. $this->assertCount(3, $descendantClosures);
  118. $this->assertEquals(1, $closure->getDepth());
  119. break;
  120. case 'Cheese':
  121. $this->assertCount(2, $descendantClosures);
  122. $this->assertEquals(2, $closure->getDepth());
  123. break;
  124. case 'Strawberries':
  125. $this->assertCount(1, $descendantClosures);
  126. $this->assertEquals(3, $closure->getDepth());
  127. break;
  128. }
  129. }
  130. }
  131. public function testUpdateOfParent()
  132. {
  133. $repo = $this->em->getRepository(self::CATEGORY);
  134. $strawberries = $repo->findOneByTitle('Strawberries');
  135. $cheese = $repo->findOneByTitle('Cheese');
  136. $strawberries->setParent($cheese);
  137. $this->em->persist($strawberries);
  138. $this->em->flush();
  139. $dql = 'SELECT c FROM '.self::CLOSURE.' c';
  140. $dql .= ' WHERE c.descendant = :descendant';
  141. $query = $this->em->createQuery($dql);
  142. $query->setParameter('descendant', $strawberries);
  143. $closures = $query->getResult();
  144. $this->assertTrue($this->hasAncestor($closures, 'Cheese'));
  145. $this->assertTrue($this->hasAncestor($closures, 'Milk'));
  146. $this->assertTrue($this->hasAncestor($closures, 'Food'));
  147. $this->assertFalse($this->hasAncestor($closures, 'Berries'));
  148. $this->assertFalse($this->hasAncestor($closures, 'Fruits'));
  149. }
  150. public function testAnotherUpdateOfParent()
  151. {
  152. $repo = $this->em->getRepository(self::CATEGORY);
  153. $strawberries = $repo->findOneByTitle('Strawberries');
  154. $strawberries->setParent(null);
  155. $this->em->persist($strawberries);
  156. $this->em->flush();
  157. $dql = 'SELECT c FROM '.self::CLOSURE.' c';
  158. $dql .= ' WHERE c.descendant = :descendant';
  159. $query = $this->em->createQuery($dql);
  160. $query->setParameter('descendant', $strawberries);
  161. $closures = $query->getResult();
  162. $this->assertCount(1, $closures);
  163. $this->assertTrue($this->hasAncestor($closures, 'Strawberries'));
  164. }
  165. public function testBranchRemoval()
  166. {
  167. $repo = $this->em->getRepository(self::CATEGORY);
  168. $fruits = $repo->findOneByTitle('Fruits');
  169. $id = $fruits->getId();
  170. $this->em->remove($fruits);
  171. $this->em->flush();
  172. $dql = 'SELECT COUNT(c) FROM '.self::CLOSURE.' c';
  173. $dql .= ' JOIN c.descendant d';
  174. $dql .= ' JOIN c.ancestor a';
  175. $dql .= ' WHERE (a.id = :id OR d.id = :id)';
  176. $query = $this->em->createQuery($dql);
  177. $query->setParameter('id', $id);
  178. $this->assertEquals(0, $query->getSingleScalarResult());
  179. // pdo_sqlite will not cascade
  180. }
  181. /**
  182. * @expectedException Gedmo\Exception\UnexpectedValueException
  183. */
  184. public function testSettingParentToChild()
  185. {
  186. $repo = $this->em->getRepository(self::CATEGORY);
  187. $fruits = $repo->findOneByTitle('Fruits');
  188. $strawberries = $repo->findOneByTitle('Strawberries');
  189. $fruits->setParent($strawberries);
  190. $this->em->flush();
  191. }
  192. public function testIfEntityHasNotIncludedTreeLevelFieldThenDontProcessIt()
  193. {
  194. $listener = $this->getMock('Gedmo\Tree\TreeListener', array('getStrategy'));
  195. $strategy = $this->getMock('Gedmo\Tree\Strategy\ORM\Closure', array('setLevelFieldOnPendingNodes'), array($listener));
  196. $listener->expects($this->any())
  197. ->method('getStrategy')
  198. ->will($this->returnValue($strategy));
  199. $strategy->expects($this->never())
  200. ->method('setLevelFieldOnPendingNodes');
  201. $evm = $this->em->getEventManager();
  202. $evm->removeEventListener($this->listener->getSubscribedEvents(), $this->listener);
  203. $evm->addEventListener($this->listener->getSubscribedEvents(), $this->listener);
  204. $cat = new CategoryWithoutLevel();
  205. $cat->setTitle('Test');
  206. $this->em->persist($cat);
  207. $this->em->flush();
  208. }
  209. private function hasAncestor($closures, $name)
  210. {
  211. $result = false;
  212. foreach ($closures as $closure) {
  213. $ancestor = $closure->getAncestor();
  214. if ($ancestor->getTitle() === $name) {
  215. $result = true;
  216. break;
  217. }
  218. }
  219. return $result;
  220. }
  221. protected function getUsedEntityFixtures()
  222. {
  223. return array(
  224. self::CATEGORY,
  225. self::CLOSURE,
  226. self::PERSON,
  227. self::PERSON_CLOSURE,
  228. self::USER,
  229. self::NEWS,
  230. self::CATEGORY_WITHOUT_LEVEL,
  231. self::CATEGORY_WITHOUT_LEVEL_CLOSURE
  232. );
  233. }
  234. private function populate()
  235. {
  236. $food = new Category;
  237. $food->setTitle("Food");
  238. $this->em->persist($food);
  239. $fruits = new Category;
  240. $fruits->setTitle('Fruits');
  241. $fruits->setParent($food);
  242. $this->em->persist($fruits);
  243. $oranges = new Category;
  244. $oranges->setTitle('Oranges');
  245. $oranges->setParent($fruits);
  246. $this->em->persist($oranges);
  247. $lemons = new Category;
  248. $lemons->setTitle('Lemons');
  249. $lemons->setParent($fruits);
  250. $this->em->persist($lemons);
  251. $berries = new Category;
  252. $berries->setTitle('Berries');
  253. $berries->setParent($fruits);
  254. $this->em->persist($berries);
  255. $strawberries = new Category;
  256. $strawberries->setTitle('Strawberries');
  257. $strawberries->setParent($berries);
  258. $this->em->persist($strawberries);
  259. $vegitables = new Category;
  260. $vegitables->setTitle('Vegitables');
  261. $vegitables->setParent($food);
  262. $this->em->persist($vegitables);
  263. $cabbages = new Category;
  264. $cabbages->setTitle('Cabbages');
  265. $cabbages->setParent($vegitables);
  266. $this->em->persist($cabbages);
  267. $carrots = new Category;
  268. $carrots->setTitle('Carrots');
  269. $carrots->setParent($vegitables);
  270. $this->em->persist($carrots);
  271. $milk = new Category;
  272. $milk->setTitle('Milk');
  273. $milk->setParent($food);
  274. $this->em->persist($milk);
  275. $cheese = new Category;
  276. $cheese->setTitle('Cheese');
  277. $cheese->setParent($milk);
  278. $this->em->persist($cheese);
  279. $mouldCheese = new Category;
  280. $mouldCheese->setTitle('Mould cheese');
  281. $mouldCheese->setParent($cheese);
  282. $this->em->persist($mouldCheese);
  283. $this->em->flush();
  284. }
  285. public function testCascadePersistTree()
  286. {
  287. $politics = new Category();
  288. $politics->setTitle('Politics');
  289. $news = new News('Lorem ipsum', $politics);
  290. $this->em->persist($news);
  291. $this->em->flush();
  292. $closure = $this->em->createQueryBuilder()
  293. ->select('c')
  294. ->from(self::CLOSURE, 'c')
  295. ->where('c.ancestor = :ancestor')
  296. ->setParameter('ancestor', $politics->getId())
  297. ->getQuery()
  298. ->getResult();
  299. $this->assertCount(1, $closure);
  300. }
  301. }