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