InMemoryUpdatesTest.php 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 InMemoryUpdatesTest 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 testInMemoryTreeInserts()
  26. {
  27. $meta = $this->em->getClassMetadata(self::CATEGORY);
  28. $repo = $this->em->getRepository(self::CATEGORY);
  29. $root = new Category();
  30. $this->em->persist($root);
  31. $root->setTitle("Root");
  32. $child = new Category();
  33. $this->em->persist($child);
  34. $child->setTitle("child");
  35. $child2 = new Category();
  36. $this->em->persist($child2);
  37. $child2->setTitle("child2");
  38. $child2->setParent($root);
  39. $child->setParent($root);
  40. $this->em->flush();
  41. $childsChild = new Category();
  42. $this->em->persist($childsChild);
  43. $childsChild->setTitle("childs_child");
  44. $childsChild->setParent($child);
  45. $this->em->flush();
  46. $this->em->clear();
  47. $node = $repo->find(2);
  48. $left = $meta->getReflectionProperty('lft')->getValue($node);
  49. $right = $meta->getReflectionProperty('rgt')->getValue($node);
  50. $this->assertEquals(2, $left);
  51. $this->assertEquals(5, $right);
  52. $node = $repo->find(3);
  53. $left = $meta->getReflectionProperty('lft')->getValue($node);
  54. $right = $meta->getReflectionProperty('rgt')->getValue($node);
  55. $this->assertEquals(6, $left);
  56. $this->assertEquals(7, $right);
  57. $node = $repo->find(4);
  58. $left = $meta->getReflectionProperty('lft')->getValue($node);
  59. $right = $meta->getReflectionProperty('rgt')->getValue($node);
  60. $this->assertEquals(3, $left);
  61. $this->assertEquals(4, $right);
  62. /*print "Tree:\n";
  63. for ($i=1; $i < 5; $i++) {
  64. $node = $this->em->getRepository(self::CATEGORY)->find($i);
  65. $left = $meta->getReflectionProperty('lft')->getValue($node);
  66. $right = $meta->getReflectionProperty('rgt')->getValue($node);
  67. $level = $meta->getReflectionProperty('level')->getValue($node);
  68. print $node->getTitle()." - $left - $right - $level\n";
  69. }
  70. print "\n\n";*/
  71. }
  72. protected function getUsedEntityFixtures()
  73. {
  74. return array(
  75. self::CATEGORY
  76. );
  77. }
  78. }