TYPO3  7.6
Unit/Service/SqlSchemaMigrationServiceTest.php
Go to the documentation of this file.
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 
19 
23 class SqlSchemaMigrationServiceTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
24 {
30  protected function getSqlSchemaMigrationService()
31  {
33  $subject = $this->getAccessibleMock(SqlSchemaMigrationService::class, array('isDbalEnabled'), array(), '', false);
34  $subject->expects($this->any())->method('isDbalEnabled')->will($this->returnValue(false));
35 
36  return $subject;
37  }
38 
44  protected function getDbalEnabledSqlSchemaMigrationService()
45  {
47  $databaseConnection = $this->getAccessibleMock(\TYPO3\CMS\Dbal\Database\DatabaseConnection::class, array('dummy'), array(), '', false);
48  $databaseConnection->_set('dbmsSpecifics', GeneralUtility::makeInstance(\TYPO3\CMS\Dbal\Database\Specifics\PostgresSpecifics::class));
49 
50  $subject = $this->getAccessibleMock(SqlSchemaMigrationService::class, array('isDbalEnabled', 'getDatabaseConnection'), array(), '', false);
51  $subject->expects($this->any())->method('isDbalEnabled')->will($this->returnValue(true));
52  $subject->expects($this->any())->method('getDatabaseConnection')->will($this->returnValue($databaseConnection));
53 
54  return $subject;
55  }
56 
61  {
62  $subject = $this->getSqlSchemaMigrationService();
63  // Multiple whitespaces and tabs in field definition
64  $inputString = 'CREATE table atable (' . LF . 'aFieldName int(11)' . TAB . TAB . TAB . 'unsigned DEFAULT \'0\'' . LF . ');';
65  $result = $subject->getFieldDefinitions_fileContent($inputString);
66 
67  $this->assertEquals(
68  array(
69  'atable' => array(
70  'fields' => array(
71  'aFieldName' => 'int(11) unsigned default \'0\'',
72  ),
73  'extra' => array(
74  'COLLATE' => '',
75  ),
76  ),
77  ),
78  $result
79  );
80  }
81 
86  {
87  $subject = $this->getSqlSchemaMigrationService();
88  $differenceArray = $subject->getDatabaseExtra(
89  array(
90  'tx_foo' => array(
91  'fields' => array(
92  'foo' => 'varchar(999) DEFAULT \'0\' NOT NULL'
93  )
94  )
95  ),
96  array(
97  'tx_foo' => array(
98  'fields' => array(
99  'foo' => 'varchar(255) DEFAULT \'0\' NOT NULL'
100  )
101  )
102  )
103  );
104 
105  $this->assertEquals(
106  $differenceArray,
107  array(
108  'extra' => array(),
109  'diff' => array(
110  'tx_foo' => array(
111  'fields' => array(
112  'foo' => 'varchar(999) DEFAULT \'0\' NOT NULL'
113  )
114  )
115  ),
116  'diff_currentValues' => array(
117  'tx_foo' => array(
118  'fields' => array(
119  'foo' => 'varchar(255) DEFAULT \'0\' NOT NULL'
120  )
121  )
122  )
123  )
124  );
125  }
126 
131  {
132  $subject = $this->getSqlSchemaMigrationService();
133  $differenceArray = $subject->getDatabaseExtra(
134  array(
135  'tx_foo' => array(
136  'fields' => array(
137  'foo' => 'varchar(999) NULL'
138  )
139  )
140  ),
141  array(
142  'tx_foo' => array(
143  'fields' => array(
144  'foo' => 'varchar(255) NULL'
145  )
146  )
147  )
148  );
149 
150  $this->assertEquals(
151  $differenceArray,
152  array(
153  'extra' => array(),
154  'diff' => array(
155  'tx_foo' => array(
156  'fields' => array(
157  'foo' => 'varchar(999) NULL'
158  )
159  )
160  ),
161  'diff_currentValues' => array(
162  'tx_foo' => array(
163  'fields' => array(
164  'foo' => 'varchar(255) NULL'
165  )
166  )
167  )
168  )
169  );
170  }
171 
176  {
177  $subject = $this->getSqlSchemaMigrationService();
178  $differenceArray = $subject->getDatabaseExtra(
179  array(
180  'tx_foo' => array(
181  'fields' => array(
182  'foo' => 'varchar(999) DEFAULT \'0\' NOT NULL'
183  )
184  )
185  ),
186  array(
187  'tx_foo' => array(
188  'fields' => array(
189  'foo' => 'varchar(255) DEFAULT \'0\' NOT NULL'
190  )
191  )
192  ),
193  '',
194  true
195  );
196 
197  $this->assertEquals(
198  $differenceArray,
199  array(
200  'extra' => array(),
201  'diff' => array(
202  'tx_foo' => array(
203  'fields' => array(
204  'foo' => 'varchar(999) DEFAULT \'0\''
205  )
206  )
207  ),
208  'diff_currentValues' => array(
209  'tx_foo' => array(
210  'fields' => array(
211  'foo' => 'varchar(255) DEFAULT \'0\''
212  )
213  )
214  )
215  )
216  );
217  }
218 
223  {
224  $subject = $this->getSqlSchemaMigrationService();
225  $differenceArray = $subject->getDatabaseExtra(
226  array(
227  'tx_foo' => array(
228  'fields' => array(
229  'foo' => 'INT(11) DEFAULT \'0\' NOT NULL',
230  )
231  )
232  ),
233  array(
234  'tx_foo' => array(
235  'fields' => array(
236  'foo' => 'int(11) DEFAULT \'0\' NOT NULL',
237  )
238  )
239  )
240  );
241 
242 
243  $this->assertEquals(
244  $differenceArray,
245  array(
246  'extra' => array(),
247  'diff' => array(),
248  'diff_currentValues' => null,
249  )
250  );
251  }
252 
257  {
258  $subject = $this->getSqlSchemaMigrationService();
259  $differenceArray = $subject->getDatabaseExtra(
260  array(
261  'tx_foo' => array(
262  'fields' => array(
263  'subtype' => 'SET(\'Tx_MyExt_Domain_Model_Xyz\',\'Tx_MyExt_Domain_Model_Abc\',\'\') NOT NULL DEFAULT \'\',',
264  )
265  )
266  ),
267  array(
268  'tx_foo' => array(
269  'fields' => array(
270  'subtype' => 'set(\'Tx_MyExt_Domain_Model_Xyz\',\'Tx_MyExt_Domain_Model_Abc\',\'\') NOT NULL DEFAULT \'\',',
271  )
272  )
273  )
274  );
275 
276  $this->assertEquals(
277  $differenceArray,
278  array(
279  'extra' => array(),
280  'diff' => array(),
281  'diff_currentValues' => null,
282  )
283  );
284  }
285 
290  {
291  $subject = $this->getSqlSchemaMigrationService();
292  $differenceArray = $subject->getDatabaseExtra(
293  array(
294  'tx_foo' => array(
295  'fields' => array(
296  'PRIMARY KEY (md5hash)',
297  )
298  )
299  ),
300  array(
301  'tx_foo' => array(
302  'fields' => array(
303  'PRIMARY KEY (md5hash)'),
304  )
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 
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 
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 
375  $this->assertSame(
376  'int(11) NOT NULL auto_increment COMMENT \'I am a comment\'',
377  $fieldDefinition
378  );
379  }
380 
385  {
386  $subject = $this->getSqlSchemaMigrationService();
387  $fieldDefinition = $subject->assembleFieldDefinition(
388  array(
389  'Field' => 'uid',
390  'Type' => 'int(11)',
391  'Null' => 'NO',
392  'Key' => 'PRI',
393  'Default' => null,
394  'Extra' => 'auto_increment',
395  )
396  );
397 
398 
399  $this->assertSame(
400  'int(11) NOT NULL auto_increment',
401  $fieldDefinition
402  );
403  }
404 
409  {
410  $subject = $this->getSqlSchemaMigrationService();
411  $differenceArray = $subject->getDatabaseExtra(
412  array(
413  'tx_foo' => array(
414  'fields' => array(
415  'foo' => 'INT(11) DEFAULT \'0\' NOT NULL',
416  ),
417  'extra' => array(
418  'ENGINE' => 'InnoDB'
419  )
420  )
421  ),
422  array(
423  'tx_foo' => array(
424  'fields' => array(
425  'foo' => 'int(11) DEFAULT \'0\' NOT NULL',
426  ),
427  'extra' => array(
428  'ENGINE' => 'InnoDB'
429  )
430  )
431  )
432  );
433 
434  $this->assertSame(
435  $differenceArray,
436  array(
437  'extra' => array(),
438  'diff' => array(),
439  'diff_currentValues' => null,
440  )
441  );
442  }
443 
448  {
449  $subject = $this->getDbalEnabledSqlSchemaMigrationService();
450  $differenceArray = $subject->getDatabaseExtra(
451  array(
452  'tx_foo' => array(
453  'fields' => array(
454  'foo' => 'INT(11) DEFAULT \'0\' NOT NULL',
455  ),
456  'extra' => array(
457  'ENGINE' => 'InnoDB'
458  )
459  )
460  ),
461  array(
462  'tx_foo' => array(
463  'fields' => array(
464  'foo' => 'int(11) DEFAULT \'0\' NOT NULL',
465  ),
466  'extra' => array()
467  )
468  )
469  );
470 
471  $this->assertSame(
472  $differenceArray,
473  array(
474  'extra' => array(),
475  'diff' => array(),
476  'diff_currentValues' => null,
477  )
478  );
479  }
480 
485  {
486  $subject = $this->getSqlSchemaMigrationService();
487  $differenceArray = $subject->getDatabaseExtra(
488  array(
489  'tx_foo' => array(
490  'fields' => array(
491  'foo' => 'INT(11) UNSIGNED DEFAULT \'0\' NOT NULL',
492  ),
493  'extra' => array(
494  'ENGINE' => 'InnoDB'
495  )
496  )
497  ),
498  array(
499  'tx_foo' => array(
500  'fields' => array(
501  'foo' => 'int(11) DEFAULT \'0\' NOT NULL',
502  ),
503  'extra' => array(
504  'ENGINE' => 'InnoDB'
505  )
506  )
507  )
508  );
509 
510  $this->assertSame(
511  $differenceArray,
512  array(
513  'extra' => array(),
514  'diff' => array(
515  'tx_foo' => array(
516  'fields' => array(
517  'foo' => 'int(11) UNSIGNED DEFAULT \'0\' NOT NULL',
518  ),
519  )
520  ),
521  'diff_currentValues' => array(
522  'tx_foo' => array(
523  'fields' => array(
524  'foo' => 'int(11) DEFAULT \'0\' NOT NULL',
525  ),
526  )
527  )
528  )
529  );
530  }
531 
536  {
537  $subject = $this->getDbalEnabledSqlSchemaMigrationService();
538  $differenceArray = $subject->getDatabaseExtra(
539  array(
540  'tx_foo' => array(
541  'fields' => array(
542  'foo' => 'INT(11) UNSIGNED DEFAULT \'0\' NOT NULL',
543  ),
544  'extra' => array(
545  'ENGINE' => 'InnoDB'
546  )
547  )
548  ),
549  array(
550  'tx_foo' => array(
551  'fields' => array(
552  'foo' => 'int(11) DEFAULT \'0\' NOT NULL',
553  ),
554  'extra' => array(
555  'ENGINE' => 'InnoDB'
556  )
557  )
558  )
559  );
560 
561  $this->assertSame(
562  $differenceArray,
563  array(
564  'extra' => array(),
565  'diff' => array(),
566  'diff_currentValues' => null
567  )
568  );
569  }
570 
575  {
576  $subject = $this->getDbalEnabledSqlSchemaMigrationService();
577  $differenceArray = $subject->getDatabaseExtra(
578  array(
579  'tx_foo' => array(
580  'keys' => array(
581  'foo' => 'KEY foo (foo(199))'
582  )
583  )
584  ),
585  array(
586  'tx_foo' => array(
587  'keys' => array(
588  'foo' => 'KEY foo (foo)'
589  )
590  )
591  )
592  );
593 
594  $this->assertSame(
595  $differenceArray,
596  array(
597  'extra' => array(),
598  'diff' => array(),
599  'diff_currentValues' => null,
600  )
601  );
602  }
603 
608  {
609  $subject = $this->getSqlSchemaMigrationService();
610  $differenceArray = $subject->getDatabaseExtra(
611  array(
612  'tx_foo' => array(
613  'keys' => array(
614  'foo' => 'KEY foo (foo(199))'
615  )
616  )
617  ),
618  array(
619  'tx_foo' => array(
620  'keys' => array(
621  'foo' => 'KEY foo (foo)'
622  )
623  )
624  )
625  );
626 
627  $this->assertSame(
628  $differenceArray,
629  array(
630  'extra' => array(),
631  'diff' => array(
632  'tx_foo' => array(
633  'keys' => array(
634  'foo' => 'KEY foo (foo(199))'
635  )
636  )
637  ),
638  'diff_currentValues' => array(
639  'tx_foo' => array(
640  'keys' => array(
641  'foo' => 'KEY foo (foo)'
642  )
643  )
644  )
645  )
646  );
647  }
648 }