diff --git a/src/Infrastructure/Doctrine/Type/AbstractIdType.php b/src/Infrastructure/Doctrine/Type/AbstractIdType.php index 927e693..4308767 100644 --- a/src/Infrastructure/Doctrine/Type/AbstractIdType.php +++ b/src/Infrastructure/Doctrine/Type/AbstractIdType.php @@ -13,6 +13,10 @@ abstract class AbstractIdType extends IntegerType { public function convertToDatabaseValue($value, AbstractPlatform $platform) { + if (is_int($value)) { + return $value; + } + if (!$value instanceof Id) { throw ConversionException::conversionFailedInvalidType( $value, diff --git a/src/Infrastructure/Doctrine/Type/AbstractUuidType.php b/src/Infrastructure/Doctrine/Type/AbstractUuidType.php index fa44cef..0113b5e 100644 --- a/src/Infrastructure/Doctrine/Type/AbstractUuidType.php +++ b/src/Infrastructure/Doctrine/Type/AbstractUuidType.php @@ -17,6 +17,10 @@ abstract class AbstractUuidType extends GuidType */ public function convertToDatabaseValue($value, AbstractPlatform $platform) { + if (is_string($value)) { + return $value; + } + if (!$value instanceof Uuid) { throw ConversionException::conversionFailedInvalidType( $value, diff --git a/tests/Unit/Infrastructure/Doctrine/Type/AbstractIdTypeTest.php b/tests/Unit/Infrastructure/Doctrine/Type/AbstractIdTypeTest.php index 8dd109d..55db631 100644 --- a/tests/Unit/Infrastructure/Doctrine/Type/AbstractIdTypeTest.php +++ b/tests/Unit/Infrastructure/Doctrine/Type/AbstractIdTypeTest.php @@ -5,6 +5,7 @@ namespace GeekCell\DddBundle\Tests\Unit\Infrastructure\Doctrine\Type; use Doctrine\DBAL\Platforms\AbstractPlatform; +use Doctrine\DBAL\Types\ConversionException; use GeekCell\Ddd\Domain\ValueObject\Id; use GeekCell\DddBundle\Infrastructure\Doctrine\Type\AbstractIdType; use Mockery; @@ -49,6 +50,33 @@ public function testConvertToDatabaseValue(): void $this->assertSame($intId, $result); } + public function testConvertToDatabaseValueScalar(): void + { + // Given + $intId = 42; + $type = new FooIdType(); + $platform = Mockery::mock(AbstractPlatform::class); + + // When + $result = $type->convertToDatabaseValue($intId, $platform); + + // Then + $this->assertSame($intId, $result); + } + + public function testConvertToDatabaseValueInvalidType(): void + { + // Given + $type = new FooIdType(); + $platform = Mockery::mock(AbstractPlatform::class); + + // Then + $this->expectException(ConversionException::class); + + // When + $type->convertToDatabaseValue('foo', $platform); + } + public function testConvertToPhpValue(): void { // Given diff --git a/tests/Unit/Infrastructure/Doctrine/Type/AbstractUuidTypeTest.php b/tests/Unit/Infrastructure/Doctrine/Type/AbstractUuidTypeTest.php index 861002b..91d2a1e 100644 --- a/tests/Unit/Infrastructure/Doctrine/Type/AbstractUuidTypeTest.php +++ b/tests/Unit/Infrastructure/Doctrine/Type/AbstractUuidTypeTest.php @@ -5,6 +5,7 @@ namespace GeekCell\DddBundle\Tests\Unit\Infrastructure\Doctrine\Type; use Doctrine\DBAL\Platforms\AbstractPlatform; +use Doctrine\DBAL\Types\ConversionException; use GeekCell\Ddd\Domain\ValueObject\Uuid; use GeekCell\DddBundle\Infrastructure\Doctrine\Type\AbstractUuidType; use Mockery; @@ -29,10 +30,12 @@ protected function getIdType(): string class AbstractUuidTypeTest extends TestCase { + private const UUID_STRING = '00000000-0000-0000-0000-000000000000'; + public function testConvertToDatabaseValue(): void { // Given - $uuidString = '00000000-0000-0000-0000-000000000000'; + $uuidString = self::UUID_STRING; $platform = Mockery::mock(AbstractPlatform::class); $type = new FooUuidType(); @@ -46,10 +49,37 @@ public function testConvertToDatabaseValue(): void $this->assertSame($uuidString, $result); } + public function testConvertToDatabaseValueScalar(): void + { + // Given + $uuidString = self::UUID_STRING; + $platform = Mockery::mock(AbstractPlatform::class); + $type = new FooUuidType(); + + // When + $result = $type->convertToDatabaseValue($uuidString, $platform); + + // Then + $this->assertSame($uuidString, $result); + } + + public function testConvertToDatabaseValueInvalidType(): void + { + // Given + $platform = Mockery::mock(AbstractPlatform::class); + $type = new FooUuidType(); + + // Then + $this->expectException(ConversionException::class); + + // When + $type->convertToDatabaseValue(42, $platform); + } + public function testConvertToPhpValue(): void { // Given - $uuidString = '00000000-0000-0000-0000-000000000000'; + $uuidString = self::UUID_STRING; $platform = Mockery::mock(AbstractPlatform::class); $type = new FooUuidType();