function KernelTestBaseDatabaseDriverModuleTest::getDatabaseConnectionInfo

Same name in other branches
  1. 10 core/tests/Drupal/KernelTests/KernelTestBaseDatabaseDriverModuleTest.php \Drupal\KernelTests\KernelTestBaseDatabaseDriverModuleTest::getDatabaseConnectionInfo()
  2. 11.x core/tests/Drupal/KernelTests/KernelTestBaseDatabaseDriverModuleTest.php \Drupal\KernelTests\KernelTestBaseDatabaseDriverModuleTest::getDatabaseConnectionInfo()

Overrides KernelTestBase::getDatabaseConnectionInfo

File

core/tests/Drupal/KernelTests/KernelTestBaseDatabaseDriverModuleTest.php, line 19

Class

KernelTestBaseDatabaseDriverModuleTest
@coversDefaultClass \Drupal\KernelTests\KernelTestBase

Namespace

Drupal\KernelTests

Code

protected function getDatabaseConnectionInfo() {
    // If the test is run with argument SIMPLETEST_DB then use it.
    $db_url = getenv('SIMPLETEST_DB');
    if (empty($db_url)) {
        throw new \Exception('There is no database connection so no tests can be run. You must provide a SIMPLETEST_DB environment variable to run PHPUnit based functional tests outside of run-tests.sh. See https://www.drupal.org/node/2116263#skipped-tests for more information.');
    }
    else {
        $database = Database::convertDbUrlToConnectionInfo($db_url, $this->root);
        if (in_array($database['driver'], [
            'mysql',
            'pgsql',
        ])) {
            // Change the used database driver to the one provided by the module
            // "driver_test".
            $driver = 'Drivertest' . ucfirst($database['driver']);
            $database['driver'] = $driver;
            $database['namespace'] = 'Drupal\\driver_test\\Driver\\Database\\' . $driver;
            $database['autoload'] = "core/modules/system/tests/modules/driver_test/src/Driver/Database/{$driver}/";
        }
        Database::addConnectionInfo('default', 'default', $database);
    }
    // Clone the current connection and replace the current prefix.
    $connection_info = Database::getConnectionInfo('default');
    if (!empty($connection_info)) {
        Database::renameConnection('default', 'simpletest_original_default');
        foreach ($connection_info as $target => $value) {
            // Replace the full table prefix definition to ensure that no table
            // prefixes of the test runner leak into the test.
            $connection_info[$target]['prefix'] = [
                'default' => $this->databasePrefix,
            ];
        }
    }
    return $connection_info;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.