Skip to content

Commit 6bdd70a

Browse files
author
David Kurka
committed
fix: max string length resolution in AbstractPlatform::getEnumDeclarationSQL()
1 parent 604277e commit 6bdd70a

5 files changed

+68
-1
lines changed

src/Platforms/AbstractPlatform.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
use function is_float;
5353
use function is_int;
5454
use function is_string;
55+
use function key;
5556
use function max;
5657
use function mb_strlen;
5758
use function preg_quote;
@@ -209,7 +210,11 @@ public function getEnumDeclarationSQL(array $column): string
209210
throw ColumnValuesRequired::new($this, 'ENUM');
210211
}
211212

212-
return $this->getStringTypeDeclarationSQL(['length' => max(...array_map(mb_strlen(...), $column['values']))]);
213+
$length = count($column['values']) > 1
214+
? max(...array_map(mb_strlen(...), $column['values']))
215+
: mb_strlen($column['values'][key($column['values'])]);
216+
217+
return $this->getStringTypeDeclarationSQL(['length' => $length]);
213218
}
214219

215220
/**

tests/Platforms/AbstractMySQLPlatformTestCase.php

+9
Original file line numberDiff line numberDiff line change
@@ -694,4 +694,13 @@ protected function createComparator(): Comparator
694694
new DefaultTableOptions('utf8mb4', 'utf8mb4_general_ci'),
695695
);
696696
}
697+
698+
/** @return array<string, array{array<string>, string}> */
699+
public static function getEnumDeclarationSQLProvider(): array
700+
{
701+
return [
702+
'single value' => [['foo'], "ENUM('foo')"],
703+
'multiple values' => [['foo', 'bar1'], "ENUM('foo', 'bar1')"],
704+
];
705+
}
697706
}

tests/Platforms/AbstractPlatformTestCase.php

+35
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Doctrine\DBAL\Exception;
88
use Doctrine\DBAL\Exception\InvalidColumnDeclaration;
9+
use Doctrine\DBAL\Exception\InvalidColumnType\ColumnValuesRequired;
910
use Doctrine\DBAL\Platforms\AbstractPlatform;
1011
use Doctrine\DBAL\Schema\Column;
1112
use Doctrine\DBAL\Schema\ColumnDiff;
@@ -1030,4 +1031,38 @@ public static function asciiStringSqlDeclarationDataProvider(): array
10301031
['CHAR(12)', ['length' => 12, 'fixed' => true]],
10311032
];
10321033
}
1034+
1035+
/** @param array<string> $values */
1036+
#[DataProvider('getEnumDeclarationSQLProvider')]
1037+
public function testGetEnumDeclarationSQL(array $values, string $expectedSQL): void
1038+
{
1039+
self::assertSame($expectedSQL, $this->platform->getEnumDeclarationSQL(['values' => $values]));
1040+
}
1041+
1042+
/** @return array<string, array{array<string>, string}> */
1043+
public static function getEnumDeclarationSQLProvider(): array
1044+
{
1045+
return [
1046+
'single value' => [['foo'], 'VARCHAR(3)'],
1047+
'multiple values' => [['foo', 'bar1'], 'VARCHAR(4)'],
1048+
];
1049+
}
1050+
1051+
/** @param array<mixed> $column */
1052+
#[DataProvider('getEnumDeclarationSQLWithInvalidValuesProvider')]
1053+
public function testGetEnumDeclarationSQLWithInvalidValues(array $column): void
1054+
{
1055+
self::expectException(ColumnValuesRequired::class);
1056+
$this->platform->getEnumDeclarationSQL($column);
1057+
}
1058+
1059+
/** @return array<string, array{array<mixed>}> */
1060+
public static function getEnumDeclarationSQLWithInvalidValuesProvider(): array
1061+
{
1062+
return [
1063+
"field 'values' does not exist" => [[]],
1064+
"field 'values' is not an array" => [['values' => 'foo']],
1065+
"field 'values' is an empty array" => [['values' => []]],
1066+
];
1067+
}
10331068
}

tests/Platforms/OraclePlatformTest.php

+9
Original file line numberDiff line numberDiff line change
@@ -595,4 +595,13 @@ public static function asciiStringSqlDeclarationDataProvider(): array
595595
['CHAR(12)', ['length' => 12, 'fixed' => true]],
596596
];
597597
}
598+
599+
/** @return array<string, array{array<string>, string}> */
600+
public static function getEnumDeclarationSQLProvider(): array
601+
{
602+
return [
603+
'single value' => [['foo'], 'VARCHAR2(3)'],
604+
'multiple values' => [['foo', 'bar1'], 'VARCHAR2(4)'],
605+
];
606+
}
598607
}

tests/Platforms/SQLServerPlatformTest.php

+9
Original file line numberDiff line numberDiff line change
@@ -1124,4 +1124,13 @@ public function testGeneratesTypeDeclarationForDateTimeTz(): void
11241124
{
11251125
self::assertEquals('DATETIMEOFFSET(6)', $this->platform->getDateTimeTzTypeDeclarationSQL([]));
11261126
}
1127+
1128+
/** @return array<string, array{array<string>, string}> */
1129+
public static function getEnumDeclarationSQLProvider(): array
1130+
{
1131+
return [
1132+
'single value' => [['foo'], 'NVARCHAR(3)'],
1133+
'multiple values' => [['foo', 'bar1'], 'NVARCHAR(4)'],
1134+
];
1135+
}
11271136
}

0 commit comments

Comments
 (0)