ConnectionTest.php 7.8KB

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