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