|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Doctrine\DBAL\Tests\Functional\Schema\PostgreSQL; |
| 6 | + |
| 7 | +use Doctrine\DBAL\Platforms\PostgreSQLPlatform; |
| 8 | +use Doctrine\DBAL\Schema\AbstractSchemaManager; |
| 9 | +use Doctrine\DBAL\Schema\Column; |
| 10 | +use Doctrine\DBAL\Schema\Comparator; |
| 11 | +use Doctrine\DBAL\Schema\Table; |
| 12 | +use Doctrine\DBAL\Tests\Functional\Schema\ComparatorTestUtils; |
| 13 | +use Doctrine\DBAL\Tests\FunctionalTestCase; |
| 14 | +use Doctrine\DBAL\Types\Type; |
| 15 | +use Doctrine\DBAL\Types\Types; |
| 16 | + |
| 17 | +final class ComparatorTest extends FunctionalTestCase |
| 18 | +{ |
| 19 | + private AbstractSchemaManager $schemaManager; |
| 20 | + |
| 21 | + private Comparator $comparator; |
| 22 | + |
| 23 | + protected function setUp(): void |
| 24 | + { |
| 25 | + if (! $this->connection->getDatabasePlatform() instanceof PostgreSQLPlatform) { |
| 26 | + self::markTestSkipped('This test covers PostgreSQL-specific schema comparison scenarios.'); |
| 27 | + } |
| 28 | + |
| 29 | + $this->schemaManager = $this->connection->createSchemaManager(); |
| 30 | + $this->comparator = $this->schemaManager->createComparator(); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * The PostgreSQL platform maps both BLOB and BINARY columns to the BYTEA column type. |
| 35 | + * |
| 36 | + * @see PostgreSQLPlatform::getBlobTypeDeclarationSQL() |
| 37 | + */ |
| 38 | + public function testCompareBinaryAndBlob(): void |
| 39 | + { |
| 40 | + $this->testColumnModification(static function (Table $table, string $name): Column { |
| 41 | + return $table->addColumn($name, Types::BINARY); |
| 42 | + }, static function (Column $column): void { |
| 43 | + $column->setType(Type::getType(Types::BLOB)); |
| 44 | + }); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * The PostgreSQL platform maps both BINARY and VARBINARY columns to the BYTEA column type. |
| 49 | + * |
| 50 | + * @see PostgreSQLPlatform::getVarbinaryTypeDeclarationSQLSnippet() |
| 51 | + */ |
| 52 | + public function testCompareBinaryAndVarbinary(): void |
| 53 | + { |
| 54 | + $this->testColumnModification(static function (Table $table, string $name): Column { |
| 55 | + return $table->addColumn($name, Types::BINARY); |
| 56 | + }, static function (Column $column): void { |
| 57 | + $column->setFixed(true); |
| 58 | + }); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * The PostgreSQL platform disregards the "length" attribute of BINARY and VARBINARY columns. |
| 63 | + * |
| 64 | + * @see PostgreSQLPlatform::getBinaryTypeDeclarationSQLSnippet() |
| 65 | + */ |
| 66 | + public function testCompareBinariesOfDifferentLength(): void |
| 67 | + { |
| 68 | + $this->testColumnModification(static function (Table $table, string $name): Column { |
| 69 | + return $table->addColumn($name, Types::BINARY, ['length' => 16]); |
| 70 | + }, static function (Column $column): void { |
| 71 | + $column->setLength(32); |
| 72 | + }); |
| 73 | + } |
| 74 | + |
| 75 | + private function testColumnModification(callable $addColumn, callable $modifyColumn): void |
| 76 | + { |
| 77 | + $table = new Table('comparator_test'); |
| 78 | + $column = $addColumn($table, 'id'); |
| 79 | + $this->dropAndCreateTable($table); |
| 80 | + |
| 81 | + $modifyColumn($column); |
| 82 | + |
| 83 | + self::assertNull(ComparatorTestUtils::diffFromActualToDesiredTable( |
| 84 | + $this->schemaManager, |
| 85 | + $this->comparator, |
| 86 | + $table, |
| 87 | + )); |
| 88 | + |
| 89 | + self::assertNull(ComparatorTestUtils::diffFromDesiredToActualTable( |
| 90 | + $this->schemaManager, |
| 91 | + $this->comparator, |
| 92 | + $table, |
| 93 | + )); |
| 94 | + } |
| 95 | +} |
0 commit comments