You are here

public function ConnectionUnitTest::testConnectionOpen in Drupal 9

Same name and namespace in other branches
  1. 8 core/tests/Drupal/KernelTests/Core/Database/ConnectionUnitTest.php \Drupal\KernelTests\Core\Database\ConnectionUnitTest::testConnectionOpen()

Tests pdo options override.

File

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

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');
}