You are here

public function MigrateSourceTestBase::testSource in Drupal 8

Same name and namespace in other branches
  1. 9 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 143

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) {
  $plugin = $this
    ->getPlugin($configuration);

  // 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);
  }
}