LockTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. namespace Doctrine\Tests\ORM\Functional\Locking;
  3. use Doctrine\Tests\Models\CMS\CmsArticle,
  4. Doctrine\Tests\Models\CMS\CmsUser,
  5. Doctrine\DBAL\LockMode,
  6. Doctrine\ORM\EntityManager;
  7. require_once __DIR__ . '/../../../TestInit.php';
  8. /**
  9. * @group locking
  10. */
  11. class LockTest extends \Doctrine\Tests\OrmFunctionalTestCase {
  12. protected function setUp() {
  13. $this->useModelSet('cms');
  14. parent::setUp();
  15. $this->handles = array();
  16. }
  17. /**
  18. * @group DDC-178
  19. * @group locking
  20. */
  21. public function testLockVersionedEntity() {
  22. $article = new CmsArticle();
  23. $article->text = "my article";
  24. $article->topic = "Hello";
  25. $this->_em->persist($article);
  26. $this->_em->flush();
  27. $this->_em->lock($article, LockMode::OPTIMISTIC, $article->version);
  28. }
  29. /**
  30. * @group DDC-178
  31. * @group locking
  32. */
  33. public function testLockVersionedEntity_MissmatchThrowsException() {
  34. $article = new CmsArticle();
  35. $article->text = "my article";
  36. $article->topic = "Hello";
  37. $this->_em->persist($article);
  38. $this->_em->flush();
  39. $this->setExpectedException('Doctrine\ORM\OptimisticLockException');
  40. $this->_em->lock($article, LockMode::OPTIMISTIC, $article->version + 1);
  41. }
  42. /**
  43. * @group DDC-178
  44. * @group locking
  45. */
  46. public function testLockUnversionedEntity_ThrowsException() {
  47. $user = new CmsUser();
  48. $user->name = "foo";
  49. $user->status = "active";
  50. $user->username = "foo";
  51. $this->_em->persist($user);
  52. $this->_em->flush();
  53. $this->setExpectedException('Doctrine\ORM\OptimisticLockException');
  54. $this->_em->lock($user, LockMode::OPTIMISTIC);
  55. }
  56. /**
  57. * @group DDC-178
  58. * @group locking
  59. */
  60. public function testLockUnmanagedEntity_ThrowsException() {
  61. $article = new CmsArticle();
  62. $this->setExpectedException('InvalidArgumentException', 'Entity Doctrine\Tests\Models\CMS\CmsArticle');
  63. $this->_em->lock($article, LockMode::OPTIMISTIC, $article->version + 1);
  64. }
  65. /**
  66. * @group DDC-178
  67. * @group locking
  68. */
  69. public function testLockPessimisticRead_NoTransaction_ThrowsException() {
  70. $article = new CmsArticle();
  71. $article->text = "my article";
  72. $article->topic = "Hello";
  73. $this->_em->persist($article);
  74. $this->_em->flush();
  75. $this->setExpectedException('Doctrine\ORM\TransactionRequiredException');
  76. $this->_em->lock($article, LockMode::PESSIMISTIC_READ);
  77. }
  78. /**
  79. * @group DDC-178
  80. * @group locking
  81. */
  82. public function testLockPessimisticWrite_NoTransaction_ThrowsException() {
  83. $article = new CmsArticle();
  84. $article->text = "my article";
  85. $article->topic = "Hello";
  86. $this->_em->persist($article);
  87. $this->_em->flush();
  88. $this->setExpectedException('Doctrine\ORM\TransactionRequiredException');
  89. $this->_em->lock($article, LockMode::PESSIMISTIC_WRITE);
  90. }
  91. /**
  92. * @group DDC-178
  93. * @group locking
  94. */
  95. public function testLockPessimisticWrite() {
  96. $writeLockSql = $this->_em->getConnection()->getDatabasePlatform()->getWriteLockSql();
  97. if (strlen($writeLockSql) == 0) {
  98. $this->markTestSkipped('Database Driver has no Write Lock support.');
  99. }
  100. $article = new CmsArticle();
  101. $article->text = "my article";
  102. $article->topic = "Hello";
  103. $this->_em->persist($article);
  104. $this->_em->flush();
  105. $this->_em->beginTransaction();
  106. try {
  107. $this->_em->lock($article, LockMode::PESSIMISTIC_WRITE);
  108. $this->_em->commit();
  109. } catch (\Exception $e) {
  110. $this->_em->rollback();
  111. throw $e;
  112. }
  113. $query = array_pop( $this->_sqlLoggerStack->queries );
  114. $this->assertContains($writeLockSql, $query['sql']);
  115. }
  116. /**
  117. * @group DDC-178
  118. */
  119. public function testLockPessimisticRead() {
  120. $readLockSql = $this->_em->getConnection()->getDatabasePlatform()->getReadLockSql();
  121. if (strlen($readLockSql) == 0) {
  122. $this->markTestSkipped('Database Driver has no Write Lock support.');
  123. }
  124. $article = new CmsArticle();
  125. $article->text = "my article";
  126. $article->topic = "Hello";
  127. $this->_em->persist($article);
  128. $this->_em->flush();
  129. $this->_em->beginTransaction();
  130. try {
  131. $this->_em->lock($article, LockMode::PESSIMISTIC_READ);
  132. $this->_em->commit();
  133. } catch (\Exception $e) {
  134. $this->_em->rollback();
  135. throw $e;
  136. }
  137. $query = array_pop( $this->_sqlLoggerStack->queries );
  138. $this->assertContains($readLockSql, $query['sql']);
  139. }
  140. /**
  141. * @group DDC-1693
  142. */
  143. public function testLockOptimisticNonVersionedThrowsExceptionInDQL()
  144. {
  145. $dql = "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.username = 'gblanco'";
  146. $this->setExpectedException('Doctrine\ORM\OptimisticLockException', 'The optimistic lock on an entity failed.');
  147. $sql = $this->_em->createQuery($dql)->setHint(
  148. \Doctrine\ORM\Query::HINT_LOCK_MODE, \Doctrine\DBAL\LockMode::OPTIMISTIC
  149. )->getSQL();
  150. }
  151. }