class MigrateExecutableTest in Drupal 9
Same name in this branch
- 9 core/modules/migrate/tests/src/Unit/MigrateExecutableTest.php \Drupal\Tests\migrate\Unit\MigrateExecutableTest
- 9 core/modules/migrate/tests/src/Kernel/MigrateExecutableTest.php \Drupal\Tests\migrate\Kernel\MigrateExecutableTest
Same name and namespace in other branches
- 8 core/modules/migrate/tests/src/Unit/MigrateExecutableTest.php \Drupal\Tests\migrate\Unit\MigrateExecutableTest
@coversDefaultClass \Drupal\migrate\MigrateExecutable @group migrate
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses \Symfony\Bridge\PhpUnit\ExpectDeprecationTrait, PhpUnitCompatibilityTrait, PhpUnitWarnings
- class \Drupal\Tests\migrate\Unit\MigrateTestCase
- class \Drupal\Tests\migrate\Unit\MigrateExecutableTest
- class \Drupal\Tests\migrate\Unit\MigrateTestCase
Expanded class hierarchy of MigrateExecutableTest
File
- core/
modules/ migrate/ tests/ src/ Unit/ MigrateExecutableTest.php, line 16
Namespace
Drupal\Tests\migrate\UnitView source
class MigrateExecutableTest extends MigrateTestCase {
/**
* The mocked migration entity.
*
* @var \Drupal\migrate\Plugin\MigrationInterface|\PHPUnit\Framework\MockObject\MockObject
*/
protected $migration;
/**
* The mocked migrate message.
*
* @var \Drupal\migrate\MigrateMessageInterface|\PHPUnit\Framework\MockObject\MockObject
*/
protected $message;
/**
* The tested migrate executable.
*
* @var \Drupal\Tests\migrate\Unit\TestMigrateExecutable
*/
protected $executable;
/**
* The migration's configuration values.
*
* @var array
*/
protected $migrationConfiguration = [
'id' => 'test',
];
/**
* {@inheritdoc}
*/
protected function setUp() : void {
parent::setUp();
$this->migration = $this
->getMigration();
$this->message = $this
->createMock('Drupal\\migrate\\MigrateMessageInterface');
$event_dispatcher = $this
->createMock('Symfony\\Contracts\\EventDispatcher\\EventDispatcherInterface');
$this->executable = new TestMigrateExecutable($this->migration, $this->message, $event_dispatcher);
$this->executable
->setStringTranslation($this
->getStringTranslationStub());
}
/**
* Tests an import with an incomplete rewinding.
*/
public function testImportWithFailingRewind() {
$exception_message = $this
->getRandomGenerator()
->string();
$source = $this
->createMock('Drupal\\migrate\\Plugin\\MigrateSourceInterface');
$source
->expects($this
->once())
->method('rewind')
->will($this
->throwException(new \Exception($exception_message)));
// The exception message contains the line number where it is thrown. Save
// it for the testing the exception message.
$line = __LINE__ - 3;
$this->migration
->expects($this
->any())
->method('getSourcePlugin')
->will($this
->returnValue($source));
// Ensure that a message with the proper message was added.
$exception_message .= " in " . __FILE__ . " line {$line}";
$this->message
->expects($this
->once())
->method('display')
->with("Migration failed with source plugin exception: " . Html::escape($exception_message));
$result = $this->executable
->import();
$this
->assertEquals(MigrationInterface::RESULT_FAILED, $result);
}
/**
* Tests the import method with a valid row.
*/
public function testImportWithValidRow() {
$source = $this
->getMockSource();
$row = $this
->getMockBuilder('Drupal\\migrate\\Row')
->disableOriginalConstructor()
->getMock();
$row
->expects($this
->once())
->method('getSourceIdValues')
->will($this
->returnValue([
'id' => 'test',
]));
$this->idMap
->expects($this
->once())
->method('lookupDestinationIds')
->with([
'id' => 'test',
])
->will($this
->returnValue([
[
'test',
],
]));
$source
->expects($this
->once())
->method('current')
->will($this
->returnValue($row));
$this->executable
->setSource($source);
$this->migration
->expects($this
->once())
->method('getProcessPlugins')
->will($this
->returnValue([]));
$destination = $this
->createMock('Drupal\\migrate\\Plugin\\MigrateDestinationInterface');
$destination
->expects($this
->once())
->method('import')
->with($row, [
'test',
])
->will($this
->returnValue([
'id' => 'test',
]));
$this->migration
->method('getDestinationPlugin')
->willReturn($destination);
$this
->assertSame(MigrationInterface::RESULT_COMPLETED, $this->executable
->import());
}
/**
* Tests the import method with a valid row.
*/
public function testImportWithValidRowWithoutDestinationId() {
$source = $this
->getMockSource();
$row = $this
->getMockBuilder('Drupal\\migrate\\Row')
->disableOriginalConstructor()
->getMock();
$row
->expects($this
->once())
->method('getSourceIdValues')
->will($this
->returnValue([
'id' => 'test',
]));
$this->idMap
->expects($this
->once())
->method('lookupDestinationIds')
->with([
'id' => 'test',
])
->will($this
->returnValue([
[
'test',
],
]));
$source
->expects($this
->once())
->method('current')
->will($this
->returnValue($row));
$this->executable
->setSource($source);
$this->migration
->expects($this
->once())
->method('getProcessPlugins')
->will($this
->returnValue([]));
$destination = $this
->createMock('Drupal\\migrate\\Plugin\\MigrateDestinationInterface');
$destination
->expects($this
->once())
->method('import')
->with($row, [
'test',
])
->will($this
->returnValue(TRUE));
$this->migration
->method('getDestinationPlugin')
->willReturn($destination);
$this->idMap
->expects($this
->never())
->method('saveIdMapping');
$this
->assertSame(MigrationInterface::RESULT_COMPLETED, $this->executable
->import());
}
/**
* Tests the import method with a valid row.
*/
public function testImportWithValidRowNoDestinationValues() {
$source = $this
->getMockSource();
$row = $this
->getMockBuilder('Drupal\\migrate\\Row')
->disableOriginalConstructor()
->getMock();
$row
->expects($this
->once())
->method('getSourceIdValues')
->will($this
->returnValue([
'id' => 'test',
]));
$source
->expects($this
->once())
->method('current')
->will($this
->returnValue($row));
$this->executable
->setSource($source);
$this->migration
->expects($this
->once())
->method('getProcessPlugins')
->will($this
->returnValue([]));
$destination = $this
->createMock('Drupal\\migrate\\Plugin\\MigrateDestinationInterface');
$destination
->expects($this
->once())
->method('import')
->with($row, [
'test',
])
->will($this
->returnValue([]));
$this->migration
->method('getDestinationPlugin')
->willReturn($destination);
$this->idMap
->expects($this
->once())
->method('saveIdMapping')
->with($row, [], MigrateIdMapInterface::STATUS_FAILED, NULL);
$this->idMap
->expects($this
->once())
->method('messageCount')
->will($this
->returnValue(0));
$this->idMap
->expects($this
->once())
->method('saveMessage');
$this->idMap
->expects($this
->once())
->method('lookupDestinationIds')
->with([
'id' => 'test',
])
->will($this
->returnValue([
[
'test',
],
]));
$this->message
->expects($this
->once())
->method('display')
->with('New object was not saved, no error provided');
$this
->assertSame(MigrationInterface::RESULT_COMPLETED, $this->executable
->import());
}
/**
* Tests the import method with a thrown MigrateException.
*
* The MigrationException in this case is being thrown from the destination.
*/
public function testImportWithValidRowWithDestinationMigrateException() {
$exception_message = $this
->getRandomGenerator()
->string();
$source = $this
->getMockSource();
$row = $this
->getMockBuilder('Drupal\\migrate\\Row')
->disableOriginalConstructor()
->getMock();
$row
->expects($this
->once())
->method('getSourceIdValues')
->will($this
->returnValue([
'id' => 'test',
]));
$source
->expects($this
->once())
->method('current')
->will($this
->returnValue($row));
$this->executable
->setSource($source);
$this->migration
->expects($this
->once())
->method('getProcessPlugins')
->will($this
->returnValue([]));
$destination = $this
->createMock('Drupal\\migrate\\Plugin\\MigrateDestinationInterface');
$destination
->expects($this
->once())
->method('import')
->with($row, [
'test',
])
->will($this
->throwException(new MigrateException($exception_message)));
$this->migration
->method('getDestinationPlugin')
->willReturn($destination);
$this->idMap
->expects($this
->once())
->method('saveIdMapping')
->with($row, [], MigrateIdMapInterface::STATUS_FAILED, NULL);
$this->idMap
->expects($this
->once())
->method('saveMessage');
$this->idMap
->expects($this
->once())
->method('lookupDestinationIds')
->with([
'id' => 'test',
])
->will($this
->returnValue([
[
'test',
],
]));
$this
->assertSame(MigrationInterface::RESULT_COMPLETED, $this->executable
->import());
}
/**
* Tests the import method with a thrown MigrateException.
*
* The MigrationException in this case is being thrown from a process plugin.
*/
public function testImportWithValidRowWithProcesMigrateException() {
$exception_message = $this
->getRandomGenerator()
->string();
$source = $this
->getMockSource();
$row = $this
->getMockBuilder('Drupal\\migrate\\Row')
->disableOriginalConstructor()
->getMock();
$row
->expects($this
->once())
->method('getSourceIdValues')
->willReturn([
'id' => 'test',
]);
$source
->expects($this
->once())
->method('current')
->willReturn($row);
$this->executable
->setSource($source);
$this->migration
->expects($this
->once())
->method('getProcessPlugins')
->willThrowException(new MigrateException($exception_message));
$destination = $this
->createMock('Drupal\\migrate\\Plugin\\MigrateDestinationInterface');
$destination
->expects($this
->never())
->method('import');
$this->migration
->method('getDestinationPlugin')
->willReturn($destination);
$this->idMap
->expects($this
->once())
->method('saveIdMapping')
->with($row, [], MigrateIdMapInterface::STATUS_FAILED, NULL);
$this->idMap
->expects($this
->once())
->method('saveMessage');
$this->idMap
->expects($this
->never())
->method('lookupDestinationIds');
$this
->assertSame(MigrationInterface::RESULT_COMPLETED, $this->executable
->import());
}
/**
* Tests the import method with a regular Exception being thrown.
*/
public function testImportWithValidRowWithException() {
$exception_message = $this
->getRandomGenerator()
->string();
$source = $this
->getMockSource();
$row = $this
->getMockBuilder('Drupal\\migrate\\Row')
->disableOriginalConstructor()
->getMock();
$row
->expects($this
->once())
->method('getSourceIdValues')
->will($this
->returnValue([
'id' => 'test',
]));
$source
->expects($this
->once())
->method('current')
->will($this
->returnValue($row));
$this->executable
->setSource($source);
$this->migration
->expects($this
->once())
->method('getProcessPlugins')
->will($this
->returnValue([]));
$destination = $this
->createMock('Drupal\\migrate\\Plugin\\MigrateDestinationInterface');
$destination
->expects($this
->once())
->method('import')
->with($row, [
'test',
])
->will($this
->throwException(new \Exception($exception_message)));
$this->migration
->method('getDestinationPlugin')
->willReturn($destination);
$this->idMap
->expects($this
->once())
->method('saveIdMapping')
->with($row, [], MigrateIdMapInterface::STATUS_FAILED, NULL);
$this->idMap
->expects($this
->once())
->method('saveMessage');
$this->idMap
->expects($this
->once())
->method('lookupDestinationIds')
->with([
'id' => 'test',
])
->will($this
->returnValue([
[
'test',
],
]));
$this->message
->expects($this
->once())
->method('display')
->with($exception_message);
$this
->assertSame(MigrationInterface::RESULT_COMPLETED, $this->executable
->import());
}
/**
* Tests the processRow method.
*/
public function testProcessRow() {
$expected = [
'test' => 'test destination',
'test1' => 'test1 destination',
];
foreach ($expected as $key => $value) {
$plugins[$key][0] = $this
->createMock('Drupal\\migrate\\Plugin\\MigrateProcessInterface');
$plugins[$key][0]
->expects($this
->once())
->method('getPluginDefinition')
->will($this
->returnValue([]));
$plugins[$key][0]
->expects($this
->once())
->method('transform')
->will($this
->returnValue($value));
}
$this->migration
->expects($this
->once())
->method('getProcessPlugins')
->with(NULL)
->will($this
->returnValue($plugins));
$row = new Row();
$this->executable
->processRow($row);
foreach ($expected as $key => $value) {
$this
->assertSame($row
->getDestinationProperty($key), $value);
}
$this
->assertSameSize($expected, $row
->getDestination());
}
/**
* Tests the processRow method with an empty pipeline.
*/
public function testProcessRowEmptyPipeline() {
$this->migration
->expects($this
->once())
->method('getProcessPlugins')
->with(NULL)
->will($this
->returnValue([
'test' => [],
]));
$row = new Row();
$this->executable
->processRow($row);
$this
->assertSame($row
->getDestination(), []);
}
/**
* Tests the processRow pipeline exception.
*/
public function testProcessRowPipelineException() {
$row = new Row();
$plugin = $this
->prophesize(MigrateProcessInterface::class);
$plugin
->getPluginDefinition()
->willReturn([
'handle_multiples' => FALSE,
]);
$plugin
->transform(NULL, $this->executable, $row, 'destination_id')
->willReturn('transform_return_string');
$plugin
->multiple()
->willReturn(TRUE);
$plugin
->getPluginId()
->willReturn('plugin_id');
$plugin = $plugin
->reveal();
$plugins['destination_id'] = [
$plugin,
$plugin,
];
$this->migration
->method('getProcessPlugins')
->willReturn($plugins);
$this
->expectException(MigrateException::class);
$this
->expectExceptionMessage('Pipeline failed at plugin_id plugin for destination destination_id: transform_return_string received instead of an array,');
$this->executable
->processRow($row);
}
/**
* Tests the processRow method.
*/
public function testProcessRowEmptyDestination() {
$expected = [
'test' => 'test destination',
'test1' => 'test1 destination',
'test2' => NULL,
];
$row = new Row();
$plugins = [];
foreach ($expected as $key => $value) {
$plugin = $this
->prophesize(MigrateProcessInterface::class);
$plugin
->getPluginDefinition()
->willReturn([]);
$plugin
->transform(NULL, $this->executable, $row, $key)
->willReturn($value);
$plugin
->multiple()
->willReturn(TRUE);
$plugins[$key][0] = $plugin
->reveal();
}
$this->migration
->method('getProcessPlugins')
->willReturn($plugins);
$this->executable
->processRow($row);
foreach ($expected as $key => $value) {
$this
->assertSame($value, $row
->getDestinationProperty($key));
}
$this
->assertCount(2, $row
->getDestination());
$this
->assertSame([
'test2',
], $row
->getEmptyDestinationProperties());
}
/**
* Returns a mock migration source instance.
*
* @return \Drupal\migrate\Plugin\MigrateSourceInterface|\PHPUnit\Framework\MockObject\MockObject
* The mocked migration source.
*/
protected function getMockSource() {
$this
->createMock('\\Iterator');
$class = 'Drupal\\migrate\\Plugin\\migrate\\source\\SourcePluginBase';
$source = $this
->getMockBuilder($class)
->disableOriginalConstructor()
->setMethods(get_class_methods($class))
->getMockForAbstractClass();
$source
->expects($this
->once())
->method('rewind')
->will($this
->returnValue(TRUE));
$source
->expects($this
->any())
->method('initializeIterator')
->will($this
->returnValue([]));
$source
->expects($this
->any())
->method('valid')
->will($this
->onConsecutiveCalls(TRUE, FALSE));
return $source;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
MigrateExecutableTest:: |
protected | property | The tested migrate executable. | |
MigrateExecutableTest:: |
protected | property | The mocked migrate message. | |
MigrateExecutableTest:: |
protected | property | The mocked migration entity. | |
MigrateExecutableTest:: |
protected | property |
The migration's configuration values. Overrides MigrateTestCase:: |
|
MigrateExecutableTest:: |
protected | function | Returns a mock migration source instance. | |
MigrateExecutableTest:: |
protected | function |
Overrides UnitTestCase:: |
|
MigrateExecutableTest:: |
public | function | Tests an import with an incomplete rewinding. | |
MigrateExecutableTest:: |
public | function | Tests the import method with a valid row. | |
MigrateExecutableTest:: |
public | function | Tests the import method with a valid row. | |
MigrateExecutableTest:: |
public | function | Tests the import method with a thrown MigrateException. | |
MigrateExecutableTest:: |
public | function | Tests the import method with a regular Exception being thrown. | |
MigrateExecutableTest:: |
public | function | Tests the import method with a valid row. | |
MigrateExecutableTest:: |
public | function | Tests the import method with a thrown MigrateException. | |
MigrateExecutableTest:: |
public | function | Tests the processRow method. | |
MigrateExecutableTest:: |
public | function | Tests the processRow method. | |
MigrateExecutableTest:: |
public | function | Tests the processRow method with an empty pipeline. | |
MigrateExecutableTest:: |
public | function | Tests the processRow pipeline exception. | |
MigrateTestCase:: |
protected | property | The migration ID map. | |
MigrateTestCase:: |
protected | property | Local store for mocking setStatus()/getStatus(). | |
MigrateTestCase:: |
protected | function | Generates a table schema from a row. | |
MigrateTestCase:: |
protected | function | Gets an SQLite database connection object for use in tests. | |
MigrateTestCase:: |
protected | function | Retrieves a mocked migration. | |
MigrateTestCase:: |
protected | function | Gets the value on a row for a given key. | |
MigrateTestCase:: |
public | function | Tests a query. | |
MigrateTestCase:: |
protected | function | Asserts tested values during test retrieval. | |
PhpUnitWarnings:: |
private static | property | Deprecation warnings from PHPUnit to raise with @trigger_error(). | |
PhpUnitWarnings:: |
public | function | Converts PHPUnit deprecation warnings to E_USER_DEPRECATED. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. | |
UnitTestCase:: |
public static | function |