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