3 namespace Doctrine\Tests\DBAL;
5 require_once __DIR__ . '/../TestInit.php';
7 use Doctrine\DBAL\Connection;
8 use Doctrine\Common\EventManager;
9 use Doctrine\DBAL\Configuration;
10 use Doctrine\DBAL\Events;
12 class ConnectionTest extends \Doctrine\Tests\DbalTestCase
15 * @var Doctrine\DBAL\Connection
17 protected $_conn = null;
19 public function setUp()
22 'driver' => 'pdo_mysql',
23 'host' => 'localhost',
25 'password' => 'password',
28 $this->_conn = \Doctrine\DBAL\DriverManager::getConnection($params);
31 public function testIsConnected()
33 $this->assertFalse($this->_conn->isConnected());
36 public function testNoTransactionActiveByDefault()
38 $this->assertFalse($this->_conn->isTransactionActive());
41 public function testCommitWithNoActiveTransaction_ThrowsException()
43 $this->setExpectedException('Doctrine\DBAL\ConnectionException');
44 $this->_conn->commit();
47 public function testRollbackWithNoActiveTransaction_ThrowsException()
49 $this->setExpectedException('Doctrine\DBAL\ConnectionException');
50 $this->_conn->rollback();
53 public function testSetRollbackOnlyNoActiveTransaction_ThrowsException()
55 $this->setExpectedException('Doctrine\DBAL\ConnectionException');
56 $this->_conn->setRollbackOnly();
59 public function testIsRollbackOnlyNoActiveTransaction_ThrowsException()
61 $this->setExpectedException('Doctrine\DBAL\ConnectionException');
62 $this->_conn->isRollbackOnly();
65 public function testGetConfiguration()
67 $config = $this->_conn->getConfiguration();
69 $this->assertInstanceOf('Doctrine\DBAL\Configuration', $config);
72 public function testGetHost()
74 $this->assertEquals('localhost', $this->_conn->getHost());
77 public function testGetPort()
79 $this->assertEquals('1234', $this->_conn->getPort());
82 public function testGetUsername()
84 $this->assertEquals('root', $this->_conn->getUsername());
87 public function testGetPassword()
89 $this->assertEquals('password', $this->_conn->getPassword());
92 public function testGetDriver()
94 $this->assertInstanceOf('Doctrine\DBAL\Driver\PDOMySql\Driver', $this->_conn->getDriver());
97 public function testGetEventManager()
99 $this->assertInstanceOf('Doctrine\Common\EventManager', $this->_conn->getEventManager());
102 public function testConnectDispatchEvent()
104 $listenerMock = $this->getMock('ConnectDispatchEventListener', array('postConnect'));
105 $listenerMock->expects($this->once())->method('postConnect');
107 $eventManager = new EventManager();
108 $eventManager->addEventListener(array(Events::postConnect), $listenerMock);
110 $driverMock = $this->getMock('Doctrine\DBAL\Driver');
111 $driverMock->expects(($this->at(0)))
113 $platform = new Mocks\MockPlatform();
115 $conn = new Connection(array('platform' => $platform), $driverMock, new Configuration(), $eventManager);
119 public function testEventManagerPassedToPlatform()
121 $this->assertInstanceOf('Doctrine\Common\EventManager', $this->_conn->getDatabasePlatform()->getEventManager());
122 $this->assertSame($this->_conn->getEventManager(), $this->_conn->getDatabasePlatform()->getEventManager());
126 * @expectedException Doctrine\DBAL\DBALException
127 * @dataProvider getQueryMethods
129 public function testDriverExceptionIsWrapped($method)
131 $this->setExpectedException('Doctrine\DBAL\DBALException', "An exception occurred while executing 'MUUHAAAAHAAAA':
133 SQLSTATE[HY000]: General error: 1 near \"MUUHAAAAHAAAA\"");
135 $con = \Doctrine\DBAL\DriverManager::getConnection(array(
136 'driver' => 'pdo_sqlite',
140 $con->$method('MUUHAAAAHAAAA');
143 public function getQueryMethods()
148 array('executeQuery'),
149 array('executeUpdate'),
155 * Pretty dumb test, however we want to check that the EchoSQLLogger correctly implements the interface.
159 public function testEchoSQLLogger()
161 $logger = new \Doctrine\DBAL\Logging\EchoSQLLogger();
162 $this->_conn->getConfiguration()->setSQLLogger($logger);
163 $this->assertSame($logger, $this->_conn->getConfiguration()->getSQLLogger());
167 * Pretty dumb test, however we want to check that the DebugStack correctly implements the interface.
171 public function testDebugSQLStack()
173 $logger = new \Doctrine\DBAL\Logging\DebugStack();
174 $this->_conn->getConfiguration()->setSQLLogger($logger);
175 $this->assertSame($logger, $this->_conn->getConfiguration()->getSQLLogger());