RepositoryTest.php 12 KB

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