You are here

public static function TestDatabase::getConnection in Drupal 10

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Test/TestDatabase.php \Drupal\Core\Test\TestDatabase::getConnection()
  2. 9 core/lib/Drupal/Core/Test/TestDatabase.php \Drupal\Core\Test\TestDatabase::getConnection()

Returns the database connection to the site under test.

Return value

\Drupal\Core\Database\Connection The database connection to use for inserting assertions.

See also

\Drupal\Core\Test\TestSetupTrait::getDatabaseConnection()

1 call to TestDatabase::getConnection()
TestSetupTrait::getDatabaseConnection in core/lib/Drupal/Core/Test/TestSetupTrait.php
Returns the database connection to the site under test.

File

core/lib/Drupal/Core/Test/TestDatabase.php, line 37

Class

TestDatabase
Provides helper methods for interacting with the fixture database.

Namespace

Drupal\Core\Test

Code

public static function getConnection() {

  // Check whether there is a test runner connection.
  // @see run-tests.sh
  try {
    $connection = Database::getConnection('default', 'test-runner');
  } catch (ConnectionNotDefinedException $e) {

    // Check whether there is a backup of the original default connection.
    // @see \Drupal\Core\Test\TestSetupTrait::changeDatabasePrefix()
    try {
      $connection = Database::getConnection('default', 'simpletest_original_default');
    } catch (ConnectionNotDefinedException $e) {

      // If TestBase::prepareEnvironment() or TestBase::restoreEnvironment()
      // failed, the test-specific database connection does not exist
      // yet/anymore, so fall back to the default of the (UI) test runner.
      $connection = Database::getConnection('default', 'default');
    }
  }
  return $connection;
}