MaterializedPathODMMongoDBTreeLockingTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace Gedmo\Tree;
  3. use Doctrine\Common\EventManager;
  4. use Tool\BaseTestCaseMongoODM;
  5. use Doctrine\Common\Util\Debug;
  6. use Tree\Fixture\RootCategory;
  7. use Tree\Fixture\Mock\TreeListenerMock;
  8. /**
  9. * These are tests for Tree behavior
  10. *
  11. * @author Gustavo Falco <comfortablynumb84@gmail.com>
  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 MaterializedPathODMMongoDBTreeLockingTest extends BaseTestCaseMongoODM
  17. {
  18. const ARTICLE = "Tree\\Fixture\\Document\\Article";
  19. protected $config;
  20. protected $listener;
  21. protected function setUp()
  22. {
  23. parent::setUp();
  24. $this->listener = new TreeListenerMock;
  25. $evm = new EventManager;
  26. $evm->addEventSubscriber($this->listener);
  27. $this->getMockDocumentManager($evm);
  28. $meta = $this->dm->getClassMetadata(self::ARTICLE);
  29. $this->config = $this->listener->getConfiguration($this->dm, $meta->name);
  30. }
  31. /**
  32. * @test
  33. */
  34. public function modifyingANodeWhileItsTreeIsLockedShouldThrowAnException()
  35. {
  36. // By default, TreeListenerMock disables the release of the locks
  37. // for testing purposes
  38. $this->setExpectedException('Gedmo\Exception\TreeLockingException');
  39. $article = $this->createArticle();
  40. $article->setTitle('1');
  41. $article2 = $this->createArticle();
  42. $article2->setTitle('2');
  43. $article2->setParent($article);
  44. $this->dm->persist($article);
  45. $this->dm->persist($article2);
  46. $this->dm->flush();
  47. $this->dm->refresh($article);
  48. $this->dm->refresh($article2);
  49. $article2->setTitle('New title');
  50. $this->dm->flush();
  51. }
  52. /**
  53. * @test
  54. */
  55. public function modifyingANodeWhileItsTreeIsNotLockedShouldNotThrowException()
  56. {
  57. $article = $this->createArticle();
  58. $article->setTitle('1');
  59. $article2 = $this->createArticle();
  60. $article2->setTitle('2');
  61. $article2->setParent($article);
  62. // These tree will be locked after flush, simulating concurrency
  63. $this->dm->persist($article);
  64. $this->dm->persist($article2);
  65. $this->dm->flush();
  66. $this->dm->clear();
  67. // These one will release the lock as normal
  68. $this->listener->setReleaseLocks(true);
  69. $article3 = $this->createArticle();
  70. $article3->setTitle('3');
  71. $this->dm->persist($article3);
  72. $this->dm->flush();
  73. // This should NOT throw an exception
  74. $article3->setTitle('New title');
  75. $this->dm->flush();
  76. // But this should throw it, because the root of its tree ($article) is still locked
  77. $this->setExpectedException('Gedmo\Exception\TreeLockingException');
  78. $repo = $this->dm->getRepository(self::ARTICLE);
  79. $article2 = $repo->findOneByTitle('2');
  80. $article2->setTitle('New title 2');
  81. $this->dm->flush();
  82. }
  83. public function createArticle()
  84. {
  85. $class = self::ARTICLE;
  86. return new $class;
  87. }
  88. }