You are here

protected function MigrateTestCase::getDatabase in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/migrate/tests/src/Unit/MigrateTestCase.php \Drupal\Tests\migrate\Unit\MigrateTestCase::getDatabase()

Gets an SQLite database connection object for use in tests.

Parameters

array $database_contents: The database contents faked as an array. Each key is a table name, each value is a list of table rows, an associative array of field => value.

array $connection_options: (optional) Options for the database connection. Defaults to an empty array.

Return value

\Drupal\Core\Database\Driver\sqlite\Connection The database connection.

7 calls to MigrateTestCase::getDatabase()
Drupal6SqlBaseTest::setUp in core/modules/migrate_drupal/tests/src/Unit/source/d6/Drupal6SqlBaseTest.php
DrupalSqlBaseTest::testMinimumVersion in core/modules/migrate_drupal/tests/src/Unit/source/DrupalSqlBaseTest.php
@covers ::checkRequirements
DrupalSqlBaseTest::testSourceProviderNotActive in core/modules/migrate_drupal/tests/src/Unit/source/DrupalSqlBaseTest.php
@covers ::checkRequirements
MigrateSqlIdMapTest::setUp in core/modules/migrate/tests/src/Unit/MigrateSqlIdMapTest.php
MigrateSqlIdMapTest::setupRows in core/modules/migrate/tests/src/Unit/MigrateSqlIdMapTest.php
Setup a database with the given rows.

... See full list

File

core/modules/migrate/tests/src/Unit/MigrateTestCase.php, line 105

Class

MigrateTestCase
Provides setup and helper methods for Migrate module tests.

Namespace

Drupal\Tests\migrate\Unit

Code

protected function getDatabase(array $database_contents, $connection_options = []) {
  if (extension_loaded('pdo_sqlite')) {
    $connection_options['database'] = ':memory:';
    $pdo = Connection::open($connection_options);
    $connection = new Connection($pdo, $connection_options);
  }
  else {
    $this
      ->markTestSkipped('The pdo_sqlite extension is not available.');
  }

  // Initialize the DIC with a fake module handler for alterable queries.
  $container = new ContainerBuilder();
  $container
    ->set('module_handler', $this
    ->createMock('\\Drupal\\Core\\Extension\\ModuleHandlerInterface'));
  \Drupal::setContainer($container);

  // Create the tables and load them up with data, skipping empty ones.
  foreach (array_filter($database_contents) as $table => $rows) {
    $pilot_row = reset($rows);
    $connection
      ->schema()
      ->createTable($table, $this
      ->createSchemaFromRow($pilot_row));
    $insert = $connection
      ->insert($table)
      ->fields(array_keys($pilot_row));
    array_walk($rows, [
      $insert,
      'values',
    ]);
    $insert
      ->execute();
  }
  return $connection;
}