Skip to content

Commit

Permalink
Merge pull request #45 from b3-it/dbal4
Browse files Browse the repository at this point in the history
CronExpressionType: new Exceptions for dbal4
  • Loading branch information
loevgaard authored Aug 15, 2024
2 parents 71be332 + 357802b commit c80b1f9
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
],
"require": {
"php": ">=7.4",
"doctrine/dbal": "^3.4",
"doctrine/dbal": "^3.4 || ^4.0",
"doctrine/doctrine-bundle": "^1.9 || ^2.0",
"dragonmantank/cron-expression": "^2.2 || ^3.0",
"symfony/config": "^5.4 || ^6.0",
Expand Down
20 changes: 18 additions & 2 deletions src/Doctrine/DBAL/Types/CronExpressionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Cron\CronExpression;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\ConversionException;
use Doctrine\DBAL\Types\Exception\InvalidType;
use Doctrine\DBAL\Types\Exception\ValueNotConvertible;
use Doctrine\DBAL\Types\Type;

final class CronExpressionType extends Type
Expand All @@ -28,7 +30,14 @@ public function convertToPHPValue($value, AbstractPlatform $platform): ?CronExpr
}

if (!is_string($value)) {
throw ConversionException::conversionFailedInvalidType($value, CronExpression::class, ['string']);
if (class_exists(InvalidType::class)) {
throw InvalidType::new($value, CronExpression::class, ['string']);
} else {
/**
* @psalm-suppress UndefinedMethod
*/
throw ConversionException::conversionFailedInvalidType($value, CronExpression::class, ['string']);
}
}

if ('' === $value) {
Expand All @@ -38,7 +47,14 @@ public function convertToPHPValue($value, AbstractPlatform $platform): ?CronExpr
try {
return CronExpression::factory($value);
} catch (\Throwable $e) {
throw ConversionException::conversionFailed($value, CronExpression::class, $e);
if (class_exists(ValueNotConvertible::class)) {
throw ValueNotConvertible::new($value, CronExpression::class, null, $e);
} else {
/**
* @psalm-suppress UndefinedMethod
*/
throw ConversionException::conversionFailed($value, CronExpression::class, $e);
}
}
}

Expand Down
14 changes: 14 additions & 0 deletions tests/Doctrine/DBAL/Types/CronExpressionTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Cron\CronExpression;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\ConversionException;
use Doctrine\DBAL\Types\Exception\InvalidType;
use Doctrine\DBAL\Types\Exception\ValueNotConvertible;
use Doctrine\DBAL\Types\Type;
use PHPUnit\Framework\TestCase;
use Setono\CronExpressionBundle\Doctrine\DBAL\Types\CronExpressionType;
Expand Down Expand Up @@ -78,6 +80,12 @@ public function convertToPhpReturnsCronExpression(): void
public function convertFaultyTypeToPhpThrowsException(): void
{
self::expectException(ConversionException::class);
if (class_exists(InvalidType::class)) {
/**
* @psalm-suppress InvalidArgument
*/
self::expectException(InvalidType::class);
}

$this->getType()->convertToPHPValue(new stdClass(), $this->getPlatform());
}
Expand All @@ -88,6 +96,12 @@ public function convertFaultyTypeToPhpThrowsException(): void
public function convertFaultyStringToPhpThrowsException(): void
{
self::expectException(ConversionException::class);
if (class_exists(ValueNotConvertible::class)) {
/**
* @psalm-suppress InvalidArgument
*/
self::expectException(ValueNotConvertible::class);
}

$this->getType()->convertToPHPValue('@never', $this->getPlatform());
}
Expand Down

0 comments on commit c80b1f9

Please sign in to comment.