comparison core/modules/migrate/tests/src/Unit/SqlBaseTest.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 /**
4 * @file
5 * Contains \Drupal\Tests\migrate\Unit\SqlBaseTest.
6 */
7
8 namespace Drupal\Tests\migrate\Unit;
9
10 use Drupal\migrate\Plugin\MigrationInterface;
11 use Drupal\migrate\Plugin\migrate\source\SqlBase;
12 use Drupal\Tests\UnitTestCase;
13
14 /**
15 * Tests the SqlBase class.
16 *
17 * @group migrate
18 */
19 class SqlBaseTest extends UnitTestCase {
20
21 /**
22 * Tests that the ID map is joinable.
23 *
24 * @param bool $expected_result
25 * The expected result.
26 * @param bool $id_map_is_sql
27 * TRUE if we want getIdMap() to return an instance of Sql.
28 * @param bool $with_id_map
29 * TRUE if we want the ID map to have a valid map of IDs.
30 * @param array $source_options
31 * (optional) An array of connection options for the source connection.
32 * Defaults to an empty array.
33 * @param array $idmap_options
34 * (optional) An array of connection options for the ID map connection.
35 * Defaults to an empty array.
36 *
37 * @dataProvider sqlBaseTestProvider
38 */
39 public function testMapJoinable($expected_result, $id_map_is_sql, $with_id_map, $source_options = [], $idmap_options = []) {
40 // Setup a connection object.
41 $source_connection = $this->getMockBuilder('Drupal\Core\Database\Connection')
42 ->disableOriginalConstructor()
43 ->getMock();
44 $source_connection->expects($id_map_is_sql && $with_id_map ? $this->once() : $this->never())
45 ->method('getConnectionOptions')
46 ->willReturn($source_options);
47
48 // Setup the ID map connection.
49 $idmap_connection = $this->getMockBuilder('Drupal\Core\Database\Connection')
50 ->disableOriginalConstructor()
51 ->getMock();
52 $idmap_connection->expects($id_map_is_sql && $with_id_map ? $this->once() : $this->never())
53 ->method('getConnectionOptions')
54 ->willReturn($idmap_options);
55
56 // Setup the Sql object.
57 $sql = $this->getMockBuilder('Drupal\migrate\Plugin\migrate\id_map\Sql')
58 ->disableOriginalConstructor()
59 ->getMock();
60 $sql->expects($id_map_is_sql && $with_id_map ? $this->once() : $this->never())
61 ->method('getDatabase')
62 ->willReturn($idmap_connection);
63
64 // Setup a migration entity.
65 $migration = $this->getMock(MigrationInterface::class);
66 $migration->expects($with_id_map ? $this->once() : $this->never())
67 ->method('getIdMap')
68 ->willReturn($id_map_is_sql ? $sql : NULL);
69
70 // Create our SqlBase test class.
71 $sql_base = new TestSqlBase();
72 $sql_base->setMigration($migration);
73 $sql_base->setDatabase($source_connection);
74
75 // Configure the idMap to make the check in mapJoinable() pass.
76 if ($with_id_map) {
77 $sql_base->setIds([
78 'uid' => ['type' => 'integer', 'alias' => 'u'],
79 ]);
80 }
81
82 $this->assertEquals($expected_result, $sql_base->mapJoinable());
83 }
84
85 /**
86 * The data provider for SqlBase.
87 *
88 * @return array
89 * An array of data per test run.
90 */
91 public function sqlBaseTestProvider() {
92 return [
93 // Source ids are empty so mapJoinable() is false.
94 [
95 FALSE,
96 FALSE,
97 FALSE,
98 ],
99 // Still false because getIdMap() is not a subclass of Sql.
100 [
101 FALSE,
102 FALSE,
103 TRUE,
104 ],
105 // Test mapJoinable() returns false when source and id connection options
106 // differ.
107 [
108 FALSE,
109 TRUE,
110 TRUE,
111 ['driver' => 'mysql', 'username' => 'different_from_map', 'password' => 'different_from_map'],
112 ['driver' => 'mysql', 'username' => 'different_from_source', 'password' => 'different_from_source'],
113 ],
114 // Returns false because driver is pgsql and the databases are not the
115 // same.
116 [
117 FALSE,
118 TRUE,
119 TRUE,
120 ['driver' => 'pgsql', 'database' => '1.pgsql', 'username' => 'same_value', 'password' => 'same_value'],
121 ['driver' => 'pgsql', 'database' => '2.pgsql', 'username' => 'same_value', 'password' => 'same_value'],
122 ],
123 // Returns false because driver is sqlite and the databases are not the
124 // same.
125 [
126 FALSE,
127 TRUE,
128 TRUE,
129 ['driver' => 'sqlite', 'database' => '1.sqlite', 'username' => '', 'password' => ''],
130 ['driver' => 'sqlite', 'database' => '2.sqlite', 'username' => '', 'password' => ''],
131 ],
132 // Returns false because driver is not the same.
133 [
134 FALSE,
135 TRUE,
136 TRUE,
137 ['driver' => 'pgsql', 'username' => 'same_value', 'password' => 'same_value'],
138 ['driver' => 'mysql', 'username' => 'same_value', 'password' => 'same_value'],
139 ],
140 ];
141 }
142
143 }
144
145 /**
146 * Creates a base source class for SQL migration testing.
147 */
148 class TestSqlBase extends SqlBase {
149
150 /**
151 * The database object.
152 *
153 * @var object
154 */
155 protected $database;
156
157 /**
158 * The migration IDs.
159 *
160 * @var array
161 */
162 protected $ids;
163
164 /**
165 * Override the constructor so we can create one easily.
166 */
167 public function __construct() {}
168
169 /**
170 * Allows us to set the database during tests.
171 *
172 * @param mixed $database
173 * The database mock object.
174 */
175 public function setDatabase($database) {
176 $this->database = $database;
177 }
178
179 /**
180 * {@inheritdoc}
181 */
182 public function getDatabase() {
183 return $this->database;
184 }
185
186 /**
187 * Allows us to set the migration during the test.
188 *
189 * @param mixed $migration
190 * The migration mock.
191 */
192 public function setMigration($migration) {
193 $this->migration = $migration;
194 }
195
196 /**
197 * {@inheritdoc}
198 */
199 public function mapJoinable() {
200 return parent::mapJoinable();
201 }
202
203 /**
204 * {@inheritdoc}
205 */
206 public function getIds() {
207 return $this->ids;
208 }
209
210 /**
211 * Allows us to set the IDs during a test.
212 *
213 * @param array $ids
214 * An array of identifiers.
215 */
216 public function setIds($ids) {
217 $this->ids = $ids;
218 }
219
220 /**
221 * {@inheritdoc}
222 */
223 public function fields() {}
224
225 /**
226 * {@inheritdoc}
227 */
228 public function query() {}
229
230 /**
231 * {@inheritdoc}
232 */
233 public function calculateDependencies() {
234 return [];
235 }
236
237 }