1 | <?php |
||
2 | |||
3 | namespace Doctrine\Tests\DBAL; |
||
4 | |||
5 | use Doctrine\DBAL\DBALException; |
||
6 | use Doctrine\Tests\Mocks\PDOMock; |
||
7 | |||
8 | class DriverManagerTest extends \Doctrine\Tests\DbalTestCase |
||
9 | { |
||
10 | /** |
||
11 | * @expectedException \Doctrine\DBAL\DBALException |
||
12 | */ |
||
13 | public function testInvalidPdoInstance() |
||
14 | { |
||
15 | $options = array( |
||
16 | 'pdo' => 'test' |
||
17 | ); |
||
18 | $test = \Doctrine\DBAL\DriverManager::getConnection($options); |
||
0 ignored issues
–
show
Unused Code
introduced
by
![]() |
|||
19 | } |
||
20 | |||
21 | public function testValidPdoInstance() |
||
22 | { |
||
23 | $options = array( |
||
24 | 'pdo' => new \PDO('sqlite::memory:') |
||
25 | ); |
||
26 | $conn = \Doctrine\DBAL\DriverManager::getConnection($options); |
||
27 | self::assertEquals('sqlite', $conn->getDatabasePlatform()->getName()); |
||
28 | } |
||
29 | |||
30 | /** |
||
31 | * @group DBAL-32 |
||
32 | */ |
||
33 | public function testPdoInstanceSetErrorMode() |
||
34 | { |
||
35 | $pdo = new \PDO('sqlite::memory:'); |
||
36 | $pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_SILENT); |
||
37 | $options = array( |
||
38 | 'pdo' => $pdo |
||
39 | ); |
||
40 | |||
41 | $conn = \Doctrine\DBAL\DriverManager::getConnection($options); |
||
42 | self::assertEquals(\PDO::ERRMODE_EXCEPTION, $pdo->getAttribute(\PDO::ATTR_ERRMODE)); |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * @expectedException \Doctrine\DBAL\DBALException |
||
47 | */ |
||
48 | public function testCheckParams() |
||
49 | { |
||
50 | $conn = \Doctrine\DBAL\DriverManager::getConnection(array()); |
||
0 ignored issues
–
show
|
|||
51 | } |
||
52 | |||
53 | /** |
||
54 | * @expectedException \Doctrine\DBAL\DBALException |
||
55 | */ |
||
56 | public function testInvalidDriver() |
||
57 | { |
||
58 | $conn = \Doctrine\DBAL\DriverManager::getConnection(array('driver' => 'invalid_driver')); |
||
0 ignored issues
–
show
|
|||
59 | } |
||
60 | |||
61 | public function testCustomPlatform() |
||
62 | { |
||
63 | $mockPlatform = new \Doctrine\Tests\DBAL\Mocks\MockPlatform(); |
||
64 | $options = array( |
||
65 | 'pdo' => new \PDO('sqlite::memory:'), |
||
66 | 'platform' => $mockPlatform |
||
67 | ); |
||
68 | |||
69 | $conn = \Doctrine\DBAL\DriverManager::getConnection($options); |
||
70 | self::assertSame($mockPlatform, $conn->getDatabasePlatform()); |
||
71 | } |
||
72 | |||
73 | public function testCustomWrapper() |
||
74 | { |
||
75 | $wrapperClass = 'Doctrine\Tests\Mocks\ConnectionMock'; |
||
76 | |||
77 | $options = array( |
||
78 | 'pdo' => new \PDO('sqlite::memory:'), |
||
79 | 'wrapperClass' => $wrapperClass, |
||
80 | ); |
||
81 | |||
82 | $conn = \Doctrine\DBAL\DriverManager::getConnection($options); |
||
83 | self::assertInstanceOf($wrapperClass, $conn); |
||
84 | } |
||
85 | |||
86 | public function testInvalidWrapperClass() |
||
87 | { |
||
88 | $this->expectException(DBALException::class); |
||
89 | |||
90 | $options = array( |
||
91 | 'pdo' => new \PDO('sqlite::memory:'), |
||
92 | 'wrapperClass' => 'stdClass', |
||
93 | ); |
||
94 | |||
95 | $conn = \Doctrine\DBAL\DriverManager::getConnection($options); |
||
96 | } |
||
97 | |||
98 | public function testInvalidDriverClass() |
||
99 | { |
||
100 | $this->expectException(DBALException::class); |
||
101 | |||
102 | $options = array( |
||
103 | 'driverClass' => 'stdClass' |
||
104 | ); |
||
105 | |||
106 | $conn = \Doctrine\DBAL\DriverManager::getConnection($options); |
||
107 | } |
||
108 | |||
109 | public function testValidDriverClass() |
||
110 | { |
||
111 | $options = array( |
||
112 | 'driverClass' => 'Doctrine\DBAL\Driver\PDOMySql\Driver', |
||
113 | ); |
||
114 | |||
115 | $conn = \Doctrine\DBAL\DriverManager::getConnection($options); |
||
116 | self::assertInstanceOf('Doctrine\DBAL\Driver\PDOMySql\Driver', $conn->getDriver()); |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * @dataProvider databaseUrls |
||
121 | */ |
||
122 | public function testDatabaseUrl($url, $expected) |
||
123 | { |
||
124 | $options = is_array($url) ? $url : array( |
||
125 | 'url' => $url, |
||
126 | ); |
||
127 | |||
128 | if ($expected === false) { |
||
129 | $this->expectException(DBALException::class); |
||
130 | } |
||
131 | |||
132 | $conn = \Doctrine\DBAL\DriverManager::getConnection($options); |
||
133 | |||
134 | $params = $conn->getParams(); |
||
135 | foreach ($expected as $key => $value) { |
||
136 | if (in_array($key, array('pdo', 'driver', 'driverClass'), true)) { |
||
137 | self::assertInstanceOf($value, $conn->getDriver()); |
||
138 | } else { |
||
139 | self::assertEquals($value, $params[$key]); |
||
140 | } |
||
141 | } |
||
142 | } |
||
143 | |||
144 | public function databaseUrls() |
||
145 | { |
||
146 | $pdoMock = $this->createMock(PDOMock::class); |
||
147 | |||
148 | return array( |
||
149 | 'simple URL' => array( |
||
150 | 'mysql://foo:bar@localhost/baz', |
||
151 | array('user' => 'foo', 'password' => 'bar', 'host' => 'localhost', 'dbname' => 'baz', 'driver' => 'Doctrine\DBAL\Driver\PDOMySQL\Driver'), |
||
152 | ), |
||
153 | 'simple URL with port' => array( |
||
154 | 'mysql://foo:bar@localhost:11211/baz', |
||
155 | array('user' => 'foo', 'password' => 'bar', 'host' => 'localhost', 'port' => 11211, 'dbname' => 'baz', 'driver' => 'Doctrine\DBAL\Driver\PDOMySQL\Driver'), |
||
156 | ), |
||
157 | 'sqlite relative URL with host' => array( |
||
158 | 'sqlite://localhost/foo/dbname.sqlite', |
||
159 | array('path' => 'foo/dbname.sqlite', 'driver' => 'Doctrine\DBAL\Driver\PDOSqlite\Driver'), |
||
160 | ), |
||
161 | 'sqlite absolute URL with host' => array( |
||
162 | 'sqlite://localhost//tmp/dbname.sqlite', |
||
163 | array('path' => '/tmp/dbname.sqlite', 'driver' => 'Doctrine\DBAL\Driver\PDOSqlite\Driver'), |
||
164 | ), |
||
165 | 'sqlite relative URL without host' => array( |
||
166 | 'sqlite:///foo/dbname.sqlite', |
||
167 | array('path' => 'foo/dbname.sqlite', 'driver' => 'Doctrine\DBAL\Driver\PDOSqlite\Driver'), |
||
168 | ), |
||
169 | 'sqlite absolute URL without host' => array( |
||
170 | 'sqlite:////tmp/dbname.sqlite', |
||
171 | array('path' => '/tmp/dbname.sqlite', 'driver' => 'Doctrine\DBAL\Driver\PDOSqlite\Driver'), |
||
172 | ), |
||
173 | 'sqlite memory' => array( |
||
174 | 'sqlite:///:memory:', |
||
175 | array('memory' => true, 'driver' => 'Doctrine\DBAL\Driver\PDOSqlite\Driver'), |
||
176 | ), |
||
177 | 'sqlite memory with host' => array( |
||
178 | 'sqlite://localhost/:memory:', |
||
179 | array('memory' => true, 'driver' => 'Doctrine\DBAL\Driver\PDOSqlite\Driver'), |
||
180 | ), |
||
181 | 'params parsed from URL override individual params' => array( |
||
182 | array('url' => 'mysql://foo:bar@localhost/baz', 'password' => 'lulz'), |
||
183 | array('user' => 'foo', 'password' => 'bar', 'host' => 'localhost', 'dbname' => 'baz', 'driver' => 'Doctrine\DBAL\Driver\PDOMySQL\Driver'), |
||
184 | ), |
||
185 | 'params not parsed from URL but individual params are preserved' => array( |
||
186 | array('url' => 'mysql://foo:bar@localhost/baz', 'port' => 1234), |
||
187 | array('user' => 'foo', 'password' => 'bar', 'host' => 'localhost', 'port' => 1234, 'dbname' => 'baz', 'driver' => 'Doctrine\DBAL\Driver\PDOMySQL\Driver'), |
||
188 | ), |
||
189 | 'query params from URL are used as extra params' => array( |
||
190 | 'url' => 'mysql://foo:bar@localhost/dbname?charset=UTF-8', |
||
191 | array('charset' => 'UTF-8'), |
||
192 | ), |
||
193 | 'simple URL with fallthrough scheme not defined in map' => array( |
||
194 | 'sqlsrv://foo:bar@localhost/baz', |
||
195 | array('user' => 'foo', 'password' => 'bar', 'host' => 'localhost', 'dbname' => 'baz', 'driver' => 'Doctrine\DBAL\Driver\SQLSrv\Driver'), |
||
196 | ), |
||
197 | 'simple URL with fallthrough scheme containing underscores fails' => array( |
||
198 | 'drizzle_pdo_mysql://foo:bar@localhost/baz', |
||
199 | false, |
||
200 | ), |
||
201 | 'simple URL with fallthrough scheme containing dashes works' => array( |
||
202 | 'drizzle-pdo-mysql://foo:bar@localhost/baz', |
||
203 | array('user' => 'foo', 'password' => 'bar', 'host' => 'localhost', 'dbname' => 'baz', 'driver' => 'Doctrine\DBAL\Driver\DrizzlePDOMySql\Driver'), |
||
204 | ), |
||
205 | 'simple URL with percent encoding' => array( |
||
206 | 'mysql://foo%3A:bar%2F@localhost/baz+baz%40', |
||
207 | array('user' => 'foo:', 'password' => 'bar/', 'host' => 'localhost', 'dbname' => 'baz+baz@', 'driver' => 'Doctrine\DBAL\Driver\PDOMySQL\Driver'), |
||
208 | ), |
||
209 | 'simple URL with percent sign in password' => array( |
||
210 | 'mysql://foo:bar%25bar@localhost/baz', |
||
211 | array('user' => 'foo', 'password' => 'bar%bar', 'host' => 'localhost', 'dbname' => 'baz', 'driver' => 'Doctrine\DBAL\Driver\PDOMySQL\Driver'), |
||
212 | ), |
||
213 | |||
214 | // DBAL-1234 |
||
215 | 'URL without scheme and without any driver information' => array( |
||
216 | array('url' => '//foo:bar@localhost/baz'), |
||
217 | false, |
||
218 | ), |
||
219 | 'URL without scheme but default PDO driver' => array( |
||
220 | array('url' => '//foo:bar@localhost/baz', 'pdo' => $pdoMock), |
||
221 | false, |
||
222 | ), |
||
223 | 'URL without scheme but default driver' => array( |
||
224 | array('url' => '//foo:bar@localhost/baz', 'driver' => 'pdo_mysql'), |
||
225 | array('user' => 'foo', 'password' => 'bar', 'host' => 'localhost', 'dbname' => 'baz', 'driver' => 'Doctrine\DBAL\Driver\PDOMySQL\Driver'), |
||
226 | ), |
||
227 | 'URL without scheme but custom driver' => array( |
||
228 | array('url' => '//foo:bar@localhost/baz', 'driverClass' => 'Doctrine\Tests\Mocks\DriverMock'), |
||
229 | array('user' => 'foo', 'password' => 'bar', 'host' => 'localhost', 'dbname' => 'baz', 'driverClass' => 'Doctrine\Tests\Mocks\DriverMock'), |
||
230 | ), |
||
231 | 'URL without scheme but default PDO driver and default driver' => array( |
||
232 | array('url' => '//foo:bar@localhost/baz', 'pdo' => $pdoMock, 'driver' => 'pdo_mysql'), |
||
233 | array('user' => 'foo', 'password' => 'bar', 'host' => 'localhost', 'dbname' => 'baz', 'driver' => 'Doctrine\DBAL\Driver\PDOMySQL\Driver'), |
||
234 | ), |
||
235 | 'URL without scheme but driver and custom driver' => array( |
||
236 | array('url' => '//foo:bar@localhost/baz', 'driver' => 'pdo_mysql', 'driverClass' => 'Doctrine\Tests\Mocks\DriverMock'), |
||
237 | array('user' => 'foo', 'password' => 'bar', 'host' => 'localhost', 'dbname' => 'baz', 'driverClass' => 'Doctrine\Tests\Mocks\DriverMock'), |
||
238 | ), |
||
239 | 'URL with default PDO driver' => array( |
||
240 | array('url' => 'mysql://foo:bar@localhost/baz', 'pdo' => $pdoMock), |
||
241 | array('user' => 'foo', 'password' => 'bar', 'host' => 'localhost', 'dbname' => 'baz', 'driver' => 'Doctrine\DBAL\Driver\PDOMySQL\Driver'), |
||
242 | ), |
||
243 | 'URL with default driver' => array( |
||
244 | array('url' => 'mysql://foo:bar@localhost/baz', 'driver' => 'sqlite'), |
||
245 | array('user' => 'foo', 'password' => 'bar', 'host' => 'localhost', 'dbname' => 'baz', 'driver' => 'Doctrine\DBAL\Driver\PDOMySQL\Driver'), |
||
246 | ), |
||
247 | 'URL with default custom driver' => array( |
||
248 | array('url' => 'mysql://foo:bar@localhost/baz', 'driverClass' => 'Doctrine\Tests\Mocks\DriverMock'), |
||
249 | array('user' => 'foo', 'password' => 'bar', 'host' => 'localhost', 'dbname' => 'baz', 'driver' => 'Doctrine\DBAL\Driver\PDOMySQL\Driver'), |
||
250 | ), |
||
251 | 'URL with default PDO driver and default driver' => array( |
||
252 | array('url' => 'mysql://foo:bar@localhost/baz', 'pdo' => $pdoMock, 'driver' => 'sqlite'), |
||
253 | array('user' => 'foo', 'password' => 'bar', 'host' => 'localhost', 'dbname' => 'baz', 'driver' => 'Doctrine\DBAL\Driver\PDOMySQL\Driver'), |
||
254 | ), |
||
255 | 'URL with default driver and default custom driver' => array( |
||
256 | array('url' => 'mysql://foo:bar@localhost/baz', 'driver' => 'sqlite', 'driverClass' => 'Doctrine\Tests\Mocks\DriverMock'), |
||
257 | array('user' => 'foo', 'password' => 'bar', 'host' => 'localhost', 'dbname' => 'baz', 'driver' => 'Doctrine\DBAL\Driver\PDOMySQL\Driver'), |
||
258 | ), |
||
259 | 'URL with default PDO driver and default driver and default custom driver' => array( |
||
260 | array('url' => 'mysql://foo:bar@localhost/baz', 'pdo' => $pdoMock, 'driver' => 'sqlite', 'driverClass' => 'Doctrine\Tests\Mocks\DriverMock'), |
||
261 | array('user' => 'foo', 'password' => 'bar', 'host' => 'localhost', 'dbname' => 'baz', 'driver' => 'Doctrine\DBAL\Driver\PDOMySQL\Driver'), |
||
262 | ), |
||
263 | ); |
||
264 | } |
||
265 | } |
||
266 |