Skip to content

Commit

Permalink
fix: max string length resolution in AbstractPlatform::getEnumDeclara…
Browse files Browse the repository at this point in the history
…tionSQL()
  • Loading branch information
David Kurka committed Nov 1, 2024
1 parent 604277e commit 6bdd70a
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Platforms/AbstractPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
use function is_float;
use function is_int;
use function is_string;
use function key;
use function max;
use function mb_strlen;
use function preg_quote;
Expand Down Expand Up @@ -209,7 +210,11 @@ public function getEnumDeclarationSQL(array $column): string
throw ColumnValuesRequired::new($this, 'ENUM');
}

return $this->getStringTypeDeclarationSQL(['length' => max(...array_map(mb_strlen(...), $column['values']))]);
$length = count($column['values']) > 1
? max(...array_map(mb_strlen(...), $column['values']))
: mb_strlen($column['values'][key($column['values'])]);

return $this->getStringTypeDeclarationSQL(['length' => $length]);
}

/**
Expand Down
9 changes: 9 additions & 0 deletions tests/Platforms/AbstractMySQLPlatformTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -694,4 +694,13 @@ protected function createComparator(): Comparator
new DefaultTableOptions('utf8mb4', 'utf8mb4_general_ci'),
);
}

/** @return array<string, array{array<string>, string}> */
public static function getEnumDeclarationSQLProvider(): array
{
return [
'single value' => [['foo'], "ENUM('foo')"],
'multiple values' => [['foo', 'bar1'], "ENUM('foo', 'bar1')"],
];
}
}
35 changes: 35 additions & 0 deletions tests/Platforms/AbstractPlatformTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Exception\InvalidColumnDeclaration;
use Doctrine\DBAL\Exception\InvalidColumnType\ColumnValuesRequired;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\ColumnDiff;
Expand Down Expand Up @@ -1030,4 +1031,38 @@ public static function asciiStringSqlDeclarationDataProvider(): array
['CHAR(12)', ['length' => 12, 'fixed' => true]],
];
}

/** @param array<string> $values */
#[DataProvider('getEnumDeclarationSQLProvider')]
public function testGetEnumDeclarationSQL(array $values, string $expectedSQL): void
{
self::assertSame($expectedSQL, $this->platform->getEnumDeclarationSQL(['values' => $values]));
}

/** @return array<string, array{array<string>, string}> */
public static function getEnumDeclarationSQLProvider(): array
{
return [
'single value' => [['foo'], 'VARCHAR(3)'],
'multiple values' => [['foo', 'bar1'], 'VARCHAR(4)'],
];
}

/** @param array<mixed> $column */
#[DataProvider('getEnumDeclarationSQLWithInvalidValuesProvider')]
public function testGetEnumDeclarationSQLWithInvalidValues(array $column): void
{
self::expectException(ColumnValuesRequired::class);
$this->platform->getEnumDeclarationSQL($column);
}

/** @return array<string, array{array<mixed>}> */
public static function getEnumDeclarationSQLWithInvalidValuesProvider(): array
{
return [
"field 'values' does not exist" => [[]],
"field 'values' is not an array" => [['values' => 'foo']],
"field 'values' is an empty array" => [['values' => []]],
];
}
}
9 changes: 9 additions & 0 deletions tests/Platforms/OraclePlatformTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -595,4 +595,13 @@ public static function asciiStringSqlDeclarationDataProvider(): array
['CHAR(12)', ['length' => 12, 'fixed' => true]],
];
}

/** @return array<string, array{array<string>, string}> */
public static function getEnumDeclarationSQLProvider(): array
{
return [
'single value' => [['foo'], 'VARCHAR2(3)'],
'multiple values' => [['foo', 'bar1'], 'VARCHAR2(4)'],
];
}
}
9 changes: 9 additions & 0 deletions tests/Platforms/SQLServerPlatformTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1124,4 +1124,13 @@ public function testGeneratesTypeDeclarationForDateTimeTz(): void
{
self::assertEquals('DATETIMEOFFSET(6)', $this->platform->getDateTimeTzTypeDeclarationSQL([]));
}

/** @return array<string, array{array<string>, string}> */
public static function getEnumDeclarationSQLProvider(): array
{
return [
'single value' => [['foo'], 'NVARCHAR(3)'],
'multiple values' => [['foo', 'bar1'], 'NVARCHAR(4)'],
];
}
}

0 comments on commit 6bdd70a

Please sign in to comment.