Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / orm / tests / Doctrine / Tests / ORM / Functional / Locking / LockTest.php
1 <?php
2
3 namespace Doctrine\Tests\ORM\Functional\Locking;
4
5 use Doctrine\Tests\Models\CMS\CmsArticle,
6     Doctrine\Tests\Models\CMS\CmsUser,
7     Doctrine\DBAL\LockMode,
8     Doctrine\ORM\EntityManager;
9
10 require_once __DIR__ . '/../../../TestInit.php';
11
12 /**
13  * @group locking
14  */
15 class LockTest extends \Doctrine\Tests\OrmFunctionalTestCase {
16     protected function setUp() {
17         $this->useModelSet('cms');
18         parent::setUp();
19         $this->handles = array();
20     }
21
22     /**
23      * @group DDC-178
24      * @group locking
25      */
26     public function testLockVersionedEntity() {
27         $article = new CmsArticle();
28         $article->text = "my article";
29         $article->topic = "Hello";
30
31         $this->_em->persist($article);
32         $this->_em->flush();
33
34         $this->_em->lock($article, LockMode::OPTIMISTIC, $article->version);
35     }
36
37     /**
38      * @group DDC-178
39      * @group locking
40      */
41     public function testLockVersionedEntity_MissmatchThrowsException() {
42         $article = new CmsArticle();
43         $article->text = "my article";
44         $article->topic = "Hello";
45
46         $this->_em->persist($article);
47         $this->_em->flush();
48
49         $this->setExpectedException('Doctrine\ORM\OptimisticLockException');
50         $this->_em->lock($article, LockMode::OPTIMISTIC, $article->version + 1);
51     }
52
53     /**
54      * @group DDC-178
55      * @group locking
56      */
57     public function testLockUnversionedEntity_ThrowsException() {
58         $user = new CmsUser();
59         $user->name = "foo";
60         $user->status = "active";
61         $user->username = "foo";
62
63         $this->_em->persist($user);
64         $this->_em->flush();
65
66         $this->setExpectedException('Doctrine\ORM\OptimisticLockException');
67         $this->_em->lock($user, LockMode::OPTIMISTIC);
68     }
69
70     /**
71      * @group DDC-178
72      * @group locking
73      */
74     public function testLockUnmanagedEntity_ThrowsException() {
75         $article = new CmsArticle();
76
77         $this->setExpectedException('InvalidArgumentException', 'Entity Doctrine\Tests\Models\CMS\CmsArticle');
78         $this->_em->lock($article, LockMode::OPTIMISTIC, $article->version + 1);
79     }
80
81     /**
82      * @group DDC-178
83      * @group locking
84      */
85     public function testLockPessimisticRead_NoTransaction_ThrowsException() {
86         $article = new CmsArticle();
87         $article->text = "my article";
88         $article->topic = "Hello";
89
90         $this->_em->persist($article);
91         $this->_em->flush();
92
93         $this->setExpectedException('Doctrine\ORM\TransactionRequiredException');
94         $this->_em->lock($article, LockMode::PESSIMISTIC_READ);
95     }
96
97     /**
98      * @group DDC-178
99      * @group locking
100      */
101     public function testLockPessimisticWrite_NoTransaction_ThrowsException() {
102         $article = new CmsArticle();
103         $article->text = "my article";
104         $article->topic = "Hello";
105
106         $this->_em->persist($article);
107         $this->_em->flush();
108
109         $this->setExpectedException('Doctrine\ORM\TransactionRequiredException');
110         $this->_em->lock($article, LockMode::PESSIMISTIC_WRITE);
111     }
112
113     /**
114      * @group DDC-178
115      * @group locking
116      */
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.');
121         }
122
123         $article = new CmsArticle();
124         $article->text = "my article";
125         $article->topic = "Hello";
126
127         $this->_em->persist($article);
128         $this->_em->flush();
129
130         $this->_em->beginTransaction();
131         try {
132             $this->_em->lock($article, LockMode::PESSIMISTIC_WRITE);
133             $this->_em->commit();
134         } catch (\Exception $e) {
135             $this->_em->rollback();
136             throw $e;
137         }
138
139         $query = array_pop( $this->_sqlLoggerStack->queries );
140         $query = array_pop( $this->_sqlLoggerStack->queries );
141         $this->assertContains($writeLockSql, $query['sql']);
142     }
143
144     /**
145      * @group DDC-178
146      */
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.');
151         }
152
153         $article = new CmsArticle();
154         $article->text = "my article";
155         $article->topic = "Hello";
156
157         $this->_em->persist($article);
158         $this->_em->flush();
159
160         $this->_em->beginTransaction();
161         try {
162             $this->_em->lock($article, LockMode::PESSIMISTIC_READ);
163             $this->_em->commit();
164         } catch (\Exception $e) {
165             $this->_em->rollback();
166             throw $e;
167         }
168
169         $query = array_pop( $this->_sqlLoggerStack->queries );
170         $query = array_pop( $this->_sqlLoggerStack->queries );
171         $this->assertContains($readLockSql, $query['sql']);
172     }
173
174     /**
175      * @group DDC-1693
176      */
177     public function testLockOptimisticNonVersionedThrowsExceptionInDQL()
178     {
179         $dql = "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.username = 'gblanco'";
180
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
184         )->getSQL();
185     }
186 }