Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / dbal / tests / Doctrine / Tests / DBAL / Functional / ConnectionTest.php
1 <?php
2
3 namespace Doctrine\Tests\DBAL\Functional;
4
5 use Doctrine\DBAL\ConnectionException;
6 use Doctrine\DBAL\Types\Type;
7
8 require_once __DIR__ . '/../../TestInit.php';
9
10 class ConnectionTest extends \Doctrine\Tests\DbalFunctionalTestCase
11 {
12     public function setUp()
13     {
14         $this->resetSharedConn();
15         parent::setUp();
16     }
17
18     public function tearDown()
19     {
20         parent::tearDown();
21         $this->resetSharedConn();
22     }
23
24     public function testGetWrappedConnection()
25     {
26         $this->assertInstanceOf('Doctrine\DBAL\Driver\Connection', $this->_conn->getWrappedConnection());
27     }
28
29     public function testCommitWithRollbackOnlyThrowsException()
30     {
31         $this->_conn->beginTransaction();
32         $this->_conn->setRollbackOnly();
33         $this->setExpectedException('Doctrine\DBAL\ConnectionException');
34         $this->_conn->commit();
35     }
36
37     public function testTransactionNestingBehavior()
38     {
39         try {
40             $this->_conn->beginTransaction();
41             $this->assertEquals(1, $this->_conn->getTransactionNestingLevel());
42
43             try {
44                 $this->_conn->beginTransaction();
45                 $this->assertEquals(2, $this->_conn->getTransactionNestingLevel());
46                 throw new \Exception;
47                 $this->_conn->commit(); // never reached
48             } catch (\Exception $e) {
49                 $this->_conn->rollback();
50                 $this->assertEquals(1, $this->_conn->getTransactionNestingLevel());
51                 //no rethrow
52             }
53             $this->assertTrue($this->_conn->isRollbackOnly());
54
55             $this->_conn->commit(); // should throw exception
56             $this->fail('Transaction commit after failed nested transaction should fail.');
57         } catch (ConnectionException $e) {
58             $this->assertEquals(1, $this->_conn->getTransactionNestingLevel());
59             $this->_conn->rollback();
60             $this->assertEquals(0, $this->_conn->getTransactionNestingLevel());
61         }
62     }
63
64     public function testTransactionNestingBehaviorWithSavepoints()
65     {
66         if (!$this->_conn->getDatabasePlatform()->supportsSavepoints()) {
67             $this->markTestSkipped('This test requires the platform to support savepoints.');
68         }
69
70         $this->_conn->setNestTransactionsWithSavepoints(true);
71         try {
72             $this->_conn->beginTransaction();
73             $this->assertEquals(1, $this->_conn->getTransactionNestingLevel());
74
75             try {
76                 $this->_conn->beginTransaction();
77                 $this->assertEquals(2, $this->_conn->getTransactionNestingLevel());
78                 $this->_conn->beginTransaction();
79                 $this->assertEquals(3, $this->_conn->getTransactionNestingLevel());
80                 $this->_conn->commit();
81                 $this->assertEquals(2, $this->_conn->getTransactionNestingLevel());
82                 throw new \Exception;
83                 $this->_conn->commit(); // never reached
84             } catch (\Exception $e) {
85                 $this->_conn->rollback();
86                 $this->assertEquals(1, $this->_conn->getTransactionNestingLevel());
87                 //no rethrow
88             }
89             $this->assertFalse($this->_conn->isRollbackOnly());
90             try {
91                 $this->_conn->setNestTransactionsWithSavepoints(false);
92                 $this->fail('Should not be able to disable savepoints in usage for nested transactions inside an open transaction.');
93             } catch (ConnectionException $e) {
94                 $this->assertTrue($this->_conn->getNestTransactionsWithSavepoints());
95             }
96             $this->_conn->commit(); // should not throw exception
97         } catch (ConnectionException $e) {
98             $this->fail('Transaction commit after failed nested transaction should not fail when using savepoints.');
99             $this->_conn->rollback();
100         }
101     }
102
103     public function testTransactionNestingBehaviorCantBeChangedInActiveTransaction()
104     {
105         if (!$this->_conn->getDatabasePlatform()->supportsSavepoints()) {
106             $this->markTestSkipped('This test requires the platform to support savepoints.');
107         }
108
109         $this->_conn->beginTransaction();
110         try {
111             $this->_conn->setNestTransactionsWithSavepoints(true);
112             $this->fail('An exception should have been thrown by chaning the nesting transaction behavior within an transaction.');
113         } catch(ConnectionException $e) {
114             $this->_conn->rollBack();
115         }
116     }
117
118     public function testSetNestedTransactionsThroughSavepointsNotSupportedThrowsException()
119     {
120         if ($this->_conn->getDatabasePlatform()->supportsSavepoints()) {
121             $this->markTestSkipped('This test requires the platform not to support savepoints.');
122         }
123
124         $this->setExpectedException('Doctrine\DBAL\ConnectionException', "Savepoints are not supported by this driver.");
125
126         $this->_conn->setNestTransactionsWithSavepoints(true);
127     }
128
129     public function testCreateSavepointsNotSupportedThrowsException()
130     {
131         if ($this->_conn->getDatabasePlatform()->supportsSavepoints()) {
132             $this->markTestSkipped('This test requires the platform not to support savepoints.');
133         }
134
135         $this->setExpectedException('Doctrine\DBAL\ConnectionException', "Savepoints are not supported by this driver.");
136
137         $this->_conn->createSavepoint('foo');
138     }
139
140     public function testReleaseSavepointsNotSupportedThrowsException()
141     {
142         if ($this->_conn->getDatabasePlatform()->supportsSavepoints()) {
143             $this->markTestSkipped('This test requires the platform not to support savepoints.');
144         }
145
146         $this->setExpectedException('Doctrine\DBAL\ConnectionException', "Savepoints are not supported by this driver.");
147
148         $this->_conn->releaseSavepoint('foo');
149     }
150
151     public function testRollbackSavepointsNotSupportedThrowsException()
152     {
153         if ($this->_conn->getDatabasePlatform()->supportsSavepoints()) {
154             $this->markTestSkipped('This test requires the platform not to support savepoints.');
155         }
156
157         $this->setExpectedException('Doctrine\DBAL\ConnectionException', "Savepoints are not supported by this driver.");
158
159         $this->_conn->rollbackSavepoint('foo');
160     }
161
162     public function testTransactionBehaviorWithRollback()
163     {
164         try {
165             $this->_conn->beginTransaction();
166             $this->assertEquals(1, $this->_conn->getTransactionNestingLevel());
167
168             throw new \Exception;
169
170             $this->_conn->commit(); // never reached
171         } catch (\Exception $e) {
172             $this->assertEquals(1, $this->_conn->getTransactionNestingLevel());
173             $this->_conn->rollback();
174             $this->assertEquals(0, $this->_conn->getTransactionNestingLevel());
175         }
176     }
177
178     public function testTransactionBehaviour()
179     {
180         try {
181             $this->_conn->beginTransaction();
182             $this->assertEquals(1, $this->_conn->getTransactionNestingLevel());
183             $this->_conn->commit();
184         } catch (\Exception $e) {
185             $this->_conn->rollback();
186             $this->assertEquals(0, $this->_conn->getTransactionNestingLevel());
187         }
188
189         $this->assertEquals(0, $this->_conn->getTransactionNestingLevel());
190     }
191
192     public function testTransactionalWithException()
193     {
194         try {
195             $this->_conn->transactional(function($conn) {
196                 $conn->executeQuery($conn->getDatabasePlatform()->getDummySelectSQL());
197                 throw new \RuntimeException("Ooops!");
198             });
199         } catch (\RuntimeException $expected) {
200             $this->assertEquals(0, $this->_conn->getTransactionNestingLevel());
201         }
202     }
203
204     public function testTransactional()
205     {
206         $this->_conn->transactional(function($conn) {
207             /* @var $conn Connection */
208             $conn->executeQuery($conn->getDatabasePlatform()->getDummySelectSQL());
209         });
210     }
211
212     /**
213      * Tests that the quote function accepts DBAL and PDO types.
214      */
215     public function testQuote()
216     {
217         $this->assertEquals($this->_conn->quote("foo", Type::STRING), $this->_conn->quote("foo", \PDO::PARAM_STR));
218     }
219 }