Skip to content

Commit c80b1f9

Browse files
authored
Merge pull request #45 from b3-it/dbal4
CronExpressionType: new Exceptions for dbal4
2 parents 71be332 + 357802b commit c80b1f9

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
],
1212
"require": {
1313
"php": ">=7.4",
14-
"doctrine/dbal": "^3.4",
14+
"doctrine/dbal": "^3.4 || ^4.0",
1515
"doctrine/doctrine-bundle": "^1.9 || ^2.0",
1616
"dragonmantank/cron-expression": "^2.2 || ^3.0",
1717
"symfony/config": "^5.4 || ^6.0",

src/Doctrine/DBAL/Types/CronExpressionType.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use Cron\CronExpression;
88
use Doctrine\DBAL\Platforms\AbstractPlatform;
99
use Doctrine\DBAL\Types\ConversionException;
10+
use Doctrine\DBAL\Types\Exception\InvalidType;
11+
use Doctrine\DBAL\Types\Exception\ValueNotConvertible;
1012
use Doctrine\DBAL\Types\Type;
1113

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

3032
if (!is_string($value)) {
31-
throw ConversionException::conversionFailedInvalidType($value, CronExpression::class, ['string']);
33+
if (class_exists(InvalidType::class)) {
34+
throw InvalidType::new($value, CronExpression::class, ['string']);
35+
} else {
36+
/**
37+
* @psalm-suppress UndefinedMethod
38+
*/
39+
throw ConversionException::conversionFailedInvalidType($value, CronExpression::class, ['string']);
40+
}
3241
}
3342

3443
if ('' === $value) {
@@ -38,7 +47,14 @@ public function convertToPHPValue($value, AbstractPlatform $platform): ?CronExpr
3847
try {
3948
return CronExpression::factory($value);
4049
} catch (\Throwable $e) {
41-
throw ConversionException::conversionFailed($value, CronExpression::class, $e);
50+
if (class_exists(ValueNotConvertible::class)) {
51+
throw ValueNotConvertible::new($value, CronExpression::class, null, $e);
52+
} else {
53+
/**
54+
* @psalm-suppress UndefinedMethod
55+
*/
56+
throw ConversionException::conversionFailed($value, CronExpression::class, $e);
57+
}
4258
}
4359
}
4460

tests/Doctrine/DBAL/Types/CronExpressionTypeTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use Cron\CronExpression;
88
use Doctrine\DBAL\Platforms\AbstractPlatform;
99
use Doctrine\DBAL\Types\ConversionException;
10+
use Doctrine\DBAL\Types\Exception\InvalidType;
11+
use Doctrine\DBAL\Types\Exception\ValueNotConvertible;
1012
use Doctrine\DBAL\Types\Type;
1113
use PHPUnit\Framework\TestCase;
1214
use Setono\CronExpressionBundle\Doctrine\DBAL\Types\CronExpressionType;
@@ -78,6 +80,12 @@ public function convertToPhpReturnsCronExpression(): void
7880
public function convertFaultyTypeToPhpThrowsException(): void
7981
{
8082
self::expectException(ConversionException::class);
83+
if (class_exists(InvalidType::class)) {
84+
/**
85+
* @psalm-suppress InvalidArgument
86+
*/
87+
self::expectException(InvalidType::class);
88+
}
8189

8290
$this->getType()->convertToPHPValue(new stdClass(), $this->getPlatform());
8391
}
@@ -88,6 +96,12 @@ public function convertFaultyTypeToPhpThrowsException(): void
8896
public function convertFaultyStringToPhpThrowsException(): void
8997
{
9098
self::expectException(ConversionException::class);
99+
if (class_exists(ValueNotConvertible::class)) {
100+
/**
101+
* @psalm-suppress InvalidArgument
102+
*/
103+
self::expectException(ValueNotConvertible::class);
104+
}
91105

92106
$this->getType()->convertToPHPValue('@never', $this->getPlatform());
93107
}

0 commit comments

Comments
 (0)