function QueryBatchTest::getDatabase

Same name in other branches
  1. 8.9.x core/modules/migrate/tests/src/Kernel/QueryBatchTest.php \Drupal\Tests\migrate\Kernel\QueryBatchTest::getDatabase()
  2. 10 core/modules/migrate/tests/src/Kernel/QueryBatchTest.php \Drupal\Tests\migrate\Kernel\QueryBatchTest::getDatabase()
  3. 11.x core/modules/migrate/tests/src/Kernel/QueryBatchTest.php \Drupal\Tests\migrate\Kernel\QueryBatchTest::getDatabase()

Builds an in-memory SQLite database from a set of source data.

Parameters

array $source_data: The source data, keyed by table name. Each table is an array containing the rows in that table.

Return value

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

1 call to QueryBatchTest::getDatabase()
QueryBatchTest::testQueryBatch in core/modules/migrate/tests/src/Kernel/QueryBatchTest.php
Tests query batch size.

File

core/modules/migrate/tests/src/Kernel/QueryBatchTest.php, line 230

Class

QueryBatchTest
Tests query batching.

Namespace

Drupal\Tests\migrate\Kernel

Code

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, [
            // SQLite uses loose affinity typing, so it's OK for every field to
            // be a text field.
'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;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.