[!!!][TASK] Extract testing framework for TYPO3
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Database / Schema / Parser / ColumnDefinitionAttributesTest.php
1 <?php
2 declare(strict_types=1);
3
4 namespace TYPO3\CMS\Core\Tests\Unit\Database\Schema\Parser;
5
6 /*
7 * This file is part of the TYPO3 CMS project.
8 *
9 * It is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License, either version 2
11 * of the License, or any later version.
12 *
13 * For the full copyright and license information, please read the
14 * LICENSE.txt file that was distributed with this source code.
15 *
16 * The TYPO3 project - inspiring people to share!
17 */
18
19 use TYPO3\CMS\Core\Database\Schema\Parser\AST\CreateColumnDefinitionItem;
20 use TYPO3\CMS\Core\Database\Schema\Parser\AST\CreateTableStatement;
21 use TYPO3\CMS\Core\Database\Schema\Parser\Parser;
22
23 /**
24 * Tests for CreateColumnDefinitionItem attributes
25 */
26 class ColumnDefinitionAttributesTest extends \TYPO3\CMS\Components\TestingFramework\Core\UnitTestCase
27 {
28 /**
29 * Each parameter array consists of the following values:
30 * - column definition attributes SQL fragment
31 * - allow null values
32 * - has default value
33 * - default value
34 * - auto increment column
35 * - create index on column
36 * - create unique index column
37 * - use column as primary key
38 * - comment
39 * - column format
40 * - storage
41 *
42 * @return array
43 */
44 public function canParseColumnDefinitionAttributesDataProvider(): array
45 {
46 return [
47 'NULL' => [
48 'NULL',
49 true,
50 false,
51 null,
52 false,
53 false,
54 false,
55 false,
56 null,
57 null,
58 null,
59 ],
60 'NOT NULL' => [
61 'NOT NULL',
62 false,
63 false,
64 null,
65 false,
66 false,
67 false,
68 false,
69 null,
70 null,
71 null,
72 ],
73 'DEFAULT' => [
74 "DEFAULT '0'",
75 true,
76 true,
77 '0',
78 false,
79 false,
80 false,
81 false,
82 null,
83 null,
84 null,
85 ],
86 'AUTO_INCREMENT' => [
87 'AUTO_INCREMENT',
88 true,
89 false,
90 null,
91 true,
92 false,
93 false,
94 false,
95 null,
96 null,
97 null,
98 ],
99 'UNIQUE' => [
100 'UNIQUE',
101 true,
102 false,
103 null,
104 false,
105 false,
106 true,
107 false,
108 null,
109 null,
110 null,
111 ],
112 'UNIQUE KEY' => [
113 'UNIQUE KEY',
114 true,
115 false,
116 null,
117 false,
118 false,
119 true,
120 false,
121 null,
122 null,
123 null,
124 ],
125 'PRIMARY' => [
126 'PRIMARY',
127 true,
128 false,
129 null,
130 false,
131 false,
132 false,
133 true,
134 null,
135 null,
136 null,
137 ],
138 'PRIMARY KEY' => [
139 'PRIMARY KEY',
140 true,
141 false,
142 null,
143 false,
144 false,
145 false,
146 true,
147 null,
148 null,
149 null,
150 ],
151 'KEY' => [
152 'KEY',
153 true,
154 false,
155 null,
156 false,
157 true,
158 false,
159 false,
160 null,
161 null,
162 null,
163 ],
164 'COMMENT' => [
165 "COMMENT 'aComment'",
166 true,
167 false,
168 null,
169 false,
170 false,
171 false,
172 false,
173 'aComment',
174 null,
175 null,
176 ],
177 'COLUMN_FORMAT FIXED' => [
178 'COLUMN_FORMAT FIXED',
179 true,
180 false,
181 null,
182 false,
183 false,
184 false,
185 false,
186 null,
187 'fixed',
188 null,
189 ],
190 'COLUMN_FORMAT DYNAMIC' => [
191 'COLUMN_FORMAT DYNAMIC',
192 true,
193 false,
194 null,
195 false,
196 false,
197 false,
198 false,
199 null,
200 'dynamic',
201 null,
202 ],
203 'COLUMN_FORMAT DEFAULT' => [
204 'COLUMN_FORMAT DEFAULT',
205 true,
206 false,
207 null,
208 false,
209 false,
210 false,
211 false,
212 null,
213 null,
214 null,
215 ],
216 'STORAGE DISK' => [
217 'STORAGE DISK',
218 true,
219 false,
220 null,
221 false,
222 false,
223 false,
224 false,
225 null,
226 null,
227 'disk',
228 ],
229 'STORAGE MEMORY' => [
230 'STORAGE MEMORY',
231 true,
232 false,
233 null,
234 false,
235 false,
236 false,
237 false,
238 null,
239 null,
240 'memory',
241 ],
242 'STORAGE DEFAULT' => [
243 'STORAGE DEFAULT',
244 true,
245 false,
246 null,
247 false,
248 false,
249 false,
250 false,
251 null,
252 null,
253 null,
254 ],
255 "NOT NULL DEFAULT '0'" => [
256 "NOT NULL DEFAULT '0'",
257 false,
258 true,
259 '0',
260 false,
261 false,
262 false,
263 false,
264 null,
265 null,
266 null,
267 ],
268 'NOT NULL AUTO_INCREMENT' => [
269 'NOT NULL AUTO_INCREMENT',
270 false,
271 false,
272 null,
273 true,
274 false,
275 false,
276 false,
277 null,
278 null,
279 null,
280 ],
281 'NULL DEFAULT NULL' => [
282 'NULL DEFAULT NULL',
283 true,
284 true,
285 null,
286 false,
287 false,
288 false,
289 false,
290 null,
291 null,
292 null,
293 ],
294 'NOT NULL PRIMARY KEY' => [
295 'NOT NULL PRIMARY KEY',
296 false,
297 false,
298 null,
299 false,
300 false,
301 false,
302 true,
303 null,
304 null,
305 null,
306 ],
307 "NULL DEFAULT 'dummy' UNIQUE" => [
308 "NULL DEFAULT 'dummy' UNIQUE",
309 true,
310 true,
311 'dummy',
312 false,
313 false,
314 true,
315 false,
316 null,
317 null,
318 null,
319 ],
320 "NOT NULL DEFAULT '0' COMMENT 'aComment with blanks' AUTO_INCREMENT PRIMARY KEY COLUMN_FORMAT DYNAMIC" => [
321 "NOT NULL DEFAULT '0' COMMENT 'aComment with blanks' AUTO_INCREMENT PRIMARY KEY COLUMN_FORMAT DYNAMIC",
322 false,
323 true,
324 '0',
325 true,
326 false,
327 false,
328 true,
329 'aComment with blanks',
330 'dynamic',
331 null,
332 ],
333 ];
334 }
335
336 /**
337 * @test
338 * @dataProvider canParseColumnDefinitionAttributesDataProvider
339 * @param string $columnAttribute
340 * @param bool $allowNull
341 * @param bool $hasDefaultValue
342 * @param mixed $defaultValue
343 * @param bool $autoIncrement
344 * @param bool $createIndex
345 * @param bool $createUniqueIndex
346 * @param bool $isPrimaryKey
347 * @param string $comment
348 * @param string $columnFormat
349 * @param string $storage
350 */
351 public function canParseColumnDefinitionAttributes(
352 string $columnAttribute,
353 bool $allowNull,
354 bool $hasDefaultValue,
355 $defaultValue,
356 bool $autoIncrement,
357 bool $createIndex,
358 bool $createUniqueIndex,
359 bool $isPrimaryKey,
360 string $comment = null,
361 string $columnFormat = null,
362 string $storage = null
363 ) {
364 $statement = sprintf('CREATE TABLE `aTable`(`aField` INT(11) %s);', $columnAttribute);
365 $subject = $this->createSubject($statement);
366
367 $this->assertInstanceOf(CreateColumnDefinitionItem::class, $subject);
368 $this->assertSame($allowNull, $subject->allowNull);
369 $this->assertSame($hasDefaultValue, $subject->hasDefaultValue);
370 $this->assertSame($defaultValue, $subject->defaultValue);
371 $this->assertSame($createIndex, $subject->index);
372 $this->assertSame($createUniqueIndex, $subject->unique);
373 $this->assertSame($isPrimaryKey, $subject->primary);
374 $this->assertSame($autoIncrement, $subject->autoIncrement);
375 $this->assertSame($comment, $subject->comment);
376 $this->assertSame($columnFormat, $subject->columnFormat);
377 $this->assertSame($storage, $subject->storage);
378 }
379
380 /**
381 * Parse the CREATE TABLE statement and return the reference definition
382 *
383 * @param string $statement
384 * @return \TYPO3\CMS\Core\Database\Schema\Parser\AST\CreateColumnDefinitionItem
385 */
386 protected function createSubject(string $statement): CreateColumnDefinitionItem
387 {
388 $parser = new Parser($statement);
389 /** @var CreateTableStatement $createTableStatement */
390 $createTableStatement = $parser->getAST();
391
392 return $createTableStatement->createDefinition->items[0];
393 }
394 }