getDatabaseConnectionInfo example


  protected Connection $connection;

  /** * {@inheritdoc} */
  protected function setUp(): void {
    // Find the current SUT database driver from the connection info. If that     // is not the one the test requires, skip before test database     // initialization so to save cycles.     $this->root = static::getDrupalRoot();
    $connectionInfo = $this->getDatabaseConnectionInfo();
    $test_class_parts = explode('\\', get_class($this));
    $expected_provider = $test_class_parts[2] ?? '';
    for ($i = 3; $i < count($test_class_parts)$i++) {
      if ($test_class_parts[$i] === 'Kernel') {
        $expected_driver = $test_class_parts[$i + 1] ?? '';
        break;
      }
    }
    if ($connectionInfo['default']['driver'] !== $expected_driver) {
      $this->markTestSkipped("This test only runs for the database driver '$expected_driver'. Current database driver is '{$connectionInfo['default']['driver']}'.");
    }

    

  protected function bootEnvironment() {
    \Drupal::unsetContainer();

    $this->classLoader = require $this->root . '/autoload.php';

    // Set up virtual filesystem.     Database::addConnectionInfo('default', 'test-runner', $this->getDatabaseConnectionInfo()['default']);
    $test_db = new TestDatabase();
    $this->siteDirectory = $test_db->getTestSitePath();

    // Ensure that all code that relies on drupal_valid_test_ua() can still be     // safely executed. This primarily affects the (test) site directory     // resolution (used by e.g. LocalStream and PhpStorage).     $this->databasePrefix = $test_db->getDatabasePrefix();
    drupal_valid_test_ua($this->databasePrefix);

    $settings = [
      'hash_salt' => static::class,
      
public function testQuotingIdentifiers(): void {
    // Use SQL-reserved words for both the table and column names.     $query = $this->connection->query('SELECT [update] FROM {select}');
    $this->assertEquals('Update value 1', $query->fetchObject()->update);
    $this->assertStringContainsString('SELECT `update` FROM `', $query->getQueryString());
  }

  /** * {@inheritdoc} */
  protected function getDatabaseConnectionInfo() {
    $info = parent::getDatabaseConnectionInfo();

    // This runs during setUp(), so is not yet skipped for non MySQL databases.     // We defer skipping the test to later in setUp(), so that that can be     // based on databaseType() rather than 'driver', but here all we have to go     // on is 'driver'.     if ($info['default']['driver'] === 'mysql') {
      $info['default']['init_commands']['sql_mode'] = "SET sql_mode = ''";
    }

    return $info;
  }

}
Home | Imprint | This part of the site doesn't use cookies.