You are here

public static function TestDatabase::getConnection in Drupal 9

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

Returns the database connection to the site running Simpletest.

Return value

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

See also

\Drupal\simpletest\TestBase::prepareEnvironment()

3 calls to TestDatabase::getConnection()
simpletest_uninstall in core/modules/simpletest/simpletest.install
Implements hook_uninstall().
TestDatabase::lastTestGet in core/lib/Drupal/Core/Test/TestDatabase.php
Get information about the last test that ran given a test ID.
TestSetupTrait::getDatabaseConnection in core/lib/Drupal/Core/Test/TestSetupTrait.php
Returns the database connection to the site running Simpletest.

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
  // @todo Convert Simpletest UI runner to create + use this connection, too.
  try {
    $connection = Database::getConnection('default', 'test-runner');
  } catch (ConnectionNotDefinedException $e) {

    // Check whether there is a backup of the original default connection.
    // @see TestBase::prepareEnvironment()
    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;
}