Skip to content

Commit

Permalink
Test column diff on all platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
bigfoot90 committed Mar 26, 2024
1 parent c48fdd9 commit b892cf2
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 16 deletions.
1 change: 0 additions & 1 deletion src/Types/EnumType.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use Doctrine\DBAL\Types\Exception\InvalidType;
use Doctrine\DBAL\Types\Exception\ValueNotConvertible;
use ReflectionClass;
use StringBackedEnum;
use Throwable;
use UnitEnum;

Expand Down
9 changes: 9 additions & 0 deletions tests/Functional/Schema/ComparatorTestUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ public static function diffFromDesiredToActualTable(
);
}

public static function assertNoDiffDetected(Connection $connection, Comparator $comparator, Table $table): void
{
$schemaManager = $connection->createSchemaManager();

$diff = self::diffFromActualToDesiredTable($schemaManager, $comparator, $table);

TestCase::assertTrue($diff->isEmpty());
}

public static function assertDiffNotEmpty(Connection $connection, Comparator $comparator, Table $table): void
{
$schemaManager = $connection->createSchemaManager();
Expand Down
17 changes: 2 additions & 15 deletions tests/Functional/Schema/MySQL/ComparatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Tests\Functional\Schema\ComparatorTestUtils;
use Doctrine\DBAL\Tests\FunctionalTestCase;
use Doctrine\DBAL\Tests\Types\EnumObject;
use Doctrine\DBAL\Tests\Types\EnumNative;
use Doctrine\DBAL\Tests\Types\EnumNativeBacked;
use Doctrine\DBAL\Types\EnumType;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Types\Types;
use PHPUnit\Framework\Attributes\DataProvider;

Expand Down Expand Up @@ -220,12 +215,8 @@ public function testMariaDb1043NativeJsonUpgradeDetected(): void
ComparatorTestUtils::assertDiffNotEmpty($this->connection, $this->comparator, $table);
}

public function testEnumDiffEmpty(): void
public function testEnumNoDiffDetected(): void
{
if (! $this->platform instanceof MySQLPlatform) {
self::markTestSkipped();
}

$table = new Table('enum_test_table');

$table->addColumn('enum_col', Types::ENUM, ['members' => ['a', 'b']]);
Expand All @@ -235,15 +226,11 @@ public function testEnumDiffEmpty(): void
$sql = 'ALTER TABLE enum_test_table CHANGE enum_col enum_col ENUM(\'a\', \'b\') NOT NULL';
$this->connection->executeStatement($sql);

ComparatorTestUtils::assertDiffNotEmpty($this->connection, $this->comparator, $table);
ComparatorTestUtils::assertNoDiffDetected($this->connection, $this->comparator, $table);
}

public function testEnumDiffDetected(): void
{
if (! $this->platform instanceof MySQLPlatform) {
self::markTestSkipped();
}

$table = new Table('enum_test_table');

$table->addColumn('enum_col', Types::ENUM, ['members' => ['a', 'b']]);
Expand Down
28 changes: 28 additions & 0 deletions tests/Functional/Schema/Oracle/ComparatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,32 @@ public function testChangeBinaryColumnFixed(): void
$table,
)->isEmpty());
}

public function testEnumNoDiffDetected(): void
{
$table = new Table('enum_test_table');

$table->addColumn('enum_col', Types::ENUM, ['members' => ['a', 'b']]);
$this->dropAndCreateTable($table);

// Revert column to previous ENUM declaration
$sql = 'ALTER TABLE enum_test_table CHANGE enum_col enum_col ENUM(\'a\', \'b\') NOT NULL';
$this->connection->executeStatement($sql);

ComparatorTestUtils::assertNoDiffDetected($this->connection, $this->comparator, $table);
}

public function testEnumDiffDetected(): void
{
$table = new Table('enum_test_table');

$table->addColumn('enum_col', Types::ENUM, ['members' => ['a', 'b']]);
$this->dropAndCreateTable($table);

// Revert column to previous ENUM declaration
$sql = 'ALTER TABLE enum_test_table CHANGE enum_col enum_col ENUM(\'NOT_A_MEMBER_ANYMORE\') NOT NULL';
$this->connection->executeStatement($sql);

ComparatorTestUtils::assertDiffNotEmpty($this->connection, $this->comparator, $table);
}
}
28 changes: 28 additions & 0 deletions tests/Functional/Schema/PostgreSQL/ComparatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,32 @@ private function testColumnModification(callable $addColumn, callable $modifyCol
$table,
)->isEmpty());
}

public function testEnumNoDiffDetected(): void
{
$table = new Table('enum_test_table');

$table->addColumn('enum_col', Types::ENUM, ['members' => ['a', 'b']]);
$this->dropAndCreateTable($table);

// Revert column to previous ENUM declaration
$sql = 'ALTER TABLE enum_test_table CHANGE enum_col enum_col ENUM(\'a\', \'b\') NOT NULL';
$this->connection->executeStatement($sql);

ComparatorTestUtils::assertNoDiffDetected($this->connection, $this->comparator, $table);
}

public function testEnumDiffDetected(): void
{
$table = new Table('enum_test_table');

$table->addColumn('enum_col', Types::ENUM, ['members' => ['a', 'b']]);
$this->dropAndCreateTable($table);

// Revert column to previous ENUM declaration
$sql = 'ALTER TABLE enum_test_table CHANGE enum_col enum_col ENUM(\'NOT_A_MEMBER_ANYMORE\') NOT NULL';
$this->connection->executeStatement($sql);

ComparatorTestUtils::assertDiffNotEmpty($this->connection, $this->comparator, $table);
}
}
28 changes: 28 additions & 0 deletions tests/Functional/Schema/SQLite/ComparatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,32 @@ public function testChangeTableCollation(): void
$column->setPlatformOption('collation', 'NOCASE');
ComparatorTestUtils::assertDiffNotEmpty($this->connection, $this->comparator, $table);
}

public function testEnumNoDiffDetected(): void
{
$table = new Table('enum_test_table');

$table->addColumn('enum_col', Types::ENUM, ['members' => ['a', 'b']]);
$this->dropAndCreateTable($table);

// Revert column to previous ENUM declaration
$sql = 'ALTER TABLE enum_test_table CHANGE enum_col enum_col ENUM(\'a\', \'b\') NOT NULL';
$this->connection->executeStatement($sql);

ComparatorTestUtils::assertNoDiffDetected($this->connection, $this->comparator, $table);
}

public function testEnumDiffDetected(): void
{
$table = new Table('enum_test_table');

$table->addColumn('enum_col', Types::ENUM, ['members' => ['a', 'b']]);
$this->dropAndCreateTable($table);

// Revert column to previous ENUM declaration
$sql = 'ALTER TABLE enum_test_table CHANGE enum_col enum_col ENUM(\'NOT_A_MEMBER_ANYMORE\') NOT NULL';
$this->connection->executeStatement($sql);

ComparatorTestUtils::assertDiffNotEmpty($this->connection, $this->comparator, $table);
}
}

0 comments on commit b892cf2

Please sign in to comment.