TYPO3 CMS  TYPO3_8-7
ForeignKeyDefinitionTest.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
3 
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 
22 
26 class ForeignKeyDefinitionTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
27 {
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 
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 
108  protected function createSubject(string $statement): CreateForeignKeyDefinitionItem
109  {
110  $parser = new Parser($statement);
112  $createTableStatement = $parser->getAST();
113 
114  return $createTableStatement->createDefinition->items[1];
115  }
116 }
canParseForeignKeyDefinition(string $indexDefinition, string $indexName, array $indexColumns, string $foreignTableName, array $foreignTableColumns)