NestedTreeRootTest.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 NestedTreeRootTest 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. /*$conn = array(
  30. 'driver' => 'pdo_mysql',
  31. 'host' => '127.0.0.1',
  32. 'dbname' => 'test',
  33. 'user' => 'root',
  34. 'password' => 'nimda'
  35. );*/
  36. //$config->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger());
  37. $evm = new \Doctrine\Common\EventManager();
  38. $treeListener = new TreeListener();
  39. $evm->addEventSubscriber($treeListener);
  40. $this->em = \Doctrine\ORM\EntityManager::create($conn, $config, $evm);
  41. $schemaTool = new \Doctrine\ORM\Tools\SchemaTool($this->em);
  42. $schemaTool->dropSchema(array());
  43. $schemaTool->createSchema(array(
  44. $this->em->getClassMetadata(self::TEST_ENTITY_CLASS)
  45. ));
  46. $this->populate();
  47. }
  48. public function testTheTree()
  49. {
  50. $repo = $this->em->getRepository(self::TEST_ENTITY_CLASS);
  51. $node = $repo->findOneByTitle('Food');
  52. $this->assertEquals(1, $node->getRoot());
  53. $this->assertEquals(1, $node->getLeft());
  54. $this->assertEquals(0, $node->getLevel());
  55. $this->assertEquals(10, $node->getRight());
  56. $node = $repo->findOneByTitle('Sports');
  57. $this->assertEquals(2, $node->getRoot());
  58. $this->assertEquals(1, $node->getLeft());
  59. $this->assertEquals(0, $node->getLevel());
  60. $this->assertEquals(2, $node->getRight());
  61. $node = $repo->findOneByTitle('Fruits');
  62. $this->assertEquals(1, $node->getRoot());
  63. $this->assertEquals(2, $node->getLeft());
  64. $this->assertEquals(1, $node->getLevel());
  65. $this->assertEquals(3, $node->getRight());
  66. $node = $repo->findOneByTitle('Vegitables');
  67. $this->assertEquals(1, $node->getRoot());
  68. $this->assertEquals(4, $node->getLeft());
  69. $this->assertEquals(1, $node->getLevel());
  70. $this->assertEquals(9, $node->getRight());
  71. $node = $repo->findOneByTitle('Carrots');
  72. $this->assertEquals(1, $node->getRoot());
  73. $this->assertEquals(5, $node->getLeft());
  74. $this->assertEquals(2, $node->getLevel());
  75. $this->assertEquals(6, $node->getRight());
  76. $node = $repo->findOneByTitle('Potatoes');
  77. $this->assertEquals(1, $node->getRoot());
  78. $this->assertEquals(7, $node->getLeft());
  79. $this->assertEquals(2, $node->getLevel());
  80. $this->assertEquals(8, $node->getRight());
  81. }
  82. public function testTreeUpdateShiftToNextBranch()
  83. {
  84. $repo = $this->em->getRepository(self::TEST_ENTITY_CLASS);
  85. $sport = $repo->findOneByTitle('Sports');
  86. $food = $repo->findOneByTitle('Food');
  87. $sport->setParent($food);
  88. $this->em->persist($sport);
  89. $this->em->flush();
  90. $this->em->clear();
  91. $node = $repo->findOneByTitle('Food');
  92. $this->assertEquals(1, $node->getLeft());
  93. $this->assertEquals(12, $node->getRight());
  94. $node = $repo->findOneByTitle('Sports');
  95. $this->assertEquals(1, $node->getRoot());
  96. $this->assertEquals(2, $node->getLeft());
  97. $this->assertEquals(1, $node->getLevel());
  98. $this->assertEquals(3, $node->getRight());
  99. $node = $repo->findOneByTitle('Vegitables');
  100. $this->assertEquals(6, $node->getLeft());
  101. $this->assertEquals(11, $node->getRight());
  102. }
  103. public function testTreeUpdateShiftToRoot()
  104. {
  105. $repo = $this->em->getRepository(self::TEST_ENTITY_CLASS);
  106. $vegies = $repo->findOneByTitle('Vegitables');
  107. $vegies->setParent(null);
  108. $this->em->persist($vegies);
  109. $this->em->flush();
  110. $this->em->clear();
  111. $node = $repo->findOneByTitle('Food');
  112. $this->assertEquals(1, $node->getLeft());
  113. $this->assertEquals(4, $node->getRight());
  114. $node = $repo->findOneByTitle('Vegitables');
  115. $this->assertEquals(4, $node->getRoot());
  116. $this->assertEquals(1, $node->getLeft());
  117. $this->assertEquals(0, $node->getLevel());
  118. $this->assertEquals(6, $node->getRight());
  119. $node = $repo->findOneByTitle('Potatoes');
  120. $this->assertEquals(4, $node->getRoot());
  121. $this->assertEquals(4, $node->getLeft());
  122. $this->assertEquals(1, $node->getLevel());
  123. $this->assertEquals(5, $node->getRight());
  124. }
  125. public function testTreeUpdateShiftToOtherParent()
  126. {
  127. $repo = $this->em->getRepository(self::TEST_ENTITY_CLASS);
  128. $carrots = $repo->findOneByTitle('Carrots');
  129. $food = $repo->findOneByTitle('Food');
  130. $carrots->setParent($food);
  131. $this->em->persist($carrots);
  132. $this->em->flush();
  133. $this->em->clear();
  134. $node = $repo->findOneByTitle('Food');
  135. $this->assertEquals(1, $node->getLeft());
  136. $this->assertEquals(10, $node->getRight());
  137. $node = $repo->findOneByTitle('Carrots');
  138. $this->assertEquals(1, $node->getRoot());
  139. $this->assertEquals(2, $node->getLeft());
  140. $this->assertEquals(1, $node->getLevel());
  141. $this->assertEquals(3, $node->getRight());
  142. $node = $repo->findOneByTitle('Potatoes');
  143. $this->assertEquals(1, $node->getRoot());
  144. $this->assertEquals(7, $node->getLeft());
  145. $this->assertEquals(2, $node->getLevel());
  146. $this->assertEquals(8, $node->getRight());
  147. }
  148. /**
  149. * @expectedException UnexpectedValueException
  150. */
  151. public function testTreeUpdateShiftToChildParent()
  152. {
  153. $repo = $this->em->getRepository(self::TEST_ENTITY_CLASS);
  154. $vegies = $repo->findOneByTitle('Vegitables');
  155. $food = $repo->findOneByTitle('Food');
  156. $food->setParent($vegies);
  157. $this->em->persist($food);
  158. $this->em->flush();
  159. $this->em->clear();
  160. }
  161. public function testTwoUpdateOperations()
  162. {
  163. $repo = $this->em->getRepository(self::TEST_ENTITY_CLASS);
  164. $sport = $repo->findOneByTitle('Sports');
  165. $food = $repo->findOneByTitle('Food');
  166. $sport->setParent($food);
  167. $vegies = $repo->findOneByTitle('Vegitables');
  168. $vegies->setParent(null);
  169. $this->em->persist($vegies);
  170. $this->em->persist($sport);
  171. $this->em->flush();
  172. $this->em->clear();
  173. $node = $repo->findOneByTitle('Carrots');
  174. $this->assertEquals(4, $node->getRoot());
  175. $this->assertEquals(2, $node->getLeft());
  176. $this->assertEquals(1, $node->getLevel());
  177. $this->assertEquals(3, $node->getRight());
  178. $node = $repo->findOneByTitle('Vegitables');
  179. $this->assertEquals(4, $node->getRoot());
  180. $this->assertEquals(1, $node->getLeft());
  181. $this->assertEquals(0, $node->getLevel());
  182. $this->assertEquals(6, $node->getRight());
  183. $node = $repo->findOneByTitle('Sports');
  184. $this->assertEquals(1, $node->getRoot());
  185. $this->assertEquals(2, $node->getLeft());
  186. $this->assertEquals(1, $node->getLevel());
  187. $this->assertEquals(3, $node->getRight());
  188. }
  189. public function testRemoval()
  190. {
  191. $repo = $this->em->getRepository(self::TEST_ENTITY_CLASS);
  192. $vegies = $repo->findOneByTitle('Vegitables');
  193. $this->em->remove($vegies);
  194. $this->em->flush();
  195. $this->em->clear();
  196. $node = $repo->findOneByTitle('Food');
  197. $this->assertEquals(1, $node->getLeft());
  198. $this->assertEquals(4, $node->getRight());
  199. }
  200. private function populate()
  201. {
  202. $root = new RootCategory();
  203. $root->setTitle("Food");
  204. $root2 = new RootCategory();
  205. $root2->setTitle("Sports");
  206. $child = new RootCategory();
  207. $child->setTitle("Fruits");
  208. $child->setParent($root);
  209. $child2 = new RootCategory();
  210. $child2->setTitle("Vegitables");
  211. $child2->setParent($root);
  212. $childsChild = new RootCategory();
  213. $childsChild->setTitle("Carrots");
  214. $childsChild->setParent($child2);
  215. $potatoes = new RootCategory();
  216. $potatoes->setTitle("Potatoes");
  217. $potatoes->setParent($child2);
  218. $this->em->persist($root);
  219. $this->em->persist($root2);
  220. $this->em->persist($child);
  221. $this->em->persist($child2);
  222. $this->em->persist($childsChild);
  223. $this->em->persist($potatoes);
  224. $this->em->flush();
  225. $this->em->clear();
  226. }
  227. }