function MigrateSourceTestBase::testSource

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

Tests the source plugin against a particular data set.

@dataProvider providerSource

Parameters

array $source_data: The source data that the source plugin will read.

array $expected_data: The result rows the source plugin is expected to return.

mixed $expected_count: (optional) How many rows the source plugin is expected to return. Defaults to count($expected_data). If set to a non-null, non-numeric value (like FALSE or 'nope'), the source plugin will not be counted.

array $configuration: (optional) Configuration for the source plugin.

mixed $high_water: (optional) The value of the high water field.

1 call to MigrateSourceTestBase::testSource()
MigrateSqlSourceTestBase::testSource in core/modules/migrate/tests/src/Kernel/MigrateSqlSourceTestBase.php
Tests the source plugin against a particular data set.
1 method overrides MigrateSourceTestBase::testSource()
MigrateSqlSourceTestBase::testSource in core/modules/migrate/tests/src/Kernel/MigrateSqlSourceTestBase.php
Tests the source plugin against a particular data set.

File

core/modules/migrate/tests/src/Kernel/MigrateSourceTestBase.php, line 149

Class

MigrateSourceTestBase
Base class for tests of Migrate source plugins.

Namespace

Drupal\Tests\migrate\Kernel

Code

public function testSource(array $source_data, array $expected_data, $expected_count = NULL, array $configuration = [], $high_water = NULL) : void {
    $plugin = $this->getPlugin($configuration);
    $clone_plugin = clone $plugin;
    // All source plugins must define IDs.
    $this->assertNotEmpty($plugin->getIds());
    // If there is a high water mark, set it in the high water storage.
    if (isset($high_water)) {
        $this->container
            ->get('keyvalue')
            ->get('migrate:high_water')
            ->set($this->migration
            ->reveal()
            ->id(), $high_water);
    }
    if (is_null($expected_count)) {
        $expected_count = count($expected_data);
    }
    // If an expected count was given, assert it only if the plugin is
    // countable.
    if (is_numeric($expected_count)) {
        $this->assertInstanceOf('\\Countable', $plugin);
        $this->assertCount($expected_count, $plugin);
    }
    $i = 0;
    
    /** @var \Drupal\migrate\Row $row */
    foreach ($plugin as $row) {
        $this->assertInstanceOf(Row::class, $row);
        $expected = $expected_data[$i++];
        $actual = $row->getSource();
        foreach ($expected as $key => $value) {
            $this->assertArrayHasKey($key, $actual);
            $msg = sprintf("Value at 'array[%s][%s]' is not correct.", $i - 1, $key);
            if (is_array($value)) {
                ksort($value);
                ksort($actual[$key]);
                $this->assertEquals($value, $actual[$key], $msg);
            }
            else {
                $this->assertEquals((string) $value, (string) $actual[$key], $msg);
            }
        }
    }
    // False positives occur if the foreach is not entered. So, confirm the
    // foreach loop was entered if the expected count is greater than 0.
    if ($expected_count > 0) {
        $this->assertGreaterThan(0, $i);
        // Test that we can skip all rows.
        \Drupal::state()->set('migrate_skip_all_rows_test_migrate_prepare_row', TRUE);
        foreach ($clone_plugin as $row) {
            $this->fail('Row not skipped');
        }
    }
}

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