ConcurrencyTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. namespace Gedmo\Tree;
  3. use Doctrine\Common\Util\Debug,
  4. Tree\Fixture\Category,
  5. Tree\Fixture\Article,
  6. Tree\Fixture\Comment;
  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 ConcurrencyTest extends \PHPUnit_Framework_TestCase
  16. {
  17. const TEST_ENTITY_CATEGORY = "Tree\Fixture\Category";
  18. const TEST_ENTITY_ARTICLE = "Tree\Fixture\Article";
  19. const TEST_ENTITY_COMMENT = "Tree\Fixture\Comment";
  20. private $em;
  21. public function setUp()
  22. {
  23. $classLoader = new \Doctrine\Common\ClassLoader('Tree\Fixture', __DIR__ . '/../');
  24. $classLoader->register();
  25. $config = new \Doctrine\ORM\Configuration();
  26. $config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache);
  27. $config->setQueryCacheImpl(new \Doctrine\Common\Cache\ArrayCache);
  28. $config->setProxyDir(__DIR__ . '/Proxy');
  29. $config->setProxyNamespace('Gedmo\Tree\Proxies');
  30. $config->setMetadataDriverImpl($config->newDefaultAnnotationDriver());
  31. $conn = array(
  32. 'driver' => 'pdo_sqlite',
  33. 'memory' => true,
  34. );
  35. //$config->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger());
  36. $evm = new \Doctrine\Common\EventManager();
  37. $treeListener = new TreeListener();
  38. $evm->addEventSubscriber($treeListener);
  39. $this->em = \Doctrine\ORM\EntityManager::create($conn, $config, $evm);
  40. $schemaTool = new \Doctrine\ORM\Tools\SchemaTool($this->em);
  41. $schemaTool->dropSchema(array());
  42. $schemaTool->createSchema(array(
  43. $this->em->getClassMetadata(self::TEST_ENTITY_CATEGORY),
  44. $this->em->getClassMetadata(self::TEST_ENTITY_ARTICLE),
  45. $this->em->getClassMetadata(self::TEST_ENTITY_COMMENT)
  46. ));
  47. $this->_populate();
  48. }
  49. public function testConcurrentEntitiesInOneFlush()
  50. {
  51. $sport = $this->em->getRepository(self::TEST_ENTITY_CATEGORY)->find(2);
  52. $sport->setTitle('Sport');
  53. $skiing = new Category();
  54. $skiing->setTitle('Skiing');
  55. $skiing->setParent($sport);
  56. $articleAboutSkiing = new Article();
  57. $articleAboutSkiing->setCategory($skiing);
  58. $articleAboutSkiing->setTitle('About Skiing');
  59. $aboutSkiingArticleComment = new Comment();
  60. $aboutSkiingArticleComment->setArticle($articleAboutSkiing);
  61. $aboutSkiingArticleComment->setMessage('hello');
  62. $carRacing = new Category();
  63. $carRacing->setParent($sport);
  64. $carRacing->setTitle('Car Racing');
  65. $articleCarRacing = new Article();
  66. $articleCarRacing->setCategory($carRacing);
  67. $articleCarRacing->setTitle('Car racing madness');
  68. $olympicSkiing = new Category();
  69. $olympicSkiing->setParent($skiing);
  70. $olympicSkiing->setTitle('Olympic Skiing Championship 2011');
  71. $this->em->persist($sport);
  72. $this->em->persist($skiing);
  73. $this->em->persist($articleAboutSkiing);
  74. $this->em->persist($aboutSkiingArticleComment);
  75. $this->em->persist($carRacing);
  76. $this->em->persist($articleCarRacing);
  77. $this->em->persist($olympicSkiing);
  78. $this->em->flush();
  79. $this->em->clear();
  80. $meta = $this->em->getClassMetadata(self::TEST_ENTITY_CATEGORY);
  81. $sport = $this->em->getRepository(self::TEST_ENTITY_CATEGORY)->find(2);
  82. $left = $meta->getReflectionProperty('lft')->getValue($sport);
  83. $right = $meta->getReflectionProperty('rgt')->getValue($sport);
  84. $this->assertEquals($left, 9);
  85. $this->assertEquals($right, 16);
  86. $skiing = $this->em->getRepository(self::TEST_ENTITY_CATEGORY)->find(6);
  87. $left = $meta->getReflectionProperty('lft')->getValue($skiing);
  88. $right = $meta->getReflectionProperty('rgt')->getValue($skiing);
  89. $this->assertEquals($left, 10);
  90. $this->assertEquals($right, 13);
  91. }
  92. public function testConcurrentTree()
  93. {
  94. $meta = $this->em->getClassMetadata(self::TEST_ENTITY_CATEGORY);
  95. $root = $this->em->getRepository(self::TEST_ENTITY_CATEGORY)->find(1);
  96. $left = $meta->getReflectionProperty('lft')->getValue($root);
  97. $right = $meta->getReflectionProperty('rgt')->getValue($root);
  98. $this->assertEquals($left, 1);
  99. $this->assertEquals($right, 8);
  100. $root2 = $this->em->getRepository(self::TEST_ENTITY_CATEGORY)->find(2);
  101. $left = $meta->getReflectionProperty('lft')->getValue($root2);
  102. $right = $meta->getReflectionProperty('rgt')->getValue($root2);
  103. $this->assertEquals($left, 9);
  104. $this->assertEquals($right, 10);
  105. $child2Child = $this->em->getRepository(self::TEST_ENTITY_CATEGORY)->find(5);
  106. $left = $meta->getReflectionProperty('lft')->getValue($child2Child);
  107. $right = $meta->getReflectionProperty('rgt')->getValue($child2Child);
  108. $this->assertEquals($left, 5);
  109. $this->assertEquals($right, 6);
  110. $child2Parent = $child2Child->getParent();
  111. $child2Parent->getId(); // initialize if proxy
  112. $left = $meta->getReflectionProperty('lft')->getValue($child2Parent);
  113. $right = $meta->getReflectionProperty('rgt')->getValue($child2Parent);
  114. $this->assertEquals($left, 4);
  115. $this->assertEquals($right, 7);
  116. }
  117. protected function _populate()
  118. {
  119. $root = new Category();
  120. $root->setTitle("Root");
  121. $root2 = new Category();
  122. $root2->setTitle("Root2");
  123. $child = new Category();
  124. $child->setTitle("child");
  125. $child->setParent($root);
  126. $child2 = new Category();
  127. $child2->setTitle("child2");
  128. $child2->setParent($root);
  129. $childsChild = new Category();
  130. $childsChild->setTitle("childs2_child");
  131. $childsChild->setParent($child2);
  132. $this->em->persist($root);
  133. $this->em->persist($root2);
  134. $this->em->persist($child);
  135. $this->em->persist($child2);
  136. $this->em->persist($childsChild);
  137. $this->em->flush();
  138. $this->em->clear();
  139. }
  140. }