From 1577e2f74a80c88f02978bc18aba209c376a47ff Mon Sep 17 00:00:00 2001 From: Benjamin Morel Date: Tue, 6 Feb 2024 23:56:35 +0100 Subject: [PATCH] Throw ConversionException in convertToPHPValue() --- src/Types/DayOfWeekType.php | 13 ++++++++++++- src/Types/DurationType.php | 13 ++++++++++++- src/Types/LocalDateTimeType.php | 13 ++++++++++++- src/Types/LocalDateType.php | 13 ++++++++++++- src/Types/LocalTimeType.php | 13 ++++++++++++- src/Types/PeriodType.php | 13 ++++++++++++- tests/Types/DayOfWeekTypeTest.php | 10 +++++----- tests/Types/DurationTypeTest.php | 12 ++++++------ tests/Types/LocalDateTimeTypeTest.php | 14 +++++++------- tests/Types/LocalDateTypeTest.php | 12 ++++++------ tests/Types/LocalTimeTypeTest.php | 12 ++++++------ tests/Types/PeriodTypeTest.php | 12 ++++++------ 12 files changed, 108 insertions(+), 42 deletions(-) diff --git a/src/Types/DayOfWeekType.php b/src/Types/DayOfWeekType.php index f446599..45ae4bf 100644 --- a/src/Types/DayOfWeekType.php +++ b/src/Types/DayOfWeekType.php @@ -7,8 +7,10 @@ use Brick\DateTime\DayOfWeek; use Doctrine\DBAL\ParameterType; use Doctrine\DBAL\Types\Exception\InvalidType; +use Doctrine\DBAL\Types\Exception\ValueNotConvertible; use Doctrine\DBAL\Types\Type; use Doctrine\DBAL\Platforms\AbstractPlatform; +use ValueError; /** * Doctrine type for DayOfWeek. @@ -45,7 +47,16 @@ public function convertToPHPValue(mixed $value, AbstractPlatform $platform): ?Da return null; } - return DayOfWeek::from((int) $value); + try { + return DayOfWeek::from((int) $value); + } catch (ValueError $e) { + throw ValueNotConvertible::new( + $value, + DayOfWeek::class, + $e->getMessage(), + $e, + ); + } } public function getBindingType(): ParameterType diff --git a/src/Types/DurationType.php b/src/Types/DurationType.php index de2af1c..5477082 100644 --- a/src/Types/DurationType.php +++ b/src/Types/DurationType.php @@ -4,9 +4,11 @@ namespace Brick\DateTime\Doctrine\Types; +use Brick\DateTime\DateTimeException; use Brick\DateTime\Duration; use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Types\Exception\InvalidType; +use Doctrine\DBAL\Types\Exception\ValueNotConvertible; use Doctrine\DBAL\Types\Type; /** @@ -48,6 +50,15 @@ public function convertToPHPValue(mixed $value, AbstractPlatform $platform): ?Du return null; } - return Duration::parse((string) $value); + try { + return Duration::parse((string) $value); + } catch (DateTimeException $e) { + throw ValueNotConvertible::new( + $value, + Duration::class, + $e->getMessage(), + $e, + ); + } } } diff --git a/src/Types/LocalDateTimeType.php b/src/Types/LocalDateTimeType.php index febc2c9..8d55eea 100644 --- a/src/Types/LocalDateTimeType.php +++ b/src/Types/LocalDateTimeType.php @@ -4,8 +4,10 @@ namespace Brick\DateTime\Doctrine\Types; +use Brick\DateTime\DateTimeException; use Brick\DateTime\LocalDateTime; use Doctrine\DBAL\Types\Exception\InvalidType; +use Doctrine\DBAL\Types\Exception\ValueNotConvertible; use Doctrine\DBAL\Types\Type; use Doctrine\DBAL\Platforms\AbstractPlatform; @@ -52,6 +54,15 @@ public function convertToPHPValue(mixed $value, AbstractPlatform $platform): ?Lo $value = str_replace(' ', 'T', (string) $value); - return LocalDateTime::parse($value); + try { + return LocalDateTime::parse($value); + } catch (DateTimeException $e) { + throw ValueNotConvertible::new( + $value, + LocalDateTime::class, + $e->getMessage(), + $e, + ); + } } } diff --git a/src/Types/LocalDateType.php b/src/Types/LocalDateType.php index 0e3fafc..d8d0a5d 100644 --- a/src/Types/LocalDateType.php +++ b/src/Types/LocalDateType.php @@ -4,8 +4,10 @@ namespace Brick\DateTime\Doctrine\Types; +use Brick\DateTime\DateTimeException; use Brick\DateTime\LocalDate; use Doctrine\DBAL\Types\Exception\InvalidType; +use Doctrine\DBAL\Types\Exception\ValueNotConvertible; use Doctrine\DBAL\Types\Type; use Doctrine\DBAL\Platforms\AbstractPlatform; @@ -44,6 +46,15 @@ public function convertToPHPValue(mixed $value, AbstractPlatform $platform): ?Lo return null; } - return LocalDate::parse((string) $value); + try { + return LocalDate::parse((string) $value); + } catch (DateTimeException $e) { + throw ValueNotConvertible::new( + $value, + LocalDate::class, + $e->getMessage(), + $e, + ); + } } } diff --git a/src/Types/LocalTimeType.php b/src/Types/LocalTimeType.php index 338ed5b..a5fe911 100644 --- a/src/Types/LocalTimeType.php +++ b/src/Types/LocalTimeType.php @@ -4,8 +4,10 @@ namespace Brick\DateTime\Doctrine\Types; +use Brick\DateTime\DateTimeException; use Brick\DateTime\LocalTime; use Doctrine\DBAL\Types\Exception\InvalidType; +use Doctrine\DBAL\Types\Exception\ValueNotConvertible; use Doctrine\DBAL\Types\Type; use Doctrine\DBAL\Platforms\AbstractPlatform; @@ -50,6 +52,15 @@ public function convertToPHPValue(mixed $value, AbstractPlatform $platform): ?Lo return null; } - return LocalTime::parse((string) $value); + try { + return LocalTime::parse((string) $value); + } catch (DateTimeException $e) { + throw ValueNotConvertible::new( + $value, + LocalTime::class, + $e->getMessage(), + $e, + ); + } } } diff --git a/src/Types/PeriodType.php b/src/Types/PeriodType.php index f317282..295defd 100644 --- a/src/Types/PeriodType.php +++ b/src/Types/PeriodType.php @@ -4,9 +4,11 @@ namespace Brick\DateTime\Doctrine\Types; +use Brick\DateTime\DateTimeException; use Brick\DateTime\Period; use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Types\Exception\InvalidType; +use Doctrine\DBAL\Types\Exception\ValueNotConvertible; use Doctrine\DBAL\Types\Type; /** @@ -48,6 +50,15 @@ public function convertToPHPValue(mixed $value, AbstractPlatform $platform): ?Pe return null; } - return Period::parse((string) $value); + try { + return Period::parse((string) $value); + } catch (DateTimeException $e) { + throw ValueNotConvertible::new( + $value, + Period::class, + $e->getMessage(), + $e, + ); + } } } diff --git a/tests/Types/DayOfWeekTypeTest.php b/tests/Types/DayOfWeekTypeTest.php index 5097b31..6c21106 100644 --- a/tests/Types/DayOfWeekTypeTest.php +++ b/tests/Types/DayOfWeekTypeTest.php @@ -14,7 +14,6 @@ use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; use stdClass; -use ValueError; class DayOfWeekTypeTest extends TestCase { @@ -97,19 +96,20 @@ public static function providerConvertToPHPValue(): array } #[DataProvider('providerConvertToPHPValueWithInvalidValue')] - public function testConvertToPHPValueWithInvalidValue(mixed $value, string $expectedExceptionClass): void + public function testConvertToPHPValueWithInvalidValue(mixed $value, string $expectedExceptionMessage): void { $type = $this->getDayOfWeekType(); - $this->expectException($expectedExceptionClass); + $this->expectException(ConversionException::class); + $this->expectExceptionMessage($expectedExceptionMessage); $type->convertToPHPValue($value, new SQLitePlatform()); } public static function providerConvertToPHPValueWithInvalidValue(): array { return [ - [0, ValueError::class], - [8, ValueError::class], + [0, '0 is not a valid backing value for enum'], + [8, '8 is not a valid backing value for enum'], ]; } } diff --git a/tests/Types/DurationTypeTest.php b/tests/Types/DurationTypeTest.php index a73881f..aaa68c8 100644 --- a/tests/Types/DurationTypeTest.php +++ b/tests/Types/DurationTypeTest.php @@ -4,7 +4,6 @@ namespace Brick\DateTime\Doctrine\Tests\Types; -use Brick\DateTime\DateTimeException; use Brick\DateTime\Doctrine\Types\DurationType; use Brick\DateTime\LocalDate; use Brick\DateTime\Duration; @@ -85,20 +84,21 @@ public static function providerConvertToPHPValue(): array } #[DataProvider('providerConvertToPHPValueWithInvalidValue')] - public function testConvertToPHPValueWithInvalidValue(mixed $value, string $expectedExceptionClass): void + public function testConvertToPHPValueWithInvalidValue(mixed $value, string $expectedExceptionMessage): void { $type = $this->getDurationType(); - $this->expectException($expectedExceptionClass); + $this->expectException(ConversionException::class); + $this->expectExceptionMessage($expectedExceptionMessage); $type->convertToPHPValue($value, new SQLitePlatform()); } public static function providerConvertToPHPValueWithInvalidValue(): array { return [ - [0, DateTimeException::class], - ['10:31:00', DateTimeException::class], - ['2021-04-00', DateTimeException::class], + [0, 'Text cannot be parsed to a Duration: 0'], + ['10:31:00', 'Text cannot be parsed to a Duration: 10:31:00'], + ['2021-04-00', 'Text cannot be parsed to a Duration: 2021-04-00'], ]; } } diff --git a/tests/Types/LocalDateTimeTypeTest.php b/tests/Types/LocalDateTimeTypeTest.php index 65fd794..9121780 100644 --- a/tests/Types/LocalDateTimeTypeTest.php +++ b/tests/Types/LocalDateTimeTypeTest.php @@ -4,7 +4,6 @@ namespace Brick\DateTime\Doctrine\Tests\Types; -use Brick\DateTime\DateTimeException; use Brick\DateTime\Doctrine\Types\LocalDateTimeType; use Brick\DateTime\LocalDate; use Brick\DateTime\LocalDateTime; @@ -92,21 +91,22 @@ public static function providerConvertToPHPValue(): array } #[DataProvider('providerConvertToPHPValueWithInvalidValue')] - public function testConvertToPHPValueWithInvalidValue(mixed $value, string $expectedExceptionClass): void + public function testConvertToPHPValueWithInvalidValue(mixed $value, string $expectedExceptionMessage): void { $type = $this->getLocalDateTimeType(); - $this->expectException($expectedExceptionClass); + $this->expectException(ConversionException::class); + $this->expectExceptionMessage($expectedExceptionMessage); $type->convertToPHPValue($value, new SQLitePlatform()); } public static function providerConvertToPHPValueWithInvalidValue(): array { return [ - [0, DateTimeException::class], - ['01:02:59', DateTimeException::class], - ['2021-04-17', DateTimeException::class], - ['2021-04-17Z01:02:03.456', DateTimeException::class], + [0, 'Failed to parse "0"'], + ['01:02:59', 'Failed to parse "01:02:59".'], + ['2021-04-17', 'Failed to parse "2021-04-17".'], + ['2021-04-17Z01:02:03.456', 'Failed to parse "2021-04-17Z01:02:03.456".'], ]; } } diff --git a/tests/Types/LocalDateTypeTest.php b/tests/Types/LocalDateTypeTest.php index ad7b885..85f9f09 100644 --- a/tests/Types/LocalDateTypeTest.php +++ b/tests/Types/LocalDateTypeTest.php @@ -4,7 +4,6 @@ namespace Brick\DateTime\Doctrine\Tests\Types; -use Brick\DateTime\DateTimeException; use Brick\DateTime\Doctrine\Types\LocalDateType; use Brick\DateTime\LocalDate; use Brick\DateTime\LocalDateTime; @@ -85,20 +84,21 @@ public static function providerConvertToPHPValue(): array } #[DataProvider('providerConvertToPHPValueWithInvalidValue')] - public function testConvertToPHPValueWithInvalidValue(mixed $value, string $expectedExceptionClass): void + public function testConvertToPHPValueWithInvalidValue(mixed $value, string $expectedExceptionMessage): void { $type = $this->getLocalDateType(); - $this->expectException($expectedExceptionClass); + $this->expectException(ConversionException::class); + $this->expectExceptionMessage($expectedExceptionMessage); $type->convertToPHPValue($value, new SQLitePlatform()); } public static function providerConvertToPHPValueWithInvalidValue(): array { return [ - [0, DateTimeException::class], - ['10:31:00', DateTimeException::class], - ['2021-04-00', DateTimeException::class], + [0, 'Failed to parse "0".'], + ['10:31:00', 'Failed to parse "10:31:00".'], + ['2021-04-00', 'Invalid day-of-month: 0 is not in the range 1 to 31.'], ]; } } diff --git a/tests/Types/LocalTimeTypeTest.php b/tests/Types/LocalTimeTypeTest.php index 71e6df7..ec4454c 100644 --- a/tests/Types/LocalTimeTypeTest.php +++ b/tests/Types/LocalTimeTypeTest.php @@ -4,7 +4,6 @@ namespace Brick\DateTime\Doctrine\Tests\Types; -use Brick\DateTime\DateTimeException; use Brick\DateTime\Doctrine\Types\LocalTimeType; use Brick\DateTime\LocalDate; use Brick\DateTime\LocalTime; @@ -89,20 +88,21 @@ public static function providerConvertToPHPValue(): array } #[DataProvider('providerConvertToPHPValueWithInvalidValue')] - public function testConvertToPHPValueWithInvalidValue(mixed $value, string $expectedExceptionClass): void + public function testConvertToPHPValueWithInvalidValue(mixed $value, string $expectedExceptionMessage): void { $type = $this->getLocalTimeType(); - $this->expectException($expectedExceptionClass); + $this->expectException(ConversionException::class); + $this->expectExceptionMessage($expectedExceptionMessage); $type->convertToPHPValue($value, new SQLitePlatform()); } public static function providerConvertToPHPValueWithInvalidValue(): array { return [ - [0, DateTimeException::class], - ['01:02:60', DateTimeException::class], - ['2021-04-17', DateTimeException::class], + [0, 'Failed to parse "0".'], + ['01:02:60', 'Invalid second-of-minute: 60 is not in the range 0 to 59.'], + ['2021-04-17', 'Failed to parse "2021-04-17".'], ]; } } diff --git a/tests/Types/PeriodTypeTest.php b/tests/Types/PeriodTypeTest.php index 507d27c..ec5f643 100644 --- a/tests/Types/PeriodTypeTest.php +++ b/tests/Types/PeriodTypeTest.php @@ -4,7 +4,6 @@ namespace Brick\DateTime\Doctrine\Tests\Types; -use Brick\DateTime\DateTimeException; use Brick\DateTime\Doctrine\Types\PeriodType; use Brick\DateTime\LocalDate; use Brick\DateTime\Period; @@ -85,20 +84,21 @@ public static function providerConvertToPHPValue(): array } #[DataProvider('providerConvertToPHPValueWithInvalidValue')] - public function testConvertToPHPValueWithInvalidValue(mixed $value, string $expectedExceptionClass): void + public function testConvertToPHPValueWithInvalidValue(mixed $value, string $expectedExceptionMessage): void { $type = $this->getPeriodType(); - $this->expectException($expectedExceptionClass); + $this->expectException(ConversionException::class); + $this->expectExceptionMessage($expectedExceptionMessage); $type->convertToPHPValue($value, new SQLitePlatform()); } public static function providerConvertToPHPValueWithInvalidValue(): array { return [ - [0, DateTimeException::class], - ['10:31:00', DateTimeException::class], - ['2021-04-00', DateTimeException::class], + [0, 'Text cannot be parsed to a Period: 0'], + ['10:31:00', 'Text cannot be parsed to a Period: 10:31:00'], + ['2021-04-00', 'Text cannot be parsed to a Period: 2021-04-00'], ]; } }