3 namespace Doctrine\Tests\ORM\Functional\Locking;
5 use Doctrine\Tests\Models\CMS\CmsArticle,
6 Doctrine\Tests\Models\CMS\CmsUser,
7 Doctrine\DBAL\LockMode,
8 Doctrine\ORM\EntityManager;
10 require_once __DIR__ . '/../../../TestInit.php';
15 class LockTest extends \Doctrine\Tests\OrmFunctionalTestCase {
16 protected function setUp() {
17 $this->useModelSet('cms');
19 $this->handles = array();
26 public function testLockVersionedEntity() {
27 $article = new CmsArticle();
28 $article->text = "my article";
29 $article->topic = "Hello";
31 $this->_em->persist($article);
34 $this->_em->lock($article, LockMode::OPTIMISTIC, $article->version);
41 public function testLockVersionedEntity_MissmatchThrowsException() {
42 $article = new CmsArticle();
43 $article->text = "my article";
44 $article->topic = "Hello";
46 $this->_em->persist($article);
49 $this->setExpectedException('Doctrine\ORM\OptimisticLockException');
50 $this->_em->lock($article, LockMode::OPTIMISTIC, $article->version + 1);
57 public function testLockUnversionedEntity_ThrowsException() {
58 $user = new CmsUser();
60 $user->status = "active";
61 $user->username = "foo";
63 $this->_em->persist($user);
66 $this->setExpectedException('Doctrine\ORM\OptimisticLockException');
67 $this->_em->lock($user, LockMode::OPTIMISTIC);
74 public function testLockUnmanagedEntity_ThrowsException() {
75 $article = new CmsArticle();
77 $this->setExpectedException('InvalidArgumentException', 'Entity Doctrine\Tests\Models\CMS\CmsArticle');
78 $this->_em->lock($article, LockMode::OPTIMISTIC, $article->version + 1);
85 public function testLockPessimisticRead_NoTransaction_ThrowsException() {
86 $article = new CmsArticle();
87 $article->text = "my article";
88 $article->topic = "Hello";
90 $this->_em->persist($article);
93 $this->setExpectedException('Doctrine\ORM\TransactionRequiredException');
94 $this->_em->lock($article, LockMode::PESSIMISTIC_READ);
101 public function testLockPessimisticWrite_NoTransaction_ThrowsException() {
102 $article = new CmsArticle();
103 $article->text = "my article";
104 $article->topic = "Hello";
106 $this->_em->persist($article);
109 $this->setExpectedException('Doctrine\ORM\TransactionRequiredException');
110 $this->_em->lock($article, LockMode::PESSIMISTIC_WRITE);
117 public function testLockPessimisticWrite() {
118 $writeLockSql = $this->_em->getConnection()->getDatabasePlatform()->getWriteLockSql();
119 if (strlen($writeLockSql) == 0) {
120 $this->markTestSkipped('Database Driver has no Write Lock support.');
123 $article = new CmsArticle();
124 $article->text = "my article";
125 $article->topic = "Hello";
127 $this->_em->persist($article);
130 $this->_em->beginTransaction();
132 $this->_em->lock($article, LockMode::PESSIMISTIC_WRITE);
133 $this->_em->commit();
134 } catch (\Exception $e) {
135 $this->_em->rollback();
139 $query = array_pop( $this->_sqlLoggerStack->queries );
140 $query = array_pop( $this->_sqlLoggerStack->queries );
141 $this->assertContains($writeLockSql, $query['sql']);
147 public function testLockPessimisticRead() {
148 $readLockSql = $this->_em->getConnection()->getDatabasePlatform()->getReadLockSql();
149 if (strlen($readLockSql) == 0) {
150 $this->markTestSkipped('Database Driver has no Write Lock support.');
153 $article = new CmsArticle();
154 $article->text = "my article";
155 $article->topic = "Hello";
157 $this->_em->persist($article);
160 $this->_em->beginTransaction();
162 $this->_em->lock($article, LockMode::PESSIMISTIC_READ);
163 $this->_em->commit();
164 } catch (\Exception $e) {
165 $this->_em->rollback();
169 $query = array_pop( $this->_sqlLoggerStack->queries );
170 $query = array_pop( $this->_sqlLoggerStack->queries );
171 $this->assertContains($readLockSql, $query['sql']);
177 public function testLockOptimisticNonVersionedThrowsExceptionInDQL()
179 $dql = "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.username = 'gblanco'";
181 $this->setExpectedException('Doctrine\ORM\OptimisticLockException', 'The optimistic lock on an entity failed.');
182 $sql = $this->_em->createQuery($dql)->setHint(
183 \Doctrine\ORM\Query::HINT_LOCK_MODE, \Doctrine\DBAL\LockMode::OPTIMISTIC