You are here

protected function MigrateTestCase::getDatabase in Zircon Profile 8

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

Get 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.

Return value

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

4 calls to MigrateTestCase::getDatabase()
Drupal6SqlBaseTest::setUp in core/modules/migrate_drupal/tests/src/Unit/source/d6/Drupal6SqlBaseTest.php
MigrateSqlIdMapTest::setUp in core/modules/migrate/tests/src/Unit/MigrateSqlIdMapTest.php
MigrateSqlIdMapTest::testGetRowBySource in core/modules/migrate/tests/src/Unit/MigrateSqlIdMapTest.php
Tests the getRowBySource method.
MigrateSqlSourceTestCase::setUp in core/modules/migrate/tests/src/Unit/MigrateSqlSourceTestCase.php

File

core/modules/migrate/tests/src/Unit/MigrateTestCase.php, line 107
Contains \Drupal\Tests\migrate\Unit\MigrateTestCase.

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
    ->getMock('\\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;
}