Issue #2867340 by Jo Fitzgerald, boaloysius, Cottser: Improve readability of Migration process plugin tests

8.4.x
Gabor Hojtsy 2017-04-24 15:29:34 -04:00
parent 963f01fbe9
commit d66aaae6c7
1 changed files with 79 additions and 80 deletions

View File

@ -19,21 +19,41 @@ use Prophecy\Argument;
*/ */
class MigrationTest extends MigrateProcessTestCase { class MigrationTest extends MigrateProcessTestCase {
/**
* @var \Drupal\migrate\Plugin\MigrationInterface
*/
protected $migration_plugin;
/**
* @var \Drupal\migrate\Plugin\MigrationPluginManagerInterface
*/
protected $migration_plugin_manager;
/**
* @var \Drupal\migrate\Plugin\MigratePluginManager
*/
protected $process_plugin_manager;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->migration_plugin = $this->prophesize(MigrationInterface::class);
$this->migration_plugin_manager = $this->prophesize(MigrationPluginManagerInterface::class);
$this->process_plugin_manager = $this->prophesize(MigratePluginManager::class);
}
/** /**
* @covers ::transform * @covers ::transform
*/ */
public function testTransformWithStubSkipping() { public function testTransformWithStubSkipping() {
$migration_plugin = $this->prophesize(MigrationInterface::class); $destination_migration = $this->getMigration();
$migration_plugin_manager = $this->prophesize(MigrationPluginManagerInterface::class); $destination_migration->getDestinationPlugin(TRUE)->shouldNotBeCalled();
$process_plugin_manager = $this->prophesize(MigratePluginManager::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->lookupDestinationId([1])->willReturn(NULL);
// Ensure the migration plugin manager returns our migration. // Ensure the migration plugin manager returns our migration.
$migration_plugin_manager->createInstances(Argument::exact(['destination_migration'])) $this->migration_plugin_manager->createInstances(Argument::exact(['destination_migration']))
->willReturn(['destination_migration' => $destination_migration->reveal()]); ->willReturn(['destination_migration' => $destination_migration->reveal()]);
$configuration = [ $configuration = [
@ -41,10 +61,9 @@ class MigrationTest extends MigrateProcessTestCase {
'migration' => 'destination_migration', 'migration' => 'destination_migration',
]; ];
$migration_plugin->id()->willReturn('actual_migration'); $this->migration_plugin->id()->willReturn('actual_migration');
$destination_migration->getDestinationPlugin(TRUE)->shouldNotBeCalled();
$migration = new Migration($configuration, '', [], $migration_plugin->reveal(), $migration_plugin_manager->reveal(), $process_plugin_manager->reveal()); $migration = new Migration($configuration, '', [], $this->migration_plugin->reveal(), $this->migration_plugin_manager->reveal(), $this->process_plugin_manager->reveal());
$result = $migration->transform(1, $this->migrateExecutable, $this->row, ''); $result = $migration->transform(1, $this->migrateExecutable, $this->row, '');
$this->assertNull($result); $this->assertNull($result);
} }
@ -53,24 +72,16 @@ class MigrationTest extends MigrateProcessTestCase {
* @covers ::transform * @covers ::transform
*/ */
public function testTransformWithStubbing() { public function testTransformWithStubbing() {
$migration_plugin = $this->prophesize(MigrationInterface::class); $destination_migration = $this->getMigration();
$migration_plugin_manager = $this->prophesize(MigrationPluginManagerInterface::class); $this->migration_plugin_manager->createInstances(['destination_migration'])
$process_plugin_manager = $this->prophesize(MigratePluginManager::class);
$destination_id_map = $this->prophesize(MigrateIdMapInterface::class);
$destination_migration = $this->prophesize('Drupal\migrate\Plugin\Migration');
$destination_migration->getIdMap()->willReturn($destination_id_map->reveal());
$migration_plugin_manager->createInstances(['destination_migration'])
->willReturn(['destination_migration' => $destination_migration->reveal()]); ->willReturn(['destination_migration' => $destination_migration->reveal()]);
$destination_id_map->lookupDestinationId([1])->willReturn(NULL);
$destination_id_map->saveIdMapping(Argument::any(), Argument::any(), MigrateIdMapInterface::STATUS_NEEDS_UPDATE)->willReturn(NULL);
$configuration = [ $configuration = [
'no_stub' => FALSE, 'no_stub' => FALSE,
'migration' => 'destination_migration', 'migration' => 'destination_migration',
]; ];
$migration_plugin->id()->willReturn('actual_migration'); $this->migration_plugin->id()->willReturn('actual_migration');
$destination_migration->id()->willReturn('destination_migration'); $destination_migration->id()->willReturn('destination_migration');
$destination_migration->getDestinationPlugin(TRUE)->shouldBeCalled(); $destination_migration->getDestinationPlugin(TRUE)->shouldBeCalled();
$destination_migration->getProcess()->willReturn([]); $destination_migration->getProcess()->willReturn([]);
@ -83,24 +94,36 @@ class MigrationTest extends MigrateProcessTestCase {
$destination_plugin->import(Argument::any())->willReturn([2]); $destination_plugin->import(Argument::any())->willReturn([2]);
$destination_migration->getDestinationPlugin(TRUE)->willReturn($destination_plugin->reveal()); $destination_migration->getDestinationPlugin(TRUE)->willReturn($destination_plugin->reveal());
$migration = new Migration($configuration, '', [], $migration_plugin->reveal(), $migration_plugin_manager->reveal(), $process_plugin_manager->reveal()); $migration = new Migration($configuration, '', [], $this->migration_plugin->reveal(), $this->migration_plugin_manager->reveal(), $this->process_plugin_manager->reveal());
$result = $migration->transform(1, $this->migrateExecutable, $this->row, ''); $result = $migration->transform(1, $this->migrateExecutable, $this->row, '');
$this->assertEquals(2, $result); $this->assertEquals(2, $result);
} }
/**
* Creates a mock Migration instance.
*
* @return \Prophecy\Prophecy\ObjectProphecy
* A mock Migration instance.
*/
protected function getMigration() {
$id_map = $this->prophesize(MigrateIdMapInterface::class);
$id_map->lookupDestinationId([1])->willReturn(NULL);
$id_map->saveIdMapping(Argument::any(), Argument::any(), MigrateIdMapInterface::STATUS_NEEDS_UPDATE)->willReturn(NULL);
$migration = $this->prophesize(MigrationInterface::class);
$migration->getIdMap()->willReturn($id_map->reveal());
return $migration;
}
/** /**
* Tests that processing is skipped when the input value is empty. * Tests that processing is skipped when the input value is empty.
*/ */
public function testSkipOnEmpty() { public function testSkipOnEmpty() {
$migration_plugin = $this->prophesize(MigrationInterface::class);
$migration_plugin_manager = $this->prophesize(MigrationPluginManagerInterface::class);
$process_plugin_manager = $this->prophesize(MigratePluginManager::class);
$configuration = [ $configuration = [
'migration' => 'foobaz', 'migration' => 'foobaz',
]; ];
$migration_plugin->id()->willReturn(uniqid()); $this->migration_plugin->id()->willReturn(uniqid());
$migration = new Migration($configuration, 'migration', [], $migration_plugin->reveal(), $migration_plugin_manager->reveal(), $process_plugin_manager->reveal()); $migration = new Migration($configuration, 'migration', [], $this->migration_plugin->reveal(), $this->migration_plugin_manager->reveal(), $this->process_plugin_manager->reveal());
$this->setExpectedException(MigrateSkipProcessException::class); $this->setExpectedException(MigrateSkipProcessException::class);
$migration->transform(0, $this->migrateExecutable, $this->row, 'foo'); $migration->transform(0, $this->migrateExecutable, $this->row, 'foo');
} }
@ -120,28 +143,24 @@ class MigrationTest extends MigrateProcessTestCase {
* The expected value(s) of the migration process plugin. * The expected value(s) of the migration process plugin.
*/ */
public function testSuccessfulLookup($source_id_values, $destination_id_values, $source_value, $expected_value) { public function testSuccessfulLookup($source_id_values, $destination_id_values, $source_value, $expected_value) {
$migration_plugin = $this->prophesize(MigrationInterface::class);
$migration_plugin_manager = $this->prophesize(MigrationPluginManagerInterface::class);
$process_plugin_manager = $this->prophesize(MigratePluginManager::class);
$configuration = [ $configuration = [
'migration' => 'foobaz', 'migration' => 'foobaz',
]; ];
$migration_plugin->id()->willReturn(uniqid()); $this->migration_plugin->id()->willReturn(uniqid());
$id_map = $this->prophesize(MigrateIdMapInterface::class); $id_map = $this->prophesize(MigrateIdMapInterface::class);
$id_map->lookupDestinationId($source_id_values)->willReturn($destination_id_values); $id_map->lookupDestinationId($source_id_values)->willReturn($destination_id_values);
$migration_plugin->getIdMap()->willReturn($id_map->reveal()); $this->migration_plugin->getIdMap()->willReturn($id_map->reveal());
$migration_plugin_manager->createInstances(['foobaz']) $this->migration_plugin_manager->createInstances(['foobaz'])
->willReturn(['foobaz' => $migration_plugin->reveal()]); ->willReturn(['foobaz' => $this->migration_plugin->reveal()]);
$migrationStorage = $this->prophesize(EntityStorageInterface::class); $migrationStorage = $this->prophesize(EntityStorageInterface::class);
$migrationStorage $migrationStorage
->loadMultiple(['foobaz']) ->loadMultiple(['foobaz'])
->willReturn([$migration_plugin->reveal()]); ->willReturn([$this->migration_plugin->reveal()]);
$migration = new Migration($configuration, 'migration', [], $migration_plugin->reveal(), $migration_plugin_manager->reveal(), $process_plugin_manager->reveal()); $migration = new Migration($configuration, 'migration', [], $this->migration_plugin->reveal(), $this->migration_plugin_manager->reveal(), $this->process_plugin_manager->reveal());
$this->assertSame($expected_value, $migration->transform($source_value, $this->migrateExecutable, $this->row, 'foo')); $this->assertSame($expected_value, $migration->transform($source_value, $this->migrateExecutable, $this->row, 'foo'));
} }
@ -152,49 +171,29 @@ class MigrationTest extends MigrateProcessTestCase {
*/ */
public function successfulLookupDataProvider() { public function successfulLookupDataProvider() {
return [ return [
// Test data for scalar to scalar. 'scalar_to_scalar' => [
[ 'source_ids' => [1],
// Source ID of the migration map. 'destination_ids' => [3],
[1], 'input_value' => 1,
// Destination ID of the migration map. 'expected_value' => 3,
[3],
// Input value for the migration plugin.
1,
// Expected output value of the migration plugin.
3,
], ],
// Test data for scalar to array. 'scalar_to_array' => [
[ 'source_ids' => [1],
// Source ID of the migration map. 'destination_ids' => [3, 'foo'],
[1], 'input_value' => 1,
// Destination IDs of the migration map. 'expected_value' => [3, 'foo'],
[3, 'foo'],
// Input value for the migration plugin.
1,
// Expected output values of the migration plugin.
[3, 'foo'],
], ],
// Test data for array to scalar. 'array_to_scalar' => [
[ 'source_ids' => [1, 3],
// Source IDs of the migration map. 'destination_ids' => ['foo'],
[1, 3], 'input_value' => [1, 3],
// Destination ID of the migration map. 'expected_value' => 'foo',
['foo'],
// Input values for the migration plugin.
[1, 3],
// Expected output value of the migration plugin.
'foo',
], ],
// Test data for array to array. 'array_to_array' => [
[ 'source_ids' => [1, 3],
// Source IDs of the migration map. 'destination_ids' => [3, 'foo'],
[1, 3], 'input_value' => [1, 3],
// Destination IDs of the migration map. 'expected_value' => [3, 'foo'],
[3, 'foo'],
// Input values for the migration plugin.
[1, 3],
// Expected output values of the migration plugin.
[3, 'foo'],
], ],
]; ];
} }