/**
  * Builds an in-memory SQLite database from a set of source data.
  *
  * @param array $source_data
  *   The source data, keyed by table name. Each table is an array containing
  *   the rows in that table.
  *
  * @return \Drupal\Core\Database\Driver\sqlite\Connection
  *   The SQLite database connection.
  */
 protected function getDatabase(array $source_data)
 {
     // Create an in-memory SQLite database. Plugins can interact with it like
     // any other database, and it will cease to exist when the connection is
     // closed.
     $connection_options = ['database' => ':memory:'];
     $pdo = Connection::open($connection_options);
     $connection = new Connection($pdo, $connection_options);
     // Create the tables and fill them with data.
     foreach ($source_data as $table => $rows) {
         // Use the biggest row to build the table schema.
         $counts = array_map('count', $rows);
         asort($counts);
         end($counts);
         $pilot = $rows[key($counts)];
         $connection->schema()->createTable($table, ['fields' => array_map(function () {
             return ['type' => 'text'];
         }, $pilot)]);
         $fields = array_keys($pilot);
         $insert = $connection->insert($table)->fields($fields);
         array_walk($rows, [$insert, 'values']);
         $insert->execute();
     }
     return $connection;
 }
Example #2
0
 /**
  * Get an SQLite database connection object for use in tests.
  *
  * @param 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.
  * @param array $connection_options
  *  (optional) Options for the database connection.
  *
  * @return \Drupal\Core\Database\Driver\sqlite\Connection
  *   The database connection.
  */
 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;
 }
Example #3
0
 /**
  * Tests the getQualifiedMapTable method with a prefixed database.
  */
 public function testGetQualifiedMapTablePrefix()
 {
     $connection_options = ['database' => ':memory:', 'prefix' => 'prefix'];
     $pdo = Connection::open($connection_options);
     $this->database = new Connection($pdo, $connection_options);
     $qualified_map_table = $this->getIdMap()->getQualifiedMapTableName();
     // The SQLite driver is a special flower. It will prefix tables with
     // PREFIX.TABLE, instead of the standard PREFIXTABLE.
     // @see \Drupal\Core\Database\Driver\sqlite\Connection::__construct()
     $this->assertEquals('prefix.migrate_map_sql_idmap_test', $qualified_map_table);
 }