You are here

public function ConnectionTest::testConnectionOptions in Drupal 10

Same name and namespace in other branches
  1. 8 core/tests/Drupal/KernelTests/Core/Database/ConnectionTest.php \Drupal\KernelTests\Core\Database\ConnectionTest::testConnectionOptions()
  2. 9 core/tests/Drupal/KernelTests/Core/Database/ConnectionTest.php \Drupal\KernelTests\Core\Database\ConnectionTest::testConnectionOptions()

Tests the connection options of the active database.

File

core/tests/Drupal/KernelTests/Core/Database/ConnectionTest.php, line 86

Class

ConnectionTest
Tests of the core database system.

Namespace

Drupal\KernelTests\Core\Database

Code

public function testConnectionOptions() {
  $connection_info = Database::getConnectionInfo('default');

  // Be sure we're connected to the default database.
  $db = Database::getConnection('default', 'default');
  $connectionOptions = $db
    ->getConnectionOptions();

  // In the MySQL driver, the port can be different, so check individual
  // options.
  $this
    ->assertEquals($connection_info['default']['driver'], $connectionOptions['driver'], 'The default connection info driver matches the current connection options driver.');
  $this
    ->assertEquals($connection_info['default']['database'], $connectionOptions['database'], 'The default connection info database matches the current connection options database.');

  // Set up identical replica and confirm connection options are identical.
  Database::addConnectionInfo('default', 'replica', $connection_info['default']);
  $db2 = Database::getConnection('replica', 'default');

  // Getting a driver class ensures the namespace option is set.
  $this
    ->assertEquals($db
    ->getDriverClass('Select'), $db2
    ->getDriverClass('Select'));
  $connectionOptions2 = $db2
    ->getConnectionOptions();

  // Get a fresh copy of the default connection options.
  $connectionOptions = $db
    ->getConnectionOptions();
  $this
    ->assertSame($connectionOptions2, $connectionOptions, 'The default and replica connection options are identical.');

  // Set up a new connection with different connection info.
  $test = $connection_info['default'];
  $test['database'] .= 'test';
  Database::addConnectionInfo('test', 'default', $test);
  $connection_info = Database::getConnectionInfo('test');

  // Get a fresh copy of the default connection options.
  $connectionOptions = $db
    ->getConnectionOptions();
  $this
    ->assertNotEquals($connection_info['default']['database'], $connectionOptions['database'], 'The test connection info database does not match the current connection options database.');
}