TYPO3 CMS  TYPO3_7-6
SqlSchemaMigrationServiceTest.php
Go to the documentation of this file.
1 <?php
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 
24 {
30  protected function getSqlSchemaMigrationService()
31  {
33  $subject = $this->getAccessibleMock(SqlSchemaMigrationService::class, ['isDbalEnabled'], [], '', 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, ['dummy'], [], '', false);
48  $databaseConnection->_set('dbmsSpecifics', GeneralUtility::makeInstance(\TYPO3\CMS\Dbal\Database\Specifics\PostgresSpecifics::class));
49 
50  $subject = $this->getAccessibleMock(SqlSchemaMigrationService::class, ['isDbalEnabled', 'getDatabaseConnection'], [], '', 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  [
69  'atable' => [
70  'fields' => [
71  'aFieldName' => 'int(11) unsigned default \'0\'',
72  ],
73  'extra' => [
74  'COLLATE' => '',
75  ],
76  ],
77  ],
78  $result
79  );
80  }
81 
86  {
87  $subject = $this->getSqlSchemaMigrationService();
88  $differenceArray = $subject->getDatabaseExtra(
89  [
90  'tx_foo' => [
91  'fields' => [
92  'foo' => 'varchar(999) DEFAULT \'0\' NOT NULL'
93  ]
94  ]
95  ],
96  [
97  'tx_foo' => [
98  'fields' => [
99  'foo' => 'varchar(255) DEFAULT \'0\' NOT NULL'
100  ]
101  ]
102  ]
103  );
104 
105  $this->assertEquals(
106  $differenceArray,
107  [
108  'extra' => [],
109  'diff' => [
110  'tx_foo' => [
111  'fields' => [
112  'foo' => 'varchar(999) DEFAULT \'0\' NOT NULL'
113  ]
114  ]
115  ],
116  'diff_currentValues' => [
117  'tx_foo' => [
118  'fields' => [
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  [
135  'tx_foo' => [
136  'fields' => [
137  'foo' => 'varchar(999) NULL'
138  ]
139  ]
140  ],
141  [
142  'tx_foo' => [
143  'fields' => [
144  'foo' => 'varchar(255) NULL'
145  ]
146  ]
147  ]
148  );
149 
150  $this->assertEquals(
151  $differenceArray,
152  [
153  'extra' => [],
154  'diff' => [
155  'tx_foo' => [
156  'fields' => [
157  'foo' => 'varchar(999) NULL'
158  ]
159  ]
160  ],
161  'diff_currentValues' => [
162  'tx_foo' => [
163  'fields' => [
164  'foo' => 'varchar(255) NULL'
165  ]
166  ]
167  ]
168  ]
169  );
170  }
171 
176  {
177  $subject = $this->getSqlSchemaMigrationService();
178  $differenceArray = $subject->getDatabaseExtra(
179  [
180  'tx_foo' => [
181  'fields' => [
182  'foo' => 'varchar(999) DEFAULT \'0\' NOT NULL'
183  ]
184  ]
185  ],
186  [
187  'tx_foo' => [
188  'fields' => [
189  'foo' => 'varchar(255) DEFAULT \'0\' NOT NULL'
190  ]
191  ]
192  ],
193  '',
194  true
195  );
196 
197  $this->assertEquals(
198  $differenceArray,
199  [
200  'extra' => [],
201  'diff' => [
202  'tx_foo' => [
203  'fields' => [
204  'foo' => 'varchar(999) DEFAULT \'0\''
205  ]
206  ]
207  ],
208  'diff_currentValues' => [
209  'tx_foo' => [
210  'fields' => [
211  'foo' => 'varchar(255) DEFAULT \'0\''
212  ]
213  ]
214  ]
215  ]
216  );
217  }
218 
223  {
224  $subject = $this->getSqlSchemaMigrationService();
225  $differenceArray = $subject->getDatabaseExtra(
226  [
227  'tx_foo' => [
228  'fields' => [
229  'foo' => 'INT(11) DEFAULT \'0\' NOT NULL',
230  ]
231  ]
232  ],
233  [
234  'tx_foo' => [
235  'fields' => [
236  'foo' => 'int(11) DEFAULT \'0\' NOT NULL',
237  ]
238  ]
239  ]
240  );
241 
242  $this->assertEquals(
243  $differenceArray,
244  [
245  'extra' => [],
246  'diff' => [],
247  'diff_currentValues' => null,
248  ]
249  );
250  }
251 
256  {
257  $subject = $this->getSqlSchemaMigrationService();
258  $differenceArray = $subject->getDatabaseExtra(
259  [
260  'tx_foo' => [
261  'fields' => [
262  'subtype' => 'SET(\'Tx_MyExt_Domain_Model_Xyz\',\'Tx_MyExt_Domain_Model_Abc\',\'\') NOT NULL DEFAULT \'\',',
263  ]
264  ]
265  ],
266  [
267  'tx_foo' => [
268  'fields' => [
269  'subtype' => 'set(\'Tx_MyExt_Domain_Model_Xyz\',\'Tx_MyExt_Domain_Model_Abc\',\'\') NOT NULL DEFAULT \'\',',
270  ]
271  ]
272  ]
273  );
274 
275  $this->assertEquals(
276  $differenceArray,
277  [
278  'extra' => [],
279  'diff' => [],
280  'diff_currentValues' => null,
281  ]
282  );
283  }
284 
289  {
290  $subject = $this->getSqlSchemaMigrationService();
291  $differenceArray = $subject->getDatabaseExtra(
292  [
293  'tx_foo' => [
294  'fields' => [
295  'PRIMARY KEY (md5hash)',
296  ]
297  ]
298  ],
299  [
300  'tx_foo' => [
301  'fields' => [
302  'PRIMARY KEY (md5hash)'],
303  ]
304  ]
305  );
306 
307  $this->assertEquals(
308  $differenceArray,
309  [
310  'extra' => [],
311  'diff' => [],
312  'diff_currentValues' => null,
313  ]
314  );
315  }
316 
321  {
322  $subject = $this->getSqlSchemaMigrationService();
323  $differenceArray = $subject->getDatabaseExtra(
324  [
325  'tx_foo' => [
326  'keys' => [
327  'foo' => 'SPATIAL foo (foo)'
328  ]
329  ]
330  ],
331  [
332  'tx_foo' => [
333  'keys' => []
334  ]
335  ]
336  );
337 
338  $this->assertEquals(
339  $differenceArray,
340  [
341  'extra' => [
342  'tx_foo' => [
343  'keys' => [
344  'foo' => 'SPATIAL foo (foo)'
345  ]
346  ]
347  ],
348  'diff' => [],
349  'diff_currentValues' => null
350  ]
351  );
352  }
353 
358  {
359  $subject = $this->getSqlSchemaMigrationService();
360  $fieldDefinition = $subject->assembleFieldDefinition(
361  [
362  'Field' => 'uid',
363  'Type' => 'int(11)',
364  'Null' => 'NO',
365  'Key' => 'PRI',
366  'Default' => null,
367  'Extra' => 'auto_increment',
368  'Comment' => 'I am a comment',
369  ]
370  );
371 
372  $this->assertSame(
373  'int(11) NOT NULL auto_increment COMMENT \'I am a comment\'',
374  $fieldDefinition
375  );
376  }
377 
382  {
383  $subject = $this->getSqlSchemaMigrationService();
384  $fieldDefinition = $subject->assembleFieldDefinition(
385  [
386  'Field' => 'uid',
387  'Type' => 'int(11)',
388  'Null' => 'NO',
389  'Key' => 'PRI',
390  'Default' => null,
391  'Extra' => 'auto_increment',
392  ]
393  );
394 
395  $this->assertSame(
396  'int(11) NOT NULL auto_increment',
397  $fieldDefinition
398  );
399  }
400 
405  {
406  $subject = $this->getSqlSchemaMigrationService();
407  $differenceArray = $subject->getDatabaseExtra(
408  [
409  'tx_foo' => [
410  'fields' => [
411  'foo' => 'INT(11) DEFAULT \'0\' NOT NULL',
412  ],
413  'extra' => [
414  'ENGINE' => 'InnoDB'
415  ]
416  ]
417  ],
418  [
419  'tx_foo' => [
420  'fields' => [
421  'foo' => 'int(11) DEFAULT \'0\' NOT NULL',
422  ],
423  'extra' => [
424  'ENGINE' => 'InnoDB'
425  ]
426  ]
427  ]
428  );
429 
430  $this->assertSame(
431  $differenceArray,
432  [
433  'extra' => [],
434  'diff' => [],
435  'diff_currentValues' => null,
436  ]
437  );
438  }
439 
444  {
445  $subject = $this->getDbalEnabledSqlSchemaMigrationService();
446  $differenceArray = $subject->getDatabaseExtra(
447  [
448  'tx_foo' => [
449  'fields' => [
450  'foo' => 'INT(11) DEFAULT \'0\' NOT NULL',
451  ],
452  'extra' => [
453  'ENGINE' => 'InnoDB'
454  ]
455  ]
456  ],
457  [
458  'tx_foo' => [
459  'fields' => [
460  'foo' => 'int(11) DEFAULT \'0\' NOT NULL',
461  ],
462  'extra' => []
463  ]
464  ]
465  );
466 
467  $this->assertSame(
468  $differenceArray,
469  [
470  'extra' => [],
471  'diff' => [],
472  'diff_currentValues' => null,
473  ]
474  );
475  }
476 
481  {
482  $subject = $this->getSqlSchemaMigrationService();
483  $differenceArray = $subject->getDatabaseExtra(
484  [
485  'tx_foo' => [
486  'fields' => [
487  'foo' => 'INT(11) UNSIGNED DEFAULT \'0\' NOT NULL',
488  ],
489  'extra' => [
490  'ENGINE' => 'InnoDB'
491  ]
492  ]
493  ],
494  [
495  'tx_foo' => [
496  'fields' => [
497  'foo' => 'int(11) DEFAULT \'0\' NOT NULL',
498  ],
499  'extra' => [
500  'ENGINE' => 'InnoDB'
501  ]
502  ]
503  ]
504  );
505 
506  $this->assertSame(
507  $differenceArray,
508  [
509  'extra' => [],
510  'diff' => [
511  'tx_foo' => [
512  'fields' => [
513  'foo' => 'int(11) UNSIGNED DEFAULT \'0\' NOT NULL',
514  ],
515  ]
516  ],
517  'diff_currentValues' => [
518  'tx_foo' => [
519  'fields' => [
520  'foo' => 'int(11) DEFAULT \'0\' NOT NULL',
521  ],
522  ]
523  ]
524  ]
525  );
526  }
527 
532  {
533  $subject = $this->getDbalEnabledSqlSchemaMigrationService();
534  $differenceArray = $subject->getDatabaseExtra(
535  [
536  'tx_foo' => [
537  'fields' => [
538  'foo' => 'INT(11) UNSIGNED DEFAULT \'0\' NOT NULL',
539  ],
540  'extra' => [
541  'ENGINE' => 'InnoDB'
542  ]
543  ]
544  ],
545  [
546  'tx_foo' => [
547  'fields' => [
548  'foo' => 'int(11) DEFAULT \'0\' NOT NULL',
549  ],
550  'extra' => [
551  'ENGINE' => 'InnoDB'
552  ]
553  ]
554  ]
555  );
556 
557  $this->assertSame(
558  $differenceArray,
559  [
560  'extra' => [],
561  'diff' => [],
562  'diff_currentValues' => null
563  ]
564  );
565  }
566 
571  {
572  $subject = $this->getDbalEnabledSqlSchemaMigrationService();
573  $differenceArray = $subject->getDatabaseExtra(
574  [
575  'tx_foo' => [
576  'keys' => [
577  'foo' => 'KEY foo (foo(199))'
578  ]
579  ]
580  ],
581  [
582  'tx_foo' => [
583  'keys' => [
584  'foo' => 'KEY foo (foo)'
585  ]
586  ]
587  ]
588  );
589 
590  $this->assertSame(
591  $differenceArray,
592  [
593  'extra' => [],
594  'diff' => [],
595  'diff_currentValues' => null,
596  ]
597  );
598  }
599 
604  {
605  $subject = $this->getSqlSchemaMigrationService();
606  $differenceArray = $subject->getDatabaseExtra(
607  [
608  'tx_foo' => [
609  'keys' => [
610  'foo' => 'KEY foo (foo(199))'
611  ]
612  ]
613  ],
614  [
615  'tx_foo' => [
616  'keys' => [
617  'foo' => 'KEY foo (foo)'
618  ]
619  ]
620  ]
621  );
622 
623  $this->assertSame(
624  $differenceArray,
625  [
626  'extra' => [],
627  'diff' => [
628  'tx_foo' => [
629  'keys' => [
630  'foo' => 'KEY foo (foo(199))'
631  ]
632  ]
633  ],
634  'diff_currentValues' => [
635  'tx_foo' => [
636  'keys' => [
637  'foo' => 'KEY foo (foo)'
638  ]
639  ]
640  ]
641  ]
642  );
643  }
644 }
getAccessibleMock( $originalClassName, $methods=[], array $arguments=[], $mockClassName='', $callOriginalConstructor=true, $callOriginalClone=true, $callAutoload=true)