InMemoryUpdatesTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. * @link http://www.gediminasm.org
  12. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  13. */
  14. class InMemoryUpdatesTest extends BaseTestCaseORM
  15. {
  16. const CATEGORY = "Tree\\Fixture\\Category";
  17. protected function setUp()
  18. {
  19. parent::setUp();
  20. $evm = new EventManager;
  21. $evm->addEventSubscriber(new TreeListener);
  22. $this->getMockSqliteEntityManager($evm);
  23. }
  24. public function testInMemoryTreeInserts()
  25. {
  26. $meta = $this->em->getClassMetadata(self::CATEGORY);
  27. $repo = $this->em->getRepository(self::CATEGORY);
  28. $root = new Category();
  29. $this->em->persist($root);
  30. $root->setTitle("Root");
  31. $child = new Category();
  32. $this->em->persist($child);
  33. $child->setTitle("child");
  34. $child2 = new Category();
  35. $this->em->persist($child2);
  36. $child2->setTitle("child2");
  37. $child2->setParent($root);
  38. $child->setParent($root);
  39. $this->em->flush();
  40. $childsChild = new Category();
  41. $this->em->persist($childsChild);
  42. $childsChild->setTitle("childs_child");
  43. $childsChild->setParent($child);
  44. $this->em->flush();
  45. $this->em->clear();
  46. $node = $repo->find(2);
  47. $left = $meta->getReflectionProperty('lft')->getValue($node);
  48. $right = $meta->getReflectionProperty('rgt')->getValue($node);
  49. $this->assertEquals(2, $left);
  50. $this->assertEquals(5, $right);
  51. $node = $repo->find(3);
  52. $left = $meta->getReflectionProperty('lft')->getValue($node);
  53. $right = $meta->getReflectionProperty('rgt')->getValue($node);
  54. $this->assertEquals(6, $left);
  55. $this->assertEquals(7, $right);
  56. $node = $repo->find(4);
  57. $left = $meta->getReflectionProperty('lft')->getValue($node);
  58. $right = $meta->getReflectionProperty('rgt')->getValue($node);
  59. $this->assertEquals(3, $left);
  60. $this->assertEquals(4, $right);
  61. /*print "Tree:\n";
  62. for ($i=1; $i < 5; $i++) {
  63. $node = $this->em->getRepository(self::CATEGORY)->find($i);
  64. $left = $meta->getReflectionProperty('lft')->getValue($node);
  65. $right = $meta->getReflectionProperty('rgt')->getValue($node);
  66. $level = $meta->getReflectionProperty('level')->getValue($node);
  67. print $node->getTitle()." - $left - $right - $level\n";
  68. }
  69. print "\n\n";*/
  70. }
  71. protected function getUsedEntityFixtures()
  72. {
  73. return array(
  74. self::CATEGORY
  75. );
  76. }
  77. }