TreeTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. namespace Gedmo\Tree;
  3. use Doctrine\Common\EventManager;
  4. use Tool\BaseTestCaseORM;
  5. use Doctrine\Common\Util\Debug;
  6. use Tree\Fixture\Category;
  7. /**
  8. * These are tests for Tree behavior
  9. *
  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 TreeTest extends BaseTestCaseORM
  16. {
  17. const CATEGORY = "Tree\\Fixture\\Category";
  18. protected function setUp()
  19. {
  20. parent::setUp();
  21. $evm = new EventManager;
  22. $evm->addEventSubscriber(new TreeListener);
  23. $this->getMockSqliteEntityManager($evm);
  24. }
  25. public function testTheTree()
  26. {
  27. $meta = $this->em->getClassMetadata(self::CATEGORY);
  28. $root = new Category();
  29. $root->setTitle("Root");
  30. $this->assertTrue($root instanceof Node);
  31. $this->em->persist($root);
  32. $this->em->flush();
  33. $this->em->clear();
  34. $root = $this->em->getRepository(self::CATEGORY)->find(1);
  35. $left = $meta->getReflectionProperty('lft')->getValue($root);
  36. $right = $meta->getReflectionProperty('rgt')->getValue($root);
  37. $this->assertEquals($left, 1);
  38. $this->assertEquals($right, 2);
  39. $child = new Category();
  40. $child->setTitle("child");
  41. $child->setParent($root);
  42. $this->em->persist($child);
  43. $this->em->flush();
  44. $this->em->clear();
  45. $root = $this->em->getRepository(self::CATEGORY)->find(1);
  46. $left = $meta->getReflectionProperty('lft')->getValue($root);
  47. $right = $meta->getReflectionProperty('rgt')->getValue($root);
  48. $level = $meta->getReflectionProperty('level')->getValue($root);
  49. $this->assertEquals($left, 1);
  50. $this->assertEquals($right, 4);
  51. $this->assertEquals($level, 0);
  52. $child = $this->em->getRepository(self::CATEGORY)->find(2);
  53. $left = $meta->getReflectionProperty('lft')->getValue($child);
  54. $right = $meta->getReflectionProperty('rgt')->getValue($child);
  55. $level = $meta->getReflectionProperty('level')->getValue($child);
  56. $this->assertEquals($left, 2);
  57. $this->assertEquals($right, 3);
  58. $this->assertEquals($level, 1);
  59. $child2 = new Category();
  60. $child2->setTitle("child2");
  61. $child2->setParent($root);
  62. $this->em->persist($child2);
  63. $this->em->flush();
  64. $this->em->clear();
  65. $root = $this->em->getRepository(self::CATEGORY)->find(1);
  66. $left = $meta->getReflectionProperty('lft')->getValue($root);
  67. $right = $meta->getReflectionProperty('rgt')->getValue($root);
  68. $level = $meta->getReflectionProperty('level')->getValue($root);
  69. $this->assertEquals($left, 1);
  70. $this->assertEquals($right, 6);
  71. $this->assertEquals($level, 0);
  72. $child2 = $this->em->getRepository(self::CATEGORY)->find(3);
  73. $left = $meta->getReflectionProperty('lft')->getValue($child2);
  74. $right = $meta->getReflectionProperty('rgt')->getValue($child2);
  75. $level = $meta->getReflectionProperty('level')->getValue($child2);
  76. $this->assertEquals($left, 4);
  77. $this->assertEquals($right, 5);
  78. $this->assertEquals($level, 1);
  79. $childsChild = new Category();
  80. $childsChild->setTitle("childs2_child");
  81. $childsChild->setParent($child2);
  82. $this->em->persist($childsChild);
  83. $this->em->flush();
  84. $this->em->clear();
  85. $child2 = $this->em->getRepository(self::CATEGORY)->find(3);
  86. $left = $meta->getReflectionProperty('lft')->getValue($child2);
  87. $right = $meta->getReflectionProperty('rgt')->getValue($child2);
  88. $level = $meta->getReflectionProperty('level')->getValue($child2);
  89. $this->assertEquals($left, 4);
  90. $this->assertEquals($right, 7);
  91. $this->assertEquals($level, 1);
  92. $level = $meta->getReflectionProperty('level')->getValue($childsChild);
  93. $this->assertEquals($level, 2);
  94. // test updates to nodes, parent changes
  95. $childsChild = $this->em->getRepository(self::CATEGORY)->find(4);
  96. $child = $this->em->getRepository(self::CATEGORY)->find(2);
  97. $childsChild->setTitle('childs_child');
  98. $childsChild->setParent($child);
  99. $this->em->persist($childsChild);
  100. $this->em->flush();
  101. $this->em->clear();
  102. $child = $this->em->getRepository(self::CATEGORY)->find(2);
  103. $left = $meta->getReflectionProperty('lft')->getValue($child);
  104. $right = $meta->getReflectionProperty('rgt')->getValue($child);
  105. $level = $meta->getReflectionProperty('level')->getValue($child);
  106. $this->assertEquals($left, 2);
  107. $this->assertEquals($right, 5);
  108. $this->assertEquals($level, 1);
  109. // test deletion
  110. $this->em->remove($child);
  111. $this->em->flush();
  112. $this->em->clear();
  113. $root = $this->em->getRepository(self::CATEGORY)->find(1);
  114. $left = $meta->getReflectionProperty('lft')->getValue($root);
  115. $right = $meta->getReflectionProperty('rgt')->getValue($root);
  116. $this->assertEquals($left, 1);
  117. $this->assertEquals($right, 4);
  118. // test persisting in any time
  119. $yetAnotherChild = new Category();
  120. $this->em->persist($yetAnotherChild);
  121. $yetAnotherChild->setTitle("yetanotherchild");
  122. $yetAnotherChild->setParent($root);
  123. $this->em->flush();
  124. $this->em->clear();
  125. $left = $meta->getReflectionProperty('lft')->getValue($yetAnotherChild);
  126. $right = $meta->getReflectionProperty('rgt')->getValue($yetAnotherChild);
  127. $level = $meta->getReflectionProperty('level')->getValue($yetAnotherChild);
  128. $this->assertEquals($left, 4);
  129. $this->assertEquals($right, 5);
  130. $this->assertEquals($level, 1);
  131. }
  132. protected function getUsedEntityFixtures()
  133. {
  134. return array(
  135. self::CATEGORY
  136. );
  137. }
  138. }