‪TYPO3CMS  ‪main
ReferenceDefinitionTest.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 
20 use PHPUnit\Framework\Attributes\DataProvider;
21 use PHPUnit\Framework\Attributes\Test;
27 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
28 
32 final class ‪ReferenceDefinitionTest extends UnitTestCase
33 {
43  public static function ‪canParseReferenceDefinitionDataProvider(): array
44  {
45  return [
46  'REFERENCES `anotherTable`(`aColumn`)' => [
47  'REFERENCES `anotherTable`(`aColumn`)',
48  'anotherTable',
49  [['aColumn', 0, null]],
50  null,
51  null,
52  null,
53  ],
54  'REFERENCES `anotherTable`(`aColumn`, anotherColumn)' => [
55  'REFERENCES `anotherTable`(`aColumn`, anotherColumn)',
56  'anotherTable',
57  [['aColumn', 0, null], ['anotherColumn', 0, null]],
58  null,
59  null,
60  null,
61  ],
62  'REFERENCES `anotherTable`(`aColumn`(199),`anotherColumn`)' => [
63  'REFERENCES `anotherTable`(`aColumn`(199),`anotherColumn`)',
64  'anotherTable',
65  [['aColumn', 199, null], ['anotherColumn', 0, null]],
66  null,
67  null,
68  null,
69  ],
70  'REFERENCES `anotherTable`(`aColumn`(199) ASC, anotherColumn DESC)' => [
71  'REFERENCES `anotherTable`(`aColumn`(199) ASC, anotherColumn DESC)',
72  'anotherTable',
73  [['aColumn', 199, 'ASC'], ['anotherColumn', 0, 'DESC']],
74  null,
75  null,
76  null,
77  ],
78  'REFERENCES anotherTable(aColumn) MATCH FULL' => [
79  'REFERENCES anotherTable(aColumn) MATCH FULL',
80  'anotherTable',
81  [['aColumn', 0, null]],
82  'FULL',
83  null,
84  null,
85  ],
86  'REFERENCES anotherTable(aColumn) MATCH PARTIAL' => [
87  'REFERENCES anotherTable(aColumn) MATCH PARTIAL',
88  'anotherTable',
89  [['aColumn', 0, null]],
90  'PARTIAL',
91  null,
92  null,
93  ],
94  'REFERENCES anotherTable(aColumn) MATCH SIMPLE' => [
95  'REFERENCES anotherTable(aColumn) MATCH SIMPLE',
96  'anotherTable',
97  [['aColumn', 0, null]],
98  'SIMPLE',
99  null,
100  null,
101  ],
102  'REFERENCES anotherTable(aColumn) ON DELETE RESTRICT' => [
103  'REFERENCES anotherTable(aColumn) ON DELETE RESTRICT',
104  'anotherTable',
105  [['aColumn', 0, null]],
106  null,
107  'RESTRICT',
108  null,
109  ],
110  'REFERENCES anotherTable(aColumn) ON DELETE CASCADE' => [
111  'REFERENCES anotherTable(aColumn) ON DELETE CASCADE',
112  'anotherTable',
113  [['aColumn', 0, null]],
114  null,
115  'CASCADE',
116  null,
117  ],
118  'REFERENCES anotherTable(aColumn) ON DELETE SET NULL' => [
119  'REFERENCES anotherTable(aColumn) ON DELETE SET NULL',
120  'anotherTable',
121  [['aColumn', 0, null]],
122  null,
123  'SET NULL',
124  null,
125  ],
126  'REFERENCES anotherTable(aColumn) ON DELETE NO ACTION' => [
127  'REFERENCES anotherTable(aColumn) ON DELETE NO ACTION',
128  'anotherTable',
129  [['aColumn', 0, null]],
130  null,
131  'NO ACTION',
132  null,
133  ],
134  'REFERENCES anotherTable(aColumn) ON UPDATE RESTRICT' => [
135  'REFERENCES anotherTable(aColumn) ON UPDATE RESTRICT',
136  'anotherTable',
137  [['aColumn', 0, null]],
138  null,
139  null,
140  'RESTRICT',
141  ],
142  'REFERENCES anotherTable(aColumn) ON UPDATE CASCADE' => [
143  'REFERENCES anotherTable(aColumn) ON UPDATE CASCADE',
144  'anotherTable',
145  [['aColumn', 0, null]],
146  null,
147  null,
148  'CASCADE',
149  ],
150  'REFERENCES anotherTable(aColumn) ON UPDATE SET NULL' => [
151  'REFERENCES anotherTable(aColumn) ON UPDATE SET NULL',
152  'anotherTable',
153  [['aColumn', 0, null]],
154  null,
155  null,
156  'SET NULL',
157  ],
158  'REFERENCES anotherTable(aColumn) ON UPDATE NO ACTION' => [
159  'REFERENCES anotherTable(aColumn) ON UPDATE NO ACTION',
160  'anotherTable',
161  [['aColumn', 0, null]],
162  null,
163  null,
164  'NO ACTION',
165  ],
166  'REFERENCES anotherTable(uid, `hash`(199) DESC) MATCH PARTIAL ON DELETE RESTRICT ON UPDATE SET NULL' => [
167  'REFERENCES anotherTable(uid, `hash`(199) DESC) MATCH PARTIAL ON DELETE RESTRICT ON UPDATE SET NULL',
168  'anotherTable',
169  [['uid', 0, null], ['hash', 199, 'DESC']],
170  'PARTIAL',
171  'RESTRICT',
172  'SET NULL',
173  ],
174  ];
175  }
176 
177  #[DataProvider('canParseReferenceDefinitionDataProvider')]
178  #[Test]
180  string $columnAttribute,
181  string $table,
182  array $columns,
183  string $match = null,
184  string $onDelete = null,
185  string $onUpdate = null
186  ): void {
187  $statement = sprintf('CREATE TABLE `aTable`(`aField` INT(11) %s);', $columnAttribute);
188  $subject = $this->‪createSubject($statement);
189 
190  self::assertInstanceOf(ReferenceDefinition::class, $subject);
191  self::assertSame($table, $subject->tableName->schemaObjectName);
192  self::assertSame($match, $subject->match);
193  self::assertSame($onDelete, $subject->onDelete);
194  self::assertSame($onUpdate, $subject->onUpdate);
195 
196  foreach ($columns as $index => $column) {
197  self::assertSame($column[0], $subject->columnNames[$index]->columnName->schemaObjectName);
198  self::assertSame($column[1], $subject->columnNames[$index]->length);
199  self::assertSame($column[2], $subject->columnNames[$index]->direction);
200  }
201  }
202 
206  private function ‪createSubject(string $statement): ‪ReferenceDefinition
207  {
208  ‪$parser = new ‪Parser(new ‪Lexer());
210  $createTableStatement = ‪$parser->getAST($statement);
212  $item = $createTableStatement->createDefinition->items[0];
213  return $item->reference;
214  }
215 }
‪TYPO3\CMS\Core\Database\Schema\Parser\AST\CreateTableStatement
Definition: CreateTableStatement.php:26
‪TYPO3\CMS\Core\Database\Schema\Parser\AST\CreateColumnDefinitionItem
Definition: CreateColumnDefinitionItem.php:29
‪$parser
‪$parser
Definition: annotationChecker.php:103
‪TYPO3\CMS\Core\Tests\Unit\Database\Schema\Parser\ReferenceDefinitionTest\createSubject
‪createSubject(string $statement)
Definition: ReferenceDefinitionTest.php:206
‪TYPO3\CMS\Core\Tests\Unit\Database\Schema\Parser\ReferenceDefinitionTest\canParseReferenceDefinition
‪canParseReferenceDefinition(string $columnAttribute, string $table, array $columns, string $match=null, string $onDelete=null, string $onUpdate=null)
Definition: ReferenceDefinitionTest.php:179
‪TYPO3\CMS\Core\Database\Schema\Parser\Parser
Definition: Parser.php:75
‪TYPO3\CMS\Core\Tests\Unit\Database\Schema\Parser\ReferenceDefinitionTest\canParseReferenceDefinitionDataProvider
‪static canParseReferenceDefinitionDataProvider()
Definition: ReferenceDefinitionTest.php:43
‪TYPO3\CMS\Core\Tests\Unit\Database\Schema\Parser\ReferenceDefinitionTest
Definition: ReferenceDefinitionTest.php:33
‪TYPO3\CMS\Core\Tests\Unit\Database\Schema\Parser
Definition: AbstractDataTypeBaseTestCase.php:18
‪TYPO3\CMS\Core\Database\Schema\Parser\AST\ReferenceDefinition
Definition: ReferenceDefinition.php:28
‪TYPO3\CMS\Core\Database\Schema\Parser\Lexer
Definition: Lexer.php:26