1 | <?php |
||
2 | |||
3 | namespace Doctrine\Tests\DBAL; |
||
4 | |||
5 | use Doctrine\Common\Cache\Cache; |
||
6 | use Doctrine\Common\EventManager; |
||
7 | use Doctrine\DBAL\Cache\ArrayStatement; |
||
8 | use Doctrine\DBAL\Cache\QueryCacheProfile; |
||
9 | use Doctrine\DBAL\Configuration; |
||
10 | use Doctrine\DBAL\Connection; |
||
11 | use Doctrine\DBAL\ConnectionException; |
||
12 | use Doctrine\DBAL\DBALException; |
||
13 | use Doctrine\DBAL\Driver; |
||
14 | use Doctrine\DBAL\Events; |
||
15 | use Doctrine\DBAL\Exception\InvalidArgumentException; |
||
16 | use Doctrine\DBAL\Platforms\AbstractPlatform; |
||
17 | use Doctrine\Tests\Mocks\DriverConnectionMock; |
||
18 | use Doctrine\Tests\Mocks\DriverMock; |
||
19 | use Doctrine\Tests\Mocks\VersionAwarePlatformDriverMock; |
||
20 | |||
21 | class ConnectionTest extends \Doctrine\Tests\DbalTestCase |
||
22 | { |
||
23 | /** |
||
24 | * @var \Doctrine\DBAL\Connection |
||
25 | */ |
||
26 | protected $_conn = null; |
||
27 | |||
28 | protected $params = array( |
||
29 | 'driver' => 'pdo_mysql', |
||
30 | 'host' => 'localhost', |
||
31 | 'user' => 'root', |
||
32 | 'password' => 'password', |
||
33 | 'port' => '1234' |
||
34 | ); |
||
35 | |||
36 | protected function setUp() |
||
37 | { |
||
38 | $this->_conn = \Doctrine\DBAL\DriverManager::getConnection($this->params); |
||
39 | } |
||
40 | |||
41 | public function getExecuteUpdateMockConnection() |
||
42 | { |
||
43 | $driverMock = $this->createMock(\Doctrine\DBAL\Driver::class); |
||
44 | |||
45 | $driverMock->expects($this->any()) |
||
46 | ->method('connect') |
||
47 | ->will($this->returnValue(new DriverConnectionMock())); |
||
48 | |||
49 | $conn = $this->getMockBuilder(Connection::class) |
||
50 | ->setMethods(['executeUpdate']) |
||
51 | ->setConstructorArgs([['platform' => new Mocks\MockPlatform()], $driverMock]) |
||
52 | ->getMock(); |
||
53 | |||
54 | return $conn; |
||
55 | } |
||
56 | |||
57 | public function testIsConnected() |
||
58 | { |
||
59 | self::assertFalse($this->_conn->isConnected()); |
||
60 | } |
||
61 | |||
62 | public function testNoTransactionActiveByDefault() |
||
63 | { |
||
64 | self::assertFalse($this->_conn->isTransactionActive()); |
||
65 | } |
||
66 | |||
67 | public function testCommitWithNoActiveTransaction_ThrowsException() |
||
68 | { |
||
69 | $this->expectException(ConnectionException::class); |
||
70 | $this->_conn->commit(); |
||
71 | } |
||
72 | |||
73 | public function testRollbackWithNoActiveTransaction_ThrowsException() |
||
74 | { |
||
75 | $this->expectException(ConnectionException::class); |
||
76 | $this->_conn->rollBack(); |
||
77 | } |
||
78 | |||
79 | public function testSetRollbackOnlyNoActiveTransaction_ThrowsException() |
||
80 | { |
||
81 | $this->expectException(ConnectionException::class); |
||
82 | $this->_conn->setRollbackOnly(); |
||
83 | } |
||
84 | |||
85 | public function testIsRollbackOnlyNoActiveTransaction_ThrowsException() |
||
86 | { |
||
87 | $this->expectException(ConnectionException::class); |
||
88 | $this->_conn->isRollbackOnly(); |
||
89 | } |
||
90 | |||
91 | public function testGetConfiguration() |
||
92 | { |
||
93 | $config = $this->_conn->getConfiguration(); |
||
94 | |||
95 | self::assertInstanceOf('Doctrine\DBAL\Configuration', $config); |
||
96 | } |
||
97 | |||
98 | public function testGetHost() |
||
99 | { |
||
100 | self::assertEquals('localhost', $this->_conn->getHost()); |
||
101 | } |
||
102 | |||
103 | public function testGetPort() |
||
104 | { |
||
105 | self::assertEquals('1234', $this->_conn->getPort()); |
||
106 | } |
||
107 | |||
108 | public function testGetUsername() |
||
109 | { |
||
110 | self::assertEquals('root', $this->_conn->getUsername()); |
||
111 | } |
||
112 | |||
113 | public function testGetPassword() |
||
114 | { |
||
115 | self::assertEquals('password', $this->_conn->getPassword()); |
||
116 | } |
||
117 | |||
118 | public function testGetDriver() |
||
119 | { |
||
120 | self::assertInstanceOf('Doctrine\DBAL\Driver\PDOMySql\Driver', $this->_conn->getDriver()); |
||
121 | } |
||
122 | |||
123 | public function testGetEventManager() |
||
124 | { |
||
125 | self::assertInstanceOf('Doctrine\Common\EventManager', $this->_conn->getEventManager()); |
||
126 | } |
||
127 | |||
128 | public function testConnectDispatchEvent() |
||
129 | { |
||
130 | $listenerMock = $this->getMockBuilder('ConnectDispatchEventListener') |
||
131 | ->setMethods(array('postConnect')) |
||
132 | ->getMock(); |
||
133 | $listenerMock->expects($this->once())->method('postConnect'); |
||
134 | |||
135 | $eventManager = new EventManager(); |
||
136 | $eventManager->addEventListener(array(Events::postConnect), $listenerMock); |
||
137 | |||
138 | $driverMock = $this->createMock('Doctrine\DBAL\Driver'); |
||
139 | $driverMock->expects(($this->at(0))) |
||
140 | ->method('connect'); |
||
141 | $platform = new Mocks\MockPlatform(); |
||
142 | |||
143 | $conn = new Connection(array('platform' => $platform), $driverMock, new Configuration(), $eventManager); |
||
144 | $conn->connect(); |
||
145 | } |
||
146 | |||
147 | public function testEventManagerPassedToPlatform() |
||
148 | { |
||
149 | $driverMock = new DriverMock(); |
||
150 | $connection = new Connection($this->params, $driverMock); |
||
151 | self::assertInstanceOf('Doctrine\Common\EventManager', $connection->getDatabasePlatform()->getEventManager()); |
||
152 | self::assertSame($connection->getEventManager(), $connection->getDatabasePlatform()->getEventManager()); |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * @expectedException \Doctrine\DBAL\DBALException |
||
157 | * @dataProvider getQueryMethods |
||
158 | */ |
||
159 | public function testDriverExceptionIsWrapped($method) |
||
160 | { |
||
161 | $this->expectException(DBALException::class); |
||
162 | $this->expectExceptionMessage("An exception occurred while executing 'MUUHAAAAHAAAA':\n\nSQLSTATE[HY000]: General error: 1 near \"MUUHAAAAHAAAA\""); |
||
163 | |||
164 | $con = \Doctrine\DBAL\DriverManager::getConnection(array( |
||
165 | 'driver' => 'pdo_sqlite', |
||
166 | 'memory' => true, |
||
167 | )); |
||
168 | |||
169 | $con->$method('MUUHAAAAHAAAA'); |
||
170 | } |
||
171 | |||
172 | public function getQueryMethods() |
||
173 | { |
||
174 | return array( |
||
175 | array('exec'), |
||
176 | array('query'), |
||
177 | array('executeQuery'), |
||
178 | array('executeUpdate'), |
||
179 | array('prepare'), |
||
180 | ); |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * Pretty dumb test, however we want to check that the EchoSQLLogger correctly implements the interface. |
||
185 | * |
||
186 | * @group DBAL-11 |
||
187 | */ |
||
188 | public function testEchoSQLLogger() |
||
189 | { |
||
190 | $logger = new \Doctrine\DBAL\Logging\EchoSQLLogger(); |
||
191 | $this->_conn->getConfiguration()->setSQLLogger($logger); |
||
192 | self::assertSame($logger, $this->_conn->getConfiguration()->getSQLLogger()); |
||
193 | } |
||
194 | |||
195 | /** |
||
196 | * Pretty dumb test, however we want to check that the DebugStack correctly implements the interface. |
||
197 | * |
||
198 | * @group DBAL-11 |
||
199 | */ |
||
200 | public function testDebugSQLStack() |
||
201 | { |
||
202 | $logger = new \Doctrine\DBAL\Logging\DebugStack(); |
||
203 | $this->_conn->getConfiguration()->setSQLLogger($logger); |
||
204 | self::assertSame($logger, $this->_conn->getConfiguration()->getSQLLogger()); |
||
205 | } |
||
206 | |||
207 | /** |
||
208 | * @group DBAL-81 |
||
209 | */ |
||
210 | public function testIsAutoCommit() |
||
211 | { |
||
212 | self::assertTrue($this->_conn->isAutoCommit()); |
||
213 | } |
||
214 | |||
215 | /** |
||
216 | * @group DBAL-81 |
||
217 | */ |
||
218 | public function testSetAutoCommit() |
||
219 | { |
||
220 | $this->_conn->setAutoCommit(false); |
||
221 | self::assertFalse($this->_conn->isAutoCommit()); |
||
222 | $this->_conn->setAutoCommit(0); |
||
223 | self::assertFalse($this->_conn->isAutoCommit()); |
||
224 | } |
||
225 | |||
226 | /** |
||
227 | * @group DBAL-81 |
||
228 | */ |
||
229 | View Code Duplication | public function testConnectStartsTransactionInNoAutoCommitMode() |
|
230 | { |
||
231 | $driverMock = $this->createMock('Doctrine\DBAL\Driver'); |
||
232 | $driverMock->expects($this->any()) |
||
233 | ->method('connect') |
||
234 | ->will($this->returnValue(new DriverConnectionMock())); |
||
235 | $conn = new Connection(array('platform' => new Mocks\MockPlatform()), $driverMock); |
||
236 | |||
237 | $conn->setAutoCommit(false); |
||
238 | |||
239 | self::assertFalse($conn->isTransactionActive()); |
||
240 | |||
241 | $conn->connect(); |
||
242 | |||
243 | self::assertTrue($conn->isTransactionActive()); |
||
244 | } |
||
245 | |||
246 | /** |
||
247 | * @group DBAL-81 |
||
248 | */ |
||
249 | View Code Duplication | public function testCommitStartsTransactionInNoAutoCommitMode() |
|
250 | { |
||
251 | $driverMock = $this->createMock('Doctrine\DBAL\Driver'); |
||
252 | $driverMock->expects($this->any()) |
||
253 | ->method('connect') |
||
254 | ->will($this->returnValue(new DriverConnectionMock())); |
||
255 | $conn = new Connection(array('platform' => new Mocks\MockPlatform()), $driverMock); |
||
256 | |||
257 | $conn->setAutoCommit(false); |
||
258 | $conn->connect(); |
||
259 | $conn->commit(); |
||
260 | |||
261 | self::assertTrue($conn->isTransactionActive()); |
||
262 | } |
||
263 | |||
264 | /** |
||
265 | * @group DBAL-81 |
||
266 | */ |
||
267 | View Code Duplication | public function testRollBackStartsTransactionInNoAutoCommitMode() |
|
268 | { |
||
269 | $driverMock = $this->createMock('Doctrine\DBAL\Driver'); |
||
270 | $driverMock->expects($this->any()) |
||
271 | ->method('connect') |
||
272 | ->will($this->returnValue(new DriverConnectionMock())); |
||
273 | $conn = new Connection(array('platform' => new Mocks\MockPlatform()), $driverMock); |
||
274 | |||
275 | $conn->setAutoCommit(false); |
||
276 | $conn->connect(); |
||
277 | $conn->rollBack(); |
||
278 | |||
279 | self::assertTrue($conn->isTransactionActive()); |
||
280 | } |
||
281 | |||
282 | /** |
||
283 | * @group DBAL-81 |
||
284 | */ |
||
285 | public function testSwitchingAutoCommitModeCommitsAllCurrentTransactions() |
||
286 | { |
||
287 | $driverMock = $this->createMock('Doctrine\DBAL\Driver'); |
||
288 | $driverMock->expects($this->any()) |
||
289 | ->method('connect') |
||
290 | ->will($this->returnValue(new DriverConnectionMock())); |
||
291 | $conn = new Connection(array('platform' => new Mocks\MockPlatform()), $driverMock); |
||
292 | |||
293 | $conn->connect(); |
||
294 | $conn->beginTransaction(); |
||
295 | $conn->beginTransaction(); |
||
296 | $conn->setAutoCommit(false); |
||
297 | |||
298 | self::assertSame(1, $conn->getTransactionNestingLevel()); |
||
299 | |||
300 | $conn->beginTransaction(); |
||
301 | $conn->beginTransaction(); |
||
302 | $conn->setAutoCommit(true); |
||
303 | |||
304 | self::assertFalse($conn->isTransactionActive()); |
||
305 | } |
||
306 | |||
307 | public function testEmptyInsert() |
||
308 | { |
||
309 | $conn = $this->getExecuteUpdateMockConnection(); |
||
310 | |||
311 | $conn->expects($this->once()) |
||
312 | ->method('executeUpdate') |
||
313 | ->with('INSERT INTO footable () VALUES ()'); |
||
314 | |||
315 | $conn->insert('footable', array()); |
||
316 | } |
||
317 | |||
318 | /** |
||
319 | * @group DBAL-2511 |
||
320 | */ |
||
321 | public function testUpdateWithDifferentColumnsInDataAndIdentifiers() |
||
322 | { |
||
323 | $conn = $this->getExecuteUpdateMockConnection(); |
||
324 | |||
325 | $conn->expects($this->once()) |
||
326 | ->method('executeUpdate') |
||
327 | ->with( |
||
328 | 'UPDATE TestTable SET text = ?, is_edited = ? WHERE id = ? AND name = ?', |
||
329 | [ |
||
330 | 'some text', |
||
331 | true, |
||
332 | 1, |
||
333 | 'foo', |
||
334 | ], |
||
335 | [ |
||
336 | 'string', |
||
337 | 'boolean', |
||
338 | 'integer', |
||
339 | 'string', |
||
340 | ] |
||
341 | ); |
||
342 | |||
343 | $conn->update( |
||
344 | 'TestTable', |
||
345 | [ |
||
346 | 'text' => 'some text', |
||
347 | 'is_edited' => true, |
||
348 | ], |
||
349 | [ |
||
350 | 'id' => 1, |
||
351 | 'name' => 'foo', |
||
352 | ], |
||
353 | [ |
||
354 | 'text' => 'string', |
||
355 | 'is_edited' => 'boolean', |
||
356 | 'id' => 'integer', |
||
357 | 'name' => 'string', |
||
358 | ] |
||
359 | ); |
||
360 | } |
||
361 | |||
362 | /** |
||
363 | * @group DBAL-2511 |
||
364 | */ |
||
365 | View Code Duplication | public function testUpdateWithSameColumnInDataAndIdentifiers() |
|
366 | { |
||
367 | $conn = $this->getExecuteUpdateMockConnection(); |
||
368 | |||
369 | $conn->expects($this->once()) |
||
370 | ->method('executeUpdate') |
||
371 | ->with( |
||
372 | 'UPDATE TestTable SET text = ?, is_edited = ? WHERE id = ? AND is_edited = ?', |
||
373 | [ |
||
374 | 'some text', |
||
375 | true, |
||
376 | 1, |
||
377 | false, |
||
378 | ], |
||
379 | [ |
||
380 | 'string', |
||
381 | 'boolean', |
||
382 | 'integer', |
||
383 | 'boolean', |
||
384 | ] |
||
385 | ); |
||
386 | |||
387 | $conn->update( |
||
388 | 'TestTable', |
||
389 | [ |
||
390 | 'text' => 'some text', |
||
391 | 'is_edited' => true, |
||
392 | ], |
||
393 | [ |
||
394 | 'id' => 1, |
||
395 | 'is_edited' => false, |
||
396 | ], |
||
397 | [ |
||
398 | 'text' => 'string', |
||
399 | 'is_edited' => 'boolean', |
||
400 | 'id' => 'integer', |
||
401 | ] |
||
402 | ); |
||
403 | } |
||
404 | |||
405 | /** |
||
406 | * @group DBAL-2688 |
||
407 | */ |
||
408 | View Code Duplication | public function testUpdateWithIsNull() |
|
409 | { |
||
410 | $conn = $this->getExecuteUpdateMockConnection(); |
||
411 | |||
412 | $conn->expects($this->once()) |
||
413 | ->method('executeUpdate') |
||
414 | ->with( |
||
415 | 'UPDATE TestTable SET text = ?, is_edited = ? WHERE id IS NULL AND name = ?', |
||
416 | [ |
||
417 | 'some text', |
||
418 | null, |
||
419 | 'foo', |
||
420 | ], |
||
421 | [ |
||
422 | 'string', |
||
423 | 'boolean', |
||
424 | 'string', |
||
425 | ] |
||
426 | ); |
||
427 | |||
428 | $conn->update( |
||
429 | 'TestTable', |
||
430 | [ |
||
431 | 'text' => 'some text', |
||
432 | 'is_edited' => null, |
||
433 | ], |
||
434 | [ |
||
435 | 'id' => null, |
||
436 | 'name' => 'foo', |
||
437 | ], |
||
438 | [ |
||
439 | 'text' => 'string', |
||
440 | 'is_edited' => 'boolean', |
||
441 | 'id' => 'integer', |
||
442 | 'name' => 'string', |
||
443 | ] |
||
444 | ); |
||
445 | } |
||
446 | |||
447 | /** |
||
448 | * @group DBAL-2688 |
||
449 | */ |
||
450 | public function testDeleteWithIsNull() |
||
451 | { |
||
452 | $conn = $this->getExecuteUpdateMockConnection(); |
||
453 | |||
454 | $conn->expects($this->once()) |
||
455 | ->method('executeUpdate') |
||
456 | ->with( |
||
457 | 'DELETE FROM TestTable WHERE id IS NULL AND name = ?', |
||
458 | [ |
||
459 | 'foo', |
||
460 | ], |
||
461 | [ |
||
462 | 'string', |
||
463 | ] |
||
464 | ); |
||
465 | |||
466 | $conn->delete( |
||
467 | 'TestTable', |
||
468 | [ |
||
469 | 'id' => null, |
||
470 | 'name' => 'foo', |
||
471 | ], |
||
472 | [ |
||
473 | 'id' => 'integer', |
||
474 | 'name' => 'string', |
||
475 | ] |
||
476 | ); |
||
477 | } |
||
478 | |||
479 | View Code Duplication | public function testFetchAssoc() |
|
480 | { |
||
481 | $statement = 'SELECT * FROM foo WHERE bar = ?'; |
||
482 | $params = array(666); |
||
483 | $types = array(\PDO::PARAM_INT); |
||
484 | $result = array(); |
||
485 | |||
486 | $driverMock = $this->createMock('Doctrine\DBAL\Driver'); |
||
487 | |||
488 | $driverMock->expects($this->any()) |
||
489 | ->method('connect') |
||
490 | ->will($this->returnValue(new DriverConnectionMock())); |
||
491 | |||
492 | $driverStatementMock = $this->createMock('Doctrine\Tests\Mocks\DriverStatementMock'); |
||
493 | |||
494 | $driverStatementMock->expects($this->once()) |
||
495 | ->method('fetch') |
||
496 | ->with(\PDO::FETCH_ASSOC) |
||
497 | ->will($this->returnValue($result)); |
||
498 | |||
499 | /** @var \PHPUnit_Framework_MockObject_MockObject|\Doctrine\DBAL\Connection $conn */ |
||
500 | $conn = $this->getMockBuilder('Doctrine\DBAL\Connection') |
||
501 | ->setMethods(array('executeQuery')) |
||
502 | ->setConstructorArgs(array(array('platform' => new Mocks\MockPlatform()), $driverMock)) |
||
503 | ->getMock(); |
||
504 | |||
505 | $conn->expects($this->once()) |
||
506 | ->method('executeQuery') |
||
507 | ->with($statement, $params, $types) |
||
508 | ->will($this->returnValue($driverStatementMock)); |
||
509 | |||
510 | self::assertSame($result, $conn->fetchAssoc($statement, $params, $types)); |
||
511 | } |
||
512 | |||
513 | View Code Duplication | public function testFetchArray() |
|
514 | { |
||
515 | $statement = 'SELECT * FROM foo WHERE bar = ?'; |
||
516 | $params = array(666); |
||
517 | $types = array(\PDO::PARAM_INT); |
||
518 | $result = array(); |
||
519 | |||
520 | $driverMock = $this->createMock('Doctrine\DBAL\Driver'); |
||
521 | |||
522 | $driverMock->expects($this->any()) |
||
523 | ->method('connect') |
||
524 | ->will($this->returnValue(new DriverConnectionMock())); |
||
525 | |||
526 | $driverStatementMock = $this->createMock('Doctrine\Tests\Mocks\DriverStatementMock'); |
||
527 | |||
528 | $driverStatementMock->expects($this->once()) |
||
529 | ->method('fetch') |
||
530 | ->with(\PDO::FETCH_NUM) |
||
531 | ->will($this->returnValue($result)); |
||
532 | |||
533 | /** @var \PHPUnit_Framework_MockObject_MockObject|\Doctrine\DBAL\Connection $conn */ |
||
534 | $conn = $this->getMockBuilder('Doctrine\DBAL\Connection') |
||
535 | ->setMethods(array('executeQuery')) |
||
536 | ->setConstructorArgs(array(array('platform' => new Mocks\MockPlatform()), $driverMock)) |
||
537 | ->getMock(); |
||
538 | |||
539 | $conn->expects($this->once()) |
||
540 | ->method('executeQuery') |
||
541 | ->with($statement, $params, $types) |
||
542 | ->will($this->returnValue($driverStatementMock)); |
||
543 | |||
544 | self::assertSame($result, $conn->fetchArray($statement, $params, $types)); |
||
545 | } |
||
546 | |||
547 | public function testFetchColumn() |
||
548 | { |
||
549 | $statement = 'SELECT * FROM foo WHERE bar = ?'; |
||
550 | $params = array(666); |
||
551 | $types = array(\PDO::PARAM_INT); |
||
552 | $column = 0; |
||
553 | $result = array(); |
||
554 | |||
555 | $driverMock = $this->createMock('Doctrine\DBAL\Driver'); |
||
556 | |||
557 | $driverMock->expects($this->any()) |
||
558 | ->method('connect') |
||
559 | ->will($this->returnValue(new DriverConnectionMock())); |
||
560 | |||
561 | $driverStatementMock = $this->createMock('Doctrine\Tests\Mocks\DriverStatementMock'); |
||
562 | |||
563 | $driverStatementMock->expects($this->once()) |
||
564 | ->method('fetchColumn') |
||
565 | ->with($column) |
||
566 | ->will($this->returnValue($result)); |
||
567 | |||
568 | /** @var \PHPUnit_Framework_MockObject_MockObject|\Doctrine\DBAL\Connection $conn */ |
||
569 | $conn = $this->getMockBuilder('Doctrine\DBAL\Connection') |
||
570 | ->setMethods(array('executeQuery')) |
||
571 | ->setConstructorArgs(array(array('platform' => new Mocks\MockPlatform()), $driverMock)) |
||
572 | ->getMock(); |
||
573 | |||
574 | $conn->expects($this->once()) |
||
575 | ->method('executeQuery') |
||
576 | ->with($statement, $params, $types) |
||
577 | ->will($this->returnValue($driverStatementMock)); |
||
578 | |||
579 | self::assertSame($result, $conn->fetchColumn($statement, $params, $column, $types)); |
||
580 | } |
||
581 | |||
582 | public function testConnectionIsClosedButNotUnset() |
||
583 | { |
||
584 | // mock Connection, and make connect() purposefully do nothing |
||
585 | $connection = $this->getMockBuilder('Doctrine\DBAL\Connection') |
||
586 | ->disableOriginalConstructor() |
||
587 | ->setMethods(array('connect')) |
||
588 | ->getMock(); |
||
589 | |||
590 | // artificially set the wrapped connection to non-null |
||
591 | $reflection = new \ReflectionObject($connection); |
||
592 | $connProperty = $reflection->getProperty('_conn'); |
||
593 | $connProperty->setAccessible(true); |
||
594 | $connProperty->setValue($connection, new \stdClass); |
||
595 | |||
596 | // close the connection (should nullify the wrapped connection) |
||
597 | $connection->close(); |
||
598 | |||
599 | // the wrapped connection should be null |
||
600 | // (and since connect() does nothing, this will not reconnect) |
||
601 | // this will also fail if this _conn property was unset instead of set to null |
||
602 | self::assertNull($connection->getWrappedConnection()); |
||
603 | } |
||
604 | |||
605 | View Code Duplication | public function testFetchAll() |
|
606 | { |
||
607 | $statement = 'SELECT * FROM foo WHERE bar = ?'; |
||
608 | $params = array(666); |
||
609 | $types = array(\PDO::PARAM_INT); |
||
610 | $result = array(); |
||
611 | |||
612 | $driverMock = $this->createMock('Doctrine\DBAL\Driver'); |
||
613 | |||
614 | $driverMock->expects($this->any()) |
||
615 | ->method('connect') |
||
616 | ->will($this->returnValue(new DriverConnectionMock())); |
||
617 | |||
618 | $driverStatementMock = $this->createMock('Doctrine\Tests\Mocks\DriverStatementMock'); |
||
619 | |||
620 | $driverStatementMock->expects($this->once()) |
||
621 | ->method('fetchAll') |
||
622 | ->will($this->returnValue($result)); |
||
623 | |||
624 | /** @var \PHPUnit_Framework_MockObject_MockObject|\Doctrine\DBAL\Connection $conn */ |
||
625 | $conn = $this->getMockBuilder('Doctrine\DBAL\Connection') |
||
626 | ->setMethods(array('executeQuery')) |
||
627 | ->setConstructorArgs(array(array('platform' => new Mocks\MockPlatform()), $driverMock)) |
||
628 | ->getMock(); |
||
629 | |||
630 | $conn->expects($this->once()) |
||
631 | ->method('executeQuery') |
||
632 | ->with($statement, $params, $types) |
||
633 | ->will($this->returnValue($driverStatementMock)); |
||
634 | |||
635 | self::assertSame($result, $conn->fetchAll($statement, $params, $types)); |
||
636 | } |
||
637 | |||
638 | View Code Duplication | public function testConnectionDoesNotMaintainTwoReferencesToExternalPDO() |
|
639 | { |
||
640 | $params['pdo'] = new \stdClass(); |
||
641 | |||
642 | $driverMock = $this->createMock('Doctrine\DBAL\Driver'); |
||
643 | |||
644 | $conn = new Connection($params, $driverMock); |
||
645 | |||
646 | self::assertArrayNotHasKey('pdo', $conn->getParams(), "Connection is maintaining additional reference to the PDO connection"); |
||
647 | } |
||
648 | |||
649 | View Code Duplication | public function testPassingExternalPDOMeansConnectionIsConnected() |
|
650 | { |
||
651 | $params['pdo'] = new \stdClass(); |
||
652 | |||
653 | $driverMock = $this->createMock('Doctrine\DBAL\Driver'); |
||
654 | |||
655 | $conn = new Connection($params, $driverMock); |
||
656 | |||
657 | self::assertTrue($conn->isConnected(), "Connection is not connected after passing external PDO"); |
||
658 | } |
||
659 | |||
660 | public function testCallingDeleteWithNoDeletionCriteriaResultsInInvalidArgumentException() |
||
661 | { |
||
662 | /* @var $driver \Doctrine\DBAL\Driver */ |
||
663 | $driver = $this->createMock('Doctrine\DBAL\Driver'); |
||
664 | $pdoMock = $this->createMock('Doctrine\DBAL\Driver\Connection'); |
||
665 | |||
666 | // should never execute queries with invalid arguments |
||
667 | $pdoMock->expects($this->never())->method('exec'); |
||
668 | $pdoMock->expects($this->never())->method('prepare'); |
||
669 | |||
670 | $conn = new Connection(array('pdo' => $pdoMock), $driver); |
||
671 | |||
672 | $this->expectException(InvalidArgumentException::class); |
||
673 | $conn->delete('kittens', array()); |
||
674 | } |
||
675 | |||
676 | public function dataCallConnectOnce() |
||
677 | { |
||
678 | return array( |
||
679 | array('delete', array('tbl', array('id' => 12345))), |
||
680 | array('insert', array('tbl', array('data' => 'foo'))), |
||
681 | array('update', array('tbl', array('data' => 'bar'), array('id' => 12345))), |
||
682 | array('prepare', array('select * from dual')), |
||
683 | array('executeUpdate', array('insert into tbl (id) values (?)'), array(123)), |
||
684 | ); |
||
685 | } |
||
686 | |||
687 | /** |
||
688 | * @dataProvider dataCallConnectOnce |
||
689 | */ |
||
690 | public function testCallConnectOnce($method, $params) |
||
691 | { |
||
692 | $driverMock = $this->createMock('Doctrine\DBAL\Driver'); |
||
693 | $pdoMock = $this->createMock('Doctrine\DBAL\Driver\Connection'); |
||
694 | $platformMock = new Mocks\MockPlatform(); |
||
695 | $stmtMock = $this->createMock('Doctrine\DBAL\Driver\Statement'); |
||
696 | |||
697 | $pdoMock->expects($this->any()) |
||
698 | ->method('prepare') |
||
699 | ->will($this->returnValue($stmtMock)); |
||
700 | |||
701 | $conn = $this->getMockBuilder('Doctrine\DBAL\Connection') |
||
702 | ->setConstructorArgs(array(array('pdo' => $pdoMock, 'platform' => $platformMock), $driverMock)) |
||
703 | ->setMethods(array('connect')) |
||
704 | ->getMock(); |
||
705 | |||
706 | $conn->expects($this->once())->method('connect'); |
||
707 | |||
708 | call_user_func_array(array($conn, $method), $params); |
||
709 | } |
||
710 | |||
711 | /** |
||
712 | * @group DBAL-1127 |
||
713 | */ |
||
714 | public function testPlatformDetectionIsTriggerOnlyOnceOnRetrievingPlatform() |
||
715 | { |
||
716 | /** @var \Doctrine\Tests\Mocks\VersionAwarePlatformDriverMock|\PHPUnit_Framework_MockObject_MockObject $driverMock */ |
||
717 | $driverMock = $this->createMock('Doctrine\Tests\Mocks\VersionAwarePlatformDriverMock'); |
||
718 | |||
719 | /** @var \Doctrine\Tests\Mocks\ServerInfoAwareConnectionMock|\PHPUnit_Framework_MockObject_MockObject $driverConnectionMock */ |
||
720 | $driverConnectionMock = $this->createMock('Doctrine\Tests\Mocks\ServerInfoAwareConnectionMock'); |
||
721 | |||
722 | /** @var \Doctrine\DBAL\Platforms\AbstractPlatform|\PHPUnit_Framework_MockObject_MockObject $platformMock */ |
||
723 | $platformMock = $this->getMockForAbstractClass('Doctrine\DBAL\Platforms\AbstractPlatform'); |
||
724 | |||
725 | $connection = new Connection(array(), $driverMock); |
||
726 | |||
727 | $driverMock->expects($this->once()) |
||
728 | ->method('connect') |
||
729 | ->will($this->returnValue($driverConnectionMock)); |
||
730 | |||
731 | $driverConnectionMock->expects($this->once()) |
||
732 | ->method('requiresQueryForServerVersion') |
||
733 | ->will($this->returnValue(false)); |
||
734 | |||
735 | $driverConnectionMock->expects($this->once()) |
||
736 | ->method('getServerVersion') |
||
737 | ->will($this->returnValue('6.6.6')); |
||
738 | |||
739 | $driverMock->expects($this->once()) |
||
740 | ->method('createDatabasePlatformForVersion') |
||
741 | ->with('6.6.6') |
||
742 | ->will($this->returnValue($platformMock)); |
||
743 | |||
744 | self::assertSame($platformMock, $connection->getDatabasePlatform()); |
||
745 | } |
||
746 | |||
747 | public function testConnectionParamsArePassedToTheQueryCacheProfileInExecuteCacheQuery() |
||
748 | { |
||
749 | $resultCacheDriverMock = $this->createMock(Cache::class); |
||
750 | |||
751 | $resultCacheDriverMock |
||
752 | ->expects($this->atLeastOnce()) |
||
753 | ->method('fetch') |
||
754 | ->with('cacheKey') |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
755 | ->will($this->returnValue(['realKey' => []])); |
||
756 | |||
757 | $query = 'SELECT * FROM foo WHERE bar = ?'; |
||
758 | $params = [666]; |
||
759 | $types = [\PDO::PARAM_INT]; |
||
760 | |||
761 | /* @var $queryCacheProfileMock QueryCacheProfile|\PHPUnit_Framework_MockObject_MockObject */ |
||
762 | $queryCacheProfileMock = $this->createMock(QueryCacheProfile::class); |
||
763 | |||
764 | $queryCacheProfileMock |
||
765 | ->expects($this->any()) |
||
766 | ->method('getResultCacheDriver') |
||
767 | ->will($this->returnValue($resultCacheDriverMock)); |
||
768 | |||
769 | // This is our main expectation |
||
770 | $queryCacheProfileMock |
||
771 | ->expects($this->once()) |
||
772 | ->method('generateCacheKeys') |
||
773 | ->with($query, $params, $types, $this->params) |
||
774 | ->will($this->returnValue(['cacheKey', 'realKey'])); |
||
775 | |||
776 | /* @var $driver Driver */ |
||
777 | $driver = $this->createMock(Driver::class); |
||
778 | |||
779 | self::assertInstanceOf( |
||
780 | ArrayStatement::class, |
||
781 | (new Connection($this->params, $driver))->executeCacheQuery($query, $params, $types, $queryCacheProfileMock) |
||
782 | ); |
||
783 | } |
||
784 | |||
785 | /** |
||
786 | * @group #2821 |
||
787 | */ |
||
788 | public function testShouldNotPassPlatformInParamsToTheQueryCacheProfileInExecuteCacheQuery(): void |
||
789 | { |
||
790 | $resultCacheDriverMock = $this->createMock(Cache::class); |
||
791 | |||
792 | $resultCacheDriverMock |
||
793 | ->expects($this->atLeastOnce()) |
||
794 | ->method('fetch') |
||
795 | ->with('cacheKey') |
||
796 | ->will($this->returnValue(['realKey' => []])); |
||
797 | |||
798 | /* @var $queryCacheProfileMock QueryCacheProfile|\PHPUnit_Framework_MockObject_MockObject */ |
||
799 | $queryCacheProfileMock = $this->createMock(QueryCacheProfile::class); |
||
800 | |||
801 | $queryCacheProfileMock |
||
802 | ->expects($this->any()) |
||
803 | ->method('getResultCacheDriver') |
||
804 | ->will($this->returnValue($resultCacheDriverMock)); |
||
805 | |||
806 | $query = 'SELECT 1'; |
||
807 | |||
808 | $connectionParams = $this->params; |
||
809 | |||
810 | $queryCacheProfileMock |
||
811 | ->expects($this->once()) |
||
812 | ->method('generateCacheKeys') |
||
813 | ->with($query, [], [], $connectionParams) |
||
814 | ->will($this->returnValue(['cacheKey', 'realKey'])); |
||
815 | |||
816 | $connectionParams['platform'] = $this->createMock(AbstractPlatform::class); |
||
817 | |||
818 | /* @var $driver Driver */ |
||
819 | $driver = $this->createMock(Driver::class); |
||
820 | |||
821 | (new Connection($connectionParams, $driver))->executeCacheQuery($query, [], [], $queryCacheProfileMock); |
||
822 | } |
||
823 | |||
824 | /** |
||
825 | * @group #2821 |
||
826 | */ |
||
827 | public function testThrowsExceptionWhenInValidPlatformSpecified(): void |
||
828 | { |
||
829 | $connectionParams = $this->params; |
||
830 | $connectionParams['platform'] = new \stdClass(); |
||
831 | |||
832 | /* @var $driver Driver */ |
||
833 | $driver = $this->createMock(Driver::class); |
||
834 | |||
835 | $this->expectException(DBALException::class); |
||
836 | |||
837 | new Connection($connectionParams, $driver); |
||
838 | } |
||
839 | |||
840 | /** |
||
841 | * @group DBAL-990 |
||
842 | */ |
||
843 | public function testRethrowsOriginalExceptionOnDeterminingPlatformWhenConnectingToNonExistentDatabase() |
||
844 | { |
||
845 | /** @var \Doctrine\Tests\Mocks\VersionAwarePlatformDriverMock|\PHPUnit_Framework_MockObject_MockObject $driverMock */ |
||
846 | $driverMock = $this->createMock(VersionAwarePlatformDriverMock::class); |
||
847 | |||
848 | $connection = new Connection(array('dbname' => 'foo'), $driverMock); |
||
849 | $originalException = new \Exception('Original exception'); |
||
850 | $fallbackException = new \Exception('Fallback exception'); |
||
851 | |||
852 | $driverMock->expects($this->at(0)) |
||
853 | ->method('connect') |
||
854 | ->willThrowException($originalException); |
||
855 | |||
856 | $driverMock->expects($this->at(1)) |
||
857 | ->method('connect') |
||
858 | ->willThrowException($fallbackException); |
||
859 | |||
860 | $this->expectExceptionMessage($originalException->getMessage()); |
||
861 | |||
862 | $connection->getDatabasePlatform(); |
||
863 | } |
||
864 | } |
||
865 |