3 namespace Doctrine\Tests\DBAL\Functional;
5 use Doctrine\DBAL\ConnectionException;
6 use Doctrine\DBAL\Types\Type;
8 require_once __DIR__ . '/../../TestInit.php';
10 class ConnectionTest extends \Doctrine\Tests\DbalFunctionalTestCase
12 public function setUp()
14 $this->resetSharedConn();
18 public function tearDown()
21 $this->resetSharedConn();
24 public function testGetWrappedConnection()
26 $this->assertInstanceOf('Doctrine\DBAL\Driver\Connection', $this->_conn->getWrappedConnection());
29 public function testCommitWithRollbackOnlyThrowsException()
31 $this->_conn->beginTransaction();
32 $this->_conn->setRollbackOnly();
33 $this->setExpectedException('Doctrine\DBAL\ConnectionException');
34 $this->_conn->commit();
37 public function testTransactionNestingBehavior()
40 $this->_conn->beginTransaction();
41 $this->assertEquals(1, $this->_conn->getTransactionNestingLevel());
44 $this->_conn->beginTransaction();
45 $this->assertEquals(2, $this->_conn->getTransactionNestingLevel());
47 $this->_conn->commit(); // never reached
48 } catch (\Exception $e) {
49 $this->_conn->rollback();
50 $this->assertEquals(1, $this->_conn->getTransactionNestingLevel());
53 $this->assertTrue($this->_conn->isRollbackOnly());
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());
64 public function testTransactionNestingBehaviorWithSavepoints()
66 if (!$this->_conn->getDatabasePlatform()->supportsSavepoints()) {
67 $this->markTestSkipped('This test requires the platform to support savepoints.');
70 $this->_conn->setNestTransactionsWithSavepoints(true);
72 $this->_conn->beginTransaction();
73 $this->assertEquals(1, $this->_conn->getTransactionNestingLevel());
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());
83 $this->_conn->commit(); // never reached
84 } catch (\Exception $e) {
85 $this->_conn->rollback();
86 $this->assertEquals(1, $this->_conn->getTransactionNestingLevel());
89 $this->assertFalse($this->_conn->isRollbackOnly());
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());
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();
103 public function testTransactionNestingBehaviorCantBeChangedInActiveTransaction()
105 if (!$this->_conn->getDatabasePlatform()->supportsSavepoints()) {
106 $this->markTestSkipped('This test requires the platform to support savepoints.');
109 $this->_conn->beginTransaction();
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();
118 public function testSetNestedTransactionsThroughSavepointsNotSupportedThrowsException()
120 if ($this->_conn->getDatabasePlatform()->supportsSavepoints()) {
121 $this->markTestSkipped('This test requires the platform not to support savepoints.');
124 $this->setExpectedException('Doctrine\DBAL\ConnectionException', "Savepoints are not supported by this driver.");
126 $this->_conn->setNestTransactionsWithSavepoints(true);
129 public function testCreateSavepointsNotSupportedThrowsException()
131 if ($this->_conn->getDatabasePlatform()->supportsSavepoints()) {
132 $this->markTestSkipped('This test requires the platform not to support savepoints.');
135 $this->setExpectedException('Doctrine\DBAL\ConnectionException', "Savepoints are not supported by this driver.");
137 $this->_conn->createSavepoint('foo');
140 public function testReleaseSavepointsNotSupportedThrowsException()
142 if ($this->_conn->getDatabasePlatform()->supportsSavepoints()) {
143 $this->markTestSkipped('This test requires the platform not to support savepoints.');
146 $this->setExpectedException('Doctrine\DBAL\ConnectionException', "Savepoints are not supported by this driver.");
148 $this->_conn->releaseSavepoint('foo');
151 public function testRollbackSavepointsNotSupportedThrowsException()
153 if ($this->_conn->getDatabasePlatform()->supportsSavepoints()) {
154 $this->markTestSkipped('This test requires the platform not to support savepoints.');
157 $this->setExpectedException('Doctrine\DBAL\ConnectionException', "Savepoints are not supported by this driver.");
159 $this->_conn->rollbackSavepoint('foo');
162 public function testTransactionBehaviorWithRollback()
165 $this->_conn->beginTransaction();
166 $this->assertEquals(1, $this->_conn->getTransactionNestingLevel());
168 throw new \Exception;
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());
178 public function testTransactionBehaviour()
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());
189 $this->assertEquals(0, $this->_conn->getTransactionNestingLevel());
192 public function testTransactionalWithException()
195 $this->_conn->transactional(function($conn) {
196 $conn->executeQuery($conn->getDatabasePlatform()->getDummySelectSQL());
197 throw new \RuntimeException("Ooops!");
199 } catch (\RuntimeException $expected) {
200 $this->assertEquals(0, $this->_conn->getTransactionNestingLevel());
204 public function testTransactional()
206 $this->_conn->transactional(function($conn) {
207 /* @var $conn Connection */
208 $conn->executeQuery($conn->getDatabasePlatform()->getDummySelectSQL());
213 * Tests that the quote function accepts DBAL and PDO types.
215 public function testQuote()
217 $this->assertEquals($this->_conn->quote("foo", Type::STRING), $this->_conn->quote("foo", \PDO::PARAM_STR));