Skip to content
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
15 changes: 12 additions & 3 deletions src/Symfony/Component/Uid/Tests/UuidTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,21 @@ class UuidTest extends TestCase
private const A_UUID_V1 = 'd9e7a184-5d5b-11ea-a62a-3499710062d0';
private const A_UUID_V4 = 'd6b3345b-2905-4048-a83c-b5988e765d98';

public function testConstructorWithInvalidUuid()
/**
* @dataProvider provideInvalidUuids
*/
public function testConstructorWithInvalidUuid(string $uuid)
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid UUID: "this is not a uuid".');
$this->expectExceptionMessage('Invalid UUID: "'.$uuid.'".');

Uuid::fromString('this is not a uuid');
Uuid::fromString($uuid);
}

public function provideInvalidUuids(): iterable
{
yield ['this is not a uuid'];
yield ['these are just thirty-six characters'];
}

public function testConstructorWithValidUuid()
Expand Down
14 changes: 12 additions & 2 deletions src/Symfony/Component/Uid/Uuid.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ class Uuid extends AbstractUid

public function __construct(string $uuid)
{
$type = uuid_type($uuid);
try {
$type = uuid_type($uuid);
} catch (\ValueError $e) {
throw new \InvalidArgumentException(sprintf('Invalid UUID%s: "%s".', static::TYPE ? 'v'.static::TYPE : '', $uuid), 0, $e);
}

if (false === $type || \UUID_TYPE_INVALID === $type || (static::TYPE ?: $type) !== $type) {
throw new \InvalidArgumentException(sprintf('Invalid UUID%s: "%s".', static::TYPE ? 'v'.static::TYPE : '', $uuid));
Expand Down Expand Up @@ -55,7 +59,13 @@ public static function fromString(string $uuid): parent
return new static($uuid);
}

switch (uuid_type($uuid)) {
try {
$type = uuid_type($uuid);
} catch (\ValueError $e) {
throw new \InvalidArgumentException(sprintf('Invalid UUID%s: "%s".', static::TYPE ? 'v'.static::TYPE : '', $uuid), 0, $e);
}

switch ($type) {
case UuidV1::TYPE: return new UuidV1($uuid);
case UuidV3::TYPE: return new UuidV3($uuid);
case UuidV4::TYPE: return new UuidV4($uuid);
Expand Down