Skip to content

Commit

Permalink
Throw ConversionException in convertToPHPValue()
Browse files Browse the repository at this point in the history
  • Loading branch information
BenMorel committed Feb 6, 2024
1 parent eaae730 commit 1577e2f
Show file tree
Hide file tree
Showing 12 changed files with 108 additions and 42 deletions.
13 changes: 12 additions & 1 deletion src/Types/DayOfWeekType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
13 changes: 12 additions & 1 deletion src/Types/DurationType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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,
);
}
}
}
13 changes: 12 additions & 1 deletion src/Types/LocalDateTimeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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,
);
}
}
}
13 changes: 12 additions & 1 deletion src/Types/LocalDateType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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,
);
}
}
}
13 changes: 12 additions & 1 deletion src/Types/LocalTimeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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,
);
}
}
}
13 changes: 12 additions & 1 deletion src/Types/PeriodType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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,
);
}
}
}
10 changes: 5 additions & 5 deletions tests/Types/DayOfWeekTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use stdClass;
use ValueError;

class DayOfWeekTypeTest extends TestCase
{
Expand Down Expand Up @@ -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'],
];
}
}
12 changes: 6 additions & 6 deletions tests/Types/DurationTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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'],
];
}
}
14 changes: 7 additions & 7 deletions tests/Types/LocalDateTimeTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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".'],
];
}
}
12 changes: 6 additions & 6 deletions tests/Types/LocalDateTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.'],
];
}
}
12 changes: 6 additions & 6 deletions tests/Types/LocalTimeTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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".'],
];
}
}
12 changes: 6 additions & 6 deletions tests/Types/PeriodTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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'],
];
}
}

0 comments on commit 1577e2f

Please sign in to comment.