Skip to content

Commit e2e36dd

Browse files
Use provided length for enum type if given for non-mysql platform (#7034)
1 parent 95bda94 commit e2e36dd

File tree

6 files changed

+93
-2
lines changed

6 files changed

+93
-2
lines changed

docs/en/reference/types.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,8 @@ Maps and converts a string which is one of a set of predefined values. This
210210
type is specifically designed for MySQL and MariaDB, where it is mapped to
211211
the native ``ENUM`` type. For other database vendors, this type is mapped to
212212
a string field (``VARCHAR``) with the maximum length being the length of the
213-
longest value in the set. Values retrieved from the database are always
214-
converted to PHP's ``string`` type or ``null`` if no data is present.
213+
longest value in the set if none was configured. Values retrieved from the database
214+
are always converted to PHP's ``string`` type or ``null`` if no data is present.
215215

216216
Binary string types
217217
^^^^^^^^^^^^^^^^^^^

src/Platforms/AbstractPlatform.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,18 @@ public function getEnumDeclarationSQL(array $column): string
248248
? max(...array_map(mb_strlen(...), $column['values']))
249249
: mb_strlen($column['values'][key($column['values'])]);
250250

251+
if (isset($column['length'])) {
252+
if ($length > $column['length']) {
253+
throw new InvalidArgumentException(sprintf(
254+
'Specified column length (%d) is less than the maximum length of provided values (%d).',
255+
$column['length'],
256+
$length,
257+
));
258+
}
259+
260+
$length = $column['length'];
261+
}
262+
251263
return $this->getStringTypeDeclarationSQL(['length' => $length]);
252264
}
253265

tests/Platforms/AbstractMySQLPlatformTestCase.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Doctrine\DBAL\Schema\Table;
2121
use Doctrine\DBAL\TransactionIsolationLevel;
2222
use Doctrine\DBAL\Types\Types;
23+
use PHPUnit\Framework\Attributes\DataProvider;
2324

2425
use function array_shift;
2526

@@ -622,6 +623,13 @@ protected function createComparator(): Comparator
622623
);
623624
}
624625

626+
/** @param array<string> $values */
627+
#[DataProvider('getEnumDeclarationExceptionWithLengthSQLProvider')]
628+
public function testGetEnumDeclarationExceptionWithLengthSQL(array $values, int $length): void
629+
{
630+
self::markTestSkipped('There is no exception thrown on MySQL.');
631+
}
632+
625633
/** @return array<string, array{array<string>, string}> */
626634
public static function getEnumDeclarationSQLProvider(): array
627635
{
@@ -630,4 +638,15 @@ public static function getEnumDeclarationSQLProvider(): array
630638
'multiple values' => [['foo', 'bar1'], "ENUM('foo', 'bar1')"],
631639
];
632640
}
641+
642+
/** @return array<string, array{array<string>, int, string}> */
643+
public static function getEnumDeclarationWithLengthSQLProvider(): array
644+
{
645+
return [
646+
'single value and bigger length' => [['foo'], 42, "ENUM('foo')"],
647+
'single value and lower length' => [['foo'], 1, "ENUM('foo')"],
648+
'multiple values and bigger length' => [['foo', 'bar1'], 42, "ENUM('foo', 'bar1')"],
649+
'multiple values and lower length' => [['foo', 'bar1'], 2, "ENUM('foo', 'bar1')"],
650+
];
651+
}
633652
}

tests/Platforms/AbstractPlatformTestCase.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1218,6 +1218,48 @@ public static function getEnumDeclarationSQLProvider(): array
12181218
];
12191219
}
12201220

1221+
/** @param array<string> $values */
1222+
#[DataProvider('getEnumDeclarationWithLengthSQLProvider')]
1223+
public function testGetEnumDeclarationWithLengthSQL(array $values, int $length, string $expectedSQL): void
1224+
{
1225+
$result = $this->platform->getEnumDeclarationSQL([
1226+
'values' => $values,
1227+
'length' => $length,
1228+
]);
1229+
1230+
self::assertSame($expectedSQL, $result);
1231+
}
1232+
1233+
/** @param array<string> $values */
1234+
#[DataProvider('getEnumDeclarationExceptionWithLengthSQLProvider')]
1235+
public function testGetEnumDeclarationExceptionWithLengthSQL(array $values, int $length): void
1236+
{
1237+
$this->expectException(InvalidArgumentException::class);
1238+
1239+
$this->platform->getEnumDeclarationSQL([
1240+
'values' => $values,
1241+
'length' => $length,
1242+
]);
1243+
}
1244+
1245+
/** @return array<string, array{array<string>, int, string}> */
1246+
public static function getEnumDeclarationWithLengthSQLProvider(): array
1247+
{
1248+
return [
1249+
'single value and bigger length' => [['foo'], 42, 'VARCHAR(42)'],
1250+
'multiple values and bigger length' => [['foo', 'bar1'], 42, 'VARCHAR(42)'],
1251+
];
1252+
}
1253+
1254+
/** @return array<string, array{array<string>, int}> */
1255+
public static function getEnumDeclarationExceptionWithLengthSQLProvider(): array
1256+
{
1257+
return [
1258+
'single value and lower length' => [['foo'], 1],
1259+
'multiple values and lower length' => [['foo', 'bar1'], 2],
1260+
];
1261+
}
1262+
12211263
/** @param array<mixed> $column */
12221264
#[DataProvider('getEnumDeclarationSQLWithInvalidValuesProvider')]
12231265
public function testGetEnumDeclarationSQLWithInvalidValues(array $column): void

tests/Platforms/OraclePlatformTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,4 +651,13 @@ public static function getEnumDeclarationSQLProvider(): array
651651
'multiple values' => [['foo', 'bar1'], 'VARCHAR2(4)'],
652652
];
653653
}
654+
655+
/** @return array<string, array{array<string>, int, string}> */
656+
public static function getEnumDeclarationWithLengthSQLProvider(): array
657+
{
658+
return [
659+
'single value and bigger length' => [['foo'], 42, 'VARCHAR2(42)'],
660+
'multiple values and bigger length' => [['foo', 'bar1'], 42, 'VARCHAR2(42)'],
661+
];
662+
}
654663
}

tests/Platforms/SQLServerPlatformTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1225,4 +1225,13 @@ public static function getEnumDeclarationSQLProvider(): array
12251225
'multiple values' => [['foo', 'bar1'], 'NVARCHAR(4)'],
12261226
];
12271227
}
1228+
1229+
/** @return array<string, array{array<string>, int, string}> */
1230+
public static function getEnumDeclarationWithLengthSQLProvider(): array
1231+
{
1232+
return [
1233+
'single value and bigger length' => [['foo'], 42, 'NVARCHAR(42)'],
1234+
'multiple values and bigger length' => [['foo', 'bar1'], 42, 'NVARCHAR(42)'],
1235+
];
1236+
}
12281237
}

0 commit comments

Comments
 (0)