ConnectionTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace Doctrine\Tests\DBAL;
  3. require_once __DIR__ . '/../TestInit.php';
  4. use Doctrine\DBAL\Connection;
  5. use Doctrine\Common\EventManager;
  6. use Doctrine\DBAL\Configuration;
  7. use Doctrine\DBAL\Events;
  8. class ConnectionTest extends \Doctrine\Tests\DbalTestCase
  9. {
  10. /**
  11. * @var Doctrine\DBAL\Connection
  12. */
  13. protected $_conn = null;
  14. public function setUp()
  15. {
  16. $params = array(
  17. 'driver' => 'pdo_mysql',
  18. 'host' => 'localhost',
  19. 'user' => 'root',
  20. 'password' => 'password',
  21. 'port' => '1234'
  22. );
  23. $this->_conn = \Doctrine\DBAL\DriverManager::getConnection($params);
  24. }
  25. public function testIsConnected()
  26. {
  27. $this->assertFalse($this->_conn->isConnected());
  28. }
  29. public function testNoTransactionActiveByDefault()
  30. {
  31. $this->assertFalse($this->_conn->isTransactionActive());
  32. }
  33. public function testCommitWithNoActiveTransaction_ThrowsException()
  34. {
  35. $this->setExpectedException('Doctrine\DBAL\ConnectionException');
  36. $this->_conn->commit();
  37. }
  38. public function testRollbackWithNoActiveTransaction_ThrowsException()
  39. {
  40. $this->setExpectedException('Doctrine\DBAL\ConnectionException');
  41. $this->_conn->rollback();
  42. }
  43. public function testSetRollbackOnlyNoActiveTransaction_ThrowsException()
  44. {
  45. $this->setExpectedException('Doctrine\DBAL\ConnectionException');
  46. $this->_conn->setRollbackOnly();
  47. }
  48. public function testIsRollbackOnlyNoActiveTransaction_ThrowsException()
  49. {
  50. $this->setExpectedException('Doctrine\DBAL\ConnectionException');
  51. $this->_conn->isRollbackOnly();
  52. }
  53. public function testGetConfiguration()
  54. {
  55. $config = $this->_conn->getConfiguration();
  56. $this->assertInstanceOf('Doctrine\DBAL\Configuration', $config);
  57. }
  58. public function testGetHost()
  59. {
  60. $this->assertEquals('localhost', $this->_conn->getHost());
  61. }
  62. public function testGetPort()
  63. {
  64. $this->assertEquals('1234', $this->_conn->getPort());
  65. }
  66. public function testGetUsername()
  67. {
  68. $this->assertEquals('root', $this->_conn->getUsername());
  69. }
  70. public function testGetPassword()
  71. {
  72. $this->assertEquals('password', $this->_conn->getPassword());
  73. }
  74. public function testGetDriver()
  75. {
  76. $this->assertInstanceOf('Doctrine\DBAL\Driver\PDOMySql\Driver', $this->_conn->getDriver());
  77. }
  78. public function testGetEventManager()
  79. {
  80. $this->assertInstanceOf('Doctrine\Common\EventManager', $this->_conn->getEventManager());
  81. }
  82. public function testConnectDispatchEvent()
  83. {
  84. $listenerMock = $this->getMock('ConnectDispatchEventListener', array('postConnect'));
  85. $listenerMock->expects($this->once())->method('postConnect');
  86. $eventManager = new EventManager();
  87. $eventManager->addEventListener(array(Events::postConnect), $listenerMock);
  88. $driverMock = $this->getMock('Doctrine\DBAL\Driver');
  89. $driverMock->expects(($this->at(0)))
  90. ->method('connect');
  91. $platform = new Mocks\MockPlatform();
  92. $conn = new Connection(array('platform' => $platform), $driverMock, new Configuration(), $eventManager);
  93. $conn->connect();
  94. }
  95. public function testEventManagerPassedToPlatform()
  96. {
  97. $this->assertInstanceOf('Doctrine\Common\EventManager', $this->_conn->getDatabasePlatform()->getEventManager());
  98. $this->assertSame($this->_conn->getEventManager(), $this->_conn->getDatabasePlatform()->getEventManager());
  99. }
  100. /**
  101. * @expectedException Doctrine\DBAL\DBALException
  102. * @dataProvider getQueryMethods
  103. */
  104. public function testDriverExceptionIsWrapped($method)
  105. {
  106. $this->setExpectedException('Doctrine\DBAL\DBALException', "An exception occurred while executing 'MUUHAAAAHAAAA':
  107. SQLSTATE[HY000]: General error: 1 near \"MUUHAAAAHAAAA\"");
  108. $con = \Doctrine\DBAL\DriverManager::getConnection(array(
  109. 'driver' => 'pdo_sqlite',
  110. 'memory' => true,
  111. ));
  112. $con->$method('MUUHAAAAHAAAA');
  113. }
  114. public function getQueryMethods()
  115. {
  116. return array(
  117. array('exec'),
  118. array('query'),
  119. array('executeQuery'),
  120. array('executeUpdate'),
  121. array('prepare'),
  122. );
  123. }
  124. /**
  125. * Pretty dumb test, however we want to check that the EchoSQLLogger correctly implements the interface.
  126. *
  127. * @group DBAL-11
  128. */
  129. public function testEchoSQLLogger()
  130. {
  131. $logger = new \Doctrine\DBAL\Logging\EchoSQLLogger();
  132. $this->_conn->getConfiguration()->setSQLLogger($logger);
  133. $this->assertSame($logger, $this->_conn->getConfiguration()->getSQLLogger());
  134. }
  135. /**
  136. * Pretty dumb test, however we want to check that the DebugStack correctly implements the interface.
  137. *
  138. * @group DBAL-11
  139. */
  140. public function testDebugSQLStack()
  141. {
  142. $logger = new \Doctrine\DBAL\Logging\DebugStack();
  143. $this->_conn->getConfiguration()->setSQLLogger($logger);
  144. $this->assertSame($logger, $this->_conn->getConfiguration()->getSQLLogger());
  145. }
  146. }