ClosureTreeTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. <?php
  2. namespace Gedmo\Tree;
  3. use Doctrine\Common\Util\Debug;
  4. use Tree\Fixture\Closure\Category;
  5. use Tool\Logging\DBAL\QueryAnalyzer;
  6. /**
  7. * These are tests for Tree behavior
  8. *
  9. * @author Gustavo Adrian <comfortablynumb84@gmail.com>
  10. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  11. * @package Gedmo.Tree
  12. * @link http://www.gediminasm.org
  13. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  14. */
  15. class ClosureTreeTest extends \PHPUnit_Framework_TestCase
  16. {
  17. const TEST_ENTITY_CLASS = "Tree\Fixture\Closure\Category";
  18. const TEST_CLOSURE_CLASS = "Tree\Fixture\Closure\CategoryClosure";
  19. const TEST_BASE_CLOSURE_CLASS = "Gedmo\Tree\Entity\AbstractClosure";
  20. /**
  21. * @var EntityManager
  22. */
  23. private $em;
  24. /**
  25. * @var QueryAnalyzer
  26. */
  27. private $analyzer;
  28. private $food;
  29. private $sports;
  30. private $fruits;
  31. private $vegitables;
  32. private $carrots;
  33. private $potatoes;
  34. public function setUp()
  35. {
  36. $config = new \Doctrine\ORM\Configuration();
  37. $config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache);
  38. $config->setQueryCacheImpl(new \Doctrine\Common\Cache\ArrayCache);
  39. $config->setProxyDir(__DIR__ . '/Proxy');
  40. $config->setProxyNamespace('Gedmo\Tree\Proxies');
  41. $config->setMetadataDriverImpl($config->newDefaultAnnotationDriver());
  42. $conn = array(
  43. 'driver' => 'pdo_sqlite',
  44. 'memory' => true,
  45. );
  46. /*$conn = array(
  47. 'driver' => 'pdo_mysql',
  48. 'host' => '127.0.0.1',
  49. 'dbname' => 'closure',
  50. 'user' => 'root',
  51. 'password' => ''
  52. );*/
  53. $evm = new \Doctrine\Common\EventManager();
  54. $treeListener = new TreeListener(TreeListener::TYPE_CLOSURE);
  55. $evm->addEventSubscriber($treeListener);
  56. $this->em = \Doctrine\ORM\EntityManager::create($conn, $config, $evm);
  57. $schema = array(
  58. $this->em->getClassMetadata(self::TEST_BASE_CLOSURE_CLASS),
  59. $this->em->getClassMetadata(self::TEST_CLOSURE_CLASS),
  60. $this->em->getClassMetadata(self::TEST_ENTITY_CLASS),
  61. );
  62. $schemaTool = new \Doctrine\ORM\Tools\SchemaTool($this->em);
  63. $schemaTool->dropSchema( $schema );
  64. $schemaTool->createSchema( $schema );
  65. $this->analyzer = new QueryAnalyzer(
  66. $this->em->getConnection()->getDatabasePlatform()
  67. );
  68. $config->setSQLLogger($this->analyzer);
  69. $this->populate();
  70. }
  71. public function tearDown()
  72. {
  73. $this->analyzer->dumpResult();
  74. $this->em->clear();
  75. }
  76. public function test_insertNodes_verifyClosurePaths()
  77. {
  78. // We check the inserted nodes fields from the closure table
  79. $repo = $this->em->getRepository(self::TEST_CLOSURE_CLASS);
  80. $rows = $this->em->createQuery( sprintf( 'SELECT c FROM %s c ORDER BY c.ancestor ASC, c.descendant ASC, c.depth ASC', self::TEST_CLOSURE_CLASS ) )
  81. ->getArrayResult();
  82. // Root self referencing row and descendants
  83. $this->assertEquals( $rows[ 0 ][ 'ancestor' ], $this->food->getId() );
  84. $this->assertEquals( $rows[ 0 ][ 'descendant' ], $this->food->getId() );
  85. $this->assertEquals( $rows[ 0 ][ 'depth' ], 0 );
  86. $this->assertEquals( $rows[ 1 ][ 'ancestor' ], $this->food->getId() );
  87. $this->assertEquals( $rows[ 1 ][ 'descendant' ], $this->fruits->getId() );
  88. $this->assertEquals( $rows[ 1 ][ 'depth' ], 1 );
  89. $this->assertEquals( $rows[ 2 ][ 'ancestor' ], $this->food->getId() );
  90. $this->assertEquals( $rows[ 2 ][ 'descendant' ], $this->vegitables->getId() );
  91. $this->assertEquals( $rows[ 2 ][ 'depth' ], 1 );
  92. $this->assertEquals( $rows[ 3 ][ 'ancestor' ], $this->food->getId() );
  93. $this->assertEquals( $rows[ 3 ][ 'descendant' ], $this->carrots->getId() );
  94. $this->assertEquals( $rows[ 3 ][ 'depth' ], 2 );
  95. $this->assertEquals( $rows[ 4 ][ 'ancestor' ], $this->food->getId() );
  96. $this->assertEquals( $rows[ 4 ][ 'descendant' ], $this->potatoes->getId() );
  97. $this->assertEquals( $rows[ 4 ][ 'depth' ], 2 );
  98. // Sports self referencing row
  99. $this->assertEquals( $rows[ 5 ][ 'ancestor' ], $this->sports->getId() );
  100. $this->assertEquals( $rows[ 5 ][ 'descendant' ], $this->sports->getId() );
  101. $this->assertEquals( $rows[ 5 ][ 'depth' ], 0 );
  102. // Fruits self referencing row
  103. $this->assertEquals( $rows[ 6 ][ 'ancestor' ], $this->fruits->getId() );
  104. $this->assertEquals( $rows[ 6 ][ 'descendant' ], $this->fruits->getId() );
  105. $this->assertEquals( $rows[ 6 ][ 'depth' ], 0 );
  106. // Vegitables self referencing row and descendants
  107. $this->assertEquals( $rows[ 7 ][ 'ancestor' ], $this->vegitables->getId() );
  108. $this->assertEquals( $rows[ 7 ][ 'descendant' ], $this->vegitables->getId() );
  109. $this->assertEquals( $rows[ 7 ][ 'depth' ], 0 );
  110. $this->assertEquals( $rows[ 8 ][ 'ancestor' ], $this->vegitables->getId() );
  111. $this->assertEquals( $rows[ 8 ][ 'descendant' ], $this->carrots->getId() );
  112. $this->assertEquals( $rows[ 8 ][ 'depth' ], 1 );
  113. $this->assertEquals( $rows[ 9 ][ 'ancestor' ], $this->vegitables->getId() );
  114. $this->assertEquals( $rows[ 9 ][ 'descendant' ], $this->potatoes->getId() );
  115. $this->assertEquals( $rows[ 9 ][ 'depth' ], 1 );
  116. // Carrots self referencing row
  117. $this->assertEquals( $rows[ 10 ][ 'ancestor' ], $this->carrots->getId() );
  118. $this->assertEquals( $rows[ 10 ][ 'descendant' ], $this->carrots->getId() );
  119. $this->assertEquals( $rows[ 10 ][ 'depth' ], 0 );
  120. // Potatoes self referencing row
  121. $this->assertEquals( $rows[ 11 ][ 'ancestor' ], $this->potatoes->getId() );
  122. $this->assertEquals( $rows[ 11 ][ 'descendant' ], $this->potatoes->getId() );
  123. $this->assertEquals( $rows[ 11 ][ 'depth' ], 0 );
  124. }
  125. public function test_updateNodes_moveASubtreeAndVerifyTreeClosurePaths()
  126. {
  127. // We change a subtree's location
  128. $vegitables = $this->em->getRepository( self::TEST_ENTITY_CLASS )
  129. ->findOneByTitle( 'Vegitables' );
  130. $sports = $this->em->getRepository( self::TEST_ENTITY_CLASS )
  131. ->findOneByTitle( 'Sports' );
  132. $vegitables->setParent( $sports );
  133. $this->em->persist( $vegitables );
  134. $this->em->flush();
  135. // We then verify the closure paths
  136. $repo = $this->em->getRepository(self::TEST_CLOSURE_CLASS);
  137. $rows = $this->em->createQuery( sprintf( 'SELECT c FROM %s c ORDER BY c.ancestor ASC, c.descendant ASC, c.depth ASC', self::TEST_CLOSURE_CLASS ) )
  138. ->getArrayResult();
  139. // Food self referencing row and descendants
  140. $this->assertEquals( $rows[ 0 ][ 'ancestor' ], $this->food->getId() );
  141. $this->assertEquals( $rows[ 0 ][ 'descendant' ], $this->food->getId() );
  142. $this->assertEquals( $rows[ 0 ][ 'depth' ], 0 );
  143. $this->assertEquals( $rows[ 1 ][ 'ancestor' ], $this->food->getId() );
  144. $this->assertEquals( $rows[ 1 ][ 'descendant' ], $this->fruits->getId() );
  145. $this->assertEquals( $rows[ 1 ][ 'depth' ], 1 );
  146. // Sports self referencing row and descendants
  147. $this->assertEquals( $rows[ 2 ][ 'ancestor' ], $this->sports->getId() );
  148. $this->assertEquals( $rows[ 2 ][ 'descendant' ], $this->sports->getId() );
  149. $this->assertEquals( $rows[ 2 ][ 'depth' ], 0 );
  150. $this->assertEquals( $rows[ 3 ][ 'ancestor' ], $this->sports->getId() );
  151. $this->assertEquals( $rows[ 3 ][ 'descendant' ], $this->vegitables->getId() );
  152. $this->assertEquals( $rows[ 3 ][ 'depth' ], 1 );
  153. $this->assertEquals( $rows[ 4 ][ 'ancestor' ], $this->sports->getId() );
  154. $this->assertEquals( $rows[ 4 ][ 'descendant' ], $this->carrots->getId() );
  155. $this->assertEquals( $rows[ 4 ][ 'depth' ], 2 );
  156. $this->assertEquals( $rows[ 5 ][ 'ancestor' ], $this->sports->getId() );
  157. $this->assertEquals( $rows[ 5 ][ 'descendant' ], $this->potatoes->getId() );
  158. $this->assertEquals( $rows[ 5 ][ 'depth' ], 2 );
  159. // Fruits self referencing row
  160. $this->assertEquals( $rows[ 6 ][ 'ancestor' ], $this->fruits->getId() );
  161. $this->assertEquals( $rows[ 6 ][ 'descendant' ], $this->fruits->getId() );
  162. $this->assertEquals( $rows[ 6 ][ 'depth' ], 0 );
  163. // Vegitables self referencing row and descendants
  164. $this->assertEquals( $rows[ 7 ][ 'ancestor' ], $this->vegitables->getId() );
  165. $this->assertEquals( $rows[ 7 ][ 'descendant' ], $this->vegitables->getId() );
  166. $this->assertEquals( $rows[ 7 ][ 'depth' ], 0 );
  167. $this->assertEquals( $rows[ 8 ][ 'ancestor' ], $this->vegitables->getId() );
  168. $this->assertEquals( $rows[ 8 ][ 'descendant' ], $this->carrots->getId() );
  169. $this->assertEquals( $rows[ 8 ][ 'depth' ], 1 );
  170. $this->assertEquals( $rows[ 9 ][ 'ancestor' ], $this->vegitables->getId() );
  171. $this->assertEquals( $rows[ 9 ][ 'descendant' ], $this->potatoes->getId() );
  172. $this->assertEquals( $rows[ 9 ][ 'depth' ], 1 );
  173. // Carrots self referencing row
  174. $this->assertEquals( $rows[ 10 ][ 'ancestor' ], $this->carrots->getId() );
  175. $this->assertEquals( $rows[ 10 ][ 'descendant' ], $this->carrots->getId() );
  176. $this->assertEquals( $rows[ 10 ][ 'depth' ], 0 );
  177. // Potatoes self referencing row
  178. $this->assertEquals( $rows[ 11 ][ 'ancestor' ], $this->potatoes->getId() );
  179. $this->assertEquals( $rows[ 11 ][ 'descendant' ], $this->potatoes->getId() );
  180. $this->assertEquals( $rows[ 11 ][ 'depth' ], 0 );
  181. }
  182. public function test_removeNode_removesClosurePathsOfNodeAndVerifyTree()
  183. {
  184. // We remove a subtree
  185. $vegitables = $this->em->getRepository( self::TEST_ENTITY_CLASS )
  186. ->findOneByTitle( 'Vegitables' );
  187. $this->em->remove( $vegitables );
  188. $this->em->flush();
  189. // We then verify the closure paths
  190. $repo = $this->em->getRepository(self::TEST_CLOSURE_CLASS);
  191. $rows = $this->em->createQuery( sprintf( 'SELECT c FROM %s c ORDER BY c.ancestor ASC, c.descendant ASC, c.depth ASC', self::TEST_CLOSURE_CLASS ) )
  192. ->getArrayResult();
  193. // Food self referencing row and descendants
  194. $this->assertEquals( $rows[ 0 ][ 'ancestor' ], $this->food->getId() );
  195. $this->assertEquals( $rows[ 0 ][ 'descendant' ], $this->food->getId() );
  196. $this->assertEquals( $rows[ 0 ][ 'depth' ], 0 );
  197. $this->assertEquals( $rows[ 1 ][ 'ancestor' ], $this->food->getId() );
  198. $this->assertEquals( $rows[ 1 ][ 'descendant' ], $this->fruits->getId() );
  199. $this->assertEquals( $rows[ 1 ][ 'depth' ], 1 );
  200. // Sports self referencing row
  201. $this->assertEquals( $rows[ 2 ][ 'ancestor' ], $this->sports->getId() );
  202. $this->assertEquals( $rows[ 2 ][ 'descendant' ], $this->sports->getId() );
  203. $this->assertEquals( $rows[ 2 ][ 'depth' ], 0 );
  204. // Fruits self referencing row
  205. $this->assertEquals( $rows[ 3 ][ 'ancestor' ], $this->fruits->getId() );
  206. $this->assertEquals( $rows[ 3 ][ 'descendant' ], $this->fruits->getId() );
  207. $this->assertEquals( $rows[ 3 ][ 'depth' ], 0 );
  208. }
  209. private function populate()
  210. {
  211. $root = new Category();
  212. $root->setTitle("Food");
  213. $this->food = $root;
  214. $root2 = new Category();
  215. $root2->setTitle("Sports");
  216. $this->sports = $root2;
  217. $child = new Category();
  218. $child->setTitle("Fruits");
  219. $child->setParent($root);
  220. $this->fruits = $child;
  221. $child2 = new Category();
  222. $child2->setTitle("Vegitables");
  223. $child2->setParent($root);
  224. $this->vegitables = $child2;
  225. $childsChild = new Category();
  226. $childsChild->setTitle("Carrots");
  227. $childsChild->setParent($child2);
  228. $this->carrots = $childsChild;
  229. $potatoes = new Category();
  230. $potatoes->setTitle("Potatoes");
  231. $potatoes->setParent($child2);
  232. $this->potatoes = $potatoes;
  233. $this->em->persist($this->food);
  234. $this->em->persist($this->sports);
  235. $this->em->persist($this->fruits);
  236. $this->em->persist($this->vegitables);
  237. $this->em->persist($this->carrots);
  238. $this->em->persist($this->potatoes);
  239. $this->em->flush();
  240. $this->em->clear();
  241. }
  242. }