Skip to content

Commit

Permalink
Functional testing
Browse files Browse the repository at this point in the history
  • Loading branch information
bigfoot90 committed Mar 26, 2024
1 parent 420239f commit bb32bc9
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/Types/EnumType.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public function convertToPHPValue(mixed $value, AbstractPlatform $platform): mix
}

if (! is_string($value)) {
throw InvalidFormat::new($value, $this->name, ['null', 'string']);
throw InvalidFormat::new($value, $this->name, 'string');

Check warning on line 112 in src/Types/EnumType.php

View check run for this annotation

Codecov / codecov/patch

src/Types/EnumType.php#L112

Added line #L112 was not covered by tests
}

if ($this->enumClassname === null) {
Expand Down
85 changes: 81 additions & 4 deletions tests/Functional/Schema/MySQL/ComparatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
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\EnumPhp;
use Doctrine\DBAL\Tests\Types\EnumPhpBacked;
use Doctrine\DBAL\Types\EnumType;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Types\Types;
use PHPUnit\Framework\Attributes\DataProvider;

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

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

$table = new Table('mariadb_enum_upgrade');
$table = new Table('enum_upgrade');

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

// Revert column to old LONGTEXT declaration
$sql = 'ALTER TABLE mariadb_json_upgrade CHANGE json_col json_col LONGTEXT NOT NULL COMMENT \'(DC2Type:json)\'';
// Revert column to previous ENUM declaration
$sql = 'ALTER TABLE enum_upgrade CHANGE enum_col enum_col ENUM(\'NOT_A_MEMBER_ANYMORE\') NOT NULL';
$this->connection->executeStatement($sql);

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

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

Type::addType('enum_php', EnumType::class);

/** @var EnumType $type */
$type = Type::getType('enum_php');
$type->enumClassname = EnumPhp::class;

$table = new Table('enum_upgrade');

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

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

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

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

Type::addType('enum_php_backed', EnumType::class);

/** @var EnumType $type */
$type = Type::getType('enum_php_backed');
$type->enumClassname = EnumPhpBacked::class;

$table = new Table('enum_upgrade');

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

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

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

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

Type::addType('enum_object', EnumType::class);

/** @var EnumType $type */
$type = Type::getType('enum_object');
$type->enumClassname = EnumObject::class;

$table = new Table('enum_upgrade');

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

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

ComparatorTestUtils::assertDiffNotEmpty($this->connection, $this->comparator, $table);
Expand Down
10 changes: 5 additions & 5 deletions tests/Types/EnumTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ public function testConvertToPHPEnumBacked(): void

public function testConvertToPHPObject(): void
{
$this->type->enumClassname = EnumClass::class;
$this->type->enumClassname = EnumObject::class;

self::assertInstanceOf($this->type->enumClassname, $this->type->convertToPHPValue('a', $this->platform));
self::assertEquals(new EnumClass('a'), $this->type->convertToPHPValue('a', $this->platform));
self::assertEquals(new EnumObject('a'), $this->type->convertToPHPValue('a', $this->platform));
self::assertNull($this->type->convertToPHPValue(null, $this->platform));
}

Expand Down Expand Up @@ -89,9 +89,9 @@ public function testConvertEnumBackedToDatabaseValue(): void

public function testConvertObjectToDatabaseValue(): void
{
$this->type->enumClassname = EnumClass::class;
$this->type->enumClassname = EnumObject::class;

self::assertSame('a', $this->type->convertToDatabaseValue(new EnumClass('a'), $this->platform));
self::assertSame('a', $this->type->convertToDatabaseValue(new EnumObject('a'), $this->platform));
self::assertNull($this->type->convertToDatabaseValue(null, $this->platform));
}

Expand Down Expand Up @@ -219,7 +219,7 @@ enum WrongEnumPhpBacked: string
case WRONG = 'wrong';
}

final class EnumClass implements Stringable
final class EnumObject implements Stringable
{
public function __construct(
private string $value,
Expand Down

0 comments on commit bb32bc9

Please sign in to comment.