ConcurrencyTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. * @link http://www.gediminasm.org
  14. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  15. */
  16. class ConcurrencyTest extends BaseTestCaseORM
  17. {
  18. const CATEGORY = "Tree\\Fixture\\Category";
  19. const ARTICLE = "Tree\\Fixture\\Article";
  20. const COMMENT = "Tree\\Fixture\\Comment";
  21. protected function setUp()
  22. {
  23. parent::setUp();
  24. $evm = new EventManager;
  25. $evm->addEventSubscriber(new TreeListener);
  26. $this->getMockSqliteEntityManager($evm);
  27. $this->populate();
  28. }
  29. public function testConcurrentEntitiesInOneFlush()
  30. {
  31. $repo = $this->em->getRepository(self::CATEGORY);
  32. $sport = $repo->findOneByTitle('Root2');
  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 = $repo->findOneByTitle('Sport');
  63. $left = $meta->getReflectionProperty('lft')->getValue($sport);
  64. $right = $meta->getReflectionProperty('rgt')->getValue($sport);
  65. $this->assertEquals(9, $left);
  66. $this->assertEquals(16, $right);
  67. $skiing = $repo->findOneByTitle('Skiing');
  68. $left = $meta->getReflectionProperty('lft')->getValue($skiing);
  69. $right = $meta->getReflectionProperty('rgt')->getValue($skiing);
  70. $this->assertEquals(10, $left);
  71. $this->assertEquals(13, $right);
  72. }
  73. public function testConcurrentTree()
  74. {
  75. $repo = $this->em->getRepository(self::CATEGORY);
  76. $meta = $this->em->getClassMetadata(self::CATEGORY);
  77. $root = $repo->findOneByTitle('Root');
  78. $this->assertEquals(1, $root->getLeft());
  79. $this->assertEquals(8, $root->getRight());
  80. $root2 = $repo->findOneByTitle('Root2');
  81. $this->assertEquals(9, $root2->getLeft());
  82. $this->assertEquals(10, $root2->getRight());
  83. $child2Child = $repo->findOneByTitle('childs2_child');
  84. $this->assertEquals(5, $child2Child->getLeft());
  85. $this->assertEquals(6, $child2Child->getRight());
  86. $child2Parent = $child2Child->getParent();
  87. $this->assertEquals(4, $child2Parent->getLeft());
  88. $this->assertEquals(7, $child2Parent->getRight());
  89. }
  90. protected function getUsedEntityFixtures()
  91. {
  92. return array(
  93. self::CATEGORY,
  94. self::ARTICLE,
  95. self::COMMENT
  96. );
  97. }
  98. private function populate()
  99. {
  100. $root = new Category();
  101. $root->setTitle("Root");
  102. $root2 = new Category();
  103. $root2->setTitle("Root2");
  104. $child = new Category();
  105. $child->setTitle("child");
  106. $child->setParent($root);
  107. $child2 = new Category();
  108. $child2->setTitle("child2");
  109. $child2->setParent($root);
  110. $childsChild = new Category();
  111. $childsChild->setTitle("childs2_child");
  112. $childsChild->setParent($child2);
  113. $this->em->persist($root);
  114. $this->em->persist($root2);
  115. $this->em->persist($child);
  116. $this->em->persist($child2);
  117. $this->em->persist($childsChild);
  118. $this->em->flush();
  119. $this->em->clear();
  120. }
  121. }