Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / orm / tests / Doctrine / Tests / ORM / Functional / Locking / OptimisticTest.php
1 <?php
2
3 namespace Doctrine\Tests\ORM\Functional\Locking;
4
5 use Doctrine\ORM\Mapping\ClassMetadata;
6 use Doctrine\ORM\OptimisticLockException;
7 use Doctrine\Common\EventManager;
8 use Doctrine\ORM\Mapping\ClassMetadataFactory;
9 use Doctrine\Tests\TestUtil;
10
11 require_once __DIR__ . '/../../../TestInit.php';
12
13 class OptimisticTest extends \Doctrine\Tests\OrmFunctionalTestCase
14 {
15     protected function setUp()
16     {
17         parent::setUp();
18
19         try {
20             $this->_schemaTool->createSchema(array(
21                 $this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\Locking\OptimisticJoinedParent'),
22                 $this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\Locking\OptimisticJoinedChild'),
23                 $this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\Locking\OptimisticStandard'),
24                 $this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\Locking\OptimisticTimestamp')
25             ));
26         } catch (\Exception $e) {
27             // Swallow all exceptions. We do not test the schema tool here.
28         }
29         $this->_conn = $this->_em->getConnection();
30     }
31
32     public function testJoinedChildInsertSetsInitialVersionValue()
33     {
34         $test = new OptimisticJoinedChild();
35         $test->name = 'child';
36         $test->whatever = 'whatever';
37         $this->_em->persist($test);
38         $this->_em->flush();
39
40         $this->assertEquals(1, $test->version);
41
42         return $test;
43     }
44
45     /**
46      * @depends testJoinedChildInsertSetsInitialVersionValue
47      */
48     public function testJoinedChildFailureThrowsException(OptimisticJoinedChild $child)
49     {
50         $q = $this->_em->createQuery('SELECT t FROM Doctrine\Tests\ORM\Functional\Locking\OptimisticJoinedChild t WHERE t.id = :id');
51         $q->setParameter('id', $child->id);
52         $test = $q->getSingleResult();
53
54         // Manually update/increment the version so we can try and save the same
55         // $test and make sure the exception is thrown saying the record was
56         // changed or updated since you read it
57         $this->_conn->executeQuery('UPDATE optimistic_joined_parent SET version = ? WHERE id = ?', array(2, $test->id));
58
59         // Now lets change a property and try and save it again
60         $test->whatever = 'ok';
61         try {
62             $this->_em->flush();
63         } catch (OptimisticLockException $e) {
64             $this->assertSame($test, $e->getEntity());
65         }
66     }
67
68     public function testJoinedParentInsertSetsInitialVersionValue()
69     {
70         $test = new OptimisticJoinedParent();
71         $test->name = 'parent';
72         $this->_em->persist($test);
73         $this->_em->flush();
74
75         $this->assertEquals(1, $test->version);
76
77         return $test;
78     }
79
80     /**
81      * @depends testJoinedParentInsertSetsInitialVersionValue
82      */
83     public function testJoinedParentFailureThrowsException(OptimisticJoinedParent $parent)
84     {
85         $q = $this->_em->createQuery('SELECT t FROM Doctrine\Tests\ORM\Functional\Locking\OptimisticJoinedParent t WHERE t.id = :id');
86         $q->setParameter('id', $parent->id);
87         $test = $q->getSingleResult();
88
89         // Manually update/increment the version so we can try and save the same
90         // $test and make sure the exception is thrown saying the record was
91         // changed or updated since you read it
92         $this->_conn->executeQuery('UPDATE optimistic_joined_parent SET version = ? WHERE id = ?', array(2, $test->id));
93
94         // Now lets change a property and try and save it again
95         $test->name = 'WHATT???';
96         try {
97             $this->_em->flush();
98         } catch (OptimisticLockException $e) {
99             $this->assertSame($test, $e->getEntity());
100         }
101     }
102
103     public function testMultipleFlushesDoIncrementalUpdates()
104     {
105         $test = new OptimisticStandard();
106
107         for ($i = 0; $i < 5; $i++) {
108             $test->name = 'test' . $i;
109             $this->_em->persist($test);
110             $this->_em->flush();
111
112             $this->assertInternalType('int', $test->getVersion());
113             $this->assertEquals($i + 1, $test->getVersion());
114         }
115     }
116
117     public function testStandardInsertSetsInitialVersionValue()
118     {
119         $test = new OptimisticStandard();
120         $test->name = 'test';
121         $this->_em->persist($test);
122         $this->_em->flush();
123
124         $this->assertInternalType('int', $test->getVersion());
125         $this->assertEquals(1, $test->getVersion());
126
127         return $test;
128     }
129
130     /**
131      * @depends testStandardInsertSetsInitialVersionValue
132      */
133     public function testStandardFailureThrowsException(OptimisticStandard $entity)
134     {
135         $q = $this->_em->createQuery('SELECT t FROM Doctrine\Tests\ORM\Functional\Locking\OptimisticStandard t WHERE t.id = :id');
136         $q->setParameter('id', $entity->id);
137         $test = $q->getSingleResult();
138
139         // Manually update/increment the version so we can try and save the same
140         // $test and make sure the exception is thrown saying the record was
141         // changed or updated since you read it
142         $this->_conn->executeQuery('UPDATE optimistic_standard SET version = ? WHERE id = ?', array(2, $test->id));
143
144         // Now lets change a property and try and save it again
145         $test->name = 'WHATT???';
146         try {
147             $this->_em->flush();
148         } catch (OptimisticLockException $e) {
149             $this->assertSame($test, $e->getEntity());
150         }
151     }
152
153     public function testOptimisticTimestampSetsDefaultValue()
154     {
155         $test = new OptimisticTimestamp();
156         $test->name = 'Testing';
157
158         $this->assertNull($test->version, "Pre-Condition");
159
160         $this->_em->persist($test);
161         $this->_em->flush();
162
163         $this->assertInstanceOf('DateTime', $test->version);
164
165         return $test;
166     }
167
168     /**
169      * @depends testOptimisticTimestampSetsDefaultValue
170      */
171     public function testOptimisticTimestampFailureThrowsException(OptimisticTimestamp $entity)
172     {
173         $q = $this->_em->createQuery('SELECT t FROM Doctrine\Tests\ORM\Functional\Locking\OptimisticTimestamp t WHERE t.id = :id');
174         $q->setParameter('id', $entity->id);
175         $test = $q->getSingleResult();
176
177         $this->assertInstanceOf('DateTime', $test->version);
178
179         // Manually increment the version datetime column
180         $format = $this->_em->getConnection()->getDatabasePlatform()->getDateTimeFormatString();
181         $this->_conn->executeQuery('UPDATE optimistic_timestamp SET version = ? WHERE id = ?', array(date($format, strtotime($test->version->format($format)) + 3600), $test->id));
182
183         // Try and update the record and it should throw an exception
184         $test->name = 'Testing again';
185         try {
186             $this->_em->flush();
187         } catch (OptimisticLockException $e) {
188             $this->assertSame($test, $e->getEntity());
189         }
190     }
191 }
192
193 /**
194  * @Entity
195  * @Table(name="optimistic_joined_parent")
196  * @InheritanceType("JOINED")
197  * @DiscriminatorColumn(name="discr", type="string")
198  * @DiscriminatorMap({"parent" = "OptimisticJoinedParent", "child" = "OptimisticJoinedChild"})
199  */
200 class OptimisticJoinedParent
201 {
202     /**
203      * @Id @Column(type="integer")
204      * @GeneratedValue(strategy="AUTO")
205      */
206     public $id;
207
208     /**
209      * @Column(type="string", length=255)
210      */
211     public $name;
212
213     /**
214      * @Version @Column(type="integer")
215      */
216     public $version;
217 }
218
219 /**
220  * @Entity
221  * @Table(name="optimistic_joined_child")
222  */
223 class OptimisticJoinedChild extends OptimisticJoinedParent
224 {
225     /**
226      * @Column(type="string", length=255)
227      */
228     public $whatever;
229 }
230
231 /**
232  * @Entity
233  * @Table(name="optimistic_standard")
234  */
235 class OptimisticStandard
236 {
237     /**
238      * @Id @Column(type="integer")
239      * @GeneratedValue(strategy="AUTO")
240      */
241     public $id;
242
243     /**
244      * @Column(type="string", length=255)
245      */
246     public $name;
247
248     /**
249      * @Version @Column(type="integer")
250      */
251     private $version;
252
253     function getVersion() {return $this->version;}
254 }
255
256 /**
257  * @Entity
258  * @Table(name="optimistic_timestamp")
259  */
260 class OptimisticTimestamp
261 {
262     /**
263      * @Id @Column(type="integer")
264      * @GeneratedValue(strategy="AUTO")
265      */
266     public $id;
267
268     /**
269      * @Column(type="string", length=255)
270      */
271     public $name;
272
273     /**
274      * @Version @Column(type="datetime")
275      */
276     public $version;
277 }