You are here

public function SettingsTest::testDatabaseInfoInitialization in Drupal 10

Tests initialization performed for the $databases variable.

@dataProvider providerTestDatabaseInfoInitialization

File

core/tests/Drupal/Tests/Core/Site/SettingsTest.php, line 316

Class

SettingsTest
@coversDefaultClass \Drupal\Core\Site\Settings @runTestsInSeparateProcesses @group Site

Namespace

Drupal\Tests\Core\Site

Code

public function testDatabaseInfoInitialization(string $driver, ?string $namespace, ?string $autoload, string $expected_namespace, ?string $expected_autoload) : void {
  $databases['mock'][$driver] = [
    'driver' => $driver,
    'prefix' => '',
  ];
  if (!is_null($namespace)) {
    $databases['mock'][$driver]['namespace'] = $namespace;
  }
  if (!is_null($autoload)) {
    $databases['mock'][$driver]['autoload'] = $autoload;
  }
  $settings_file_content = "<?php\n\$databases = " . var_export($databases, TRUE) . ";\n";
  $vfs_root = vfsStream::setup('root');
  $sites_directory = vfsStream::newDirectory('sites')
    ->at($vfs_root);
  vfsStream::newFile('settings.php')
    ->at($sites_directory)
    ->setContent($settings_file_content);
  $class_loader = $this
    ->createMock(ClassLoader::class);
  if (!empty($expected_autoload)) {
    $class_loader
      ->expects($this
      ->once())
      ->method('addPsr4')
      ->with($expected_namespace . '\\', vfsStream::url('root') . '/' . $expected_autoload);
  }
  else {
    $class_loader
      ->expects($this
      ->never())
      ->method('addPsr4');
  }
  Settings::initialize(vfsStream::url('root'), 'sites', $class_loader);
  $expected = [
    $driver => [
      'driver' => $driver,
      'namespace' => $expected_namespace,
      'prefix' => '',
    ],
  ];
  if (!is_null($expected_autoload)) {
    $expected[$driver]['autoload'] = $expected_autoload;
  }
  $this
    ->assertEquals($expected, Database::getConnectionInfo('mock'));
}