[TASK] Doctrine: Migrate SqlSchemaMigrationService
[Packages/TYPO3.CMS.git] / typo3 / sysext / install / Tests / Unit / Service / SqlSchemaMigrationServiceTest.php
1 <?php
2 namespace TYPO3\CMS\Install\Tests\Unit\Service;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Install\Service\SqlSchemaMigrationService;
18
19 /**
20 * Test case
21 */
22 class SqlSchemaMigrationServiceTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
23 {
24 /**
25 * Get a SchemaService instance with mocked DBAL enable database connection, DBAL not enabled
26 *
27 * @return \PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface
28 */
29 protected function getSqlSchemaMigrationService()
30 {
31 /** @var \TYPO3\CMS\Dbal\Database\DatabaseConnection|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface $databaseConnection */
32 $subject = $this->getAccessibleMock(SqlSchemaMigrationService::class, ['isDbalEnabled'], [], '', false);
33 $subject->expects($this->any())->method('isDbalEnabled')->will($this->returnValue(false));
34
35 return $subject;
36 }
37
38 /**
39 * @test
40 */
41 public function getDatabaseExtraFindsChangedFields()
42 {
43 $subject = $this->getSqlSchemaMigrationService();
44 $differenceArray = $subject->getDatabaseExtra(
45 [
46 'tx_foo' => [
47 'fields' => [
48 'foo' => 'varchar(999) DEFAULT \'0\' NOT NULL'
49 ]
50 ]
51 ],
52 [
53 'tx_foo' => [
54 'fields' => [
55 'foo' => 'varchar(255) DEFAULT \'0\' NOT NULL'
56 ]
57 ]
58 ]
59 );
60
61 $this->assertEquals(
62 $differenceArray,
63 [
64 'extra' => [],
65 'diff' => [
66 'tx_foo' => [
67 'fields' => [
68 'foo' => 'varchar(999) DEFAULT \'0\' NOT NULL'
69 ]
70 ]
71 ],
72 'diff_currentValues' => [
73 'tx_foo' => [
74 'fields' => [
75 'foo' => 'varchar(255) DEFAULT \'0\' NOT NULL'
76 ]
77 ]
78 ]
79 ]
80 );
81 }
82
83 /**
84 * @test
85 */
86 public function getDatabaseExtraFindsChangedFieldsIncludingNull()
87 {
88 $subject = $this->getSqlSchemaMigrationService();
89 $differenceArray = $subject->getDatabaseExtra(
90 [
91 'tx_foo' => [
92 'fields' => [
93 'foo' => 'varchar(999) NULL'
94 ]
95 ]
96 ],
97 [
98 'tx_foo' => [
99 'fields' => [
100 'foo' => 'varchar(255) NULL'
101 ]
102 ]
103 ]
104 );
105
106 $this->assertEquals(
107 $differenceArray,
108 [
109 'extra' => [],
110 'diff' => [
111 'tx_foo' => [
112 'fields' => [
113 'foo' => 'varchar(999) NULL'
114 ]
115 ]
116 ],
117 'diff_currentValues' => [
118 'tx_foo' => [
119 'fields' => [
120 'foo' => 'varchar(255) NULL'
121 ]
122 ]
123 ]
124 ]
125 );
126 }
127
128 /**
129 * @test
130 */
131 public function getDatabaseExtraFindsChangedFieldsIgnoreNotNull()
132 {
133 $subject = $this->getSqlSchemaMigrationService();
134 $differenceArray = $subject->getDatabaseExtra(
135 [
136 'tx_foo' => [
137 'fields' => [
138 'foo' => 'varchar(999) DEFAULT \'0\' NOT NULL'
139 ]
140 ]
141 ],
142 [
143 'tx_foo' => [
144 'fields' => [
145 'foo' => 'varchar(255) DEFAULT \'0\' NOT NULL'
146 ]
147 ]
148 ],
149 '',
150 true
151 );
152
153 $this->assertEquals(
154 $differenceArray,
155 [
156 'extra' => [],
157 'diff' => [
158 'tx_foo' => [
159 'fields' => [
160 'foo' => 'varchar(999) DEFAULT \'0\''
161 ]
162 ]
163 ],
164 'diff_currentValues' => [
165 'tx_foo' => [
166 'fields' => [
167 'foo' => 'varchar(255) DEFAULT \'0\''
168 ]
169 ]
170 ]
171 ]
172 );
173 }
174
175 /**
176 * @test
177 */
178 public function getDatabaseExtraIgnoresCaseDifference()
179 {
180 $subject = $this->getSqlSchemaMigrationService();
181 $differenceArray = $subject->getDatabaseExtra(
182 [
183 'tx_foo' => [
184 'fields' => [
185 'foo' => 'INT(11) DEFAULT \'0\' NOT NULL',
186 ]
187 ]
188 ],
189 [
190 'tx_foo' => [
191 'fields' => [
192 'foo' => 'int(11) DEFAULT \'0\' NOT NULL',
193 ]
194 ]
195 ]
196 );
197
198 $this->assertEquals(
199 $differenceArray,
200 [
201 'extra' => [],
202 'diff' => [],
203 'diff_currentValues' => null,
204 ]
205 );
206 }
207
208 /**
209 * @test
210 */
211 public function getDatabaseExtraIgnoresCaseDifferenceButKeepsCaseInSetIntact()
212 {
213 $subject = $this->getSqlSchemaMigrationService();
214 $differenceArray = $subject->getDatabaseExtra(
215 [
216 'tx_foo' => [
217 'fields' => [
218 'subtype' => 'SET(\'Tx_MyExt_Domain_Model_Xyz\',\'Tx_MyExt_Domain_Model_Abc\',\'\') NOT NULL DEFAULT \'\',',
219 ]
220 ]
221 ],
222 [
223 'tx_foo' => [
224 'fields' => [
225 'subtype' => 'set(\'Tx_MyExt_Domain_Model_Xyz\',\'Tx_MyExt_Domain_Model_Abc\',\'\') NOT NULL DEFAULT \'\',',
226 ]
227 ]
228 ]
229 );
230
231 $this->assertEquals(
232 $differenceArray,
233 [
234 'extra' => [],
235 'diff' => [],
236 'diff_currentValues' => null,
237 ]
238 );
239 }
240
241 /**
242 * @test
243 */
244 public function getDatabaseExtraDoesNotLowercaseReservedWordsForTheComparison()
245 {
246 $subject = $this->getSqlSchemaMigrationService();
247 $differenceArray = $subject->getDatabaseExtra(
248 [
249 'tx_foo' => [
250 'fields' => [
251 'PRIMARY KEY (md5hash)',
252 ]
253 ]
254 ],
255 [
256 'tx_foo' => [
257 'fields' => [
258 'PRIMARY KEY (md5hash)'],
259 ]
260 ]
261 );
262
263 $this->assertEquals(
264 $differenceArray,
265 [
266 'extra' => [],
267 'diff' => [],
268 'diff_currentValues' => null,
269 ]
270 );
271 }
272
273 /**
274 * @test
275 */
276 public function getDatabaseExtraFindsNewSpatialKeys()
277 {
278 $subject = $this->getSqlSchemaMigrationService();
279 $differenceArray = $subject->getDatabaseExtra(
280 [
281 'tx_foo' => [
282 'keys' => [
283 'foo' => 'SPATIAL foo (foo)'
284 ]
285 ]
286 ],
287 [
288 'tx_foo' => [
289 'keys' => []
290 ]
291 ]
292 );
293
294 $this->assertEquals(
295 $differenceArray,
296 [
297 'extra' => [
298 'tx_foo' => [
299 'keys' => [
300 'foo' => 'SPATIAL foo (foo)'
301 ]
302 ]
303 ],
304 'diff' => [],
305 'diff_currentValues' => null
306 ]
307 );
308 }
309
310 /**
311 * @test
312 */
313 public function checkColumnDefinitionIfCommentIsSupplied()
314 {
315 $subject = $this->getSqlSchemaMigrationService();
316 $fieldDefinition = $subject->assembleFieldDefinition(
317 [
318 'Field' => 'uid',
319 'Type' => 'int(11)',
320 'Null' => 'NO',
321 'Key' => 'PRI',
322 'Default' => null,
323 'Extra' => 'auto_increment',
324 'Comment' => 'I am a comment',
325 ]
326 );
327
328 $this->assertSame(
329 'int(11) NOT NULL auto_increment COMMENT \'I am a comment\'',
330 $fieldDefinition
331 );
332 }
333
334 /**
335 * @test
336 */
337 public function checkColumnDefinitionIfNoCommentIsSupplied()
338 {
339 $subject = $this->getSqlSchemaMigrationService();
340 $fieldDefinition = $subject->assembleFieldDefinition(
341 [
342 'Field' => 'uid',
343 'Type' => 'int(11)',
344 'Null' => 'NO',
345 'Key' => 'PRI',
346 'Default' => null,
347 'Extra' => 'auto_increment',
348 ]
349 );
350
351 $this->assertSame(
352 'int(11) NOT NULL auto_increment',
353 $fieldDefinition
354 );
355 }
356
357 /**
358 * @test
359 */
360 public function getDatabaseExtraIncludesEngineIfMySQLIsUsed()
361 {
362 $subject = $this->getSqlSchemaMigrationService();
363 $differenceArray = $subject->getDatabaseExtra(
364 [
365 'tx_foo' => [
366 'fields' => [
367 'foo' => 'INT(11) DEFAULT \'0\' NOT NULL',
368 ],
369 'extra' => [
370 'ENGINE' => 'InnoDB'
371 ]
372 ]
373 ],
374 [
375 'tx_foo' => [
376 'fields' => [
377 'foo' => 'int(11) DEFAULT \'0\' NOT NULL',
378 ],
379 'extra' => [
380 'ENGINE' => 'InnoDB'
381 ]
382 ]
383 ]
384 );
385
386 $this->assertSame(
387 $differenceArray,
388 [
389 'extra' => [],
390 'diff' => [],
391 'diff_currentValues' => null,
392 ]
393 );
394 }
395
396 /**
397 * @test
398 */
399 public function getDatabaseExtraIncludesUnsignedAttributeIfMySQLIsUsed()
400 {
401 $subject = $this->getSqlSchemaMigrationService();
402 $differenceArray = $subject->getDatabaseExtra(
403 [
404 'tx_foo' => [
405 'fields' => [
406 'foo' => 'INT(11) UNSIGNED DEFAULT \'0\' NOT NULL',
407 ],
408 'extra' => [
409 'ENGINE' => 'InnoDB'
410 ]
411 ]
412 ],
413 [
414 'tx_foo' => [
415 'fields' => [
416 'foo' => 'int(11) DEFAULT \'0\' NOT NULL',
417 ],
418 'extra' => [
419 'ENGINE' => 'InnoDB'
420 ]
421 ]
422 ]
423 );
424
425 $this->assertSame(
426 $differenceArray,
427 [
428 'extra' => [],
429 'diff' => [
430 'tx_foo' => [
431 'fields' => [
432 'foo' => 'int(11) UNSIGNED DEFAULT \'0\' NOT NULL',
433 ],
434 ]
435 ],
436 'diff_currentValues' => [
437 'tx_foo' => [
438 'fields' => [
439 'foo' => 'int(11) DEFAULT \'0\' NOT NULL',
440 ],
441 ]
442 ]
443 ]
444 );
445 }
446
447 /**
448 * @test
449 */
450 public function getDatabaseExtraComparesIndexPrefixLengthIfMySQLIsUsed()
451 {
452 $subject = $this->getSqlSchemaMigrationService();
453 $differenceArray = $subject->getDatabaseExtra(
454 [
455 'tx_foo' => [
456 'keys' => [
457 'foo' => 'KEY foo (foo(199))'
458 ]
459 ]
460 ],
461 [
462 'tx_foo' => [
463 'keys' => [
464 'foo' => 'KEY foo (foo)'
465 ]
466 ]
467 ]
468 );
469
470 $this->assertSame(
471 $differenceArray,
472 [
473 'extra' => [],
474 'diff' => [
475 'tx_foo' => [
476 'keys' => [
477 'foo' => 'KEY foo (foo(199))'
478 ]
479 ]
480 ],
481 'diff_currentValues' => [
482 'tx_foo' => [
483 'keys' => [
484 'foo' => 'KEY foo (foo)'
485 ]
486 ]
487 ]
488 ]
489 );
490 }
491 }