Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CronExpressionType: new Exceptions for dbal4 #45

Merged
merged 4 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading