function ExistingDrupal8StyleDatabaseConnectionInSettingsPhpTest::setUp

Same name in other branches
  1. 9 core/tests/Drupal/FunctionalTests/ExistingDrupal8StyleDatabaseConnectionInSettingsPhpTest.php \Drupal\FunctionalTests\ExistingDrupal8StyleDatabaseConnectionInSettingsPhpTest::setUp()
  2. 11.x core/tests/Drupal/FunctionalTests/ExistingDrupal8StyleDatabaseConnectionInSettingsPhpTest.php \Drupal\FunctionalTests\ExistingDrupal8StyleDatabaseConnectionInSettingsPhpTest::setUp()

Overrides BrowserTestBase::setUp

File

core/tests/Drupal/FunctionalTests/ExistingDrupal8StyleDatabaseConnectionInSettingsPhpTest.php, line 24

Class

ExistingDrupal8StyleDatabaseConnectionInSettingsPhpTest
@group Database

Namespace

Drupal\FunctionalTests

Code

protected function setUp() : void {
    parent::setUp();
    $driver = Database::getConnection()->driver();
    if (!in_array($driver, [
        'mysql',
        'pgsql',
        'sqlite',
    ])) {
        $this->markTestSkipped("This test does not support the {$driver} database driver.");
    }
    $filename = $this->siteDirectory . '/settings.php';
    chmod($filename, 0777);
    $contents = file_get_contents($filename);
    $autoload = "'autoload' => 'core/modules/{$driver}/src/Driver/Database/{$driver}/',";
    $contents = str_replace($autoload, '', $contents);
    $namespace_search = "'namespace' => 'Drupal\\\\{$driver}\\\\Driver\\\\Database\\\\{$driver}',";
    $namespace_replace = "'namespace' => 'Drupal\\\\Core\\\\Database\\\\Driver\\\\{$driver}',";
    $contents = str_replace($namespace_search, $namespace_replace, $contents);
    // Add a replica connection to the database settings.
    $contents .= "\$databases['default']['replica'][] = array (\n";
    $contents .= "  'database' => 'db',\n";
    $contents .= "  'username' => 'db',\n";
    $contents .= "  'password' => 'db',\n";
    $contents .= "  'prefix' => 'test22806835',\n";
    $contents .= "  'host' => 'db',\n";
    $contents .= "  'port' => 3306,\n";
    $contents .= "  {$namespace_replace}\n";
    $contents .= "  'driver' => 'mysql',\n";
    $contents .= ");\n";
    file_put_contents($filename, $contents);
}

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