Skip to content

Commit b6f0cb1

Browse files
authored
fix: Add additional check for scalar values. (#9)
1 parent 1e04f2b commit b6f0cb1

File tree

4 files changed

+68
-2
lines changed

4 files changed

+68
-2
lines changed

src/Infrastructure/Doctrine/Type/AbstractIdType.php

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ abstract class AbstractIdType extends IntegerType
1313
{
1414
public function convertToDatabaseValue($value, AbstractPlatform $platform)
1515
{
16+
if (is_int($value)) {
17+
return $value;
18+
}
19+
1620
if (!$value instanceof Id) {
1721
throw ConversionException::conversionFailedInvalidType(
1822
$value,

src/Infrastructure/Doctrine/Type/AbstractUuidType.php

+4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ abstract class AbstractUuidType extends GuidType
1717
*/
1818
public function convertToDatabaseValue($value, AbstractPlatform $platform)
1919
{
20+
if (is_string($value)) {
21+
return $value;
22+
}
23+
2024
if (!$value instanceof Uuid) {
2125
throw ConversionException::conversionFailedInvalidType(
2226
$value,

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

+28
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace GeekCell\DddBundle\Tests\Unit\Infrastructure\Doctrine\Type;
66

77
use Doctrine\DBAL\Platforms\AbstractPlatform;
8+
use Doctrine\DBAL\Types\ConversionException;
89
use GeekCell\Ddd\Domain\ValueObject\Id;
910
use GeekCell\DddBundle\Infrastructure\Doctrine\Type\AbstractIdType;
1011
use Mockery;
@@ -49,6 +50,33 @@ public function testConvertToDatabaseValue(): void
4950
$this->assertSame($intId, $result);
5051
}
5152

53+
public function testConvertToDatabaseValueScalar(): void
54+
{
55+
// Given
56+
$intId = 42;
57+
$type = new FooIdType();
58+
$platform = Mockery::mock(AbstractPlatform::class);
59+
60+
// When
61+
$result = $type->convertToDatabaseValue($intId, $platform);
62+
63+
// Then
64+
$this->assertSame($intId, $result);
65+
}
66+
67+
public function testConvertToDatabaseValueInvalidType(): void
68+
{
69+
// Given
70+
$type = new FooIdType();
71+
$platform = Mockery::mock(AbstractPlatform::class);
72+
73+
// Then
74+
$this->expectException(ConversionException::class);
75+
76+
// When
77+
$type->convertToDatabaseValue('foo', $platform);
78+
}
79+
5280
public function testConvertToPhpValue(): void
5381
{
5482
// Given

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

+32-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace GeekCell\DddBundle\Tests\Unit\Infrastructure\Doctrine\Type;
66

77
use Doctrine\DBAL\Platforms\AbstractPlatform;
8+
use Doctrine\DBAL\Types\ConversionException;
89
use GeekCell\Ddd\Domain\ValueObject\Uuid;
910
use GeekCell\DddBundle\Infrastructure\Doctrine\Type\AbstractUuidType;
1011
use Mockery;
@@ -29,10 +30,12 @@ protected function getIdType(): string
2930

3031
class AbstractUuidTypeTest extends TestCase
3132
{
33+
private const UUID_STRING = '00000000-0000-0000-0000-000000000000';
34+
3235
public function testConvertToDatabaseValue(): void
3336
{
3437
// Given
35-
$uuidString = '00000000-0000-0000-0000-000000000000';
38+
$uuidString = self::UUID_STRING;
3639
$platform = Mockery::mock(AbstractPlatform::class);
3740
$type = new FooUuidType();
3841

@@ -46,10 +49,37 @@ public function testConvertToDatabaseValue(): void
4649
$this->assertSame($uuidString, $result);
4750
}
4851

52+
public function testConvertToDatabaseValueScalar(): void
53+
{
54+
// Given
55+
$uuidString = self::UUID_STRING;
56+
$platform = Mockery::mock(AbstractPlatform::class);
57+
$type = new FooUuidType();
58+
59+
// When
60+
$result = $type->convertToDatabaseValue($uuidString, $platform);
61+
62+
// Then
63+
$this->assertSame($uuidString, $result);
64+
}
65+
66+
public function testConvertToDatabaseValueInvalidType(): void
67+
{
68+
// Given
69+
$platform = Mockery::mock(AbstractPlatform::class);
70+
$type = new FooUuidType();
71+
72+
// Then
73+
$this->expectException(ConversionException::class);
74+
75+
// When
76+
$type->convertToDatabaseValue(42, $platform);
77+
}
78+
4979
public function testConvertToPhpValue(): void
5080
{
5181
// Given
52-
$uuidString = '00000000-0000-0000-0000-000000000000';
82+
$uuidString = self::UUID_STRING;
5383
$platform = Mockery::mock(AbstractPlatform::class);
5484
$type = new FooUuidType();
5585

0 commit comments

Comments
 (0)