annotate core/modules/migrate/tests/src/Kernel/MigrateSqlSourceTestBase.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Tests\migrate\Kernel;
Chris@0 4
Chris@0 5 use Drupal\Core\Database\Driver\sqlite\Connection;
Chris@0 6
Chris@0 7 /**
Chris@0 8 * Base class for tests of Migrate source plugins that use a database.
Chris@0 9 */
Chris@0 10 abstract class MigrateSqlSourceTestBase extends MigrateSourceTestBase {
Chris@0 11
Chris@0 12 /**
Chris@0 13 * Builds an in-memory SQLite database from a set of source data.
Chris@0 14 *
Chris@0 15 * @param array $source_data
Chris@0 16 * The source data, keyed by table name. Each table is an array containing
Chris@0 17 * the rows in that table.
Chris@0 18 *
Chris@0 19 * @return \Drupal\Core\Database\Driver\sqlite\Connection
Chris@0 20 * The SQLite database connection.
Chris@0 21 */
Chris@0 22 protected function getDatabase(array $source_data) {
Chris@0 23 // Create an in-memory SQLite database. Plugins can interact with it like
Chris@0 24 // any other database, and it will cease to exist when the connection is
Chris@0 25 // closed.
Chris@0 26 $connection_options = ['database' => ':memory:'];
Chris@0 27 $pdo = Connection::open($connection_options);
Chris@0 28 $connection = new Connection($pdo, $connection_options);
Chris@0 29
Chris@0 30 // Create the tables and fill them with data.
Chris@0 31 foreach ($source_data as $table => $rows) {
Chris@0 32 // Use the biggest row to build the table schema.
Chris@0 33 $counts = array_map('count', $rows);
Chris@0 34 asort($counts);
Chris@0 35 end($counts);
Chris@0 36 $pilot = $rows[key($counts)];
Chris@0 37
Chris@0 38 $connection->schema()
Chris@0 39 ->createTable($table, [
Chris@0 40 // SQLite uses loose affinity typing, so it's OK for every field to
Chris@0 41 // be a text field.
Chris@0 42 'fields' => array_map(function () {
Chris@0 43 return ['type' => 'text'];
Chris@0 44 }, $pilot),
Chris@0 45 ]);
Chris@0 46
Chris@0 47 $fields = array_keys($pilot);
Chris@0 48 $insert = $connection->insert($table)->fields($fields);
Chris@0 49 array_walk($rows, [$insert, 'values']);
Chris@0 50 $insert->execute();
Chris@0 51 }
Chris@0 52
Chris@0 53 return $connection;
Chris@0 54 }
Chris@0 55
Chris@0 56 /**
Chris@0 57 * Tests the source plugin against a particular data set.
Chris@0 58 *
Chris@0 59 * @param array $source_data
Chris@0 60 * The source data that the plugin will read. See getDatabase() for the
Chris@0 61 * expected format.
Chris@0 62 * @param array $expected_data
Chris@0 63 * The result rows the plugin is expected to return.
Chris@0 64 * @param int $expected_count
Chris@0 65 * (optional) How many rows the source plugin is expected to return.
Chris@0 66 * @param array $configuration
Chris@0 67 * (optional) Configuration for the source plugin.
Chris@0 68 * @param mixed $high_water
Chris@0 69 * (optional) The value of the high water field.
Chris@0 70 *
Chris@0 71 * @dataProvider providerSource
Chris@0 72 *
Chris@0 73 * @requires extension pdo_sqlite
Chris@0 74 */
Chris@0 75 public function testSource(array $source_data, array $expected_data, $expected_count = NULL, array $configuration = [], $high_water = NULL) {
Chris@0 76 $plugin = $this->getPlugin($configuration);
Chris@0 77
Chris@0 78 // Since we don't yet inject the database connection, we need to use a
Chris@0 79 // reflection hack to set it in the plugin instance.
Chris@0 80 $reflector = new \ReflectionObject($plugin);
Chris@0 81 $property = $reflector->getProperty('database');
Chris@0 82 $property->setAccessible(TRUE);
Chris@0 83 $property->setValue($plugin, $this->getDatabase($source_data));
Chris@0 84
Chris@0 85 parent::testSource($source_data, $expected_data, $expected_count, $configuration, $high_water);
Chris@0 86 }
Chris@0 87
Chris@0 88 }