[TASK] Add test for NULL fields when comparing SQL field definitions
[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 * @author Mario Rimann <mario.rimann@typo3.org>
23 */
24 class SqlSchemaMigrationServiceTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
25
26 /**
27 * @test
28 */
29 public function getFieldDefinitionsFileContentHandlesMultipleWhitespacesInFieldDefinitions() {
30 $subject = new SqlSchemaMigrationService();
31 // Multiple whitespaces and tabs in field definition
32 $inputString = 'CREATE table atable (' . LF . 'aFieldName int(11)' . TAB . TAB . TAB . 'unsigned DEFAULT \'0\'' . LF . ');';
33 $result = $subject->getFieldDefinitions_fileContent($inputString);
34
35 $this->assertEquals(
36 array(
37 'atable' => array(
38 'fields' => array(
39 'aFieldName' => 'int(11) unsigned default \'0\'',
40 ),
41 'extra' => array(
42 'COLLATE' => '',
43 ),
44 ),
45 ),
46 $result
47 );
48 }
49
50 /**
51 * @test
52 */
53 public function getDatabaseExtraFindsChangedFields() {
54 $subject = new SqlSchemaMigrationService();
55 $differenceArray = $subject->getDatabaseExtra(
56 array(
57 'tx_foo' => array(
58 'fields' => array(
59 'foo' => 'varchar(999) DEFAULT \'0\' NOT NULL'
60 )
61 )
62 ),
63 array(
64 'tx_foo' => array(
65 'fields' => array(
66 'foo' => 'varchar(255) DEFAULT \'0\' NOT NULL'
67 )
68 )
69 )
70 );
71
72 $this->assertEquals(
73 $differenceArray,
74 array(
75 'extra' => array(),
76 'diff' => array(
77 'tx_foo' => array(
78 'fields' => array(
79 'foo' => 'varchar(999) DEFAULT \'0\' NOT NULL'
80 )
81 )
82 ),
83 'diff_currentValues' => array(
84 'tx_foo' => array(
85 'fields' => array(
86 'foo' => 'varchar(255) DEFAULT \'0\' NOT NULL'
87 )
88 )
89 )
90 )
91 );
92 }
93
94 /**
95 * @test
96 */
97 public function getDatabaseExtraFindsChangedFieldsIncludingNull() {
98 $subject = new SqlSchemaMigrationService();
99 $differenceArray = $subject->getDatabaseExtra(
100 array(
101 'tx_foo' => array(
102 'fields' => array(
103 'foo' => 'varchar(999) NULL'
104 )
105 )
106 ),
107 array(
108 'tx_foo' => array(
109 'fields' => array(
110 'foo' => 'varchar(255) NULL'
111 )
112 )
113 )
114 );
115
116 $this->assertEquals(
117 $differenceArray,
118 array(
119 'extra' => array(),
120 'diff' => array(
121 'tx_foo' => array(
122 'fields' => array(
123 'foo' => 'varchar(999) NULL'
124 )
125 )
126 ),
127 'diff_currentValues' => array(
128 'tx_foo' => array(
129 'fields' => array(
130 'foo' => 'varchar(255) NULL'
131 )
132 )
133 )
134 )
135 );
136 }
137
138 /**
139 * @test
140 */
141 public function getDatabaseExtraFindsChangedFieldsIgnoreNotNull() {
142 $subject = new SqlSchemaMigrationService();
143 $differenceArray = $subject->getDatabaseExtra(
144 array(
145 'tx_foo' => array(
146 'fields' => array(
147 'foo' => 'varchar(999) DEFAULT \'0\' NOT NULL'
148 )
149 )
150 ),
151 array(
152 'tx_foo' => array(
153 'fields' => array(
154 'foo' => 'varchar(255) DEFAULT \'0\' NOT NULL'
155 )
156 )
157 ),
158 '',
159 TRUE
160 );
161
162 $this->assertEquals(
163 $differenceArray,
164 array(
165 'extra' => array(),
166 'diff' => array(
167 'tx_foo' => array(
168 'fields' => array(
169 'foo' => 'varchar(999) DEFAULT \'0\''
170 )
171 )
172 ),
173 'diff_currentValues' => array(
174 'tx_foo' => array(
175 'fields' => array(
176 'foo' => 'varchar(255) DEFAULT \'0\''
177 )
178 )
179 )
180 )
181 );
182 }
183
184 /**
185 * @test
186 */
187 public function getDatabaseExtraIgnoresCaseDifference() {
188 $subject = new SqlSchemaMigrationService();
189 $differenceArray = $subject->getDatabaseExtra(
190 array(
191 'tx_foo' => array(
192 'fields' => array(
193 'foo' => 'INT(11) DEFAULT \'0\' NOT NULL',
194 )
195 )
196 ),
197 array(
198 'tx_foo' => array(
199 'fields' => array(
200 'foo' => 'int(11) DEFAULT \'0\' NOT NULL',
201 )
202 )
203 )
204 );
205
206
207 $this->assertEquals(
208 $differenceArray,
209 array(
210 'extra' => array(),
211 'diff' => array(),
212 'diff_currentValues' => NULL,
213 )
214 );
215 }
216
217 /**
218 * @test
219 */
220 public function getDatabaseExtraDoesNotLowercaseReservedWordsForTheComparison() {
221 $subject = new SqlSchemaMigrationService();
222 $differenceArray = $subject->getDatabaseExtra(
223 array(
224 'tx_foo' => array(
225 'fields' => array(
226 'PRIMARY KEY (md5hash)',
227 )
228 )
229 ),
230 array(
231 'tx_foo' => array(
232 'fields' => array(
233 'PRIMARY KEY (md5hash)'),
234 )
235 )
236 );
237
238
239 $this->assertEquals(
240 $differenceArray,
241 array(
242 'extra' => array(),
243 'diff' => array(),
244 'diff_currentValues' => NULL,
245 )
246 );
247 }
248
249 /**
250 * @test
251 */
252 public function checkColumnDefinitionIfCommentIsSupplied() {
253 $subject = new SqlSchemaMigrationService();
254 $fieldDefinition = $subject->assembleFieldDefinition(
255 array(
256 'Field' => 'uid',
257 'Type' => 'int(11)',
258 'Null' => 'NO',
259 'Key' => 'PRI',
260 'Default' => NULL,
261 'Extra' => 'auto_increment',
262 'Comment' => 'I am a comment',
263 )
264 );
265
266
267 $this->assertSame(
268 'int(11) NOT NULL auto_increment COMMENT \'I am a comment\'',
269 $fieldDefinition
270 );
271 }
272
273 /**
274 * @test
275 */
276 public function checkColumnDefinitionIfNoCommentIsSupplied() {
277 $subject = new SqlSchemaMigrationService();
278 $fieldDefinition = $subject->assembleFieldDefinition(
279 array(
280 'Field' => 'uid',
281 'Type' => 'int(11)',
282 'Null' => 'NO',
283 'Key' => 'PRI',
284 'Default' => NULL,
285 'Extra' => 'auto_increment',
286 )
287 );
288
289
290 $this->assertSame(
291 'int(11) NOT NULL auto_increment',
292 $fieldDefinition
293 );
294 }
295
296 }