ConcurrencyTest.php 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. $repo = $this->em->getRepository(self::CATEGORY);
  33. $sport = $repo->findOneByTitle('Root2');
  34. $sport->setTitle('Sport');
  35. $skiing = new Category();
  36. $skiing->setTitle('Skiing');
  37. $skiing->setParent($sport);
  38. $articleAboutSkiing = new Article();
  39. $articleAboutSkiing->setCategory($skiing);
  40. $articleAboutSkiing->setTitle('About Skiing');
  41. $aboutSkiingArticleComment = new Comment();
  42. $aboutSkiingArticleComment->setArticle($articleAboutSkiing);
  43. $aboutSkiingArticleComment->setMessage('hello');
  44. $carRacing = new Category();
  45. $carRacing->setParent($sport);
  46. $carRacing->setTitle('Car Racing');
  47. $articleCarRacing = new Article();
  48. $articleCarRacing->setCategory($carRacing);
  49. $articleCarRacing->setTitle('Car racing madness');
  50. $olympicSkiing = new Category();
  51. $olympicSkiing->setParent($skiing);
  52. $olympicSkiing->setTitle('Olympic Skiing Championship 2011');
  53. $this->em->persist($sport);
  54. $this->em->persist($skiing);
  55. $this->em->persist($articleAboutSkiing);
  56. $this->em->persist($aboutSkiingArticleComment);
  57. $this->em->persist($carRacing);
  58. $this->em->persist($articleCarRacing);
  59. $this->em->persist($olympicSkiing);
  60. $this->em->flush();
  61. $this->em->clear();
  62. $meta = $this->em->getClassMetadata(self::CATEGORY);
  63. $sport = $repo->findOneByTitle('Sport');
  64. $left = $meta->getReflectionProperty('lft')->getValue($sport);
  65. $right = $meta->getReflectionProperty('rgt')->getValue($sport);
  66. $this->assertEquals(9, $left);
  67. $this->assertEquals(16, $right);
  68. $skiing = $repo->findOneByTitle('Skiing');
  69. $left = $meta->getReflectionProperty('lft')->getValue($skiing);
  70. $right = $meta->getReflectionProperty('rgt')->getValue($skiing);
  71. $this->assertEquals(10, $left);
  72. $this->assertEquals(13, $right);
  73. }
  74. public function testConcurrentTree()
  75. {
  76. $repo = $this->em->getRepository(self::CATEGORY);
  77. $meta = $this->em->getClassMetadata(self::CATEGORY);
  78. $root = $repo->findOneByTitle('Root');
  79. $this->assertEquals(1, $root->getLeft());
  80. $this->assertEquals(8, $root->getRight());
  81. $root2 = $repo->findOneByTitle('Root2');
  82. $this->assertEquals(9, $root2->getLeft());
  83. $this->assertEquals(10, $root2->getRight());
  84. $child2Child = $repo->findOneByTitle('childs2_child');
  85. $this->assertEquals(5, $child2Child->getLeft());
  86. $this->assertEquals(6, $child2Child->getRight());
  87. $child2Parent = $child2Child->getParent();
  88. $this->assertEquals(4, $child2Parent->getLeft());
  89. $this->assertEquals(7, $child2Parent->getRight());
  90. }
  91. protected function getUsedEntityFixtures()
  92. {
  93. return array(
  94. self::CATEGORY,
  95. self::ARTICLE,
  96. self::COMMENT
  97. );
  98. }
  99. private function populate()
  100. {
  101. $root = new Category();
  102. $root->setTitle("Root");
  103. $root2 = new Category();
  104. $root2->setTitle("Root2");
  105. $child = new Category();
  106. $child->setTitle("child");
  107. $child->setParent($root);
  108. $child2 = new Category();
  109. $child2->setTitle("child2");
  110. $child2->setParent($root);
  111. $childsChild = new Category();
  112. $childsChild->setTitle("childs2_child");
  113. $childsChild->setParent($child2);
  114. $this->em->persist($root);
  115. $this->em->persist($root2);
  116. $this->em->persist($child);
  117. $this->em->persist($child2);
  118. $this->em->persist($childsChild);
  119. $this->em->flush();
  120. $this->em->clear();
  121. }
  122. }