You are here

protected function MediaMigrationSourceTestBase::assertPluginCountCacheability in Media Migration 8

Tests the cacheablility of the given source plugin.

Parameters

\Drupal\migrate\Plugin\MigrateSourceInterface $plugin: The source plugin instance.

int $expected_count: The expected source record count.

string|null $expected_cache_key: The expected cache key (if any). Defaults to NULL.

1 call to MediaMigrationSourceTestBase::assertPluginCountCacheability()
MediaMigrationSourceTestBase::testSource in tests/src/Kernel/Plugin/migrate/source/d7/MediaMigrationSourceTestBase.php
@dataProvider providerSource

File

tests/src/Kernel/Plugin/migrate/source/d7/MediaMigrationSourceTestBase.php, line 131

Class

MediaMigrationSourceTestBase
Base class for testing media migrate source plugins with native databases.

Namespace

Drupal\Tests\media_migration\Kernel\Plugin\migrate\source\d7

Code

protected function assertPluginCountCacheability(MigrateSourceInterface $plugin, int $expected_count, ?string $expected_cache_key) {

  /** @var \Drupal\Core\Cache\MemoryCounterBackend $cache **/
  $cache = \Drupal::cache('migrate');
  if (!is_callable([
    $cache,
    'getCounter',
  ])) {
    return;
  }
  if ($expected_cache_key) {

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

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