06cce8c3cef28d31bbc9cdd5f79ea56599d1588c
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Database / Schema / Parser / ForeignKeyDefinitionTest.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\CreateForeignKeyDefinitionItem;
20 use TYPO3\CMS\Core\Database\Schema\Parser\AST\CreateTableStatement;
21 use TYPO3\CMS\Core\Database\Schema\Parser\Parser;
22
23 /**
24 * Tests for CreateForeignKeyDefinitionItem
25 */
26 class ForeignKeyDefinitionTest extends \TYPO3\Components\TestingFramework\Core\UnitTestCase
27 {
28 /**
29 * Each parameter array consists of the following values:
30 * - index definition SQL fragment
31 * - index name
32 * - array of index column definitions [name, length, direction]
33 * - foreign table name
34 * - array of foreign column definitions [name, length, direction]
35 *
36 * @return array
37 */
38 public function canParseForeignKeyDefinitionDataProvider(): array
39 {
40 return [
41 // See ReferenceDefinitionTest for actual reference definition parsing tests
42 'FOREIGN KEY (single column)' => [
43 'FOREIGN KEY (`aField`) REFERENCES `bTable` (`bField`)',
44 '',
45 [['aField', 0, null]],
46 'bTable',
47 [['bField', 0, null]],
48 ],
49 'FOREIGN KEY (multiple columns)' => [
50 'FOREIGN KEY (`aField`(20) ASC, `bField`) REFERENCES `bTable` (`cField`, `dField`)',
51 '',
52 [['aField', 20, 'ASC'], ['bField', 0, null]],
53 'bTable',
54 [['cField', 0, null], ['dField', 0, null]],
55 ],
56 'FOREIGN KEY (index name)' => [
57 'FOREIGN KEY `aIndex`(`aField`, `bField`) REFERENCES `bTable` (`cField`(240) DESC, `dField`)',
58 'aIndex',
59 [['aField', 0, null], ['bField', 0, null]],
60 'bTable',
61 [['cField', 240, 'DESC'], ['dField', 0, null]],
62 ],
63 ];
64 }
65
66 /**
67 * @test
68 * @dataProvider canParseForeignKeyDefinitionDataProvider
69 * @param string $indexDefinition
70 * @param string $indexName
71 * @param array $indexColumns
72 * @param string $foreignTableName
73 * @param array $foreignTableColumns
74 */
75 public function canParseForeignKeyDefinition(
76 string $indexDefinition,
77 string $indexName,
78 array $indexColumns,
79 string $foreignTableName,
80 array $foreignTableColumns
81 ) {
82 $statement = sprintf('CREATE TABLE `aTable`(`aField` INT(11), %s);', $indexDefinition);
83 $subject = $this->createSubject($statement);
84
85 $this->assertInstanceOf(CreateForeignKeyDefinitionItem::class, $subject);
86 $this->assertSame($indexName, $subject->indexName->schemaObjectName);
87 $this->assertSame($foreignTableName, $subject->reference->tableName->schemaObjectName);
88
89 foreach ($indexColumns as $index => $column) {
90 $this->assertSame($column[0], $subject->columnNames[$index]->columnName->schemaObjectName);
91 $this->assertSame($column[1], $subject->columnNames[$index]->length);
92 $this->assertSame($column[2], $subject->columnNames[$index]->direction);
93 }
94
95 foreach ($foreignTableColumns as $index => $column) {
96 $this->assertSame($column[0], $subject->reference->columnNames[$index]->columnName->schemaObjectName);
97 $this->assertSame($column[1], $subject->reference->columnNames[$index]->length);
98 $this->assertSame($column[2], $subject->reference->columnNames[$index]->direction);
99 }
100 }
101
102 /**
103 * Parse the CREATE TABLE statement and return the reference definition
104 *
105 * @param string $statement
106 * @return \TYPO3\CMS\Core\Database\Schema\Parser\AST\CreateForeignKeyDefinitionItem
107 */
108 protected function createSubject(string $statement): CreateForeignKeyDefinitionItem
109 {
110 $parser = new Parser($statement);
111 /** @var CreateTableStatement $createTableStatement */
112 $createTableStatement = $parser->getAST();
113
114 return $createTableStatement->createDefinition->items[1];
115 }
116 }