‪TYPO3CMS  10.4
ForeignKeyDefinitionTest.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 ‪ForeignKeyDefinitionTest extends UnitTestCase
29 {
41  {
42  return [
43  // See ReferenceDefinitionTest for actual reference definition parsing tests
44  'FOREIGN KEY (single column)' => [
45  'FOREIGN KEY (`aField`) REFERENCES `bTable` (`bField`)',
46  '',
47  [['aField', 0, null]],
48  'bTable',
49  [['bField', 0, null]],
50  ],
51  'FOREIGN KEY (multiple columns)' => [
52  'FOREIGN KEY (`aField`(20) ASC, `bField`) REFERENCES `bTable` (`cField`, `dField`)',
53  '',
54  [['aField', 20, 'ASC'], ['bField', 0, null]],
55  'bTable',
56  [['cField', 0, null], ['dField', 0, null]],
57  ],
58  'FOREIGN KEY (index name)' => [
59  'FOREIGN KEY `aIndex`(`aField`, `bField`) REFERENCES `bTable` (`cField`(240) DESC, `dField`)',
60  'aIndex',
61  [['aField', 0, null], ['bField', 0, null]],
62  'bTable',
63  [['cField', 240, 'DESC'], ['dField', 0, null]],
64  ],
65  ];
66  }
67 
78  string $indexDefinition,
79  string $indexName,
80  array $indexColumns,
81  string $foreignTableName,
82  array $foreignTableColumns
83  ) {
84  $statement = sprintf('CREATE TABLE `aTable`(`aField` INT(11), %s);', $indexDefinition);
85  $subject = $this->‪createSubject($statement);
86 
87  self::assertInstanceOf(CreateForeignKeyDefinitionItem::class, $subject);
88  self::assertSame($indexName, $subject->indexName->schemaObjectName);
89  self::assertSame($foreignTableName, $subject->reference->tableName->schemaObjectName);
90 
91  foreach ($indexColumns as $index => $column) {
92  self::assertSame($column[0], $subject->columnNames[$index]->columnName->schemaObjectName);
93  self::assertSame($column[1], $subject->columnNames[$index]->length);
94  self::assertSame($column[2], $subject->columnNames[$index]->direction);
95  }
96 
97  foreach ($foreignTableColumns as $index => $column) {
98  self::assertSame($column[0], $subject->reference->columnNames[$index]->columnName->schemaObjectName);
99  self::assertSame($column[1], $subject->reference->columnNames[$index]->length);
100  self::assertSame($column[2], $subject->reference->columnNames[$index]->direction);
101  }
102  }
103 
110  protected function ‪createSubject(string $statement): ‪CreateForeignKeyDefinitionItem
111  {
112  ‪$parser = new ‪Parser($statement);
114  $createTableStatement = ‪$parser->getAST();
115 
116  return $createTableStatement->createDefinition->items[1];
117  }
118 }
‪TYPO3\CMS\Core\Tests\Unit\Database\Schema\Parser\ForeignKeyDefinitionTest\canParseForeignKeyDefinition
‪canParseForeignKeyDefinition(string $indexDefinition, string $indexName, array $indexColumns, string $foreignTableName, array $foreignTableColumns)
Definition: ForeignKeyDefinitionTest.php:77
‪TYPO3\CMS\Core\Database\Schema\Parser\AST\CreateTableStatement
Definition: CreateTableStatement.php:24
‪TYPO3\CMS\Core\Tests\Unit\Database\Schema\Parser\ForeignKeyDefinitionTest\canParseForeignKeyDefinitionDataProvider
‪array canParseForeignKeyDefinitionDataProvider()
Definition: ForeignKeyDefinitionTest.php:40
‪$parser
‪$parser
Definition: annotationChecker.php:108
‪TYPO3\CMS\Core\Database\Schema\Parser\AST\CreateForeignKeyDefinitionItem
Definition: CreateForeignKeyDefinitionItem.php:24
‪TYPO3\CMS\Core\Database\Schema\Parser\Parser
Definition: Parser.php:71
‪TYPO3\CMS\Core\Tests\Unit\Database\Schema\Parser\ForeignKeyDefinitionTest\createSubject
‪TYPO3 CMS Core Database Schema Parser AST CreateForeignKeyDefinitionItem createSubject(string $statement)
Definition: ForeignKeyDefinitionTest.php:110
‪TYPO3\CMS\Core\Tests\Unit\Database\Schema\Parser
Definition: AbstractDataTypeBaseTestCase.php:18
‪TYPO3\CMS\Core\Tests\Unit\Database\Schema\Parser\ForeignKeyDefinitionTest
Definition: ForeignKeyDefinitionTest.php:29