You are here

public function MigrateSqlSourceTestBase::testSource in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/migrate/tests/src/Kernel/MigrateSqlSourceTestBase.php \Drupal\Tests\migrate\Kernel\MigrateSqlSourceTestBase::testSource()

Tests the source plugin against a particular data set.

@dataProvider providerSource

@requires extension pdo_sqlite

Parameters

array $source_data: The source data that the plugin will read. See getDatabase() for the expected format.

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

int $expected_count: (optional) How many rows the source plugin is expected to return.

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

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

string|null $expected_cache_key: (optional) The expected cache key.

Overrides MigrateSourceTestBase::testSource

File

core/modules/migrate/tests/src/Kernel/MigrateSqlSourceTestBase.php, line 87

Class

MigrateSqlSourceTestBase
Base class for tests of Migrate source plugins that use a database.

Namespace

Drupal\Tests\migrate\Kernel

Code

public function testSource(array $source_data, array $expected_data, $expected_count = NULL, array $configuration = [], $high_water = NULL, $expected_cache_key = NULL) {
  $plugin = $this
    ->getPlugin($configuration);

  // Since we don't yet inject the database connection, we need to use a
  // reflection hack to set it in the plugin instance.
  $reflector = new \ReflectionObject($plugin);
  $property = $reflector
    ->getProperty('database');
  $property
    ->setAccessible(TRUE);
  $property
    ->setValue($plugin, $this
    ->getDatabase($source_data));

  /** @var MemoryCounterBackend $cache **/
  $cache = \Drupal::cache('migrate');
  if ($expected_cache_key) {

    // Verify the the computed cache key.
    $property = $reflector
      ->getProperty('cacheKey');
    $property
      ->setAccessible(TRUE);
    $this
      ->assertSame($expected_cache_key, $property
      ->getValue($plugin));

    // Cache miss prior to calling ::count().
    $this
      ->assertFalse($cache
      ->get($expected_cache_key, 'cache'));
    $this
      ->assertSame([], $cache
      ->getCounter('set'));
    $count = $plugin
      ->count();
    $this
      ->assertSame($expected_count, $count);
    $this
      ->assertSame([
      $expected_cache_key => 1,
    ], $cache
      ->getCounter('set'));

    // Cache hit afterwards.
    $cache_item = $cache
      ->get($expected_cache_key, 'cache');
    $this
      ->assertNotSame(FALSE, $cache_item, 'This is not a cache hit.');
    $this
      ->assertSame($expected_count, $cache_item->data);
  }
  else {
    $this
      ->assertSame([], $cache
      ->getCounter('set'));
    $plugin
      ->count();
    $this
      ->assertSame([], $cache
      ->getCounter('set'));
  }
  parent::testSource($source_data, $expected_data, $expected_count, $configuration, $high_water);
}