Skip to content

Commit

Permalink
Add a new scalar BigInt
Browse files Browse the repository at this point in the history
  • Loading branch information
spawnia committed May 9, 2023
1 parent 1d89d56 commit 365ad80
Show file tree
Hide file tree
Showing 11 changed files with 94 additions and 117 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)

This comment has been minimized.

Copy link
@simPod

simPod May 9, 2023

Contributor

I think the desc is wrong

This comment has been minimized.

Copy link
@spawnia

spawnia May 9, 2023

Author Member

I fixed it before release. Feel free to improve it further.

This comment has been minimized.

Copy link
@simPod

simPod May 9, 2023

Contributor

ah ok, i checked only relevant commits

are hexadecimal integers between `00` and `FF` specifying the intensity of the color.
DESCRIPTION;

public static function regex(): string
{
return "/\d+/";
}
}
12 changes: 3 additions & 9 deletions src/Regex.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ public static function regex(): string
return $concreteRegex;
}

/**
* Return the Regex that the values are validated against.
*/
/** Return the Regex that the values are validated against. */
abstract public static function regex(): string;

public function serialize($value): string
Expand All @@ -58,9 +56,7 @@ public function serialize($value): string
return $stringValue;
}

/**
* Determine if the given string matches the regex defined in this class.
*/
/** Determine if the given string matches the regex defined in this class. */
protected static function matchesRegex(string $value): bool
{
return RegexValidator::match(static::regex(), $value)
Expand Down Expand Up @@ -94,9 +90,7 @@ public function parseLiteral($valueNode, ?array $variables = null): string
return $value;
}

