Mercurial > hg > isophonics-drupal-site
comparison core/modules/migrate/tests/src/Unit/MigrationTest.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | 1fec387a4317 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 /** | |
4 * @file | |
5 * Contains \Drupal\Tests\migrate\Unit\MigrationTest. | |
6 */ | |
7 | |
8 namespace Drupal\Tests\migrate\Unit; | |
9 | |
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; | |
18 | |
19 /** | |
20 * @coversDefaultClass \Drupal\migrate\Plugin\Migration | |
21 * | |
22 * @group Migration | |
23 */ | |
24 class MigrationTest extends UnitTestCase { | |
25 | |
26 /** | |
27 * Tests checking requirements for source plugins. | |
28 * | |
29 * @covers ::checkRequirements | |
30 */ | |
31 public function testRequirementsForSourcePlugin() { | |
32 $migration = new TestMigration(); | |
33 | |
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'); | |
39 | |
40 $migration->setSourcePlugin($source_plugin); | |
41 $migration->setDestinationPlugin($destination_plugin); | |
42 | |
43 $this->setExpectedException(RequirementsException::class, 'Missing source requirement'); | |
44 $migration->checkRequirements(); | |
45 } | |
46 | |
47 /** | |
48 * Tests checking requirements for destination plugins. | |
49 * | |
50 * @covers ::checkRequirements | |
51 */ | |
52 public function testRequirementsForDestinationPlugin() { | |
53 $migration = new TestMigration(); | |
54 | |
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'])); | |
60 | |
61 $migration->setSourcePlugin($source_plugin); | |
62 $migration->setDestinationPlugin($destination_plugin); | |
63 | |
64 $this->setExpectedException(RequirementsException::class, 'Missing destination requirement'); | |
65 $migration->checkRequirements(); | |
66 } | |
67 | |
68 /** | |
69 * Tests checking requirements for destination plugins. | |
70 * | |
71 * @covers ::checkRequirements | |
72 */ | |
73 public function testRequirementsForMigrations() { | |
74 $migration = new TestMigration(); | |
75 | |
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); | |
81 | |
82 $plugin_manager = $this->getMock('Drupal\migrate\Plugin\MigrationPluginManagerInterface'); | |
83 $migration->setMigrationPluginManager($plugin_manager); | |
84 | |
85 // We setup the requirements that test_a doesn't exist and test_c is not | |
86 // completed yet. | |
87 $migration->setRequirements(['test_a', 'test_b', 'test_c', 'test_d']); | |
88 | |
89 $migration_b = $this->getMock(MigrationInterface::class); | |
90 $migration_c = $this->getMock(MigrationInterface::class); | |
91 $migration_d = $this->getMock(MigrationInterface::class); | |
92 | |
93 $migration_b->expects($this->once()) | |
94 ->method('allRowsProcessed') | |
95 ->willReturn(TRUE); | |
96 $migration_c->expects($this->once()) | |
97 ->method('allRowsProcessed') | |
98 ->willReturn(FALSE); | |
99 $migration_d->expects($this->once()) | |
100 ->method('allRowsProcessed') | |
101 ->willReturn(TRUE); | |
102 | |
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]); | |
107 | |
108 $this->setExpectedException(RequirementsException::class, 'Missing migrations test_a, test_c'); | |
109 $migration->checkRequirements(); | |
110 } | |
111 | |
112 } | |
113 | |
114 /** | |
115 * Defines the TestMigration class. | |
116 */ | |
117 class TestMigration extends Migration { | |
118 | |
119 /** | |
120 * Constructs an instance of TestMigration object. | |
121 */ | |
122 public function __construct() { | |
123 } | |
124 | |
125 /** | |
126 * Sets the requirements values. | |
127 * | |
128 * @param array $requirements | |
129 * The array of requirement values. | |
130 */ | |
131 public function setRequirements(array $requirements) { | |
132 $this->requirements = $requirements; | |
133 } | |
134 | |
135 /** | |
136 * Sets the source Plugin. | |
137 * | |
138 * @param \Drupal\migrate\Plugin\MigrateSourceInterface $source_plugin | |
139 * The source Plugin. | |
140 */ | |
141 public function setSourcePlugin(MigrateSourceInterface $source_plugin) { | |
142 $this->sourcePlugin = $source_plugin; | |
143 } | |
144 | |
145 /** | |
146 * Sets the destination Plugin. | |
147 * | |
148 * @param \Drupal\migrate\Plugin\MigrateDestinationInterface $destination_plugin | |
149 * The destination Plugin. | |
150 */ | |
151 public function setDestinationPlugin(MigrateDestinationInterface $destination_plugin) { | |
152 $this->destinationPlugin = $destination_plugin; | |
153 } | |
154 | |
155 /** | |
156 * Sets the plugin manager service. | |
157 * | |
158 * @param \Drupal\migrate\Plugin\MigrationPluginManagerInterface $plugin_manager | |
159 * The plugin manager service. | |
160 */ | |
161 public function setMigrationPluginManager(MigrationPluginManagerInterface $plugin_manager) { | |
162 $this->migrationPluginManager = $plugin_manager; | |
163 } | |
164 | |
165 } | |
166 | |
167 /** | |
168 * Defines the RequirementsAwareSourceInterface. | |
169 */ | |
170 interface RequirementsAwareSourceInterface extends MigrateSourceInterface, RequirementsInterface {} | |
171 | |
172 /** | |
173 * Defines the RequirementsAwareDestinationInterface. | |
174 */ | |
175 interface RequirementsAwareDestinationInterface extends MigrateDestinationInterface, RequirementsInterface {} |