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