/**
* Construct the error message that occurs when the given string does not match the regex.
*/
/** Construct the error message that occurs when the given string does not match the regex. */
public static function unmatchedRegexMessage(string $value): string
{
$safeValue = GraphQLUtils::printSafeJson($value);
Expand Down
12 changes: 3 additions & 9 deletions src/StringScalar.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ abstract class StringScalar extends ScalarType
public static function make(string $name, ?string $description, callable $isValid): self
{
$concreteStringScalar = new class() extends StringScalar {
/**
* @var callable(string): bool
*/
/** @var callable(string): bool */
public $isValid;

protected function isValid(string $stringValue): bool
Expand All @@ -37,9 +35,7 @@ protected function isValid(string $stringValue): bool
return $concreteStringScalar;
}

/**
* Check if the given string is valid.
*/
/** Check if the given string is valid. */
abstract protected function isValid(string $stringValue): bool;

public function serialize($value): string
Expand All @@ -55,9 +51,7 @@ public function serialize($value): string
return $stringValue;
}

/**
* Construct an error message that occurs when an invalid string is passed.
*/
/** Construct an error message that occurs when an invalid string is passed. */
public function invalidStringMessage(string $stringValue): string
{
$safeValue = GraphQLUtils::printSafeJson($stringValue);
Expand Down
4 changes: 1 addition & 3 deletions src/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@

final class Utils
{
/**
* Check if a value can be serialized to a string.
*/
/** Check if a value can be serialized to a string. */
public static function canBeString(mixed $value): bool
{
return null === $value
Expand Down
39 changes: 39 additions & 0 deletions tests/BigIntTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php declare(strict_types=1);

namespace MLL\GraphQLScalars\Tests;

use GraphQL\Error\InvariantViolation;
use MLL\GraphQLScalars\BigInt;
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);
}
}
20 changes: 5 additions & 15 deletions tests/DateScalarTestBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@

abstract class DateScalarTestBase extends TestCase
{
/**
* @dataProvider invalidDateValues
*/
/** @dataProvider invalidDateValues */
public function testThrowsIfSerializingInvalidDates(mixed $value): void
{
$dateScalar = $this->scalarInstance();
Expand All @@ -22,9 +20,7 @@ public function testThrowsIfSerializingInvalidDates(mixed $value): void
$dateScalar->serialize($value);
}

/**
* @dataProvider invalidDateValues
*/
/** @dataProvider invalidDateValues */
public function testThrowsIfParseValueInvalidDate(mixed $value): void
{
$dateScalar = $this->scalarInstance();
Expand All @@ -46,19 +42,15 @@ public static function invalidDateValues(): iterable
yield [''];
}

/**
* @dataProvider validDates
*/
/** @dataProvider validDates */
public function testParsesValueString(string $value, string $expected): void
{
$parsedValue = $this->scalarInstance()->parseValue($value);

self::assertSame($expected, $parsedValue->format('Y-m-d\TH:i:s.uP'));
}

/**
* @dataProvider validDates
*/
/** @dataProvider validDates */
public function testParsesLiteral(string $value, string $expected): void
{
$dateLiteral = new StringValueNode(
Expand Down Expand Up @@ -86,9 +78,7 @@ public function testSerializesDateTimeInterfaceInstance(): void
self::assertNotEmpty($result);
}

/**
* The specific instance under test.
*/
/** The specific instance under test. */
abstract protected function scalarInstance(): DateScalar;

/**
Expand Down
12 changes: 3 additions & 9 deletions tests/MixedScalarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ public function setUp(): void
$this->schema = new Schema($schemaConfig);
}

/**
* @dataProvider singleValues
*/
/** @dataProvider singleValues */
public function testSerializePassesThroughAnything(mixed $value): void
{
self::assertSame(
Expand All @@ -54,9 +52,7 @@ public function testSerializePassesThroughAnything(mixed $value): void
);
}

/**
* @dataProvider singleValues
*/
/** @dataProvider singleValues */
public function testParseValuePassesThroughAnything(mixed $value): void
{
self::assertSame(
Expand All @@ -81,9 +77,7 @@ public static function singleValues(): iterable
yield [[self::class, 'singleValues']];
}

/**
* @dataProvider literalToPhpMap
*/
/** @dataProvider literalToPhpMap */
public function testCastsValuesIntoAppropriatePhpValue(string $graphQLLiteral, string $jsonLiteral, mixed $expected): void
{
$graphqlResult = $this->executeQueryWithLiteral($graphQLLiteral);
Expand Down
4 changes: 1 addition & 3 deletions tests/NullScalarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ final class NullScalarTest extends TestCase
{
private Schema $schema;

/**
* @var mixed will be returned by field mixed
*/
/** @var mixed will be returned by field mixed */
private $return;

public function setUp(): void
Expand Down
44 changes: 11 additions & 33 deletions tests/RegexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,14 @@ public static function regex(): string
];
}

/**
* @dataProvider regexClassProvider
*/
/** @dataProvider regexClassProvider */
public function testCreateNamedRegexClass(Regex $regex): void
{
self::assertSame('MyRegex', $regex->name);
self::assertSame('Bar', $regex->description);
}

/**
* @dataProvider regexClassProvider
*/
/** @dataProvider regexClassProvider */
public function testSerializeThrowsIfUnserializableValueIsGiven(Regex $regex): void
{
$object = new class() {};
Expand All @@ -67,9 +63,7 @@ public function testSerializeThrowsIfUnserializableValueIsGiven(Regex $regex): v
$regex->serialize($object);
}

/**
* @dataProvider regexClassProvider
*/
/** @dataProvider regexClassProvider */
public function testSerializeThrowsIfRegexIsNotMatched(Regex $regex): void
{
$this->expectExceptionObject(new InvariantViolation(
Expand All @@ -79,19 +73,15 @@ public function testSerializeThrowsIfRegexIsNotMatched(Regex $regex): void
$regex->serialize('bar');
}

/**
* @dataProvider regexClassProvider
*/
/** @dataProvider regexClassProvider */
public function testSerializePassesWhenRegexMatches(Regex $regex): void
{
$serializedResult = $regex->serialize('foo');

self::assertSame('foo', $serializedResult);
}

/**
* @dataProvider regexClassProvider
*/
/** @dataProvider regexClassProvider */
public function testSerializePassesForStringableObject(Regex $regex): void
{
$serializedResult = $regex->serialize(
Expand All @@ -106,9 +96,7 @@ public function __toString(): string
self::assertSame('Contains foo right?', $serializedResult);
}

/**
* @dataProvider regexClassProvider
*/
/** @dataProvider regexClassProvider */
public function testParseValueThrowsIfValueCantBeString(Regex $regex): void
{
$object = new class() {};
Expand All @@ -118,19 +106,15 @@ public function testParseValueThrowsIfValueCantBeString(Regex $regex): void
$regex->parseValue($object);
}

/**
* @dataProvider regexClassProvider
*/
/** @dataProvider regexClassProvider */
public function testParseValueThrowsIfValueDoesNotMatch(Regex $regex): void
{
$this->expectException(Error::class);
$this->expectExceptionMessageMatches(/** @lang RegExp */ '/did not match the regex/');
$regex->parseValue('');
}

/**
* @dataProvider regexClassProvider
*/
/** @dataProvider regexClassProvider */
public function testParseValuePassesOnMatch(Regex $regex): void
{
self::assertSame(
Expand All @@ -139,9 +123,7 @@ public function testParseValuePassesOnMatch(Regex $regex): void
);
}

/**
* @dataProvider regexClassProvider
*/
/** @dataProvider regexClassProvider */
public function testParseLiteralThrowsIfNotString(Regex $regex): void
{
$intValueNode = new IntValueNode([]);
Expand All @@ -151,9 +133,7 @@ public function testParseLiteralThrowsIfNotString(Regex $regex): void
$regex->parseLiteral($intValueNode);
}

/**
* @dataProvider regexClassProvider
*/
/** @dataProvider regexClassProvider */
public function testParseLiteralThrowsIfValueDoesNotMatch(Regex $regex): void
{
$stringValueNode = new StringValueNode(['value' => 'asdf']);
Expand All @@ -163,9 +143,7 @@ public function testParseLiteralThrowsIfValueDoesNotMatch(Regex $regex): void
$regex->parseLiteral($stringValueNode);
}

/**
* @dataProvider regexClassProvider
*/
/** @dataProvider regexClassProvider */
public function testParseLiteralPassesOnMatch(Regex $regex): void
{
self::assertSame(
Expand Down

0 comments on commit 365ad80

Please sign in to comment.