Skip to content

Commit 8f2d52c

Browse files
authored
fix: Enable custom doctrine types to handle null values
* chore: rename DoctrinePaginatorTest to match file name * fix: let custom doctrine types handle null values refs: #26
1 parent d258d7e commit 8f2d52c

File tree

5 files changed

+69
-1
lines changed

5 files changed

+69
-1
lines changed

src/Infrastructure/Doctrine/Type/AbstractIdType.php

+8
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform)
1717
return $value;
1818
}
1919

20+
if ($value === null) {
21+
return null;
22+
}
23+
2024
if (!$value instanceof Id) {
2125
throw ConversionException::conversionFailedInvalidType(
2226
$value,
@@ -30,6 +34,10 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform)
3034

3135
public function convertToPHPValue($value, AbstractPlatform $platform)
3236
{
37+
if ($value === null) {
38+
return null;
39+
}
40+
3341
$idType = $this->getIdType();
3442
if (!is_subclass_of($idType, Id::class)) {
3543
throw ConversionException::conversionFailedUnserialization(

src/Infrastructure/Doctrine/Type/AbstractUuidType.php

+8
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform)
2121
return $value;
2222
}
2323

24+
if ($value === null) {
25+
return null;
26+
}
27+
2428
if (!$value instanceof Uuid) {
2529
throw ConversionException::conversionFailedInvalidType(
2630
$value,
@@ -38,6 +42,10 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform)
3842
*/
3943
public function convertToPHPValue($value, AbstractPlatform $platform)
4044
{
45+
if ($value === null) {
46+
return null;
47+
}
48+
4149
$idType = $this->getIdType();
4250
if (!is_subclass_of($idType, Uuid::class)) {
4351
throw ConversionException::conversionFailedUnserialization(

tests/Unit/Infrastructure/Doctrine/PaginatorTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use Mockery;
1010
use PHPUnit\Framework\TestCase;
1111

12-
class DoctrinePaginatorTest extends TestCase
12+
class PaginatorTest extends TestCase
1313
{
1414
/** @var OrmPaginator|Mockery\MockInterface */
1515
private mixed $ormPaginatorMock;

tests/Unit/Infrastructure/Doctrine/Type/AbstractIdTypeTest.php

+26
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,19 @@ public function testConvertToDatabaseValueInvalidType(): void
7777
$type->convertToDatabaseValue('foo', $platform);
7878
}
7979

80+
public function testConvertToDatabaseValueNull(): void
81+
{
82+
// Given
83+
$type = new FooIdType();
84+
$platform = Mockery::mock(AbstractPlatform::class);
85+
86+
// When
87+
$result = $type->convertToDatabaseValue(null, $platform);
88+
89+
// Then
90+
$this->assertNull($result);
91+
}
92+
8093
public function testConvertToPhpValue(): void
8194
{
8295
// Given
@@ -91,4 +104,17 @@ public function testConvertToPhpValue(): void
91104
$this->assertInstanceOf(FooId::class, $result);
92105
$this->assertSame($intId, $result->getValue());
93106
}
107+
108+
public function testConvertToPhpValueNull(): void
109+
{
110+
// Given
111+
$type = new FooIdType();
112+
$platform = Mockery::mock(AbstractPlatform::class);
113+
114+
// When
115+
$result = $type->convertToPHPValue(null, $platform);
116+
117+
// Then
118+
$this->assertNull($result);
119+
}
94120
}

tests/Unit/Infrastructure/Doctrine/Type/AbstractUuidTypeTest.php

+26
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,19 @@ public function testConvertToDatabaseValueScalar(): void
6363
$this->assertSame($uuidString, $result);
6464
}
6565

66+
public function testConvertToDatabaseValueNull(): void
67+
{
68+
// Given
69+
$platform = Mockery::mock(AbstractPlatform::class);
70+
$type = new FooUuidType();
71+
72+
// When
73+
$result = $type->convertToDatabaseValue(null, $platform);
74+
75+
// Then
76+
$this->assertNull($result);
77+
}
78+
6679
public function testConvertToDatabaseValueInvalidType(): void
6780
{
6881
// Given
@@ -90,4 +103,17 @@ public function testConvertToPhpValue(): void
90103
$this->assertInstanceOf(FooUuid::class, $result);
91104
$this->assertSame($uuidString, $result->getValue());
92105
}
106+
107+
public function testConvertToPhpValueNull(): void
108+
{
109+
// Given
110+
$platform = Mockery::mock(AbstractPlatform::class);
111+
$type = new FooUuidType();
112+
113+
// When
114+
$result = $type->convertToPHPValue(null, $platform);
115+
116+
// Then
117+
$this->assertNull($result);
118+
}
93119
}

0 commit comments

Comments
 (0)