Skip to content

Commit

Permalink
Added UUID type
Browse files Browse the repository at this point in the history
  • Loading branch information
peldax committed Jun 5, 2021
1 parent 191bcb4 commit 44b0dd4
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/UUIDType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types = 1);

namespace Graphpinator\ExtraTypes;

final class UUIDType extends \Graphpinator\Type\ScalarType
{
protected const NAME = 'UUID';
protected const DESCRIPTION = 'UUID type - string which contains valid UUID (universally unique identifier).';

public function __construct()
{
parent::__construct();

$this->setSpecifiedBy('https://datatracker.ietf.org/doc/html/rfc4122');
}

public function validateNonNullValue(mixed $rawValue) : bool
{
return \is_string($rawValue)
&& \preg_match('/^{?[A-Z0-9]{8}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{12}}?$/', $rawValue) === 1;
}
}
62 changes: 62 additions & 0 deletions tests/Unit/UUIDTypeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

declare(strict_types = 1);

namespace Graphpinator\ExtraTypes\Tests\Unit;

final class UUIDTypeTest extends \PHPUnit\Framework\TestCase
{
public function simpleDataProvider() : array
{
return [
['A98C5A1E-A742-4808-96FA-6F409E799937'],
];
}

public function invalidDataProvider() : array
{
return [
[''],
['A98C5A1E-A742-4808-96FA-6F409E799937A'],
['A98C5A1E-A742-4808-96FA-6F409E799937-'],
[true],
[420],
[420.42],
['beetlejuice'],
[(object) []],
[[]],
];
}

/**
* @dataProvider simpleDataProvider
* @param string $rawValue
*/
public function testValidateValue(string $rawValue) : void
{
$type = new \Graphpinator\ExtraTypes\UUIDType();
$value = $type->accept(new \Graphpinator\Resolver\CreateResolvedValueVisitor($rawValue));

self::assertSame($type, $value->getType());
self::assertSame($rawValue, $value->getRawValue());
}

/**
* @dataProvider invalidDataProvider
* @param int|bool|string|float|array $rawValue
*/
public function testValidateValueInvalid($rawValue) : void
{
$this->expectException(\Graphpinator\Exception\Value\InvalidValue::class);

$ipv4 = new \Graphpinator\ExtraTypes\UUIDType();
$ipv4->accept(new \Graphpinator\Resolver\CreateResolvedValueVisitor($rawValue));
}

public function testSpecifiedBy() : void
{
$type = new \Graphpinator\ExtraTypes\UUIDType();

self::assertSame('https://datatracker.ietf.org/doc/html/rfc4122', $type->getSpecifiedByUrl());
}
}

0 comments on commit 44b0dd4

Please sign in to comment.