ConcurrencyTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. namespace Gedmo\Tree;
  3. use Doctrine\Common\EventManager;
  4. use Tool\BaseTestCaseORM;
  5. use Doctrine\Common\Util\Debug,
  6. Tree\Fixture\Category,
  7. Tree\Fixture\Article,
  8. Tree\Fixture\Comment;
  9. /**
  10. * These are tests for Tree behavior
  11. *
  12. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  13. * @package Gedmo.Tree
  14. * @link http://www.gediminasm.org
  15. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  16. */
  17. class ConcurrencyTest extends BaseTestCaseORM
  18. {
  19. const CATEGORY = "Tree\\Fixture\\Category";
  20. const ARTICLE = "Tree\\Fixture\\Article";
  21. const COMMENT = "Tree\\Fixture\\Comment";
  22. protected function setUp()
  23. {
  24. parent::setUp();
  25. $evm = new EventManager;
  26. $evm->addEventSubscriber(new TreeListener);
  27. $this->getMockSqliteEntityManager($evm);
  28. $this->populate();
  29. }
  30. public function testConcurrentEntitiesInOneFlush()
  31. {
  32. $sport = $this->em->getRepository(self::CATEGORY)->find(2);
  33. $sport->setTitle('Sport');
  34. $skiing = new Category();
  35. $skiing->setTitle('Skiing');
  36. $skiing->setParent($sport);
  37. $articleAboutSkiing = new Article();
  38. $articleAboutSkiing->setCategory($skiing);
  39. $articleAboutSkiing->setTitle('About Skiing');
  40. $aboutSkiingArticleComment = new Comment();
  41. $aboutSkiingArticleComment->setArticle($articleAboutSkiing);
  42. $aboutSkiingArticleComment->setMessage('hello');
  43. $carRacing = new Category();
  44. $carRacing->setParent($sport);
  45. $carRacing->setTitle('Car Racing');
  46. $articleCarRacing = new Article();
  47. $articleCarRacing->setCategory($carRacing);
  48. $articleCarRacing->setTitle('Car racing madness');
  49. $olympicSkiing = new Category();
  50. $olympicSkiing->setParent($skiing);
  51. $olympicSkiing->setTitle('Olympic Skiing Championship 2011');
  52. $this->em->persist($sport);
  53. $this->em->persist($skiing);
  54. $this->em->persist($articleAboutSkiing);
  55. $this->em->persist($aboutSkiingArticleComment);
  56. $this->em->persist($carRacing);
  57. $this->em->persist($articleCarRacing);
  58. $this->em->persist($olympicSkiing);
  59. $this->em->flush();
  60. $this->em->clear();
  61. $meta = $this->em->getClassMetadata(self::CATEGORY);
  62. $sport = $this->em->getRepository(self::CATEGORY)->find(2);
  63. $left = $meta->getReflectionProperty('lft')->getValue($sport);
  64. $right = $meta->getReflectionProperty('rgt')->getValue($sport);
  65. $this->assertEquals($left, 9);
  66. $this->assertEquals($right, 16);
  67. $skiing = $this->em->getRepository(self::CATEGORY)->find(6);
  68. $left = $meta->getReflectionProperty('lft')->getValue($skiing);
  69. $right = $meta->getReflectionProperty('rgt')->getValue($skiing);
  70. $this->assertEquals($left, 10);
  71. $this->assertEquals($right, 13);
  72. }
  73. public function testConcurrentTree()
  74. {
  75. $meta = $this->em->getClassMetadata(self::CATEGORY);
  76. $root = $this->em->getRepository(self::CATEGORY)->find(1);
  77. $left = $meta->getReflectionProperty('lft')->getValue($root);
  78. $right = $meta->getReflectionProperty('rgt')->getValue($root);
  79. $this->assertEquals($left, 1);
  80. $this->assertEquals($right, 8);
  81. $root2 = $this->em->getRepository(self::CATEGORY)->find(2);
  82. $left = $meta->getReflectionProperty('lft')->getValue($root2);
  83. $right = $meta->getReflectionProperty('rgt')->getValue($root2);
  84. $this->assertEquals($left, 9);
  85. $this->assertEquals($right, 10);
  86. $child2Child = $this->em->getRepository(self::CATEGORY)->find(5);
  87. $left = $meta->getReflectionProperty('lft')->getValue($child2Child);
  88. $right = $meta->getReflectionProperty('rgt')->getValue($child2Child);
  89. $this->assertEquals($left, 5);
  90. $this->assertEquals($right, 6);
  91. $child2Parent = $child2Child->getParent();
  92. $child2Parent->getId(); // initialize if proxy
  93. $left = $meta->getReflectionProperty('lft')->getValue($child2Parent);
  94. $right = $meta->getReflectionProperty('rgt')->getValue($child2Parent);
  95. $this->assertEquals($left, 4);
  96. $this->assertEquals($right, 7);
  97. }
  98. protected function getUsedEntityFixtures()
  99. {
  100. return array(
  101. self::CATEGORY,
  102. self::ARTICLE,
  103. self::COMMENT
  104. );
  105. }
  106. private function populate()
  107. {
  108. $root = new Category();
  109. $root->setTitle("Root");
  110. $root2 = new Category();
  111. $root2->setTitle("Root2");
  112. $child = new Category();
  113. $child->setTitle("child");
  114. $child->setParent($root);
  115. $child2 = new Category();
  116. $child2->setTitle("child2");
  117. $child2->setParent($root);
  118. $childsChild = new Category();
  119. $childsChild->setTitle("childs2_child");
  120. $childsChild->setParent($child2);
  121. $this->em->persist($root);
  122. $this->em->persist($root2);
  123. $this->em->persist($child);
  124. $this->em->persist($child2);
  125. $this->em->persist($childsChild);
  126. $this->em->flush();
  127. $this->em->clear();
  128. }
  129. }