TreeTest.php 6.3 KB

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