ConnectionTest.php 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. /**
  96. * Pretty dumb test, however we want to check that the EchoSQLLogger correctly implements the interface.
  97. *
  98. * @group DBAL-11
  99. */
  100. public function testEchoSQLLogger()
  101. {
  102. $logger = new \Doctrine\DBAL\Logging\EchoSQLLogger();
  103. $this->_conn->getConfiguration()->setSQLLogger($logger);
  104. $this->assertSame($logger, $this->_conn->getConfiguration()->getSQLLogger());
  105. }
  106. /**
  107. * Pretty dumb test, however we want to check that the DebugStack correctly implements the interface.
  108. *
  109. * @group DBAL-11
  110. */
  111. public function testDebugSQLStack()
  112. {
  113. $logger = new \Doctrine\DBAL\Logging\DebugStack();
  114. $this->_conn->getConfiguration()->setSQLLogger($logger);
  115. $this->assertSame($logger, $this->_conn->getConfiguration()->getSQLLogger());
  116. }
  117. }