function ConnectionUnitTest::testConnectionOpen

Same name and namespace in other branches
  1. 11.x core/modules/mysqli/tests/src/Kernel/mysqli/ConnectionUnitTest.php \Drupal\Tests\mysqli\Kernel\mysqli\ConnectionUnitTest::testConnectionOpen()

Tests pdo options override.

File

core/tests/Drupal/KernelTests/Core/Database/ConnectionUnitTest.php, line 234

Class

ConnectionUnitTest
Tests management of database connections.

Namespace

Drupal\KernelTests\Core\Database

Code

public function testConnectionOpen() {
  $reflection = new \ReflectionObject($this->connection);
  $connection_property = $reflection->getProperty('connection');
  $connection_property->setAccessible(TRUE);
  // Skip this test when a database driver does not implement PDO.
  // An alternative database driver that does not implement PDO
  // should implement its own connection test.
  if (get_class($connection_property->getValue($this->connection)) !== 'PDO') {
    $this->markTestSkipped('Ignored PDO connection unit test for this driver because it does not implement PDO.');
  }
  $error_mode = $connection_property->getValue($this->connection)
    ->getAttribute(\PDO::ATTR_ERRMODE);
  // Ensure the default error mode is set to exception.
  $this->assertSame(\PDO::ERRMODE_EXCEPTION, $error_mode);
  $connection_info = Database::getConnectionInfo();
  $connection_info['default']['pdo'][\PDO::ATTR_ERRMODE] = \PDO::ERRMODE_SILENT;
  Database::addConnectionInfo('test', 'default', $connection_info['default']);
  $test_connection = Database::getConnection('default', 'test');
  $reflection = new \ReflectionObject($test_connection);
  $connection_property = $reflection->getProperty('connection');
  $connection_property->setAccessible(TRUE);
  $error_mode = $connection_property->getValue($test_connection)
    ->getAttribute(\PDO::ATTR_ERRMODE);
  // Ensure PDO connection options can be overridden.
  $this->assertSame(\PDO::ERRMODE_SILENT, $error_mode);
  Database::removeConnection('test');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.