[TASK] Shorten the extend class in all unit tests
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Database / Schema / Parser / ReferenceDefinitionTest.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\CreateTableStatement;
20 use TYPO3\CMS\Core\Database\Schema\Parser\AST\ReferenceDefinition;
21 use TYPO3\CMS\Core\Database\Schema\Parser\Parser;
22 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
23
24 /**
25 * Tests for ReferenceDefinition
26 */
27 class ReferenceDefinitionTest extends UnitTestCase
28 {
29 /**
30 * Each parameter array consists of the following values:
31 * - reference definition SQL fragment
32 * - expected table Name
33 * - array of index column definitions [name, length, direction]
34 * - MATCH value
35 * - ON DELETE value
36 * - ON UPDATE value
37 *
38 * @return array
39 */
40 public function canParseReferenceDefinitionDataProvider(): array
41 {
42 return [
43 'REFERENCES `anotherTable`(`aColumn`)' => [
44 'REFERENCES `anotherTable`(`aColumn`)',
45 'anotherTable',
46 [['aColumn', 0, null]],
47 null,
48 null,
49 null,
50 ],
51 'REFERENCES `anotherTable`(`aColumn`, anotherColumn)' => [
52 'REFERENCES `anotherTable`(`aColumn`, anotherColumn)',
53 'anotherTable',
54 [['aColumn', 0, null], ['anotherColumn', 0, null]],
55 null,
56 null,
57 null,
58 ],
59 'REFERENCES `anotherTable`(`aColumn`(199),`anotherColumn`)' => [
60 'REFERENCES `anotherTable`(`aColumn`(199),`anotherColumn`)',
61 'anotherTable',
62 [['aColumn', 199, null], ['anotherColumn', 0, null]],
63 null,
64 null,
65 null,
66 ],
67 'REFERENCES `anotherTable`(`aColumn`(199) ASC, anotherColumn DESC)' => [
68 'REFERENCES `anotherTable`(`aColumn`(199) ASC, anotherColumn DESC)',
69 'anotherTable',
70 [['aColumn', 199, 'ASC'], ['anotherColumn', 0, 'DESC']],
71 null,
72 null,
73 null,
74 ],
75 'REFERENCES anotherTable(aColumn) MATCH FULL' => [
76 'REFERENCES anotherTable(aColumn) MATCH FULL',
77 'anotherTable',
78 [['aColumn', 0, null]],
79 'FULL',
80 null,
81 null,
82 ],
83 'REFERENCES anotherTable(aColumn) MATCH PARTIAL' => [
84 'REFERENCES anotherTable(aColumn) MATCH PARTIAL',
85 'anotherTable',
86 [['aColumn', 0, null]],
87 'PARTIAL',
88 null,
89 null,
90 ],
91 'REFERENCES anotherTable(aColumn) MATCH SIMPLE' => [
92 'REFERENCES anotherTable(aColumn) MATCH SIMPLE',
93 'anotherTable',
94 [['aColumn', 0, null]],
95 'SIMPLE',
96 null,
97 null,
98 ],
99 'REFERENCES anotherTable(aColumn) ON DELETE RESTRICT' => [
100 'REFERENCES anotherTable(aColumn) ON DELETE RESTRICT',
101 'anotherTable',
102 [['aColumn', 0, null]],
103 null,
104 'RESTRICT',
105 null,
106 ],
107 'REFERENCES anotherTable(aColumn) ON DELETE CASCADE' => [
108 'REFERENCES anotherTable(aColumn) ON DELETE CASCADE',
109 'anotherTable',
110 [['aColumn', 0, null]],
111 null,
112 'CASCADE',
113 null,
114 ],
115 'REFERENCES anotherTable(aColumn) ON DELETE SET NULL' => [
116 'REFERENCES anotherTable(aColumn) ON DELETE SET NULL',
117 'anotherTable',
118 [['aColumn', 0, null]],
119 null,
120 'SET NULL',
121 null,
122 ],
123 'REFERENCES anotherTable(aColumn) ON DELETE NO ACTION' => [
124 'REFERENCES anotherTable(aColumn) ON DELETE NO ACTION',
125 'anotherTable',
126 [['aColumn', 0, null]],
127 null,
128 'NO ACTION',
129 null,
130 ],
131 'REFERENCES anotherTable(aColumn) ON UPDATE RESTRICT' => [
132 'REFERENCES anotherTable(aColumn) ON UPDATE RESTRICT',
133 'anotherTable',
134 [['aColumn', 0, null]],
135 null,
136 null,
137 'RESTRICT',
138 ],
139 'REFERENCES anotherTable(aColumn) ON UPDATE CASCADE' => [
140 'REFERENCES anotherTable(aColumn) ON UPDATE CASCADE',
141 'anotherTable',
142 [['aColumn', 0, null]],
143 null,
144 null,
145 'CASCADE',
146 ],
147 'REFERENCES anotherTable(aColumn) ON UPDATE SET NULL' => [
148 'REFERENCES anotherTable(aColumn) ON UPDATE SET NULL',
149 'anotherTable',
150 [['aColumn', 0, null]],
151 null,
152 null,
153 'SET NULL',
154 ],
155 'REFERENCES anotherTable(aColumn) ON UPDATE NO ACTION' => [
156 'REFERENCES anotherTable(aColumn) ON UPDATE NO ACTION',
157 'anotherTable',
158 [['aColumn', 0, null]],
159 null,
160 null,
161 'NO ACTION',
162 ],
163 'REFERENCES anotherTable(uid, `hash`(199) DESC) MATCH PARTIAL ON DELETE RESTRICT ON UPDATE SET NULL' => [
164 'REFERENCES anotherTable(uid, `hash`(199) DESC) MATCH PARTIAL ON DELETE RESTRICT ON UPDATE SET NULL',
165 'anotherTable',
166 [['uid', 0, null], ['hash', 199, 'DESC']],
167 'PARTIAL',
168 'RESTRICT',
169 'SET NULL',
170 ],
171 ];
172 }
173
174 /**
175 * @test
176 * @dataProvider canParseReferenceDefinitionDataProvider
177 * @param string $columnAttribute
178 * @param string $table
179 * @param array $columns
180 * @param string $match
181 * @param string $onDelete
182 * @param string $onUpdate
183 */
184 public function canParseReferenceDefinition(
185 string $columnAttribute,
186 string $table,
187 array $columns,
188 string $match = null,
189 string $onDelete = null,
190 string $onUpdate = null
191 ) {
192 $statement = sprintf('CREATE TABLE `aTable`(`aField` INT(11) %s);', $columnAttribute);
193 $subject = $this->createSubject($statement);
194
195 $this->assertInstanceOf(ReferenceDefinition::class, $subject);
196 $this->assertSame($table, $subject->tableName->schemaObjectName);
197 $this->assertSame($match, $subject->match);
198 $this->assertSame($onDelete, $subject->onDelete);
199 $this->assertSame($onUpdate, $subject->onUpdate);
200
201 foreach ($columns as $index => $column) {
202 $this->assertSame($column[0], $subject->columnNames[$index]->columnName->schemaObjectName);
203 $this->assertSame($column[1], $subject->columnNames[$index]->length);
204 $this->assertSame($column[2], $subject->columnNames[$index]->direction);
205 }
206 }
207
208 /**
209 * Parse the CREATE TABLE statement and return the reference definition
210 *
211 * @param string $statement
212 * @return \TYPO3\CMS\Core\Database\Schema\Parser\AST\ReferenceDefinition
213 */
214 protected function createSubject(string $statement): ReferenceDefinition
215 {
216 $parser = new Parser($statement);
217 /** @var CreateTableStatement $createTableStatement */
218 $createTableStatement = $parser->getAST();
219
220 return $createTableStatement->createDefinition->items[0]->reference;
221 }
222 }