From d3fd2b98c7a9485d1bd4590ddb93d2310a39e1cd Mon Sep 17 00:00:00 2001 From: Bl00D4NGEL Date: Mon, 13 Mar 2023 17:49:25 +0100 Subject: [PATCH 1/2] chore: rename DoctrinePaginatorTest to match file name --- tests/Unit/Infrastructure/Doctrine/PaginatorTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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; From 8d45c154d7057622d61d9aa3b9fc64573f3847d8 Mon Sep 17 00:00:00 2001 From: Bl00D4NGEL Date: Mon, 13 Mar 2023 17:57:09 +0100 Subject: [PATCH 2/2] feat: let custom doctrine types handle null values --- .../Doctrine/Type/AbstractIdType.php | 8 ++++++ .../Doctrine/Type/AbstractUuidType.php | 8 ++++++ .../Doctrine/Type/AbstractIdTypeTest.php | 26 +++++++++++++++++++ .../Doctrine/Type/AbstractUuidTypeTest.php | 26 +++++++++++++++++++ 4 files changed, 68 insertions(+) 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/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); + } }