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