3 namespace Drupal\Tests\migrate\Kernel;
5 use Drupal\KernelTests\KernelTestBase;
6 use Drupal\migrate\Plugin\MigrateIdMapInterface;
7 use Drupal\migrate\Plugin\MigrationInterface;
8 use Drupal\migrate\Row;
11 * Base class for tests of Migrate source plugins.
13 abstract class MigrateSourceTestBase extends KernelTestBase {
18 public static $modules = ['migrate'];
21 * The mocked migration.
23 * @var \Drupal\migrate\Plugin\MigrationInterface|\Prophecy\Prophecy\ObjectProphecy
28 * The source plugin under test.
30 * @var \Drupal\migrate\Plugin\MigrateSourceInterface
37 * @see \Drupal\Tests\migrate\Kernel\MigrateSourceTestBase::testSource
40 * Array of data sets to test, each of which is a numerically indexed array
41 * with the following elements:
42 * - An array of source data, which can be optionally processed and set up
44 * - An array of expected result rows.
45 * - (optional) The number of result rows the plugin under test is expected
46 * to return. If this is not a numeric value, the plugin will not be
48 * - (optional) Array of configuration options for the plugin under test.
50 abstract public function providerSource();
55 protected function setUp() {
58 // Create a mock migration. This will be injected into the source plugin
60 $this->migration = $this->prophesize(MigrationInterface::class);
62 $this->migration->id()->willReturn(
63 $this->randomMachineName(16)
65 // Prophesize a useless ID map plugin and an empty set of destination IDs.
66 // Calling code can override these prophecies later and set up different
68 $this->migration->getIdMap()->willReturn(
69 $this->prophesize(MigrateIdMapInterface::class)->reveal()
71 $this->migration->getDestinationIds()->willReturn([]);
75 * Determines the plugin to be tested by reading the class @covers annotation.
79 protected function getPluginClass() {
80 $annotations = $this->getAnnotations();
82 if (isset($annotations['class']['covers'])) {
83 return $annotations['class']['covers'][0];
86 $this->fail('No plugin class was specified');
91 * Instantiates the source plugin under test.
93 * @param array $configuration
94 * The source plugin configuration.
96 * @return \Drupal\migrate\Plugin\MigrateSourceInterface|object
97 * The fully configured source plugin.
99 protected function getPlugin(array $configuration) {
100 // Only create the plugin once per test.
102 return $this->plugin;
105 $class = ltrim($this->getPluginClass(), '\\');
107 /** @var \Drupal\migrate\Plugin\MigratePluginManager $plugin_manager */
108 $plugin_manager = $this->container->get('plugin.manager.migrate.source');
110 foreach ($plugin_manager->getDefinitions() as $id => $definition) {
111 if (ltrim($definition['class'], '\\') == $class) {
112 $this->plugin = $plugin_manager
113 ->createInstance($id, $configuration, $this->migration->reveal());
117 ->willReturn($this->plugin);
119 return $this->plugin;
122 $this->fail('No plugin found for class ' . $class);
126 * Tests the source plugin against a particular data set.
128 * @param array $source_data
129 * The source data that the source plugin will read.
130 * @param array $expected_data
131 * The result rows the source plugin is expected to return.
132 * @param mixed $expected_count
133 * (optional) How many rows the source plugin is expected to return.
134 * Defaults to count($expected_data). If set to a non-null, non-numeric
135 * value (like FALSE or 'nope'), the source plugin will not be counted.
136 * @param array $configuration
137 * (optional) Configuration for the source plugin.
138 * @param mixed $high_water
139 * (optional) The value of the high water field.
141 * @dataProvider providerSource
143 public function testSource(array $source_data, array $expected_data, $expected_count = NULL, array $configuration = [], $high_water = NULL) {
144 $plugin = $this->getPlugin($configuration);
146 // All source plugins must define IDs.
147 $this->assertNotEmpty($plugin->getIds());
149 // If there is a high water mark, set it in the high water storage.
150 if (isset($high_water)) {
153 ->get('migrate:high_water')
154 ->set($this->migration->reveal()->id(), $high_water);
157 if (is_null($expected_count)) {
158 $expected_count = count($expected_data);
160 // If an expected count was given, assert it only if the plugin is
162 if (is_numeric($expected_count)) {
163 $this->assertInstanceOf('\Countable', $plugin);
164 $this->assertCount($expected_count, $plugin);
168 /** @var \Drupal\migrate\Row $row */
169 foreach ($plugin as $row) {
170 $this->assertInstanceOf(Row::class, $row);
172 $expected = $expected_data[$i++];
173 $actual = $row->getSource();
175 foreach ($expected as $key => $value) {
176 $this->assertArrayHasKey($key, $actual);
178 if (is_array($value)) {
180 ksort($actual[$key]);
181 $this->assertEquals($value, $actual[$key]);
184 $this->assertSame((string) $value, (string) $actual[$key]);
188 // False positives occur if the foreach is not entered. So, confirm the
189 // foreach loop was entered if the expected count is greater than 0.
190 if ($expected_count > 0) {
191 $this->assertGreaterThan(0, $i);