Skip to content

Add additional check for scalar values. #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Infrastructure/Doctrine/Type/AbstractIdType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions src/Infrastructure/Doctrine/Type/AbstractUuidType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
28 changes: 28 additions & 0 deletions tests/Unit/Infrastructure/Doctrine/Type/AbstractIdTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();

Expand All @@ -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();

Expand Down