MaterializedPathODMMongoDBTreeLockingTest.php 3.1KB

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