5 * Contains \Drupal\Tests\migrate\Unit\MigrationTest.
8 namespace Drupal\Tests\migrate\Unit;
10 use Drupal\migrate\Plugin\MigrationInterface;
11 use Drupal\migrate\Plugin\Migration;
12 use Drupal\migrate\Exception\RequirementsException;
13 use Drupal\migrate\Plugin\MigrateDestinationInterface;
14 use Drupal\migrate\Plugin\MigrateSourceInterface;
15 use Drupal\migrate\Plugin\MigrationPluginManagerInterface;
16 use Drupal\migrate\Plugin\RequirementsInterface;
17 use Drupal\Tests\UnitTestCase;
20 * @coversDefaultClass \Drupal\migrate\Plugin\Migration
24 class MigrationTest extends UnitTestCase {
27 * Tests checking requirements for source plugins.
29 * @covers ::checkRequirements
31 public function testRequirementsForSourcePlugin() {
32 $migration = new TestMigration();
34 $source_plugin = $this->getMock('Drupal\Tests\migrate\Unit\RequirementsAwareSourceInterface');
35 $source_plugin->expects($this->once())
36 ->method('checkRequirements')
37 ->willThrowException(new RequirementsException('Missing source requirement', ['key' => 'value']));
38 $destination_plugin = $this->getMock('Drupal\Tests\migrate\Unit\RequirementsAwareDestinationInterface');
40 $migration->setSourcePlugin($source_plugin);
41 $migration->setDestinationPlugin($destination_plugin);
43 $this->setExpectedException(RequirementsException::class, 'Missing source requirement');
44 $migration->checkRequirements();
48 * Tests checking requirements for destination plugins.
50 * @covers ::checkRequirements
52 public function testRequirementsForDestinationPlugin() {
53 $migration = new TestMigration();
55 $source_plugin = $this->getMock('Drupal\migrate\Plugin\MigrateSourceInterface');
56 $destination_plugin = $this->getMock('Drupal\Tests\migrate\Unit\RequirementsAwareDestinationInterface');
57 $destination_plugin->expects($this->once())
58 ->method('checkRequirements')
59 ->willThrowException(new RequirementsException('Missing destination requirement', ['key' => 'value']));
61 $migration->setSourcePlugin($source_plugin);
62 $migration->setDestinationPlugin($destination_plugin);
64 $this->setExpectedException(RequirementsException::class, 'Missing destination requirement');
65 $migration->checkRequirements();
69 * Tests checking requirements for destination plugins.
71 * @covers ::checkRequirements
73 public function testRequirementsForMigrations() {
74 $migration = new TestMigration();
76 // Setup source and destination plugins without any requirements.
77 $source_plugin = $this->getMock('Drupal\migrate\Plugin\MigrateSourceInterface');
78 $destination_plugin = $this->getMock('Drupal\migrate\Plugin\MigrateDestinationInterface');
79 $migration->setSourcePlugin($source_plugin);
80 $migration->setDestinationPlugin($destination_plugin);
82 $plugin_manager = $this->getMock('Drupal\migrate\Plugin\MigrationPluginManagerInterface');
83 $migration->setMigrationPluginManager($plugin_manager);
85 // We setup the requirements that test_a doesn't exist and test_c is not
87 $migration->setRequirements(['test_a', 'test_b', 'test_c', 'test_d']);
89 $migration_b = $this->getMock(MigrationInterface::class);
90 $migration_c = $this->getMock(MigrationInterface::class);
91 $migration_d = $this->getMock(MigrationInterface::class);
93 $migration_b->expects($this->once())
94 ->method('allRowsProcessed')
96 $migration_c->expects($this->once())
97 ->method('allRowsProcessed')
99 $migration_d->expects($this->once())
100 ->method('allRowsProcessed')
103 $plugin_manager->expects($this->once())
104 ->method('createInstances')
105 ->with(['test_a', 'test_b', 'test_c', 'test_d'])
106 ->willReturn(['test_b' => $migration_b, 'test_c' => $migration_c, 'test_d' => $migration_d]);
108 $this->setExpectedException(RequirementsException::class, 'Missing migrations test_a, test_c');
109 $migration->checkRequirements();
115 * Defines the TestMigration class.
117 class TestMigration extends Migration {
120 * Constructs an instance of TestMigration object.
122 public function __construct() {
126 * Sets the requirements values.
128 * @param array $requirements
129 * The array of requirement values.
131 public function setRequirements(array $requirements) {
132 $this->requirements = $requirements;
136 * Sets the source Plugin.
138 * @param \Drupal\migrate\Plugin\MigrateSourceInterface $source_plugin
141 public function setSourcePlugin(MigrateSourceInterface $source_plugin) {
142 $this->sourcePlugin = $source_plugin;
146 * Sets the destination Plugin.
148 * @param \Drupal\migrate\Plugin\MigrateDestinationInterface $destination_plugin
149 * The destination Plugin.
151 public function setDestinationPlugin(MigrateDestinationInterface $destination_plugin) {
152 $this->destinationPlugin = $destination_plugin;
156 * Sets the plugin manager service.
158 * @param \Drupal\migrate\Plugin\MigrationPluginManagerInterface $plugin_manager
159 * The plugin manager service.
161 public function setMigrationPluginManager(MigrationPluginManagerInterface $plugin_manager) {
162 $this->migrationPluginManager = $plugin_manager;
168 * Defines the RequirementsAwareSourceInterface.
170 interface RequirementsAwareSourceInterface extends MigrateSourceInterface, RequirementsInterface {}
173 * Defines the RequirementsAwareDestinationInterface.
175 interface RequirementsAwareDestinationInterface extends MigrateDestinationInterface, RequirementsInterface {}