diff --git a/src/Infrastructure/Doctrine/Type/AbstractIdType.php b/src/Infrastructure/Doctrine/Type/AbstractIdType.php index 4308767..3335a66 100644 --- a/src/Infrastructure/Doctrine/Type/AbstractIdType.php +++ b/src/Infrastructure/Doctrine/Type/AbstractIdType.php @@ -17,6 +17,10 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform) return $value; } + if ($value === null) { + return null; + } + if (!$value instanceof Id) { throw ConversionException::conversionFailedInvalidType( $value, @@ -30,6 +34,10 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform) public function convertToPHPValue($value, AbstractPlatform $platform) { + if ($value === null) { + return null; + } + $idType = $this->getIdType(); if (!is_subclass_of($idType, Id::class)) { throw ConversionException::conversionFailedUnserialization( diff --git a/src/Infrastructure/Doctrine/Type/AbstractUuidType.php b/src/Infrastructure/Doctrine/Type/AbstractUuidType.php index 0113b5e..ef6af97 100644 --- a/src/Infrastructure/Doctrine/Type/AbstractUuidType.php +++ b/src/Infrastructure/Doctrine/Type/AbstractUuidType.php @@ -21,6 +21,10 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform) return $value; } + if ($value === null) { + return null; + } + if (!$value instanceof Uuid) { throw ConversionException::conversionFailedInvalidType( $value, @@ -38,6 +42,10 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform) */ public function convertToPHPValue($value, AbstractPlatform $platform) { + if ($value === null) { + return null; + } + $idType = $this->getIdType(); if (!is_subclass_of($idType, Uuid::class)) { throw ConversionException::conversionFailedUnserialization( diff --git a/tests/Unit/Infrastructure/Doctrine/PaginatorTest.php b/tests/Unit/Infrastructure/Doctrine/PaginatorTest.php index 1ebd63d..d77cccf 100644 --- a/tests/Unit/Infrastructure/Doctrine/PaginatorTest.php +++ b/tests/Unit/Infrastructure/Doctrine/PaginatorTest.php @@ -9,7 +9,7 @@ use Mockery; use PHPUnit\Framework\TestCase; -class DoctrinePaginatorTest extends TestCase +class PaginatorTest extends TestCase { /** @var OrmPaginator|Mockery\MockInterface */ private mixed $ormPaginatorMock; diff --git a/tests/Unit/Infrastructure/Doctrine/Type/AbstractIdTypeTest.php b/tests/Unit/Infrastructure/Doctrine/Type/AbstractIdTypeTest.php index 55db631..eff3520 100644 --- a/tests/Unit/Infrastructure/Doctrine/Type/AbstractIdTypeTest.php +++ b/tests/Unit/Infrastructure/Doctrine/Type/AbstractIdTypeTest.php @@ -77,6 +77,19 @@ public function testConvertToDatabaseValueInvalidType(): void $type->convertToDatabaseValue('foo', $platform); } + public function testConvertToDatabaseValueNull(): void + { + // Given + $type = new FooIdType(); + $platform = Mockery::mock(AbstractPlatform::class); + + // When + $result = $type->convertToDatabaseValue(null, $platform); + + // Then + $this->assertNull($result); + } + public function testConvertToPhpValue(): void { // Given @@ -91,4 +104,17 @@ public function testConvertToPhpValue(): void $this->assertInstanceOf(FooId::class, $result); $this->assertSame($intId, $result->getValue()); } + + public function testConvertToPhpValueNull(): void + { + // Given + $type = new FooIdType(); + $platform = Mockery::mock(AbstractPlatform::class); + + // When + $result = $type->convertToPHPValue(null, $platform); + + // Then + $this->assertNull($result); + } } diff --git a/tests/Unit/Infrastructure/Doctrine/Type/AbstractUuidTypeTest.php b/tests/Unit/Infrastructure/Doctrine/Type/AbstractUuidTypeTest.php index 91d2a1e..3cda058 100644 --- a/tests/Unit/Infrastructure/Doctrine/Type/AbstractUuidTypeTest.php +++ b/tests/Unit/Infrastructure/Doctrine/Type/AbstractUuidTypeTest.php @@ -63,6 +63,19 @@ public function testConvertToDatabaseValueScalar(): void $this->assertSame($uuidString, $result); } + public function testConvertToDatabaseValueNull(): void + { + // Given + $platform = Mockery::mock(AbstractPlatform::class); + $type = new FooUuidType(); + + // When + $result = $type->convertToDatabaseValue(null, $platform); + + // Then + $this->assertNull($result); + } + public function testConvertToDatabaseValueInvalidType(): void { // Given @@ -90,4 +103,17 @@ public function testConvertToPhpValue(): void $this->assertInstanceOf(FooUuid::class, $result); $this->assertSame($uuidString, $result->getValue()); } + + public function testConvertToPhpValueNull(): void + { + // Given + $platform = Mockery::mock(AbstractPlatform::class); + $type = new FooUuidType(); + + // When + $result = $type->convertToPHPValue(null, $platform); + + // Then + $this->assertNull($result); + } }