Skip to content

Commit 89241ce

Browse files
Throws exception
1 parent 055946f commit 89241ce

File tree

5 files changed

+32
-16
lines changed

5 files changed

+32
-16
lines changed

src/Platforms/AbstractPlatform.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,12 +244,20 @@ public function getEnumDeclarationSQL(array $column): string
244244
throw ColumnValuesRequired::new($this, 'ENUM');
245245
}
246246

247-
$configuredLength = $column['length'] ?? 0;
248-
$neededLength = count($column['values']) > 1
247+
$length = count($column['values']) > 1
249248
? max(...array_map(mb_strlen(...), $column['values']))
250249
: mb_strlen($column['values'][key($column['values'])]);
251250

252-
$length = max($configuredLength, $neededLength);
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+
$length = $column['length'];
260+
}
253261

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

tests/Platforms/AbstractMySQLPlatformTestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ public static function getEnumDeclarationSQLProvider(): array
631631
];
632632
}
633633

634-
/** @return array<string, array{array<string>, int, string}> */
634+
/** @return array<string, array{array<string>, int, string|null}> */
635635
public static function getEnumDeclarationWithLengthSQLProvider(): array
636636
{
637637
return [

tests/Platforms/AbstractPlatformTestCase.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1220,22 +1220,30 @@ public static function getEnumDeclarationSQLProvider(): array
12201220

12211221
/** @param array<string> $values */
12221222
#[DataProvider('getEnumDeclarationWithLengthSQLProvider')]
1223-
public function testGetEnumDeclarationWithLengthSQL(array $values, int $length, string $expectedSQL): void
1223+
public function testGetEnumDeclarationWithLengthSQL(array $values, int $length, ?string $expectedSQL = null): void
12241224
{
1225-
self::assertSame($expectedSQL, $this->platform->getEnumDeclarationSQL([
1225+
if ($expectedSQL === null) {
1226+
$this->expectException(InvalidArgumentException::class);
1227+
}
1228+
1229+
$result = $this->platform->getEnumDeclarationSQL([
12261230
'values' => $values,
12271231
'length' => $length,
1228-
]));
1232+
]);
1233+
1234+
if ($expectedSQL !== null) {
1235+
self::assertSame($expectedSQL, $result);
1236+
}
12291237
}
12301238

1231-
/** @return array<string, array{array<string>, int, string}> */
1239+
/** @return array<string, array{array<string>, int, string|null}> */
12321240
public static function getEnumDeclarationWithLengthSQLProvider(): array
12331241
{
12341242
return [
12351243
'single value and bigger length' => [['foo'], 42, 'VARCHAR(42)'],
1236-
'single value and lower length' => [['foo'], 1, 'VARCHAR(3)'],
1244+
'single value and lower length' => [['foo'], 1, null],
12371245
'multiple values and bigger length' => [['foo', 'bar1'], 42, 'VARCHAR(42)'],
1238-
'multiple values and lower length' => [['foo', 'bar1'], 2, 'VARCHAR(4)'],
1246+
'multiple values and lower length' => [['foo', 'bar1'], 2, null],
12391247
];
12401248
}
12411249

tests/Platforms/OraclePlatformTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -652,14 +652,14 @@ public static function getEnumDeclarationSQLProvider(): array
652652
];
653653
}
654654

655-
/** @return array<string, array{array<string>, int, string}> */
655+
/** @return array<string, array{array<string>, int, string|null}> */
656656
public static function getEnumDeclarationWithLengthSQLProvider(): array
657657
{
658658
return [
659659
'single value and bigger length' => [['foo'], 42, 'VARCHAR2(42)'],
660-
'single value and lower length' => [['foo'], 1, 'VARCHAR2(3)'],
660+
'single value and lower length' => [['foo'], 1, null],
661661
'multiple values and bigger length' => [['foo', 'bar1'], 42, 'VARCHAR2(42)'],
662-
'multiple values and lower length' => [['foo', 'bar1'], 2, 'VARCHAR2(4)'],
662+
'multiple values and lower length' => [['foo', 'bar1'], 2, null],
663663
];
664664
}
665665
}

tests/Platforms/SQLServerPlatformTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1226,14 +1226,14 @@ public static function getEnumDeclarationSQLProvider(): array
12261226
];
12271227
}
12281228

1229-
/** @return array<string, array{array<string>, int, string}> */
1229+
/** @return array<string, array{array<string>, int, string|null}> */
12301230
public static function getEnumDeclarationWithLengthSQLProvider(): array
12311231
{
12321232
return [
12331233
'single value and bigger length' => [['foo'], 42, 'NVARCHAR(42)'],
1234-
'single value and lower length' => [['foo'], 1, 'NVARCHAR(3)'],
1234+
'single value and lower length' => [['foo'], 1, null],
12351235
'multiple values and bigger length' => [['foo', 'bar1'], 42, 'NVARCHAR(42)'],
1236-
'multiple values and lower length' => [['foo', 'bar1'], 2, 'NVARCHAR(4)'],
1236+
'multiple values and lower length' => [['foo', 'bar1'], 2, null],
12371237
];
12381238
}
12391239
}

0 commit comments

Comments
 (0)