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