NestedTreeRootTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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 testSetParentToNull()
  83. {
  84. $repo = $this->em->getRepository(self::TEST_ENTITY_CLASS);
  85. $node = $repo->findOneByTitle('Vegitables');
  86. $node->setParent(null);
  87. $this->em->persist($node);
  88. $this->em->flush();
  89. $this->em->clear();
  90. $node = $repo->findOneByTitle('Vegitables');
  91. $this->assertEquals(4, $node->getRoot());
  92. $this->assertEquals(1, $node->getLeft());
  93. $this->assertEquals(6, $node->getRight());
  94. $this->assertEquals(0, $node->getLevel());
  95. }
  96. public function testTreeUpdateShiftToNextBranch()
  97. {
  98. $repo = $this->em->getRepository(self::TEST_ENTITY_CLASS);
  99. $sport = $repo->findOneByTitle('Sports');
  100. $food = $repo->findOneByTitle('Food');
  101. $sport->setParent($food);
  102. $this->em->persist($sport);
  103. $this->em->flush();
  104. $this->em->clear();
  105. $node = $repo->findOneByTitle('Food');
  106. $this->assertEquals(1, $node->getLeft());
  107. $this->assertEquals(12, $node->getRight());
  108. $node = $repo->findOneByTitle('Sports');
  109. $this->assertEquals(1, $node->getRoot());
  110. $this->assertEquals(2, $node->getLeft());
  111. $this->assertEquals(1, $node->getLevel());
  112. $this->assertEquals(3, $node->getRight());
  113. $node = $repo->findOneByTitle('Vegitables');
  114. $this->assertEquals(6, $node->getLeft());
  115. $this->assertEquals(11, $node->getRight());
  116. }
  117. public function testTreeUpdateShiftToRoot()
  118. {
  119. $repo = $this->em->getRepository(self::TEST_ENTITY_CLASS);
  120. $vegies = $repo->findOneByTitle('Vegitables');
  121. $vegies->setParent(null);
  122. $this->em->persist($vegies);
  123. $this->em->flush();
  124. $this->em->clear();
  125. $node = $repo->findOneByTitle('Food');
  126. $this->assertEquals(1, $node->getLeft());
  127. $this->assertEquals(4, $node->getRight());
  128. $node = $repo->findOneByTitle('Vegitables');
  129. $this->assertEquals(4, $node->getRoot());
  130. $this->assertEquals(1, $node->getLeft());
  131. $this->assertEquals(0, $node->getLevel());
  132. $this->assertEquals(6, $node->getRight());
  133. $node = $repo->findOneByTitle('Potatoes');
  134. $this->assertEquals(4, $node->getRoot());
  135. $this->assertEquals(4, $node->getLeft());
  136. $this->assertEquals(1, $node->getLevel());
  137. $this->assertEquals(5, $node->getRight());
  138. }
  139. public function testTreeUpdateShiftToOtherParent()
  140. {
  141. $repo = $this->em->getRepository(self::TEST_ENTITY_CLASS);
  142. $carrots = $repo->findOneByTitle('Carrots');
  143. $food = $repo->findOneByTitle('Food');
  144. $carrots->setParent($food);
  145. $this->em->persist($carrots);
  146. $this->em->flush();
  147. $this->em->clear();
  148. $node = $repo->findOneByTitle('Food');
  149. $this->assertEquals(1, $node->getLeft());
  150. $this->assertEquals(10, $node->getRight());
  151. $node = $repo->findOneByTitle('Carrots');
  152. $this->assertEquals(1, $node->getRoot());
  153. $this->assertEquals(2, $node->getLeft());
  154. $this->assertEquals(1, $node->getLevel());
  155. $this->assertEquals(3, $node->getRight());
  156. $node = $repo->findOneByTitle('Potatoes');
  157. $this->assertEquals(1, $node->getRoot());
  158. $this->assertEquals(7, $node->getLeft());
  159. $this->assertEquals(2, $node->getLevel());
  160. $this->assertEquals(8, $node->getRight());
  161. }
  162. /**
  163. * @expectedException UnexpectedValueException
  164. */
  165. public function testTreeUpdateShiftToChildParent()
  166. {
  167. $repo = $this->em->getRepository(self::TEST_ENTITY_CLASS);
  168. $vegies = $repo->findOneByTitle('Vegitables');
  169. $food = $repo->findOneByTitle('Food');
  170. $food->setParent($vegies);
  171. $this->em->persist($food);
  172. $this->em->flush();
  173. $this->em->clear();
  174. }
  175. public function testTwoUpdateOperations()
  176. {
  177. $repo = $this->em->getRepository(self::TEST_ENTITY_CLASS);
  178. $sport = $repo->findOneByTitle('Sports');
  179. $food = $repo->findOneByTitle('Food');
  180. $sport->setParent($food);
  181. $vegies = $repo->findOneByTitle('Vegitables');
  182. $vegies->setParent(null);
  183. $this->em->persist($vegies);
  184. $this->em->persist($sport);
  185. $this->em->flush();
  186. $this->em->clear();
  187. $node = $repo->findOneByTitle('Carrots');
  188. $this->assertEquals(4, $node->getRoot());
  189. $this->assertEquals(2, $node->getLeft());
  190. $this->assertEquals(1, $node->getLevel());
  191. $this->assertEquals(3, $node->getRight());
  192. $node = $repo->findOneByTitle('Vegitables');
  193. $this->assertEquals(4, $node->getRoot());
  194. $this->assertEquals(1, $node->getLeft());
  195. $this->assertEquals(0, $node->getLevel());
  196. $this->assertEquals(6, $node->getRight());
  197. $node = $repo->findOneByTitle('Sports');
  198. $this->assertEquals(1, $node->getRoot());
  199. $this->assertEquals(2, $node->getLeft());
  200. $this->assertEquals(1, $node->getLevel());
  201. $this->assertEquals(3, $node->getRight());
  202. }
  203. public function testRemoval()
  204. {
  205. $repo = $this->em->getRepository(self::TEST_ENTITY_CLASS);
  206. $vegies = $repo->findOneByTitle('Vegitables');
  207. $this->em->remove($vegies);
  208. $this->em->flush();
  209. $this->em->clear();
  210. $node = $repo->findOneByTitle('Food');
  211. $this->assertEquals(1, $node->getLeft());
  212. $this->assertEquals(4, $node->getRight());
  213. }
  214. private function populate()
  215. {
  216. $root = new RootCategory();
  217. $root->setTitle("Food");
  218. $root2 = new RootCategory();
  219. $root2->setTitle("Sports");
  220. $child = new RootCategory();
  221. $child->setTitle("Fruits");
  222. $child->setParent($root);
  223. $child2 = new RootCategory();
  224. $child2->setTitle("Vegitables");
  225. $child2->setParent($root);
  226. $childsChild = new RootCategory();
  227. $childsChild->setTitle("Carrots");
  228. $childsChild->setParent($child2);
  229. $potatoes = new RootCategory();
  230. $potatoes->setTitle("Potatoes");
  231. $potatoes->setParent($child2);
  232. $this->em->persist($root);
  233. $this->em->persist($root2);
  234. $this->em->persist($child);
  235. $this->em->persist($child2);
  236. $this->em->persist($childsChild);
  237. $this->em->persist($potatoes);
  238. $this->em->flush();
  239. $this->em->clear();
  240. }
  241. }