View source
<?php
namespace Drupal\Tests\migrate\Unit\process;
use Drupal\migrate\MigrateSkipProcessException;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\Plugin\migrate\process\MigrationLookup;
use Drupal\migrate\Plugin\MigrateIdMapInterface;
use Drupal\migrate\Plugin\MigrationPluginManagerInterface;
use Prophecy\Argument;
class MigrationLookupTest extends MigrationLookupTestCase {
public function testTransformWithStubSkipping() {
$migration_plugin = $this
->prophesize(MigrationInterface::class);
$migration_plugin_manager = $this
->prophesize(MigrationPluginManagerInterface::class);
$destination_id_map = $this
->prophesize(MigrateIdMapInterface::class);
$destination_migration = $this
->prophesize(MigrationInterface::class);
$destination_migration
->getIdMap()
->willReturn($destination_id_map
->reveal());
$destination_id_map
->lookupDestinationIds([
1,
])
->willReturn(NULL);
$migration_plugin_manager
->createInstances(Argument::exact([
'destination_migration',
]))
->willReturn([
'destination_migration' => $destination_migration
->reveal(),
]);
$configuration = [
'no_stub' => TRUE,
'migration' => 'destination_migration',
];
$migration_plugin
->id()
->willReturn('actual_migration');
$destination_migration
->getDestinationPlugin(TRUE)
->shouldNotBeCalled();
$migration = MigrationLookup::create($this
->prepareContainer(), $configuration, '', [], $migration_plugin
->reveal());
$result = $migration
->transform(1, $this->migrateExecutable, $this->row, '');
$this
->assertNull($result);
}
public function testTransformWithStubbing() {
$migration_plugin = $this
->prophesize(MigrationInterface::class);
$this->migrateLookup
->lookup('destination_migration', [
1,
])
->willReturn(NULL);
$this->migrateStub
->createStub('destination_migration', [
1,
], [], FALSE)
->willReturn([
2,
]);
$configuration = [
'no_stub' => FALSE,
'migration' => 'destination_migration',
];
$migration = MigrationLookup::create($this
->prepareContainer(), $configuration, '', [], $migration_plugin
->reveal());
$result = $migration
->transform(1, $this->migrateExecutable, $this->row, '');
$this
->assertEquals(2, $result);
}
public function testSkipInvalid($value) {
$migration_plugin = $this
->prophesize(MigrationInterface::class);
$migration_plugin_manager = $this
->prophesize(MigrationPluginManagerInterface::class);
$configuration = [
'migration' => 'foobaz',
];
$migration_plugin
->id()
->willReturn(uniqid());
$migration_plugin_manager
->createInstances([
'foobaz',
])
->willReturn([
'foobaz' => $migration_plugin
->reveal(),
]);
$migration = MigrationLookup::create($this
->prepareContainer(), $configuration, '', [], $migration_plugin
->reveal());
$this
->expectException(MigrateSkipProcessException::class);
$migration
->transform($value, $this->migrateExecutable, $this->row, 'foo');
}
public function skipInvalidDataProvider() {
return [
'Empty String' => [
'',
],
'Boolean False' => [
FALSE,
],
'Empty Array' => [
[],
],
'Null' => [
NULL,
],
];
}
public function testNoSkipValid($value) {
$migration_plugin = $this
->prophesize(MigrationInterface::class);
$migration_plugin_manager = $this
->prophesize(MigrationPluginManagerInterface::class);
$process_plugin_manager = $this
->prophesize(MigratePluginManager::class);
$id_map = $this
->prophesize(MigrateIdMapInterface::class);
$id_map
->lookupDestinationIds([
$value,
])
->willReturn([]);
$migration_plugin
->getIdMap()
->willReturn($id_map
->reveal());
$configuration = [
'migration' => 'foobaz',
'no_stub' => TRUE,
];
$migration_plugin
->id()
->willReturn(uniqid());
$migration_plugin_manager
->createInstances([
'foobaz',
])
->willReturn([
'foobaz' => $migration_plugin
->reveal(),
]);
$migration = MigrationLookup::create($this
->prepareContainer(), $configuration, '', [], $migration_plugin
->reveal());
$lookup = $migration
->transform($value, $this->migrateExecutable, $this->row, 'foo');
$this
->assertNull($lookup);
}
public function noSkipValidDataProvider() {
return [
'Integer Zero' => [
0,
],
'String Zero' => [
'0',
],
'Float Zero' => [
0.0,
],
];
}
public function testSuccessfulLookup(array $source_id_values, array $destination_id_values, $source_value, $expected_value) {
$migration_plugin = $this
->prophesize(MigrationInterface::class);
$this->migrateLookup
->lookup('foobaz', $source_id_values)
->willReturn([
$destination_id_values,
]);
$configuration = [
'migration' => 'foobaz',
];
$migration = MigrationLookup::create($this
->prepareContainer(), $configuration, '', [], $migration_plugin
->reveal());
$this
->assertSame($expected_value, $migration
->transform($source_value, $this->migrateExecutable, $this->row, 'foo'));
}
public function successfulLookupDataProvider() {
return [
[
[
1,
],
[
3,
],
1,
3,
],
[
[
0,
],
[
3,
],
0,
3,
],
[
[
1,
],
[
3,
'foo',
],
1,
[
3,
'foo',
],
],
[
[
1,
3,
],
[
'foo',
],
[
1,
3,
],
'foo',
],
[
[
1,
3,
],
[
3,
'foo',
],
[
1,
3,
],
[
3,
'foo',
],
],
];
}
public function testMultipleSourceIds() {
$migration_plugin = $this
->prophesize(MigrationInterface::class);
$this->migrateLookup
->lookup('foobaz', [
'id',
6,
])
->willReturn([
[
2,
],
]);
$configuration = [
'migration' => 'foobaz',
];
$migration = MigrationLookup::create($this
->prepareContainer(), $configuration, '', [], $migration_plugin
->reveal());
$result = $migration
->transform([
'id',
6,
], $this->migrateExecutable, $this->row, '');
$this
->assertEquals(2, $result);
}
}