You are here

public static function TestDatabase::getConnection in Drupal 8

Same name and namespace in other branches
  1. 9 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()
EnvironmentCleanerFactory::createCleaner in core/modules/simpletest/src/EnvironmentCleanerFactory.php
Factory method to create the environment cleaner service.
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;
}