NestedTreeRootRepositoryTest.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. namespace Gedmo\Tree;
  3. use Doctrine\Common\Util\Debug;
  4. use Tree\Fixture\RootCategory;
  5. /**
  6. * These are tests for Tree behavior
  7. *
  8. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  9. * @package Gedmo.Tree
  10. * @link http://www.gediminasm.org
  11. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  12. */
  13. class NestedTreeRootRepositoryTest extends \PHPUnit_Framework_TestCase
  14. {
  15. const TEST_ENTITY_CLASS = "Tree\Fixture\RootCategory";
  16. private $em;
  17. public function setUp()
  18. {
  19. $config = new \Doctrine\ORM\Configuration();
  20. $config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache);
  21. $config->setQueryCacheImpl(new \Doctrine\Common\Cache\ArrayCache);
  22. $config->setProxyDir(__DIR__ . '/Proxy');
  23. $config->setProxyNamespace('Gedmo\Tree\Proxies');
  24. $config->setMetadataDriverImpl($config->newDefaultAnnotationDriver());
  25. $conn = array(
  26. 'driver' => 'pdo_sqlite',
  27. 'memory' => true,
  28. );
  29. //$config->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger());
  30. $evm = new \Doctrine\Common\EventManager();
  31. $treeListener = new TreeListener();
  32. $evm->addEventSubscriber($treeListener);
  33. $this->em = \Doctrine\ORM\EntityManager::create($conn, $config, $evm);
  34. $schemaTool = new \Doctrine\ORM\Tools\SchemaTool($this->em);
  35. $schemaTool->dropSchema(array());
  36. $schemaTool->createSchema(array(
  37. $this->em->getClassMetadata(self::TEST_ENTITY_CLASS)
  38. ));
  39. $this->populate();
  40. }
  41. public function testRepository()
  42. {
  43. $repo = $this->em->getRepository(self::TEST_ENTITY_CLASS);
  44. $carrots = $repo->findOneByTitle('Carrots');
  45. $path = $repo->getPath($carrots);
  46. $this->assertEquals(3, count($path));
  47. $this->assertEquals('Food', $path[0]->getTitle());
  48. $this->assertEquals('Vegitables', $path[1]->getTitle());
  49. $this->assertEquals('Carrots', $path[2]->getTitle());
  50. $vegies = $repo->findOneByTitle('Vegitables');
  51. $childCount = $repo->childCount($vegies);
  52. $this->assertEquals(2, $childCount);
  53. $food = $repo->findOneByTitle('Food');
  54. $childCount = $repo->childCount($food, true);
  55. $this->assertEquals(2, $childCount);
  56. $childCount = $repo->childCount($food);
  57. $this->assertEquals(4, $childCount);
  58. $childCount = $repo->childCount();
  59. $this->assertEquals(6, $childCount);
  60. }
  61. public function testAdvancedRepositoryFunctions()
  62. {
  63. $this->populateMore();
  64. $repo = $this->em->getRepository(self::TEST_ENTITY_CLASS);
  65. // verification
  66. $this->assertTrue($repo->verify());
  67. $dql = 'UPDATE ' . self::TEST_ENTITY_CLASS . ' node';
  68. $dql .= ' SET node.lft = 1';
  69. $dql .= ' WHERE node.id = 4';
  70. $this->em->createQuery($dql)->getSingleScalarResult();
  71. $this->em->clear(); // must clear cached entities
  72. $errors = $repo->verify();
  73. $this->assertEquals(2, count($errors));
  74. $this->assertEquals('index [1], duplicate on tree root: 1', $errors[0]);
  75. $this->assertEquals('index [4], missing on tree root: 1', $errors[1]);
  76. $dql = 'UPDATE ' . self::TEST_ENTITY_CLASS . ' node';
  77. $dql .= ' SET node.lft = 4';
  78. $dql .= ' WHERE node.id = 4';
  79. $this->em->createQuery($dql)->getSingleScalarResult();
  80. //@todo implement
  81. /*$this->em->clear();
  82. $repo->recover();
  83. $this->em->clear();
  84. $this->assertTrue($repo->verify());*/
  85. $this->em->clear();
  86. $onions = $repo->findOneByTitle('Onions');
  87. $this->assertEquals(11, $onions->getLeft());
  88. $this->assertEquals(12, $onions->getRight());
  89. // move up
  90. $repo->moveUp($onions);
  91. $this->em->refresh($onions);
  92. $this->assertEquals(9, $onions->getLeft());
  93. $this->assertEquals(10, $onions->getRight());
  94. $repo->moveUp($onions, true);
  95. $this->em->refresh($onions);
  96. $this->assertEquals(5, $onions->getLeft());
  97. $this->assertEquals(6, $onions->getRight());
  98. // move down
  99. $repo->moveDown($onions, 2);
  100. $this->em->refresh($onions);
  101. $this->assertEquals(9, $onions->getLeft());
  102. $this->assertEquals(10, $onions->getRight());
  103. // reorder
  104. $this->em->clear();
  105. $node = $repo->findOneByTitle('Food');
  106. $repo->reorder($node, 'title', 'ASC', false);
  107. $this->em->clear();
  108. $node = $repo->findOneByTitle('Cabbages');
  109. $this->assertEquals(5, $node->getLeft());
  110. $this->assertEquals(6, $node->getRight());
  111. $node = $repo->findOneByTitle('Carrots');
  112. $this->assertEquals(7, $node->getLeft());
  113. $this->assertEquals(8, $node->getRight());
  114. $node = $repo->findOneByTitle('Onions');
  115. $this->assertEquals(9, $node->getLeft());
  116. $this->assertEquals(10, $node->getRight());
  117. $node = $repo->findOneByTitle('Potatoes');
  118. $this->assertEquals(11, $node->getLeft());
  119. $this->assertEquals(12, $node->getRight());
  120. // leafs
  121. $leafs = $repo->getLeafs($node);
  122. $this->assertEquals(5, count($leafs));
  123. $this->assertEquals('Fruits', $leafs[0]->getTitle());
  124. $this->assertEquals('Cabbages', $leafs[1]->getTitle());
  125. $this->assertEquals('Carrots', $leafs[2]->getTitle());
  126. $this->assertEquals('Onions', $leafs[3]->getTitle());
  127. $this->assertEquals('Potatoes', $leafs[4]->getTitle());
  128. // remove
  129. $this->em->clear();
  130. $node = $repo->findOneByTitle('Fruits');
  131. $id = $node->getId();
  132. $repo->removeFromTree($node);
  133. $this->assertTrue(is_null($repo->find($id)));
  134. $node = $repo->findOneByTitle('Vegitables');
  135. $id = $node->getId();
  136. $repo->removeFromTree($node);
  137. $this->assertTrue(is_null($repo->find($id)));
  138. $this->em->clear();
  139. $node = $repo->findOneByTitle('Cabbages');
  140. $this->assertEquals(1, $node->getRoot());
  141. $this->assertEquals(1, $node->getParent()->getId());
  142. }
  143. private function populateMore()
  144. {
  145. $vegies = $this->em->getRepository(self::TEST_ENTITY_CLASS)
  146. ->findOneByTitle('Vegitables');
  147. $cabbages = new RootCategory();
  148. $cabbages->setParent($vegies);
  149. $cabbages->setTitle('Cabbages');
  150. $onions = new RootCategory();
  151. $onions->setParent($vegies);
  152. $onions->setTitle('Onions');
  153. $this->em->persist($cabbages);
  154. $this->em->persist($onions);
  155. $this->em->flush();
  156. $this->em->clear();
  157. }
  158. private function populate()
  159. {
  160. $root = new RootCategory();
  161. $root->setTitle("Food");
  162. $root2 = new RootCategory();
  163. $root2->setTitle("Sports");
  164. $child = new RootCategory();
  165. $child->setTitle("Fruits");
  166. $child->setParent($root);
  167. $child2 = new RootCategory();
  168. $child2->setTitle("Vegitables");
  169. $child2->setParent($root);
  170. $childsChild = new RootCategory();
  171. $childsChild->setTitle("Carrots");
  172. $childsChild->setParent($child2);
  173. $potatoes = new RootCategory();
  174. $potatoes->setTitle("Potatoes");
  175. $potatoes->setParent($child2);
  176. $this->em->persist($root);
  177. $this->em->persist($root2);
  178. $this->em->persist($child);
  179. $this->em->persist($child2);
  180. $this->em->persist($childsChild);
  181. $this->em->persist($potatoes);
  182. $this->em->flush();
  183. $this->em->clear();
  184. }
  185. }