You are here

protected function MigrateSourceTest::getSource in Drupal 10

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

Gets the source plugin to test.

Parameters

array $configuration: (optional) The source configuration. Defaults to an empty array.

array $migrate_config: (optional) The migration configuration to be used in parent::getMigration(). Defaults to an empty array.

int $status: (optional) The default status for the new rows to be imported. Defaults to MigrateIdMapInterface::STATUS_NEEDS_UPDATE.

int $high_water_value: (optional) The high water mark to start from, if set.

Return value

\Drupal\migrate\Plugin\MigrateSourceInterface A mocked source plugin.

File

core/modules/migrate/tests/src/Unit/MigrateSourceTest.php, line 84
Contains \Drupal\Tests\migrate\Unit\MigrateSourceTest.

Class

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

Namespace

Drupal\Tests\migrate\Unit

Code

protected function getSource($configuration = [], $migrate_config = [], $status = MigrateIdMapInterface::STATUS_NEEDS_UPDATE, $high_water_value = NULL) {
  $container = new ContainerBuilder();
  \Drupal::setContainer($container);
  $key_value = $this
    ->createMock(KeyValueStoreInterface::class);
  $key_value_factory = $this
    ->createMock(KeyValueFactoryInterface::class);
  $key_value_factory
    ->method('get')
    ->with('migrate:high_water')
    ->willReturn($key_value);
  $container
    ->set('keyvalue', $key_value_factory);
  $container
    ->set('cache.migrate', $this
    ->createMock(CacheBackendInterface::class));
  $this->migrationConfiguration = $this->defaultMigrationConfiguration + $migrate_config;
  $this->migration = parent::getMigration();
  $this->executable = $this
    ->getMigrateExecutable($this->migration);

  // Update the idMap for Source so the default is that the row has already
  // been imported. This allows us to use the highwater mark to decide on the
  // outcome of whether we choose to import the row.
  $id_map_array = [
    'original_hash' => '',
    'hash' => '',
    'source_row_status' => $status,
  ];
  $this->idMap
    ->expects($this
    ->any())
    ->method('getRowBySource')
    ->willReturn($id_map_array);
  $constructor_args = [
    $configuration,
    'd6_action',
    [],
    $this->migration,
  ];
  $methods = [
    'getModuleHandler',
    'fields',
    'getIds',
    '__toString',
    'prepareRow',
    'initializeIterator',
  ];
  $source_plugin = $this
    ->getMockBuilder(SourcePluginBase::class)
    ->onlyMethods($methods)
    ->setConstructorArgs($constructor_args)
    ->getMock();
  $source_plugin
    ->method('fields')
    ->willReturn([]);
  $source_plugin
    ->method('getIds')
    ->willReturn([]);
  $source_plugin
    ->method('__toString')
    ->willReturn('');
  $source_plugin
    ->method('prepareRow')
    ->willReturn(empty($migrate_config['prepare_row_false']));
  $rows = [
    $this->row,
  ];
  if (isset($configuration['high_water_property']) && isset($high_water_value)) {
    $property = $configuration['high_water_property']['name'];
    $rows = array_filter($rows, function (array $row) use ($property, $high_water_value) {
      return $row[$property] >= $high_water_value;
    });
  }
  $iterator = new \ArrayIterator($rows);
  $source_plugin
    ->method('initializeIterator')
    ->willReturn($iterator);
  $module_handler = $this
    ->createMock(ModuleHandlerInterface::class);
  $source_plugin
    ->method('getModuleHandler')
    ->willReturn($module_handler);
  $this->migration
    ->method('getSourcePlugin')
    ->willReturn($source_plugin);
  return $source_plugin;
}