‪TYPO3CMS  10.4
IndexDefinitionTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
24 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
25 
29 class ‪IndexDefinitionTest extends UnitTestCase
30 {
45  public function ‪canParseIndexDefinitionDataProvider(): array
46  {
47  return [
48  'PRIMARY KEY (single column)' => [
49  'PRIMARY KEY (`aField`)',
50  '',
51  [['aField', 0, null]],
52  true,
53  false,
54  false,
55  false,
56  '',
57  [],
58  ],
59  'PRIMARY KEY (multiple columns)' => [
60  'PRIMARY KEY (`aField`, `bField`(199), cField)',
61  '',
62  [['aField', 0, null], ['bField', 199, null], ['cField', 0, null]],
63  true,
64  false,
65  false,
66  false,
67  '',
68  [],
69  ],
70  'PRIMARY KEY (index type)' => [
71  'PRIMARY KEY USING HASH (`aField`)',
72  '',
73  [['aField', 0, null]],
74  true,
75  false,
76  false,
77  false,
78  'HASH',
79  [],
80  ],
81  'PRIMARY KEY (index options)' => [
82  "PRIMARY KEY (`aField`, bField(199)) KEY_BLOCK_SIZE 4 WITH PARSER `something` COMMENT 'aTest'",
83  '',
84  [['aField', 0, null], ['bField', 199, null]],
85  true,
86  false,
87  false,
88  false,
89  '',
90  [
91  'key_block_size' => 4,
92  'parser' => new ‪Identifier('something'),
93  'comment' => 'aTest',
94  ],
95  ],
96  'PRIMARY KEY (all parts)' => [
97  "PRIMARY KEY USING BTREE (`aField`, bField(199)) KEY_BLOCK_SIZE 4 COMMENT 'aTest'",
98  '',
99  [['aField', 0, null], ['bField', 199, null]],
100  true,
101  false,
102  false,
103  false,
104  'BTREE',
105  [
106  'key_block_size' => 4,
107  'comment' => 'aTest',
108  ],
109  ],
110  'INDEX (single column)' => [
111  'INDEX (`aField`(24))',
112  '',
113  [['aField', 24, null]],
114  false,
115  false,
116  false,
117  false,
118  '',
119  [],
120  ],
121  'INDEX (multiple columns)' => [
122  'INDEX (`aField`(24), bField)',
123  '',
124  [['aField', 24, null], ['bField', 0, null]],
125  false,
126  false,
127  false,
128  false,
129  '',
130  [],
131  ],
132  'INDEX (index name)' => [
133  'INDEX aIndex (`aField`)',
134  'aIndex',
135  [['aField', 0, null]],
136  false,
137  false,
138  false,
139  false,
140  '',
141  [],
142  ],
143  'INDEX (index type)' => [
144  'INDEX USING HASH (`aField`)',
145  '',
146  [['aField', 0, null]],
147  false,
148  false,
149  false,
150  false,
151  'HASH',
152  [],
153  ],
154  'INDEX (index name & type)' => [
155  'INDEX `aIndex` USING BTREE (`aField`)',
156  'aIndex',
157  [['aField', 0, null]],
158  false,
159  false,
160  false,
161  false,
162  'BTREE',
163  [],
164  ],
165  'INDEX (all parts)' => [
166  "INDEX `aIndex` USING BTREE (`aField`) COMMENT 'aComment'",
167  'aIndex',
168  [['aField', 0, null]],
169  false,
170  false,
171  false,
172  false,
173  'BTREE',
174  [
175  'comment' => 'aComment',
176  ],
177  ],
178  'KEY (single column)' => [
179  'KEY (`aField`(24))',
180  '',
181  [['aField', 24, null]],
182  false,
183  false,
184  false,
185  false,
186  '',
187  [],
188  ],
189  'KEY (multiple columns)' => [
190  'KEY (`aField`(24), bField)',
191  '',
192  [['aField', 24, null], ['bField', 0, null]],
193  false,
194  false,
195  false,
196  false,
197  '',
198  [],
199  ],
200  'KEY (index name)' => [
201  'KEY aIndex (`aField`)',
202  'aIndex',
203  [['aField', 0, null]],
204  false,
205  false,
206  false,
207  false,
208  '',
209  [],
210  ],
211  'KEY (index type)' => [
212  'KEY USING BTREE (aField(96))',
213  '',
214  [['aField', 96, null]],
215  false,
216  false,
217  false,
218  false,
219  'BTREE',
220  [],
221  ],
222  'KEY (index name & type)' => [
223  'KEY `aIndex` USING HASH (`aField`)',
224  'aIndex',
225  [['aField', 0, null]],
226  false,
227  false,
228  false,
229  false,
230  'HASH',
231  [],
232  ],
233  'KEY (all parts)' => [
234  'KEY `aIndex` USING HASH (`aField`) WITH PARSER aParser',
235  'aIndex',
236  [['aField', 0, null]],
237  false,
238  false,
239  false,
240  false,
241  'HASH',
242  [
243  'parser' => new ‪Identifier('aParser'),
244  ],
245  ],
246  'UNIQUE (single column)' => [
247  'UNIQUE (`aField`)',
248  '',
249  [['aField', 0, null]],
250  false,
251  true,
252  false,
253  false,
254  '',
255  [],
256  ],
257  'UNIQUE (multiple columns)' => [
258  'UNIQUE (`aField`, bField, cField(40))',
259  '',
260  [['aField', 0, null], ['bField', 0, null], ['cField', 40, null]],
261  false,
262  true,
263  false,
264  false,
265  '',
266  [],
267  ],
268  'UNIQUE INDEX (single column)' => [
269  'UNIQUE INDEX (`aField`)',
270  '',
271  [['aField', 0, null]],
272  false,
273  true,
274  false,
275  false,
276  '',
277  [],
278  ],
279  'UNIQUE KEY (multiple columns)' => [
280  'UNIQUE KEY (`aField`, bField, cField(40))',
281  '',
282  [['aField', 0, null], ['bField', 0, null], ['cField', 40, null]],
283  false,
284  true,
285  false,
286  false,
287  '',
288  [],
289  ],
290  'UNIQUE (index name)' => [
291  'UNIQUE aIndex (`aField`)',
292  'aIndex',
293  [['aField', 0, null]],
294  false,
295  true,
296  false,
297  false,
298  '',
299  [],
300  ],
301  'UNIQUE (index type)' => [
302  'UNIQUE USING BTREE (`aField`)',
303  '',
304  [['aField', 0, null]],
305  false,
306  true,
307  false,
308  false,
309  'BTREE',
310  [],
311  ],
312  'UNIQUE (index name & type)' => [
313  'UNIQUE `aIndex` USING BTREE (`aField`)',
314  'aIndex',
315  [['aField', 0, null]],
316  false,
317  true,
318  false,
319  false,
320  'BTREE',
321  [],
322  ],
323  'UNIQUE (all parts)' => [
324  'UNIQUE `aIndex` USING BTREE (`aField`) KEY_BLOCK_SIZE = 24',
325  'aIndex',
326  [['aField', 0, null]],
327  false,
328  true,
329  false,
330  false,
331  'BTREE',
332  [
333  'key_block_size' => 24,
334  ],
335  ],
336  'FULLTEXT (single column)' => [
337  'FULLTEXT (`aField`)',
338  '',
339  [['aField', 0, null]],
340  false,
341  false,
342  true,
343  false,
344  '',
345  [],
346  ],
347  'FULLTEXT (multiple columns)' => [
348  'FULLTEXT (`aField`, `bField`)',
349  '',
350  [['aField', 0, null], ['bField', 0, null]],
351  false,
352  false,
353  true,
354  false,
355  '',
356  [],
357  ],
358  'FULLTEXT (index name)' => [
359  'FULLTEXT aIndex (`aField`, `bField`)',
360  'aIndex',
361  [['aField', 0, null], ['bField', 0, null]],
362  false,
363  false,
364  true,
365  false,
366  '',
367  [],
368  ],
369  'FULLTEXT (all parts)' => [
370  "FULLTEXT `aIndex` (`aField`, `bField`) COMMENT 'aComment'",
371  'aIndex',
372  [['aField', 0, null], ['bField', 0, null]],
373  false,
374  false,
375  true,
376  false,
377  '',
378  [
379  'comment' => 'aComment',
380  ],
381  ],
382  'FULLTEXT INDEX (single column)' => [
383  'FULLTEXT INDEX (`aField`)',
384  '',
385  [['aField', 0, null]],
386  false,
387  false,
388  true,
389  false,
390  '',
391  [],
392  ],
393  'FULLTEXT INDEX (multiple columns)' => [
394  'FULLTEXT INDEX (`aField`, bField(19))',
395  '',
396  [['aField', 0, null], ['bField', 19, null]],
397  false,
398  false,
399  true,
400  false,
401  '',
402  [],
403  ],
404  'FULLTEXT KEY (single column)' => [
405  'FULLTEXT KEY (aField(20))',
406  '',
407  [['aField', 20, null]],
408  false,
409  false,
410  true,
411  false,
412  '',
413  [],
414  ],
415  'FULLTEXT KEY (multiple columns)' => [
416  'FULLTEXT KEY (aField(20), `bField`)',
417  '',
418  [['aField', 20, null], ['bField', 0, null]],
419  false,
420  false,
421  true,
422  false,
423  '',
424  [],
425  ],
426  'SPATIAL (single column)' => [
427  'SPATIAL (`aField`)',
428  '',
429  [['aField', 0, null]],
430  false,
431  false,
432  false,
433  true,
434  '',
435  [],
436  ],
437  'SPATIAL (multiple columns)' => [
438  'SPATIAL (`aField`, `bField`)',
439  '',
440  [['aField', 0, null], ['bField', 0, null]],
441  false,
442  false,
443  false,
444  true,
445  '',
446  [],
447  ],
448  'SPATIAL (index name)' => [
449  'SPATIAL `aIndex` (`aField`, `bField`)',
450  'aIndex',
451  [['aField', 0, null], ['bField', 0, null]],
452  false,
453  false,
454  false,
455  true,
456  '',
457  [],
458  ],
459  'SPATIAL (all parts)' => [
460  "SPATIAL `aIndex` (`aField`, `bField`) WITH PARSER aParser COMMENT 'aComment'",
461  'aIndex',
462  [['aField', 0, null], ['bField', 0, null]],
463  false,
464  false,
465  false,
466  true,
467  '',
468  [
469  'parser' => new ‪Identifier('aParser'),
470  'comment' => 'aComment',
471  ],
472  ],
473  'SPATIAL INDEX (single column)' => [
474  'SPATIAL INDEX (`aField`)',
475  '',
476  [['aField', 0, null]],
477  false,
478  false,
479  false,
480  true,
481  '',
482  [],
483  ],
484  'SPATIAL INDEX (multiple columns)' => [
485  'SPATIAL INDEX (aField, bField)',
486  '',
487  [['aField', 0, null], ['bField', 0, null]],
488  false,
489  false,
490  false,
491  true,
492  '',
493  [],
494  ],
495  'SPATIAL KEY (single column)' => [
496  'SPATIAL KEY (aField)',
497  '',
498  [['aField', 0, null]],
499  false,
500  false,
501  false,
502  true,
503  '',
504  [],
505  ],
506  'SPATIAL KEY (multiple columns)' => [
507  'SPATIAL KEY (aField, bField(240))',
508  '',
509  [['aField', 0, null], ['bField', 240, null]],
510  false,
511  false,
512  false,
513  true,
514  '',
515  [],
516  ],
517  ];
518  }
519 
533  public function ‪canParseIndexDefinition(
534  string $indexDefinition,
535  string $indexName,
536  array $indexColumns,
537  bool $isPrimary,
538  bool $isUnique,
539  bool $isFulltext,
540  bool $isSpatial,
541  string $indexType,
542  array $indexOptions
543  ) {
544  $statement = sprintf('CREATE TABLE `aTable`(`aField` INT(11), %s);', $indexDefinition);
545  $subject = $this->‪createSubject($statement);
546 
547  self::assertInstanceOf(CreateIndexDefinitionItem::class, $subject);
548  self::assertSame($indexName, $subject->indexName->schemaObjectName);
549  self::assertSame($isPrimary, $subject->isPrimary);
550  self::assertSame($isUnique, $subject->isUnique);
551  self::assertSame($isFulltext, $subject->isFulltext);
552  self::assertSame($isSpatial, $subject->isSpatial);
553  self::assertSame($indexType, $subject->indexType);
554  self::assertEquals($indexOptions, $subject->options);
555 
556  foreach ($indexColumns as $index => $column) {
557  self::assertSame($column[0], $subject->columnNames[$index]->columnName->schemaObjectName);
558  self::assertSame($column[1], $subject->columnNames[$index]->length);
559  self::assertSame($column[2], $subject->columnNames[$index]->direction);
560  }
561  }
562 
569  protected function ‪createSubject(string $statement): ‪CreateIndexDefinitionItem
570  {
571  ‪$parser = new ‪Parser($statement);
573  $createTableStatement = ‪$parser->getAST();
574 
575  return $createTableStatement->createDefinition->items[1];
576  }
577 }
‪TYPO3\CMS\Core\Database\Schema\Parser\AST\Identifier
Definition: Identifier.php:25
‪TYPO3\CMS\Core\Tests\Unit\Database\Schema\Parser\IndexDefinitionTest
Definition: IndexDefinitionTest.php:30
‪TYPO3\CMS\Core\Database\Schema\Parser\AST\CreateTableStatement
Definition: CreateTableStatement.php:24
‪TYPO3\CMS\Core\Database\Schema\Parser\AST\CreateIndexDefinitionItem
Definition: CreateIndexDefinitionItem.php:24
‪TYPO3\CMS\Core\Tests\Unit\Database\Schema\Parser\IndexDefinitionTest\canParseIndexDefinitionDataProvider
‪array canParseIndexDefinitionDataProvider()
Definition: IndexDefinitionTest.php:45
‪$parser
‪$parser
Definition: annotationChecker.php:108
‪TYPO3\CMS\Core\Tests\Unit\Database\Schema\Parser\IndexDefinitionTest\createSubject
‪TYPO3 CMS Core Database Schema Parser AST CreateIndexDefinitionItem createSubject(string $statement)
Definition: IndexDefinitionTest.php:569
‪TYPO3\CMS\Core\Database\Schema\Parser\Parser
Definition: Parser.php:71
‪TYPO3\CMS\Core\Tests\Unit\Database\Schema\Parser\IndexDefinitionTest\canParseIndexDefinition
‪canParseIndexDefinition(string $indexDefinition, string $indexName, array $indexColumns, bool $isPrimary, bool $isUnique, bool $isFulltext, bool $isSpatial, string $indexType, array $indexOptions)
Definition: IndexDefinitionTest.php:533
‪TYPO3\CMS\Core\Tests\Unit\Database\Schema\Parser
Definition: AbstractDataTypeBaseTestCase.php:18