Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Form] fix UUID tranformer #47528

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
Sep 9, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,14 @@ public function reverseTransform($value)
throw new TransformationFailedException('Expected a string.');
}

if (!Uuid::isValid($value)) {
throw new TransformationFailedException(sprintf('The value "%s" is not a valid UUID.', $value));
}

try {
$uuid = new Uuid($value);
return Uuid::fromString($value);
} catch (\InvalidArgumentException $e) {
throw new TransformationFailedException(sprintf('The value "%s" is not a valid UUID.', $value), $e->getCode(), $e);
}

return $uuid;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,15 @@
use Symfony\Component\Form\Exception\TransformationFailedException;
use Symfony\Component\Form\Extension\Core\DataTransformer\UuidToStringTransformer;
use Symfony\Component\Uid\Uuid;
use Symfony\Component\Uid\UuidV1;

class UuidToStringTransformerTest extends TestCase
{
public function provideValidUuid()
{
return [
['123e4567-e89b-12d3-a456-426655440000', new Uuid('123e4567-e89b-12d3-a456-426655440000')],
];
}

/**
* @dataProvider provideValidUuid
*/
public function testTransform($output, $input)
public function testTransform()
{
$transformer = new UuidToStringTransformer();

$input = new Uuid($input);

$this->assertEquals($output, $transformer->transform($input));
$this->assertEquals('123e4567-e89b-12d3-a456-426655440000', $transformer->transform(new UuidV1('123e4567-e89b-12d3-a456-426655440000')));
}

public function testTransformEmpty()
Expand All @@ -53,16 +42,11 @@ public function testTransformExpectsUuid()
$transformer->transform('1234');
}

/**
* @dataProvider provideValidUuid
*/
public function testReverseTransform($input, $output)
public function testReverseTransform()
{
$reverseTransformer = new UuidToStringTransformer();

$output = new Uuid($output);
$transformer = new UuidToStringTransformer();

$this->assertEquals($output, $reverseTransformer->reverseTransform($input));
$this->assertEquals(new UuidV1('123e4567-e89b-12d3-a456-426655440000'), $transformer->reverseTransform('123e4567-e89b-12d3-a456-426655440000'));
}

public function testReverseTransformEmpty()
Expand All @@ -78,7 +62,7 @@ public function testReverseTransformExpectsString()

$this->expectException(TransformationFailedException::class);

$reverseTransformer->reverseTransform(1234);
$reverseTransformer->reverseTransform(Uuid::fromString('123e4567-e89b-12d3-a456-426655440000')->toBase32());
}

public function testReverseTransformExpectsValidUuidString()
Expand Down