Skip to content

Commit

Permalink
fix(types): fix on object usage
Browse files Browse the repository at this point in the history
  • Loading branch information
Guikingone committed Jul 11, 2020
1 parent 8f81911 commit cf26413
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/Ulid/UlidType.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Doctrine\DBAL\Types\ConversionException;
use Doctrine\DBAL\Types\GuidType;
use Symfony\Component\Uid\Ulid;
use Throwable;

/**
* @author Guillaume Loulier <[email protected]>
Expand Down Expand Up @@ -42,6 +41,10 @@ public function convertToPHPValue($value, AbstractPlatform $platform)
return null;
}

if ($value instanceof Ulid) {
return $value;
}

if (!Ulid::isValid($value)) {
throw ConversionException::conversionFailed($value, self::NAME);
}
Expand Down
4 changes: 4 additions & 0 deletions src/Uuid/UuidType.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ public function convertToPHPValue($value, AbstractPlatform $platform)
return null;
}

if ($value instanceof Uuid) {
return $value;
}

if (!Uuid::isValid($value)) {
throw ConversionException::conversionFailed($value, self::NAME);
}
Expand Down
10 changes: 10 additions & 0 deletions tests/Ulid/UlidTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,14 @@ public function testTypeCanBeConvertedToPHPValueWhenUsingValidString(): void
$value = Type::getType('ulid')->convertToPHPValue(strtr($entryValue, ['-' => '0']), $platform);
static::assertInstanceOf(Ulid::class, $value);
}

public function testTypeCanBeConvertedToPHPValueWhenUsingAnUlidObject(): void
{
$platform = $this->createMock(AbstractPlatform::class);

$ulid = new Ulid();

$value = Type::getType('ulid')->convertToPHPValue($ulid, $platform);
static::assertInstanceOf(Ulid::class, $value);
}
}
10 changes: 10 additions & 0 deletions tests/Uuid/UuidTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,14 @@ public function testTypeCanBeConvertedToPHPValueWhenUsingUuid(): void
$value = Type::getType('uuid')->convertToPHPValue($uuid->toRfc4122(), $platform);
static::assertInstanceOf(Uuid::class, $value);
}

public function testTypeCanBeConvertedToPHPValueWhenUsingAnUuidObject(): void
{
$platform = $this->createMock(AbstractPlatform::class);

$uuid = Uuid::v4();

$value = Type::getType('uuid')->convertToPHPValue($uuid, $platform);
static::assertInstanceOf(Uuid::class, $value);
}
}

0 comments on commit cf26413

Please sign in to comment.