[TASK] Use new instead of makeInstance in unit tests
[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, array('isDbalEnabled'), array(), '', false);
33 $subject->expects($this->any())->method('isDbalEnabled')->will($this->returnValue(false));
34
35 return $subject;
36 }
37
38 /**
39 * Get a SchemaService instance with mocked DBAL enable database connection, DBAL enabled
40 *
41 * @return \PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface
42 */
43 protected function getDbalEnabledSqlSchemaMigrationService()
44 {
45 /** @var \TYPO3\CMS\Dbal\Database\DatabaseConnection|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface $databaseConnection */
46 $databaseConnection = $this->getAccessibleMock(\TYPO3\CMS\Dbal\Database\DatabaseConnection::class, array('dummy'), array(), '', false);
47 $databaseConnection->_set(
48 'dbmsSpecifics',
49 new \TYPO3\CMS\Dbal\Database\Specifics\PostgresSpecifics()
50 );
51
52 $subject = $this->getAccessibleMock(SqlSchemaMigrationService::class, array('isDbalEnabled', 'getDatabaseConnection'), array(), '', false);
53 $subject->expects($this->any())->method('isDbalEnabled')->will($this->returnValue(true));
54 $subject->expects($this->any())->method('getDatabaseConnection')->will($this->returnValue($databaseConnection));
55
56 return $subject;
57 }
58
59 /**
60 * @test
61 */
62 public function getFieldDefinitionsFileContentHandlesMultipleWhitespacesInFieldDefinitions()
63 {
64 $subject = $this->getSqlSchemaMigrationService();
65 // Multiple whitespaces and tabs in field definition
66 $inputString = 'CREATE table atable (' . LF . 'aFieldName int(11)' . TAB . TAB . TAB . 'unsigned DEFAULT \'0\'' . LF . ');';
67 $result = $subject->getFieldDefinitions_fileContent($inputString);
68
69 $this->assertEquals(
70 array(
71 'atable' => array(
72 'fields' => array(
73 'aFieldName' => 'int(11) unsigned default \'0\'',
74 ),
75 'extra' => array(
76 'COLLATE' => '',
77 ),
78 ),
79 ),
80 $result
81 );
82 }
83
84 /**
85 * @test
86 */
87 public function getDatabaseExtraFindsChangedFields()
88 {
89 $subject = $this->getSqlSchemaMigrationService();
90 $differenceArray = $subject->getDatabaseExtra(
91 array(
92 'tx_foo' => array(
93 'fields' => array(
94 'foo' => 'varchar(999) DEFAULT \'0\' NOT NULL'
95 )
96 )
97 ),
98 array(
99 'tx_foo' => array(
100 'fields' => array(
101 'foo' => 'varchar(255) DEFAULT \'0\' NOT NULL'
102 )
103 )
104 )
105 );
106
107 $this->assertEquals(
108 $differenceArray,
109 array(
110 'extra' => array(),
111 'diff' => array(
112 'tx_foo' => array(
113 'fields' => array(
114 'foo' => 'varchar(999) DEFAULT \'0\' NOT NULL'
115 )
116 )
117 ),
118 'diff_currentValues' => array(
119 'tx_foo' => array(
120 'fields' => array(
121 'foo' => 'varchar(255) DEFAULT \'0\' NOT NULL'
122 )
123 )
124 )
125 )
126 );
127 }
128
129 /**
130 * @test
131 */
132 public function getDatabaseExtraFindsChangedFieldsIncludingNull()
133 {
134 $subject = $this->getSqlSchemaMigrationService();
135 $differenceArray = $subject->getDatabaseExtra(
136 array(
137 'tx_foo' => array(
138 'fields' => array(
139 'foo' => 'varchar(999) NULL'
140 )
141 )
142 ),
143 array(
144 'tx_foo' => array(
145 'fields' => array(
146 'foo' => 'varchar(255) NULL'
147 )
148 )
149 )
150 );
151
152 $this->assertEquals(
153 $differenceArray,
154 array(
155 'extra' => array(),
156 'diff' => array(
157 'tx_foo' => array(
158 'fields' => array(
159 'foo' => 'varchar(999) NULL'
160 )
161 )
162 ),
163 'diff_currentValues' => array(
164 'tx_foo' => array(
165 'fields' => array(
166 'foo' => 'varchar(255) NULL'
167 )
168 )
169 )
170 )
171 );
172 }
173
174 /**
175 * @test
176 */
177 public function getDatabaseExtraFindsChangedFieldsIgnoreNotNull()
178 {
179 $subject = $this->getSqlSchemaMigrationService();
180 $differenceArray = $subject->getDatabaseExtra(
181 array(
182 'tx_foo' => array(
183 'fields' => array(
184 'foo' => 'varchar(999) DEFAULT \'0\' NOT NULL'
185 )
186 )
187 ),
188 array(
189 'tx_foo' => array(
190 'fields' => array(
191 'foo' => 'varchar(255) DEFAULT \'0\' NOT NULL'
192 )
193 )
194 ),
195 '',
196 true
197 );
198
199 $this->assertEquals(
200 $differenceArray,
201 array(
202 'extra' => array(),
203 'diff' => array(
204 'tx_foo' => array(
205 'fields' => array(
206 'foo' => 'varchar(999) DEFAULT \'0\''
207 )
208 )
209 ),
210 'diff_currentValues' => array(
211 'tx_foo' => array(
212 'fields' => array(
213 'foo' => 'varchar(255) DEFAULT \'0\''
214 )
215 )
216 )
217 )
218 );
219 }
220
221 /**
222 * @test
223 */
224 public function getDatabaseExtraIgnoresCaseDifference()
225 {
226 $subject = $this->getSqlSchemaMigrationService();
227 $differenceArray = $subject->getDatabaseExtra(
228 array(
229 'tx_foo' => array(
230 'fields' => array(
231 'foo' => 'INT(11) DEFAULT \'0\' NOT NULL',
232 )
233 )
234 ),
235 array(
236 'tx_foo' => array(
237 'fields' => array(
238 'foo' => 'int(11) DEFAULT \'0\' NOT NULL',
239 )
240 )
241 )
242 );
243
244 $this->assertEquals(
245 $differenceArray,
246 array(
247 'extra' => array(),
248 'diff' => array(),
249 'diff_currentValues' => null,
250 )
251 );
252 }
253
254 /**
255 * @test
256 */
257 public function getDatabaseExtraIgnoresCaseDifferenceButKeepsCaseInSetIntact()
258 {
259 $subject = $this->getSqlSchemaMigrationService();
260 $differenceArray = $subject->getDatabaseExtra(
261 array(
262 'tx_foo' => array(
263 'fields' => array(
264 'subtype' => 'SET(\'Tx_MyExt_Domain_Model_Xyz\',\'Tx_MyExt_Domain_Model_Abc\',\'\') NOT NULL DEFAULT \'\',',
265 )
266 )
267 ),
268 array(
269 'tx_foo' => array(
270 'fields' => array(
271 'subtype' => 'set(\'Tx_MyExt_Domain_Model_Xyz\',\'Tx_MyExt_Domain_Model_Abc\',\'\') NOT NULL DEFAULT \'\',',
272 )
273 )
274 )
275 );
276
277 $this->assertEquals(
278 $differenceArray,
279 array(
280 'extra' => array(),
281 'diff' => array(),
282 'diff_currentValues' => null,
283 )
284 );
285 }
286
287 /**
288 * @test
289 */
290 public function getDatabaseExtraDoesNotLowercaseReservedWordsForTheComparison()
291 {
292 $subject = $this->getSqlSchemaMigrationService();
293 $differenceArray = $subject->getDatabaseExtra(
294 array(
295 'tx_foo' => array(
296 'fields' => array(
297 'PRIMARY KEY (md5hash)',
298 )
299 )
300 ),
301 array(
302 'tx_foo' => array(
303 'fields' => array(
304 'PRIMARY KEY (md5hash)'),
305 )
306 )
307 );
308
309 $this->assertEquals(
310 $differenceArray,
311 array(
312 'extra' => array(),
313 'diff' => array(),
314 'diff_currentValues' => null,
315 )
316 );
317 }
318
319 /**
320 * @test
321 */
322 public function getDatabaseExtraFindsNewSpatialKeys()
323 {
324 $subject = $this->getSqlSchemaMigrationService();
325 $differenceArray = $subject->getDatabaseExtra(
326 array(
327 'tx_foo' => array(
328 'keys' => array(
329 'foo' => 'SPATIAL foo (foo)'
330 )
331 )
332 ),
333 array(
334 'tx_foo' => array(
335 'keys' => array()
336 )
337 )
338 );
339
340 $this->assertEquals(
341 $differenceArray,
342 array(
343 'extra' => array(
344 'tx_foo' => array(
345 'keys' => array(
346 'foo' => 'SPATIAL foo (foo)'
347 )
348 )
349 ),
350 'diff' => array(),
351 'diff_currentValues' => null
352 )
353 );
354 }
355
356 /**
357 * @test
358 */
359 public function checkColumnDefinitionIfCommentIsSupplied()
360 {
361 $subject = $this->getSqlSchemaMigrationService();
362 $fieldDefinition = $subject->assembleFieldDefinition(
363 array(
364 'Field' => 'uid',
365 'Type' => 'int(11)',
366 'Null' => 'NO',
367 'Key' => 'PRI',
368 'Default' => null,
369 'Extra' => 'auto_increment',
370 'Comment' => 'I am a comment',
371 )
372 );
373
374 $this->assertSame(
375 'int(11) NOT NULL auto_increment COMMENT \'I am a comment\'',
376 $fieldDefinition
377 );
378 }
379
380 /**
381 * @test
382 */
383 public function checkColumnDefinitionIfNoCommentIsSupplied()
384 {
385 $subject = $this->getSqlSchemaMigrationService();
386 $fieldDefinition = $subject->assembleFieldDefinition(
387 array(
388 'Field' => 'uid',
389 'Type' => 'int(11)',
390 'Null' => 'NO',
391 'Key' => 'PRI',
392 'Default' => null,
393 'Extra' => 'auto_increment',
394 )
395 );
396
397 $this->assertSame(
398 'int(11) NOT NULL auto_increment',
399 $fieldDefinition
400 );
401 }
402
403 /**
404 * @test
405 */
406 public function getDatabaseExtraIncludesEngineIfMySQLIsUsed()
407 {
408 $subject = $this->getSqlSchemaMigrationService();
409 $differenceArray = $subject->getDatabaseExtra(
410 array(
411 'tx_foo' => array(
412 'fields' => array(
413 'foo' => 'INT(11) DEFAULT \'0\' NOT NULL',
414 ),
415 'extra' => array(
416 'ENGINE' => 'InnoDB'
417 )
418 )
419 ),
420 array(
421 'tx_foo' => array(
422 'fields' => array(
423 'foo' => 'int(11) DEFAULT \'0\' NOT NULL',
424 ),
425 'extra' => array(
426 'ENGINE' => 'InnoDB'
427 )
428 )
429 )
430 );
431
432 $this->assertSame(
433 $differenceArray,
434 array(
435 'extra' => array(),
436 'diff' => array(),
437 'diff_currentValues' => null,
438 )
439 );
440 }
441
442 /**
443 * @test
444 */
445 public function getDatabaseExtraExcludesEngineIfDbalIsUsed()
446 {
447 $subject = $this->getDbalEnabledSqlSchemaMigrationService();
448 $differenceArray = $subject->getDatabaseExtra(
449 array(
450 'tx_foo' => array(
451 'fields' => array(
452 'foo' => 'INT(11) DEFAULT \'0\' NOT NULL',
453 ),
454 'extra' => array(
455 'ENGINE' => 'InnoDB'
456 )
457 )
458 ),
459 array(
460 'tx_foo' => array(
461 'fields' => array(
462 'foo' => 'int(11) DEFAULT \'0\' NOT NULL',
463 ),
464 'extra' => array()
465 )
466 )
467 );
468
469 $this->assertSame(
470 $differenceArray,
471 array(
472 'extra' => array(),
473 'diff' => array(),
474 'diff_currentValues' => null,
475 )
476 );
477 }
478
479 /**
480 * @test
481 */
482 public function getDatabaseExtraIncludesUnsignedAttributeIfMySQLIsUsed()
483 {
484 $subject = $this->getSqlSchemaMigrationService();
485 $differenceArray = $subject->getDatabaseExtra(
486 array(
487 'tx_foo' => array(
488 'fields' => array(
489 'foo' => 'INT(11) UNSIGNED DEFAULT \'0\' NOT NULL',
490 ),
491 'extra' => array(
492 'ENGINE' => 'InnoDB'
493 )
494 )
495 ),
496 array(
497 'tx_foo' => array(
498 'fields' => array(
499 'foo' => 'int(11) DEFAULT \'0\' NOT NULL',
500 ),
501 'extra' => array(
502 'ENGINE' => 'InnoDB'
503 )
504 )
505 )
506 );
507
508 $this->assertSame(
509 $differenceArray,
510 array(
511 'extra' => array(),
512 'diff' => array(
513 'tx_foo' => array(
514 'fields' => array(
515 'foo' => 'int(11) UNSIGNED DEFAULT \'0\' NOT NULL',
516 ),
517 )
518 ),
519 'diff_currentValues' => array(
520 'tx_foo' => array(
521 'fields' => array(
522 'foo' => 'int(11) DEFAULT \'0\' NOT NULL',
523 ),
524 )
525 )
526 )
527 );
528 }
529
530 /**
531 * @test
532 */
533 public function getDatabaseExtraExcludesUnsignedAttributeIfDbalIsUsed()
534 {
535 $subject = $this->getDbalEnabledSqlSchemaMigrationService();
536 $differenceArray = $subject->getDatabaseExtra(
537 array(
538 'tx_foo' => array(
539 'fields' => array(
540 'foo' => 'INT(11) UNSIGNED DEFAULT \'0\' NOT NULL',
541 ),
542 'extra' => array(
543 'ENGINE' => 'InnoDB'
544 )
545 )
546 ),
547 array(
548 'tx_foo' => array(
549 'fields' => array(
550 'foo' => 'int(11) DEFAULT \'0\' NOT NULL',
551 ),
552 'extra' => array(
553 'ENGINE' => 'InnoDB'
554 )
555 )
556 )
557 );
558
559 $this->assertSame(
560 $differenceArray,
561 array(
562 'extra' => array(),
563 'diff' => array(),
564 'diff_currentValues' => null
565 )
566 );
567 }
568
569 /**
570 * @test
571 */
572 public function getDatabaseExtraIgnoresIndexPrefixLengthIfDbalIsUsed()
573 {
574 $subject = $this->getDbalEnabledSqlSchemaMigrationService();
575 $differenceArray = $subject->getDatabaseExtra(
576 array(
577 'tx_foo' => array(
578 'keys' => array(
579 'foo' => 'KEY foo (foo(199))'
580 )
581 )
582 ),
583 array(
584 'tx_foo' => array(
585 'keys' => array(
586 'foo' => 'KEY foo (foo)'
587 )
588 )
589 )
590 );
591
592 $this->assertSame(
593 $differenceArray,
594 array(
595 'extra' => array(),
596 'diff' => array(),
597 'diff_currentValues' => null,
598 )
599 );
600 }
601
602 /**
603 * @test
604 */
605 public function getDatabaseExtraComparesIndexPrefixLengthIfMySQLIsUsed()
606 {
607 $subject = $this->getSqlSchemaMigrationService();
608 $differenceArray = $subject->getDatabaseExtra(
609 array(
610 'tx_foo' => array(
611 'keys' => array(
612 'foo' => 'KEY foo (foo(199))'
613 )
614 )
615 ),
616 array(
617 'tx_foo' => array(
618 'keys' => array(
619 'foo' => 'KEY foo (foo)'
620 )
621 )
622 )
623 );
624
625 $this->assertSame(
626 $differenceArray,
627 array(
628 'extra' => array(),
629 'diff' => array(
630 'tx_foo' => array(
631 'keys' => array(
632 'foo' => 'KEY foo (foo(199))'
633 )
634 )
635 ),
636 'diff_currentValues' => array(
637 'tx_foo' => array(
638 'keys' => array(
639 'foo' => 'KEY foo (foo)'
640 )
641 )
642 )
643 )
644 );
645 }
646 }