ConnectionTest.php 8.0 KB

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