You are here

public static function BrowserTestBase::getDatabaseConnection in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/simpletest/src/BrowserTestBase.php \Drupal\simpletest\BrowserTestBase::getDatabaseConnection()

Returns the database connection to the site running Simpletest.

Return value

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

File

core/modules/simpletest/src/BrowserTestBase.php, line 1152
Contains \Drupal\simpletest\BrowserTestBase.

Class

BrowserTestBase
Provides a test case for functional Drupal tests.

Namespace

Drupal\simpletest

Code

public static function getDatabaseConnection() {

  // 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 BrowserTestBase::prepareEnvironment()
    try {
      $connection = Database::getConnection('default', 'simpletest_original_default');
    } catch (ConnectionNotDefinedException $e) {

      // If BrowserTestBase::prepareEnvironment() or
      // BrowserTestBase::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;
}