| Total Complexity | 51 |
| Total Lines | 997 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like ConnectionTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ConnectionTest, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 36 | class ConnectionTest extends DbalTestCase |
||
| 37 | { |
||
| 38 | /** @var Connection */ |
||
| 39 | private $connection; |
||
| 40 | |||
| 41 | /** @var string[] */ |
||
| 42 | protected $params = [ |
||
| 43 | 'driver' => 'pdo_mysql', |
||
| 44 | 'host' => 'localhost', |
||
| 45 | 'user' => 'root', |
||
| 46 | 'password' => 'password', |
||
| 47 | 'port' => '1234', |
||
| 48 | ]; |
||
| 49 | |||
| 50 | protected function setUp() : void |
||
| 51 | { |
||
| 52 | $this->connection = DriverManager::getConnection($this->params); |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @return Connection|MockObject |
||
| 57 | */ |
||
| 58 | private function getExecuteUpdateMockConnection() |
||
| 59 | { |
||
| 60 | $driverMock = $this->createMock(Driver::class); |
||
| 61 | |||
| 62 | $driverMock->expects($this->any()) |
||
| 63 | ->method('connect') |
||
| 64 | ->will($this->returnValue( |
||
| 65 | $this->createMock(DriverConnection::class) |
||
| 66 | )); |
||
| 67 | |||
| 68 | $platform = $this->getMockForAbstractClass(AbstractPlatform::class); |
||
| 69 | |||
| 70 | return $this->getMockBuilder(Connection::class) |
||
| 71 | ->setMethods(['executeUpdate']) |
||
| 72 | ->setConstructorArgs([['platform' => $platform], $driverMock]) |
||
| 73 | ->getMock(); |
||
| 74 | } |
||
| 75 | |||
| 76 | public function testIsConnected() : void |
||
| 77 | { |
||
| 78 | self::assertFalse($this->connection->isConnected()); |
||
| 79 | } |
||
| 80 | |||
| 81 | public function testNoTransactionActiveByDefault() : void |
||
| 82 | { |
||
| 83 | self::assertFalse($this->connection->isTransactionActive()); |
||
| 84 | } |
||
| 85 | |||
| 86 | public function testCommitWithNoActiveTransactionThrowsException() : void |
||
| 87 | { |
||
| 88 | $this->expectException(ConnectionException::class); |
||
| 89 | $this->connection->commit(); |
||
| 90 | } |
||
| 91 | |||
| 92 | public function testRollbackWithNoActiveTransactionThrowsException() : void |
||
| 93 | { |
||
| 94 | $this->expectException(ConnectionException::class); |
||
| 95 | $this->connection->rollBack(); |
||
| 96 | } |
||
| 97 | |||
| 98 | public function testSetRollbackOnlyNoActiveTransactionThrowsException() : void |
||
| 99 | { |
||
| 100 | $this->expectException(ConnectionException::class); |
||
| 101 | $this->connection->setRollbackOnly(); |
||
| 102 | } |
||
| 103 | |||
| 104 | public function testIsRollbackOnlyNoActiveTransactionThrowsException() : void |
||
| 105 | { |
||
| 106 | $this->expectException(ConnectionException::class); |
||
| 107 | $this->connection->isRollbackOnly(); |
||
| 108 | } |
||
| 109 | |||
| 110 | public function testGetConfiguration() : void |
||
| 111 | { |
||
| 112 | $config = $this->connection->getConfiguration(); |
||
| 113 | |||
| 114 | self::assertInstanceOf(Configuration::class, $config); |
||
| 115 | } |
||
| 116 | |||
| 117 | public function testGetHost() : void |
||
| 118 | { |
||
| 119 | self::assertEquals('localhost', $this->connection->getHost()); |
||
| 120 | } |
||
| 121 | |||
| 122 | public function testGetPort() : void |
||
| 123 | { |
||
| 124 | self::assertEquals('1234', $this->connection->getPort()); |
||
| 125 | } |
||
| 126 | |||
| 127 | public function testGetUsername() : void |
||
| 128 | { |
||
| 129 | self::assertEquals('root', $this->connection->getUsername()); |
||
| 130 | } |
||
| 131 | |||
| 132 | public function testGetPassword() : void |
||
| 135 | } |
||
| 136 | |||
| 137 | public function testGetDriver() : void |
||
| 138 | { |
||
| 139 | self::assertInstanceOf(\Doctrine\DBAL\Driver\PDOMySql\Driver::class, $this->connection->getDriver()); |
||
| 140 | } |
||
| 141 | |||
| 142 | public function testGetEventManager() : void |
||
| 143 | { |
||
| 144 | self::assertInstanceOf(EventManager::class, $this->connection->getEventManager()); |
||
| 145 | } |
||
| 146 | |||
| 147 | public function testConnectDispatchEvent() : void |
||
| 148 | { |
||
| 149 | $listenerMock = $this->getMockBuilder('ConnectDispatchEventListener') |
||
| 150 | ->setMethods(['postConnect']) |
||
| 151 | ->getMock(); |
||
| 152 | $listenerMock->expects($this->once())->method('postConnect'); |
||
| 153 | |||
| 154 | $eventManager = new EventManager(); |
||
| 155 | $eventManager->addEventListener([Events::postConnect], $listenerMock); |
||
| 156 | |||
| 157 | $driverMock = $this->createMock(Driver::class); |
||
| 158 | $driverMock->expects($this->at(0)) |
||
| 159 | ->method('connect'); |
||
| 160 | |||
| 161 | $conn = new Connection([], $driverMock, new Configuration(), $eventManager); |
||
| 162 | $conn->connect(); |
||
| 163 | } |
||
| 164 | |||
| 165 | public function testEventManagerPassedToPlatform() : void |
||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @requires extension pdo_sqlite |
||
| 187 | * @dataProvider getQueryMethods |
||
| 188 | */ |
||
| 189 | public function testDriverExceptionIsWrapped(string $method) : void |
||
| 190 | { |
||
| 191 | $this->expectException(DBALException::class); |
||
| 192 | $this->expectExceptionMessage("An exception occurred while executing 'MUUHAAAAHAAAA':\n\nSQLSTATE[HY000]: General error: 1 near \"MUUHAAAAHAAAA\""); |
||
| 200 | } |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @return array<int, array<int, mixed>> |
||
| 204 | */ |
||
| 205 | public static function getQueryMethods() : iterable |
||
| 206 | { |
||
| 207 | return [ |
||
| 208 | ['exec'], |
||
| 209 | ['query'], |
||
| 210 | ['executeQuery'], |
||
| 211 | ['executeUpdate'], |
||
| 212 | ['prepare'], |
||
| 213 | ]; |
||
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Pretty dumb test, however we want to check that the EchoSQLLogger correctly implements the interface. |
||
| 218 | * |
||
| 219 | * @group DBAL-11 |
||
| 220 | */ |
||
| 221 | public function testEchoSQLLogger() : void |
||
| 222 | { |
||
| 223 | $logger = new EchoSQLLogger(); |
||
| 224 | $this->connection->getConfiguration()->setSQLLogger($logger); |
||
| 225 | self::assertSame($logger, $this->connection->getConfiguration()->getSQLLogger()); |
||
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Pretty dumb test, however we want to check that the DebugStack correctly implements the interface. |
||
| 230 | * |
||
| 231 | * @group DBAL-11 |
||
| 232 | */ |
||
| 233 | public function testDebugSQLStack() : void |
||
| 234 | { |
||
| 235 | $logger = new DebugStack(); |
||
| 236 | $this->connection->getConfiguration()->setSQLLogger($logger); |
||
| 237 | self::assertSame($logger, $this->connection->getConfiguration()->getSQLLogger()); |
||
| 238 | } |
||
| 239 | |||
| 240 | /** |
||
| 241 | * @group DBAL-81 |
||
| 242 | */ |
||
| 243 | public function testIsAutoCommit() : void |
||
| 246 | } |
||
| 247 | |||
| 248 | /** |
||
| 249 | * @group DBAL-81 |
||
| 250 | */ |
||
| 251 | public function testSetAutoCommit() : void |
||
| 252 | { |
||
| 253 | $this->connection->setAutoCommit(false); |
||
| 254 | self::assertFalse($this->connection->isAutoCommit()); |
||
| 255 | $this->connection->setAutoCommit(0); |
||
| 256 | self::assertFalse($this->connection->isAutoCommit()); |
||
| 257 | } |
||
| 258 | |||
| 259 | /** |
||
| 260 | * @group DBAL-81 |
||
| 261 | */ |
||
| 262 | public function testConnectStartsTransactionInNoAutoCommitMode() : void |
||
| 279 | } |
||
| 280 | |||
| 281 | /** |
||
| 282 | * @group DBAL-81 |
||
| 283 | */ |
||
| 284 | public function testCommitStartsTransactionInNoAutoCommitMode() : void |
||
| 299 | } |
||
| 300 | |||
| 301 | /** |
||
| 302 | * @group DBAL-81 |
||
| 303 | */ |
||
| 304 | public function testCommitReturnTrue() : void |
||
| 305 | { |
||
| 306 | $driverMock = $this->createMock(Driver::class); |
||
| 307 | $driverMock->expects($this->any()) |
||
| 308 | ->method('connect') |
||
| 309 | ->will($this->returnValue( |
||
| 310 | $this->createMock(DriverConnection::class) |
||
| 311 | )); |
||
| 312 | $pdo = $this->createMock(PDO::class); |
||
| 313 | $pdo->expects($this->once()) |
||
| 314 | ->method('commit')->willReturn(true); |
||
| 315 | |||
| 316 | $conn = new Connection(['pdo' => $pdo], $driverMock); |
||
| 317 | |||
| 318 | $conn->connect(); |
||
| 319 | $conn->beginTransaction(); |
||
| 320 | |||
| 321 | self::assertTrue($conn->commit()); |
||
| 322 | } |
||
| 323 | |||
| 324 | /** |
||
| 325 | * @group DBAL-81 |
||
| 326 | */ |
||
| 327 | public function testCommitReturnFalse() : void |
||
| 345 | } |
||
| 346 | |||
| 347 | /** |
||
| 348 | * @group DBAL-81 |
||
| 349 | */ |
||
| 350 | public function testRollackReturnTrue() : void |
||
| 368 | } |
||
| 369 | |||
| 370 | /** |
||
| 371 | * @group DBAL-81 |
||
| 372 | */ |
||
| 373 | public function testRollackReturnFalse() : void |
||
| 391 | } |
||
| 392 | |||
| 393 | /** |
||
| 394 | * @group DBAL-81 |
||
| 395 | */ |
||
| 396 | public function testRollBackStartsTransactionInNoAutoCommitMode() : void |
||
| 397 | { |
||
| 398 | $driverMock = $this->createMock(Driver::class); |
||
| 399 | $driverMock->expects($this->any()) |
||
| 400 | ->method('connect') |
||
| 401 | ->will($this->returnValue( |
||
| 402 | $this->createMock(DriverConnection::class) |
||
| 403 | )); |
||
| 404 | $conn = new Connection([], $driverMock); |
||
| 405 | |||
| 406 | $conn->setAutoCommit(false); |
||
| 407 | $conn->connect(); |
||
| 408 | $conn->rollBack(); |
||
| 409 | |||
| 410 | self::assertTrue($conn->isTransactionActive()); |
||
| 411 | } |
||
| 412 | |||
| 413 | /** |
||
| 414 | * @group DBAL-81 |
||
| 415 | */ |
||
| 416 | public function testSwitchingAutoCommitModeCommitsAllCurrentTransactions() : void |
||
| 438 | } |
||
| 439 | |||
| 440 | public function testEmptyInsert() : void |
||
| 441 | { |
||
| 442 | $conn = $this->getExecuteUpdateMockConnection(); |
||
| 443 | |||
| 444 | $conn->expects($this->once()) |
||
| 445 | ->method('executeUpdate') |
||
| 446 | ->with('INSERT INTO footable () VALUES ()'); |
||
| 447 | |||
| 448 | $conn->insert('footable', []); |
||
| 449 | } |
||
| 450 | |||
| 451 | /** |
||
| 452 | * @group DBAL-2511 |
||
| 453 | */ |
||
| 454 | public function testUpdateWithDifferentColumnsInDataAndIdentifiers() : void |
||
| 455 | { |
||
| 456 | $conn = $this->getExecuteUpdateMockConnection(); |
||
| 457 | |||
| 458 | $conn->expects($this->once()) |
||
| 459 | ->method('executeUpdate') |
||
| 460 | ->with( |
||
| 461 | 'UPDATE TestTable SET text = ?, is_edited = ? WHERE id = ? AND name = ?', |
||
| 462 | [ |
||
| 463 | 'some text', |
||
| 464 | true, |
||
| 465 | 1, |
||
| 466 | 'foo', |
||
| 467 | ], |
||
| 468 | [ |
||
| 469 | 'string', |
||
| 470 | 'boolean', |
||
| 471 | 'integer', |
||
| 472 | 'string', |
||
| 473 | ] |
||
| 474 | ); |
||
| 475 | |||
| 476 | $conn->update( |
||
| 477 | 'TestTable', |
||
| 478 | [ |
||
| 479 | 'text' => 'some text', |
||
| 480 | 'is_edited' => true, |
||
| 481 | ], |
||
| 482 | [ |
||
| 483 | 'id' => 1, |
||
| 484 | 'name' => 'foo', |
||
| 485 | ], |
||
| 486 | [ |
||
| 487 | 'text' => 'string', |
||
| 488 | 'is_edited' => 'boolean', |
||
| 489 | 'id' => 'integer', |
||
| 490 | 'name' => 'string', |
||
| 491 | ] |
||
| 492 | ); |
||
| 493 | } |
||
| 494 | |||
| 495 | /** |
||
| 496 | * @group DBAL-2511 |
||
| 497 | */ |
||
| 498 | public function testUpdateWithSameColumnInDataAndIdentifiers() : void |
||
| 499 | { |
||
| 500 | $conn = $this->getExecuteUpdateMockConnection(); |
||
| 501 | |||
| 502 | $conn->expects($this->once()) |
||
| 503 | ->method('executeUpdate') |
||
| 504 | ->with( |
||
| 505 | 'UPDATE TestTable SET text = ?, is_edited = ? WHERE id = ? AND is_edited = ?', |
||
| 506 | [ |
||
| 507 | 'some text', |
||
| 508 | true, |
||
| 509 | 1, |
||
| 510 | false, |
||
| 511 | ], |
||
| 512 | [ |
||
| 513 | 'string', |
||
| 514 | 'boolean', |
||
| 515 | 'integer', |
||
| 516 | 'boolean', |
||
| 517 | ] |
||
| 518 | ); |
||
| 519 | |||
| 520 | $conn->update( |
||
| 521 | 'TestTable', |
||
| 522 | [ |
||
| 523 | 'text' => 'some text', |
||
| 524 | 'is_edited' => true, |
||
| 525 | ], |
||
| 526 | [ |
||
| 527 | 'id' => 1, |
||
| 528 | 'is_edited' => false, |
||
| 529 | ], |
||
| 530 | [ |
||
| 531 | 'text' => 'string', |
||
| 532 | 'is_edited' => 'boolean', |
||
| 533 | 'id' => 'integer', |
||
| 534 | ] |
||
| 535 | ); |
||
| 536 | } |
||
| 537 | |||
| 538 | /** |
||
| 539 | * @group DBAL-2688 |
||
| 540 | */ |
||
| 541 | public function testUpdateWithIsNull() : void |
||
| 542 | { |
||
| 543 | $conn = $this->getExecuteUpdateMockConnection(); |
||
| 544 | |||
| 545 | $conn->expects($this->once()) |
||
| 546 | ->method('executeUpdate') |
||
| 547 | ->with( |
||
| 548 | 'UPDATE TestTable SET text = ?, is_edited = ? WHERE id IS NULL AND name = ?', |
||
| 549 | [ |
||
| 550 | 'some text', |
||
| 551 | null, |
||
| 552 | 'foo', |
||
| 553 | ], |
||
| 554 | [ |
||
| 555 | 'string', |
||
| 556 | 'boolean', |
||
| 557 | 'string', |
||
| 558 | ] |
||
| 559 | ); |
||
| 560 | |||
| 561 | $conn->update( |
||
| 562 | 'TestTable', |
||
| 563 | [ |
||
| 564 | 'text' => 'some text', |
||
| 565 | 'is_edited' => null, |
||
| 566 | ], |
||
| 567 | [ |
||
| 568 | 'id' => null, |
||
| 569 | 'name' => 'foo', |
||
| 570 | ], |
||
| 571 | [ |
||
| 572 | 'text' => 'string', |
||
| 573 | 'is_edited' => 'boolean', |
||
| 574 | 'id' => 'integer', |
||
| 575 | 'name' => 'string', |
||
| 576 | ] |
||
| 577 | ); |
||
| 578 | } |
||
| 579 | |||
| 580 | /** |
||
| 581 | * @group DBAL-2688 |
||
| 582 | */ |
||
| 583 | public function testDeleteWithIsNull() : void |
||
| 584 | { |
||
| 585 | $conn = $this->getExecuteUpdateMockConnection(); |
||
| 586 | |||
| 587 | $conn->expects($this->once()) |
||
| 588 | ->method('executeUpdate') |
||
| 589 | ->with( |
||
| 590 | 'DELETE FROM TestTable WHERE id IS NULL AND name = ?', |
||
| 591 | ['foo'], |
||
| 592 | ['string'] |
||
| 593 | ); |
||
| 594 | |||
| 595 | $conn->delete( |
||
| 596 | 'TestTable', |
||
| 597 | [ |
||
| 598 | 'id' => null, |
||
| 599 | 'name' => 'foo', |
||
| 600 | ], |
||
| 601 | [ |
||
| 602 | 'id' => 'integer', |
||
| 603 | 'name' => 'string', |
||
| 604 | ] |
||
| 605 | ); |
||
| 606 | } |
||
| 607 | |||
| 608 | public function testFetchAssoc() : void |
||
| 609 | { |
||
| 610 | $statement = 'SELECT * FROM foo WHERE bar = ?'; |
||
| 611 | $params = [666]; |
||
| 612 | $types = [ParameterType::INTEGER]; |
||
| 613 | $result = []; |
||
| 614 | |||
| 615 | $driverMock = $this->createMock(Driver::class); |
||
| 616 | |||
| 617 | $driverMock->expects($this->any()) |
||
| 618 | ->method('connect') |
||
| 619 | ->will($this->returnValue( |
||
| 620 | $this->createMock(DriverConnection::class) |
||
| 621 | )); |
||
| 622 | |||
| 623 | $driverStatementMock = $this->createMock(Statement::class); |
||
| 624 | |||
| 625 | $driverStatementMock->expects($this->once()) |
||
| 626 | ->method('fetch') |
||
| 627 | ->with(FetchMode::ASSOCIATIVE) |
||
| 628 | ->will($this->returnValue($result)); |
||
| 629 | |||
| 630 | /** @var Connection|MockObject $conn */ |
||
| 631 | $conn = $this->getMockBuilder(Connection::class) |
||
| 632 | ->setMethods(['executeQuery']) |
||
| 633 | ->setConstructorArgs([[], $driverMock]) |
||
| 634 | ->getMock(); |
||
| 635 | |||
| 636 | $conn->expects($this->once()) |
||
| 637 | ->method('executeQuery') |
||
| 638 | ->with($statement, $params, $types) |
||
| 639 | ->will($this->returnValue($driverStatementMock)); |
||
| 640 | |||
| 641 | self::assertSame($result, $conn->fetchAssoc($statement, $params, $types)); |
||
| 642 | } |
||
| 643 | |||
| 644 | public function testFetchArray() : void |
||
| 645 | { |
||
| 646 | $statement = 'SELECT * FROM foo WHERE bar = ?'; |
||
| 647 | $params = [666]; |
||
| 648 | $types = [ParameterType::INTEGER]; |
||
| 649 | $result = []; |
||
| 650 | |||
| 651 | $driverMock = $this->createMock(Driver::class); |
||
| 652 | |||
| 653 | $driverMock->expects($this->any()) |
||
| 654 | ->method('connect') |
||
| 655 | ->will($this->returnValue( |
||
| 656 | $this->createMock(DriverConnection::class) |
||
| 657 | )); |
||
| 658 | |||
| 659 | $driverStatementMock = $this->createMock(Statement::class); |
||
| 660 | |||
| 661 | $driverStatementMock->expects($this->once()) |
||
| 662 | ->method('fetch') |
||
| 663 | ->with(FetchMode::NUMERIC) |
||
| 664 | ->will($this->returnValue($result)); |
||
| 665 | |||
| 666 | /** @var Connection|MockObject $conn */ |
||
| 667 | $conn = $this->getMockBuilder(Connection::class) |
||
| 668 | ->setMethods(['executeQuery']) |
||
| 669 | ->setConstructorArgs([[], $driverMock]) |
||
| 670 | ->getMock(); |
||
| 671 | |||
| 672 | $conn->expects($this->once()) |
||
| 673 | ->method('executeQuery') |
||
| 674 | ->with($statement, $params, $types) |
||
| 675 | ->will($this->returnValue($driverStatementMock)); |
||
| 676 | |||
| 677 | self::assertSame($result, $conn->fetchArray($statement, $params, $types)); |
||
| 678 | } |
||
| 679 | |||
| 680 | public function testFetchColumn() : void |
||
| 681 | { |
||
| 682 | $statement = 'SELECT * FROM foo WHERE bar = ?'; |
||
| 683 | $params = [666]; |
||
| 684 | $types = [ParameterType::INTEGER]; |
||
| 685 | $column = 0; |
||
| 686 | $result = []; |
||
| 687 | |||
| 688 | $driverMock = $this->createMock(Driver::class); |
||
| 689 | |||
| 690 | $driverMock->expects($this->any()) |
||
| 691 | ->method('connect') |
||
| 692 | ->will($this->returnValue( |
||
| 693 | $this->createMock(DriverConnection::class) |
||
| 694 | )); |
||
| 695 | |||
| 696 | $driverStatementMock = $this->createMock(Statement::class); |
||
| 697 | |||
| 698 | $driverStatementMock->expects($this->once()) |
||
| 699 | ->method('fetchColumn') |
||
| 700 | ->with($column) |
||
| 701 | ->will($this->returnValue($result)); |
||
| 702 | |||
| 703 | /** @var Connection|MockObject $conn */ |
||
| 704 | $conn = $this->getMockBuilder(Connection::class) |
||
| 705 | ->setMethods(['executeQuery']) |
||
| 706 | ->setConstructorArgs([[], $driverMock]) |
||
| 707 | ->getMock(); |
||
| 708 | |||
| 709 | $conn->expects($this->once()) |
||
| 710 | ->method('executeQuery') |
||
| 711 | ->with($statement, $params, $types) |
||
| 712 | ->will($this->returnValue($driverStatementMock)); |
||
| 713 | |||
| 714 | self::assertSame($result, $conn->fetchColumn($statement, $params, $column, $types)); |
||
| 715 | } |
||
| 716 | |||
| 717 | public function testFetchAll() : void |
||
| 718 | { |
||
| 719 | $statement = 'SELECT * FROM foo WHERE bar = ?'; |
||
| 720 | $params = [666]; |
||
| 721 | $types = [ParameterType::INTEGER]; |
||
| 722 | $result = []; |
||
| 723 | |||
| 724 | $driverMock = $this->createMock(Driver::class); |
||
| 725 | |||
| 726 | $driverMock->expects($this->any()) |
||
| 727 | ->method('connect') |
||
| 728 | ->will($this->returnValue( |
||
| 729 | $this->createMock(DriverConnection::class) |
||
| 730 | )); |
||
| 731 | |||
| 732 | $driverStatementMock = $this->createMock(Statement::class); |
||
| 733 | |||
| 734 | $driverStatementMock->expects($this->once()) |
||
| 735 | ->method('fetchAll') |
||
| 736 | ->will($this->returnValue($result)); |
||
| 737 | |||
| 738 | /** @var Connection|MockObject $conn */ |
||
| 739 | $conn = $this->getMockBuilder(Connection::class) |
||
| 740 | ->setMethods(['executeQuery']) |
||
| 741 | ->setConstructorArgs([[], $driverMock]) |
||
| 742 | ->getMock(); |
||
| 743 | |||
| 744 | $conn->expects($this->once()) |
||
| 745 | ->method('executeQuery') |
||
| 746 | ->with($statement, $params, $types) |
||
| 747 | ->will($this->returnValue($driverStatementMock)); |
||
| 748 | |||
| 749 | self::assertSame($result, $conn->fetchAll($statement, $params, $types)); |
||
| 750 | } |
||
| 751 | |||
| 752 | public function testConnectionDoesNotMaintainTwoReferencesToExternalPDO() : void |
||
| 753 | { |
||
| 754 | $params['pdo'] = new stdClass(); |
||
|
|
|||
| 755 | |||
| 756 | $driverMock = $this->createMock(Driver::class); |
||
| 757 | |||
| 758 | $conn = new Connection($params, $driverMock); |
||
| 759 | |||
| 760 | self::assertArrayNotHasKey('pdo', $conn->getParams(), 'Connection is maintaining additional reference to the PDO connection'); |
||
| 761 | } |
||
| 762 | |||
| 763 | public function testPassingExternalPDOMeansConnectionIsConnected() : void |
||
| 764 | { |
||
| 765 | $params['pdo'] = new stdClass(); |
||
| 766 | |||
| 767 | $driverMock = $this->createMock(Driver::class); |
||
| 768 | |||
| 769 | $conn = new Connection($params, $driverMock); |
||
| 770 | |||
| 771 | self::assertTrue($conn->isConnected(), 'Connection is not connected after passing external PDO'); |
||
| 772 | } |
||
| 773 | |||
| 774 | public function testCallingDeleteWithNoDeletionCriteriaResultsInInvalidArgumentException() : void |
||
| 775 | { |
||
| 776 | /** @var Driver $driver */ |
||
| 777 | $driver = $this->createMock(Driver::class); |
||
| 778 | $pdoMock = $this->createMock(\Doctrine\DBAL\Driver\Connection::class); |
||
| 779 | |||
| 780 | // should never execute queries with invalid arguments |
||
| 781 | $pdoMock->expects($this->never())->method('exec'); |
||
| 782 | $pdoMock->expects($this->never())->method('prepare'); |
||
| 783 | |||
| 784 | $conn = new Connection(['pdo' => $pdoMock], $driver); |
||
| 785 | |||
| 786 | $this->expectException(InvalidArgumentException::class); |
||
| 787 | $conn->delete('kittens', []); |
||
| 788 | } |
||
| 789 | |||
| 790 | /** |
||
| 791 | * @return array<int, array<int, mixed>> |
||
| 792 | */ |
||
| 793 | public static function dataCallConnectOnce() : iterable |
||
| 794 | { |
||
| 795 | return [ |
||
| 796 | ['delete', ['tbl', ['id' => 12345]]], |
||
| 797 | ['insert', ['tbl', ['data' => 'foo']]], |
||
| 798 | ['update', ['tbl', ['data' => 'bar'], ['id' => 12345]]], |
||
| 799 | ['prepare', ['select * from dual']], |
||
| 800 | ['executeUpdate', ['insert into tbl (id) values (?)'], [123]], |
||
| 801 | ]; |
||
| 802 | } |
||
| 803 | |||
| 804 | /** |
||
| 805 | * @param array<int, mixed> $params |
||
| 806 | * |
||
| 807 | * @dataProvider dataCallConnectOnce |
||
| 808 | */ |
||
| 809 | public function testCallConnectOnce(string $method, array $params) : void |
||
| 810 | { |
||
| 811 | $driverMock = $this->createMock(Driver::class); |
||
| 812 | $pdoMock = $this->createMock(Connection::class); |
||
| 813 | $platformMock = $this->createMock(AbstractPlatform::class); |
||
| 814 | $stmtMock = $this->createMock(Statement::class); |
||
| 815 | |||
| 816 | $pdoMock->expects($this->any()) |
||
| 817 | ->method('prepare') |
||
| 818 | ->will($this->returnValue($stmtMock)); |
||
| 819 | |||
| 820 | $conn = $this->getMockBuilder(Connection::class) |
||
| 821 | ->setConstructorArgs([['pdo' => $pdoMock, 'platform' => $platformMock], $driverMock]) |
||
| 822 | ->setMethods(['connect']) |
||
| 823 | ->getMock(); |
||
| 824 | |||
| 825 | $conn->expects($this->once())->method('connect'); |
||
| 826 | |||
| 827 | call_user_func_array([$conn, $method], $params); |
||
| 828 | } |
||
| 829 | |||
| 830 | /** |
||
| 831 | * @group DBAL-1127 |
||
| 832 | */ |
||
| 833 | public function testPlatformDetectionIsTriggerOnlyOnceOnRetrievingPlatform() : void |
||
| 834 | { |
||
| 835 | /** @var Driver|VersionAwarePlatformDriver|MockObject $driverMock */ |
||
| 836 | $driverMock = $this->createMock([Driver::class, VersionAwarePlatformDriver::class]); |
||
| 837 | |||
| 838 | /** @var ServerInfoAwareConnection|MockObject $driverConnectionMock */ |
||
| 839 | $driverConnectionMock = $this->createMock(ServerInfoAwareConnection::class); |
||
| 840 | |||
| 841 | /** @var AbstractPlatform|MockObject $platformMock */ |
||
| 842 | $platformMock = $this->getMockForAbstractClass(AbstractPlatform::class); |
||
| 843 | |||
| 844 | $connection = new Connection([], $driverMock); |
||
| 845 | |||
| 846 | $driverMock->expects($this->once()) |
||
| 847 | ->method('connect') |
||
| 848 | ->will($this->returnValue($driverConnectionMock)); |
||
| 849 | |||
| 850 | $driverConnectionMock->expects($this->once()) |
||
| 851 | ->method('requiresQueryForServerVersion') |
||
| 852 | ->will($this->returnValue(false)); |
||
| 853 | |||
| 854 | $driverConnectionMock->expects($this->once()) |
||
| 855 | ->method('getServerVersion') |
||
| 856 | ->will($this->returnValue('6.6.6')); |
||
| 857 | |||
| 858 | $driverMock->expects($this->once()) |
||
| 859 | ->method('createDatabasePlatformForVersion') |
||
| 860 | ->with('6.6.6') |
||
| 861 | ->will($this->returnValue($platformMock)); |
||
| 862 | |||
| 863 | self::assertSame($platformMock, $connection->getDatabasePlatform()); |
||
| 864 | } |
||
| 865 | |||
| 866 | public function testConnectionParamsArePassedToTheQueryCacheProfileInExecuteCacheQuery() : void |
||
| 867 | { |
||
| 868 | $resultCacheDriverMock = $this->createMock(Cache::class); |
||
| 869 | |||
| 870 | $resultCacheDriverMock |
||
| 871 | ->expects($this->atLeastOnce()) |
||
| 872 | ->method('fetch') |
||
| 873 | ->with('cacheKey') |
||
| 874 | ->will($this->returnValue(['realKey' => []])); |
||
| 875 | |||
| 876 | $query = 'SELECT * FROM foo WHERE bar = ?'; |
||
| 877 | $params = [666]; |
||
| 878 | $types = [ParameterType::INTEGER]; |
||
| 879 | |||
| 880 | /** @var QueryCacheProfile|MockObject $queryCacheProfileMock */ |
||
| 881 | $queryCacheProfileMock = $this->createMock(QueryCacheProfile::class); |
||
| 882 | |||
| 883 | $queryCacheProfileMock |
||
| 884 | ->expects($this->any()) |
||
| 885 | ->method('getResultCacheDriver') |
||
| 886 | ->will($this->returnValue($resultCacheDriverMock)); |
||
| 887 | |||
| 888 | // This is our main expectation |
||
| 889 | $queryCacheProfileMock |
||
| 890 | ->expects($this->once()) |
||
| 891 | ->method('generateCacheKeys') |
||
| 892 | ->with($query, $params, $types, $this->params) |
||
| 893 | ->will($this->returnValue(['cacheKey', 'realKey'])); |
||
| 894 | |||
| 895 | /** @var Driver $driver */ |
||
| 896 | $driver = $this->createMock(Driver::class); |
||
| 897 | |||
| 898 | self::assertInstanceOf( |
||
| 899 | ArrayStatement::class, |
||
| 900 | (new Connection($this->params, $driver))->executeCacheQuery($query, $params, $types, $queryCacheProfileMock) |
||
| 901 | ); |
||
| 902 | } |
||
| 903 | |||
| 904 | /** |
||
| 905 | * @group #2821 |
||
| 906 | */ |
||
| 907 | public function testShouldNotPassPlatformInParamsToTheQueryCacheProfileInExecuteCacheQuery() : void |
||
| 941 | } |
||
| 942 | |||
| 943 | /** |
||
| 944 | * @group #2821 |
||
| 945 | */ |
||
| 946 | public function testThrowsExceptionWhenInValidPlatformSpecified() : void |
||
| 947 | { |
||
| 948 | $connectionParams = $this->params; |
||
| 949 | $connectionParams['platform'] = new stdClass(); |
||
| 950 | |||
| 951 | /** @var Driver $driver */ |
||
| 952 | $driver = $this->createMock(Driver::class); |
||
| 953 | |||
| 957 | } |
||
| 958 | |||
| 959 | /** |
||
| 960 | * @group DBAL-990 |
||
| 961 | */ |
||
| 962 | public function testRethrowsOriginalExceptionOnDeterminingPlatformWhenConnectingToNonExistentDatabase() : void |
||
| 982 | } |
||
| 983 | |||
| 984 | /** |
||
| 985 | * @group #3194 |
||
| 986 | */ |
||
| 987 | public function testExecuteCacheQueryStripsPlatformFromConnectionParamsBeforeGeneratingCacheKeys() : void |
||
| 1035 |