TreeTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace Gedmo\Tree;
  3. use Doctrine\Common\Util\Debug;
  4. use 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 TreeTest extends \PHPUnit_Framework_TestCase
  14. {
  15. const TEST_ENTITY_CLASS = "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_CLASS)
  40. ));
  41. }
  42. public function testTheTree()
  43. {
  44. $meta = $this->em->getClassMetadata(self::TEST_ENTITY_CLASS);
  45. $root = new Category();
  46. $root->setTitle("Root");
  47. $this->assertTrue($root instanceof Node);
  48. $this->em->persist($root);
  49. $this->em->flush();
  50. $this->em->clear();
  51. $root = $this->em->getRepository(self::TEST_ENTITY_CLASS)->find(1);
  52. $left = $meta->getReflectionProperty('lft')->getValue($root);
  53. $right = $meta->getReflectionProperty('rgt')->getValue($root);
  54. $this->assertEquals($left, 1);
  55. $this->assertEquals($right, 2);
  56. $child = new Category();
  57. $child->setTitle("child");
  58. $child->setParent($root);
  59. $this->em->persist($child);
  60. $this->em->flush();
  61. $this->em->clear();
  62. $root = $this->em->getRepository(self::TEST_ENTITY_CLASS)->find(1);
  63. $left = $meta->getReflectionProperty('lft')->getValue($root);
  64. $right = $meta->getReflectionProperty('rgt')->getValue($root);
  65. $level = $meta->getReflectionProperty('level')->getValue($root);
  66. $this->assertEquals($left, 1);
  67. $this->assertEquals($right, 4);
  68. $this->assertEquals($level, 0);
  69. $child = $this->em->getRepository(self::TEST_ENTITY_CLASS)->find(2);
  70. $left = $meta->getReflectionProperty('lft')->getValue($child);
  71. $right = $meta->getReflectionProperty('rgt')->getValue($child);
  72. $level = $meta->getReflectionProperty('level')->getValue($child);
  73. $this->assertEquals($left, 2);
  74. $this->assertEquals($right, 3);
  75. $this->assertEquals($level, 1);
  76. $child2 = new Category();
  77. $child2->setTitle("child2");
  78. $child2->setParent($root);
  79. $this->em->persist($child2);
  80. $this->em->flush();
  81. $this->em->clear();
  82. $root = $this->em->getRepository(self::TEST_ENTITY_CLASS)->find(1);
  83. $left = $meta->getReflectionProperty('lft')->getValue($root);
  84. $right = $meta->getReflectionProperty('rgt')->getValue($root);
  85. $level = $meta->getReflectionProperty('level')->getValue($root);
  86. $this->assertEquals($left, 1);
  87. $this->assertEquals($right, 6);
  88. $this->assertEquals($level, 0);
  89. $child2 = $this->em->getRepository(self::TEST_ENTITY_CLASS)->find(3);
  90. $left = $meta->getReflectionProperty('lft')->getValue($child2);
  91. $right = $meta->getReflectionProperty('rgt')->getValue($child2);
  92. $level = $meta->getReflectionProperty('level')->getValue($child2);
  93. $this->assertEquals($left, 4);
  94. $this->assertEquals($right, 5);
  95. $this->assertEquals($level, 1);
  96. $childsChild = new Category();
  97. $childsChild->setTitle("childs2_child");
  98. $childsChild->setParent($child2);
  99. $this->em->persist($childsChild);
  100. $this->em->flush();
  101. $this->em->clear();
  102. $child2 = $this->em->getRepository(self::TEST_ENTITY_CLASS)->find(3);
  103. $left = $meta->getReflectionProperty('lft')->getValue($child2);
  104. $right = $meta->getReflectionProperty('rgt')->getValue($child2);
  105. $level = $meta->getReflectionProperty('level')->getValue($child2);
  106. $this->assertEquals($left, 4);
  107. $this->assertEquals($right, 7);
  108. $this->assertEquals($level, 1);
  109. $level = $meta->getReflectionProperty('level')->getValue($childsChild);
  110. $this->assertEquals($level, 2);
  111. // test updates to nodes, parent changes
  112. $childsChild = $this->em->getRepository(self::TEST_ENTITY_CLASS)->find(4);
  113. $child = $this->em->getRepository(self::TEST_ENTITY_CLASS)->find(2);
  114. $childsChild->setTitle('childs_child');
  115. $childsChild->setParent($child);
  116. $this->em->persist($childsChild);
  117. $this->em->flush();
  118. $this->em->clear();
  119. $child = $this->em->getRepository(self::TEST_ENTITY_CLASS)->find(2);
  120. $left = $meta->getReflectionProperty('lft')->getValue($child);
  121. $right = $meta->getReflectionProperty('rgt')->getValue($child);
  122. $level = $meta->getReflectionProperty('level')->getValue($child);
  123. $this->assertEquals($left, 2);
  124. $this->assertEquals($right, 5);
  125. $this->assertEquals($level, 1);
  126. // test deletion
  127. $this->em->remove($child);
  128. $this->em->flush();
  129. $this->em->clear();
  130. $root = $this->em->getRepository(self::TEST_ENTITY_CLASS)->find(1);
  131. $left = $meta->getReflectionProperty('lft')->getValue($root);
  132. $right = $meta->getReflectionProperty('rgt')->getValue($root);
  133. $this->assertEquals($left, 1);
  134. $this->assertEquals($right, 4);
  135. }
  136. }