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

Allow add previous exception in ConversionException #3222

Merged
merged 1 commit into from
Mar 3, 2020
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
16 changes: 10 additions & 6 deletions lib/Doctrine/DBAL/Types/ConversionException.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ class ConversionException extends DBALException
*
* @return \Doctrine\DBAL\Types\ConversionException
*/
public static function conversionFailed($value, $toType)
public static function conversionFailed($value, $toType, ?Throwable $previous = null)
{
$value = strlen($value) > 32 ? substr($value, 0, 20) . '...' : $value;

return new self('Could not convert database value "' . $value . '" to Doctrine Type ' . $toType);
return new self('Could not convert database value "' . $value . '" to Doctrine Type ' . $toType, 0, $previous);
}

/**
Expand Down Expand Up @@ -64,8 +64,12 @@ public static function conversionFailedFormat($value, $toType, $expectedFormat,
*
* @return \Doctrine\DBAL\Types\ConversionException
*/
public static function conversionFailedInvalidType($value, $toType, array $possibleTypes)
{
public static function conversionFailedInvalidType(
$value,
$toType,
array $possibleTypes,
?Throwable $previous = null
) {
$actualType = is_object($value) ? get_class($value) : gettype($value);

if (is_scalar($value)) {
Expand All @@ -75,15 +79,15 @@ public static function conversionFailedInvalidType($value, $toType, array $possi
$actualType,
$toType,
implode(', ', $possibleTypes)
));
), 0, $previous);
}

return new self(sprintf(
"Could not convert PHP value of type '%s' to type '%s'. Expected one of the following types: %s",
$actualType,
$toType,
implode(', ', $possibleTypes)
));
), 0, $previous);
}

public static function conversionFailedSerialization($value, $format, $error)
Expand Down
24 changes: 22 additions & 2 deletions tests/Doctrine/Tests/DBAL/Types/ConversionExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,23 @@
namespace Doctrine\Tests\DBAL\Types;

use Doctrine\DBAL\Types\ConversionException;
use Exception;
use PHPUnit\Framework\TestCase;
use stdClass;
use Throwable;
use function tmpfile;

class ConversionExceptionTest extends TestCase
{
public function testConversionFailedPreviousException() : void
{
$previous = $this->createMock(Throwable::class);

$exception = ConversionException::conversionFailed('foo', 'foo', $previous);

self::assertInstanceOf(ConversionException::class, $exception);
self::assertSame($previous, $exception->getPrevious());
}

/**
* @param mixed $scalarValue
*
Expand Down Expand Up @@ -44,9 +54,19 @@ public function testConversionFailedInvalidTypeWithNonScalar($nonScalar) : void
);
}

public function testConversionFailedInvalidTypePreviousException() : void
{
$previous = $this->createMock(Throwable::class);

$exception = ConversionException::conversionFailedInvalidType('foo', 'foo', ['bar', 'baz'], $previous);

self::assertInstanceOf(ConversionException::class, $exception);
self::assertSame($previous, $exception->getPrevious());
}

public function testConversionFailedFormatPreservesPreviousException() : void
{
$previous = new Exception();
$previous = $this->createMock(Throwable::class);

$exception = ConversionException::conversionFailedFormat('foo', 'bar', 'baz', $previous);

Expand Down