Same name and namespace in other branches
  1. 8.9.x core/modules/migrate/tests/src/Unit/MigrateSourceTest.php \Drupal\Tests\migrate\Unit\MigrateSourceTest::testCount()
  2. 9 core/modules/migrate/tests/src/Unit/MigrateSourceTest.php \Drupal\Tests\migrate\Unit\MigrateSourceTest::testCount()

Tests that the source count is correct.

@covers ::count

File

core/modules/migrate/tests/src/Unit/MigrateSourceTest.php, line 168

Class

MigrateSourceTest
@coversDefaultClass \Drupal\migrate\Plugin\migrate\source\SourcePluginBase @group migrate

Namespace

Drupal\Tests\migrate\Unit

Code

public function testCount() {

  // Mock the cache to validate set() receives appropriate arguments.
  $container = new ContainerBuilder();
  $cache = $this
    ->createMock(CacheBackendInterface::class);
  $cache
    ->expects($this
    ->any())
    ->method('set')
    ->with($this
    ->isType('string'), $this
    ->isType('int'), $this
    ->isType('int'));
  $container
    ->set('cache.migrate', $cache);
  \Drupal::setContainer($container);

  // Test that the basic count works.
  $source = $this
    ->getSource();
  $this
    ->assertEquals(1, $source
    ->count());

  // Test caching the count works.
  $source = $this
    ->getSource([
    'cache_counts' => TRUE,
  ]);
  $this
    ->assertEquals(1, $source
    ->count());

  // Test the skip argument.
  $source = $this
    ->getSource([
    'skip_count' => TRUE,
  ]);
  $this
    ->assertEquals(MigrateSourceInterface::NOT_COUNTABLE, $source
    ->count());
  $this->migrationConfiguration['id'] = 'test_migration';
  $migration = $this
    ->getMigration();
  $source = new StubSourceGeneratorPlugin([], '', [], $migration);

  // Test the skipCount property's default value.
  $this
    ->assertEquals(MigrateSourceInterface::NOT_COUNTABLE, $source
    ->count());

  // Test the count value using a generator.
  $source = new StubSourceGeneratorPlugin([
    'skip_count' => FALSE,
  ], '', [], $migration);
  $this
    ->assertEquals(3, $source
    ->count());
}