Skip to content

Commit

Permalink
Adding a new scalar BigInt
Browse files Browse the repository at this point in the history
  • Loading branch information
dhaupt88 committed May 9, 2023
1 parent 1d89d56 commit f1f4fe7
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 3 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,8 @@ class HexValue extends Regex
{
/**
* The description that is used for schema introspection.
*
* @var string
*/
public $description = <<<'DESCRIPTION'
public ?string $description = <<<'DESCRIPTION'
A hexadecimal color is specified with: `#RRGGBB`, where `RR` (red), `GG` (green) and `BB` (blue)
are hexadecimal integers between `00` and `FF` specifying the intensity of the color.
DESCRIPTION;
Expand Down
16 changes: 16 additions & 0 deletions src/BigInt.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php declare(strict_types=1);

namespace MLL\GraphQLScalars;

class BigInt extends Regex
{
public ?string $description = <<<'DESCRIPTION'
A hexadecimal color is specified with: `#RRGGBB`, where `RR` (red), `GG` (green) and `BB` (blue)
are hexadecimal integers between `00` and `FF` specifying the intensity of the color.
DESCRIPTION;

public static function regex(): string
{
return "/\d+/";
}
}
42 changes: 42 additions & 0 deletions tests/BigIntTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php declare(strict_types=1);

namespace MLL\GraphQLScalars\Tests;

use GraphQL\Error\Error;
use GraphQL\Error\InvariantViolation;
use GraphQL\Language\AST\StringValueNode;
use MLL\GraphQLScalars\BigInt;
use MLL\GraphQLScalars\Email;
use PHPUnit\Framework\TestCase;

final class BigIntTest extends TestCase
{
public function testSerializeThrowsIfBigIntIsInvalid(): void
{
$bigInt = new BigInt();

$this->expectExceptionObject(new InvariantViolation('The given value "foo" did not match the regex /\d+/.'));
$bigInt->serialize('foo');
}

public function testSerializePassesWhenBigIntIsValid(): void
{
$serializedResult = (new BigInt())->serialize(10000000000000);

self::assertSame("10000000000000", $serializedResult);
}

public function testSerializePassesWhenBigIntIsValidAsString(): void
{
$serializedResult = (new BigInt())->serialize("10000000000000");

self::assertSame("10000000000000", $serializedResult);
}

public function testParseBigIntIsValid(): void
{
$parsedResult = (new BigInt())->parseValue(10000000000000);

self::assertSame("10000000000000", $parsedResult);
}
}

0 comments on commit f1f4fe7

Please sign in to comment.