|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Doctrine\DBAL\Tests\Functional\Schema; |
| 6 | + |
| 7 | +use Doctrine\DBAL\Platforms\DB2Platform; |
| 8 | +use Doctrine\DBAL\Platforms\OraclePlatform; |
| 9 | +use Doctrine\DBAL\Schema\AbstractSchemaManager; |
| 10 | +use Doctrine\DBAL\Schema\Column; |
| 11 | +use Doctrine\DBAL\Schema\ForeignKeyConstraint; |
| 12 | +use Doctrine\DBAL\Schema\PrimaryKeyConstraint; |
| 13 | +use Doctrine\DBAL\Schema\Table; |
| 14 | +use Doctrine\DBAL\Tests\FunctionalTestCase; |
| 15 | +use Doctrine\DBAL\Types\Types; |
| 16 | + |
| 17 | +class DoubleForeignKeyConstraintTest extends FunctionalTestCase |
| 18 | +{ |
| 19 | + private AbstractSchemaManager $schemaManager; |
| 20 | + |
| 21 | + protected function setUp(): void |
| 22 | + { |
| 23 | + $this->schemaManager = $this->connection->createSchemaManager(); |
| 24 | + } |
| 25 | + |
| 26 | + public function testDoubleForeignKeyConstraint(): void |
| 27 | + { |
| 28 | + $platform = $this->connection->getDatabasePlatform(); |
| 29 | + if ($platform instanceof DB2Platform || $platform instanceof OraclePlatform) { |
| 30 | + self::markTestSkipped('DB2 and Oracle do not allow multiple FKs with the same columns.'); |
| 31 | + } |
| 32 | + |
| 33 | + $articles = Table::editor() |
| 34 | + ->setUnquotedName('articles') |
| 35 | + ->setColumns( |
| 36 | + Column::editor() |
| 37 | + ->setUnquotedName('id') |
| 38 | + ->setTypeName(Types::INTEGER) |
| 39 | + ->create(), |
| 40 | + ) |
| 41 | + ->setPrimaryKeyConstraint( |
| 42 | + PrimaryKeyConstraint::editor() |
| 43 | + ->setUnquotedColumnNames('id') |
| 44 | + ->create(), |
| 45 | + ) |
| 46 | + ->create(); |
| 47 | + |
| 48 | + $orders = Table::editor() |
| 49 | + ->setUnquotedName('orders') |
| 50 | + ->setColumns( |
| 51 | + Column::editor() |
| 52 | + ->setUnquotedName('id') |
| 53 | + ->setTypeName(Types::INTEGER) |
| 54 | + ->create(), |
| 55 | + Column::editor() |
| 56 | + ->setUnquotedName('article_id') |
| 57 | + ->setTypeName(Types::INTEGER) |
| 58 | + ->create(), |
| 59 | + ) |
| 60 | + ->setForeignKeyConstraints( |
| 61 | + ForeignKeyConstraint::editor() |
| 62 | + ->setUnquotedName('articles_fk') |
| 63 | + ->setUnquotedReferencingColumnNames('article_id') |
| 64 | + ->setUnquotedReferencedTableName('articles') |
| 65 | + ->setUnquotedReferencedColumnNames('id') |
| 66 | + ->create(), |
| 67 | + ForeignKeyConstraint::editor() |
| 68 | + ->setUnquotedName('articles_fk_2') |
| 69 | + ->setUnquotedReferencingColumnNames('article_id') |
| 70 | + ->setUnquotedReferencedTableName('articles') |
| 71 | + ->setUnquotedReferencedColumnNames('id') |
| 72 | + ->create(), |
| 73 | + ) |
| 74 | + ->create(); |
| 75 | + |
| 76 | + $this->dropTableIfExists('orders'); |
| 77 | + $this->dropTableIfExists('articles'); |
| 78 | + |
| 79 | + $this->connection->createSchemaManager() |
| 80 | + ->createTable($articles); |
| 81 | + $this->connection->createSchemaManager() |
| 82 | + ->createTable($orders); |
| 83 | + |
| 84 | + $onlineTable = $this->schemaManager->introspectTable('orders'); |
| 85 | + |
| 86 | + self::assertTrue( |
| 87 | + $this->schemaManager->createComparator() |
| 88 | + ->compareTables($orders, $onlineTable) |
| 89 | + ->isEmpty(), |
| 90 | + ); |
| 91 | + } |
| 92 | +} |
0 commit comments