RepositoryTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. <?php
  2. namespace Gedmo\Tree;
  3. use Doctrine\Common\Util\Debug,
  4. Tree\Fixture\Category;
  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 RepositoryTest extends \PHPUnit_Framework_TestCase
  14. {
  15. const TEST_ENTITY_CATEGORY = "Tree\Fixture\Category";
  16. private $em;
  17. public function setUp()
  18. {
  19. $classLoader = new \Doctrine\Common\ClassLoader('Tree\Fixture', __DIR__ . '/../');
  20. $classLoader->register();
  21. $config = new \Doctrine\ORM\Configuration();
  22. $config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache);
  23. $config->setQueryCacheImpl(new \Doctrine\Common\Cache\ArrayCache);
  24. $config->setProxyDir(__DIR__ . '/Proxy');
  25. $config->setProxyNamespace('Gedmo\Tree\Proxies');
  26. $config->setMetadataDriverImpl($config->newDefaultAnnotationDriver());
  27. $conn = array(
  28. 'driver' => 'pdo_sqlite',
  29. 'memory' => true,
  30. );
  31. //$config->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger());
  32. $evm = new \Doctrine\Common\EventManager();
  33. $treeListener = new TreeListener();
  34. $evm->addEventSubscriber($treeListener);
  35. $this->em = \Doctrine\ORM\EntityManager::create($conn, $config, $evm);
  36. $schemaTool = new \Doctrine\ORM\Tools\SchemaTool($this->em);
  37. $schemaTool->dropSchema(array());
  38. $schemaTool->createSchema(array(
  39. $this->em->getClassMetadata(self::TEST_ENTITY_CATEGORY)
  40. ));
  41. $this->_populate();
  42. }
  43. public function testBasicFunctions()
  44. {
  45. $vegies = $this->em->getRepository(self::TEST_ENTITY_CATEGORY)
  46. ->findOneByTitle('Vegitables');
  47. $food = $this->em->getRepository(self::TEST_ENTITY_CATEGORY)
  48. ->findOneByTitle('Food');
  49. // test childCount
  50. $childCount = $this->em->getRepository(self::TEST_ENTITY_CATEGORY)
  51. ->childCount($vegies);
  52. $this->assertEquals(2, $childCount);
  53. $childCount = $this->em->getRepository(self::TEST_ENTITY_CATEGORY)
  54. ->childCount($food);
  55. $this->assertEquals(4, $childCount);
  56. $childCount = $this->em->getRepository(self::TEST_ENTITY_CATEGORY)
  57. ->childCount($food, true);
  58. $this->assertEquals(2, $childCount);
  59. $childCount = $this->em->getRepository(self::TEST_ENTITY_CATEGORY)
  60. ->childCount();
  61. $this->assertEquals(6, $childCount);
  62. // test children
  63. $children = $this->em->getRepository(self::TEST_ENTITY_CATEGORY)
  64. ->children($vegies);
  65. $this->assertEquals(2, count($children));
  66. $this->assertEquals('Carrots', $children[0]->getTitle());
  67. $this->assertEquals('Potatoes', $children[1]->getTitle());
  68. $children = $this->em->getRepository(self::TEST_ENTITY_CATEGORY)
  69. ->children($food);
  70. $this->assertEquals(4, count($children));
  71. $this->assertEquals('Fruits', $children[0]->getTitle());
  72. $this->assertEquals('Vegitables', $children[1]->getTitle());
  73. $this->assertEquals('Carrots', $children[2]->getTitle());
  74. $this->assertEquals('Potatoes', $children[3]->getTitle());
  75. $children = $this->em->getRepository(self::TEST_ENTITY_CATEGORY)
  76. ->children($food, true);
  77. $this->assertEquals(2, count($children));
  78. $this->assertEquals('Fruits', $children[0]->getTitle());
  79. $this->assertEquals('Vegitables', $children[1]->getTitle());
  80. $children = $this->em->getRepository(self::TEST_ENTITY_CATEGORY)
  81. ->children();
  82. $this->assertEquals(6, count($children));
  83. // path
  84. $path = $this->em->getRepository(self::TEST_ENTITY_CATEGORY)
  85. ->getPath($vegies);
  86. $this->assertEquals(2, count($path));
  87. $this->assertEquals('Food', $path[0]->getTitle());
  88. $this->assertEquals('Vegitables', $path[1]->getTitle());
  89. $carrots = $this->em->getRepository(self::TEST_ENTITY_CATEGORY)
  90. ->findOneByTitle('Carrots');
  91. $path = $this->em->getRepository(self::TEST_ENTITY_CATEGORY)
  92. ->getPath($carrots);
  93. $this->assertEquals(3, count($path));
  94. $this->assertEquals('Food', $path[0]->getTitle());
  95. $this->assertEquals('Vegitables', $path[1]->getTitle());
  96. $this->assertEquals('Carrots', $path[2]->getTitle());
  97. // leafs
  98. $leafs = $this->em->getRepository(self::TEST_ENTITY_CATEGORY)
  99. ->getLeafs();
  100. $this->assertEquals(4, count($leafs));
  101. $this->assertEquals('Fruits', $leafs[0]->getTitle());
  102. $this->assertEquals('Carrots', $leafs[1]->getTitle());
  103. $this->assertEquals('Potatoes', $leafs[2]->getTitle());
  104. $this->assertEquals('Sports', $leafs[3]->getTitle());
  105. }
  106. public function testAdvancedFunctions()
  107. {
  108. $this->_populateMore();
  109. $onions = $this->em->getRepository(self::TEST_ENTITY_CATEGORY)
  110. ->findOneByTitle('Onions');
  111. $repo = $this->em->getRepository(self::TEST_ENTITY_CATEGORY);
  112. $meta = $this->em->getClassMetadata(self::TEST_ENTITY_CATEGORY);
  113. $left = $meta->getReflectionProperty('lft')->getValue($onions);
  114. $right = $meta->getReflectionProperty('rgt')->getValue($onions);
  115. $this->assertEquals($left, 11);
  116. $this->assertEquals($right, 12);
  117. // move up onions by one position
  118. $repo->moveUp($onions, 1);
  119. $this->em->refresh($onions);
  120. $left = $meta->getReflectionProperty('lft')->getValue($onions);
  121. $right = $meta->getReflectionProperty('rgt')->getValue($onions);
  122. $this->assertEquals($left, 9);
  123. $this->assertEquals($right, 10);
  124. // move down onions by one position
  125. $repo->moveDown($onions, 1);
  126. $this->em->refresh($onions);
  127. $left = $meta->getReflectionProperty('lft')->getValue($onions);
  128. $right = $meta->getReflectionProperty('rgt')->getValue($onions);
  129. $this->assertEquals($left, 11);
  130. $this->assertEquals($right, 12);
  131. // move to the up onions on this level
  132. $repo->moveUp($onions, true);
  133. $this->em->refresh($onions);
  134. $left = $meta->getReflectionProperty('lft')->getValue($onions);
  135. $right = $meta->getReflectionProperty('rgt')->getValue($onions);
  136. $this->assertEquals($left, 5);
  137. $this->assertEquals($right, 6);
  138. // test tree reordering
  139. $this->em->clear(); // clear all cached nodes
  140. // reorder tree by title
  141. $repo->reorder(null, 'title');
  142. $this->em->clear(); // clear all cached nodes
  143. $node = $this->em->getRepository(self::TEST_ENTITY_CATEGORY)
  144. ->findOneByTitle('Cabbages');
  145. $left = $meta->getReflectionProperty('lft')->getValue($node);
  146. $right = $meta->getReflectionProperty('rgt')->getValue($node);
  147. $this->assertEquals($left, 5);
  148. $this->assertEquals($right, 6);
  149. $node = $this->em->getRepository(self::TEST_ENTITY_CATEGORY)
  150. ->findOneByTitle('Carrots');
  151. $left = $meta->getReflectionProperty('lft')->getValue($node);
  152. $right = $meta->getReflectionProperty('rgt')->getValue($node);
  153. $this->assertEquals($left, 7);
  154. $this->assertEquals($right, 8);
  155. $node = $this->em->getRepository(self::TEST_ENTITY_CATEGORY)
  156. ->findOneByTitle('Onions');
  157. $left = $meta->getReflectionProperty('lft')->getValue($node);
  158. $right = $meta->getReflectionProperty('rgt')->getValue($node);
  159. $this->assertEquals($left, 9);
  160. $this->assertEquals($right, 10);
  161. $node = $this->em->getRepository(self::TEST_ENTITY_CATEGORY)
  162. ->findOneByTitle('Potatoes');
  163. $left = $meta->getReflectionProperty('lft')->getValue($node);
  164. $right = $meta->getReflectionProperty('rgt')->getValue($node);
  165. $this->assertEquals($left, 11);
  166. $this->assertEquals($right, 12);
  167. // test removal with reparenting
  168. $this->em->clear(); // clear all cached nodes
  169. $vegies = $this->em->getRepository(self::TEST_ENTITY_CATEGORY)
  170. ->findOneByTitle('Vegitables');
  171. $repo->removeFromTree($vegies);
  172. $this->em->clear(); // clear all cached nodes
  173. $vegies = $this->em->getRepository(self::TEST_ENTITY_CATEGORY)
  174. ->findOneByTitle('Vegitables');
  175. $this->assertTrue($vegies === null);
  176. $node = $this->em->getRepository(self::TEST_ENTITY_CATEGORY)
  177. ->findOneByTitle('Fruits');
  178. $left = $meta->getReflectionProperty('lft')->getValue($node);
  179. $right = $meta->getReflectionProperty('rgt')->getValue($node);
  180. $this->assertEquals($left, 2);
  181. $this->assertEquals($right, 3);
  182. $this->assertEquals('Food', $node->getParent()->getTitle());
  183. $node = $this->em->getRepository(self::TEST_ENTITY_CATEGORY)
  184. ->findOneByTitle('Cabbages');
  185. $left = $meta->getReflectionProperty('lft')->getValue($node);
  186. $right = $meta->getReflectionProperty('rgt')->getValue($node);
  187. $this->assertEquals($left, 4);
  188. $this->assertEquals($right, 5);
  189. $this->assertEquals('Food', $node->getParent()->getTitle());
  190. }
  191. public function testVerificationAndRecover()
  192. {
  193. $repo = $this->em->getRepository(self::TEST_ENTITY_CATEGORY);
  194. $meta = $this->em->getClassMetadata(self::TEST_ENTITY_CATEGORY);
  195. $this->_populateMore();
  196. // test verification of tree
  197. $this->assertTrue($repo->verify());
  198. // now lets brake something
  199. $dql = 'UPDATE ' . self::TEST_ENTITY_CATEGORY . ' node';
  200. $dql .= ' SET node.lft = 1';
  201. $dql .= ' WHERE node.id = 8';
  202. $q = $this->em->createQuery($dql);
  203. $q->getSingleScalarResult();
  204. $this->em->clear(); // must clear cached entities
  205. // verify again
  206. $result = $repo->verify();
  207. $this->assertTrue(is_array($result));
  208. $this->assertArrayHasKey(0, $result);
  209. $this->assertArrayHasKey(1, $result);
  210. $this->assertArrayHasKey(2, $result);
  211. $duplicate = $result[0];
  212. $missing = $result[1];
  213. $invalidLeft = $result[2];
  214. $this->assertEquals('index [1], duplicate', $duplicate);
  215. $this->assertEquals('index [11], missing', $missing);
  216. $this->assertEquals('node [8] left is less than parent`s [4] left value', $invalidLeft);
  217. // test recover functionality
  218. $repo->recover();
  219. $this->em->clear(); // must clear cached entities
  220. $this->assertTrue($repo->verify());
  221. }
  222. protected function _populateMore()
  223. {
  224. $vegies = $this->em->getRepository(self::TEST_ENTITY_CATEGORY)
  225. ->findOneByTitle('Vegitables');
  226. $cabbages = new Category();
  227. $cabbages->setParent($vegies);
  228. $cabbages->setTitle('Cabbages');
  229. $onions = new Category();
  230. $onions->setParent($vegies);
  231. $onions->setTitle('Onions');
  232. $this->em->persist($cabbages);
  233. $this->em->persist($onions);
  234. $this->em->flush();
  235. $this->em->clear();
  236. }
  237. protected function _populate()
  238. {
  239. $root = new Category();
  240. $root->setTitle("Food");
  241. $root2 = new Category();
  242. $root2->setTitle("Sports");
  243. $child = new Category();
  244. $child->setTitle("Fruits");
  245. $child->setParent($root);
  246. $child2 = new Category();
  247. $child2->setTitle("Vegitables");
  248. $child2->setParent($root);
  249. $childsChild = new Category();
  250. $childsChild->setTitle("Carrots");
  251. $childsChild->setParent($child2);
  252. $potatoes = new Category();
  253. $potatoes->setTitle("Potatoes");
  254. $potatoes->setParent($child2);
  255. $this->em->persist($root);
  256. $this->em->persist($root2);
  257. $this->em->persist($child);
  258. $this->em->persist($child2);
  259. $this->em->persist($childsChild);
  260. $this->em->persist($potatoes);
  261. $this->em->flush();
  262. $this->em->clear();
  263. }
  264. }