Skip to content

Commit 365ad80

Browse files
authored
Add a new scalar BigInt
1 parent 1d89d56 commit 365ad80

11 files changed

+94
-117
lines changed

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,8 @@ class HexValue extends Regex
8181
{
8282
/**
8383
* The description that is used for schema introspection.
84-
*
85-
* @var string
8684
*/
87-
public $description = <<<'DESCRIPTION'
85+
public ?string $description = <<<'DESCRIPTION'
8886
A hexadecimal color is specified with: `#RRGGBB`, where `RR` (red), `GG` (green) and `BB` (blue)
8987
are hexadecimal integers between `00` and `FF` specifying the intensity of the color.
9088
DESCRIPTION;

src/BigInt.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace MLL\GraphQLScalars;
4+
5+
class BigInt extends Regex
6+
{
7+
public ?string $description = <<<'DESCRIPTION'
8+
A hexadecimal color is specified with: `#RRGGBB`, where `RR` (red), `GG` (green) and `BB` (blue)
9+
are hexadecimal integers between `00` and `FF` specifying the intensity of the color.
10+
DESCRIPTION;
11+
12+
public static function regex(): string
13+
{
14+
return "/\d+/";
15+
}
16+
}

src/Regex.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@ public static function regex(): string
4040
return $concreteRegex;
4141
}
4242

43-
/**
44-
* Return the Regex that the values are validated against.
45-
*/
43+
/** Return the Regex that the values are validated against. */
4644
abstract public static function regex(): string;
4745

4846
public function serialize($value): string
@@ -58,9 +56,7 @@ public function serialize($value): string
5856
return $stringValue;
5957
}
6058

61-
/**
62-
* Determine if the given string matches the regex defined in this class.
63-
*/
59+
/** Determine if the given string matches the regex defined in this class. */
6460
protected static function matchesRegex(string $value): bool
6561
{
6662
return RegexValidator::match(static::regex(), $value)
@@ -94,9 +90,7 @@ public function parseLiteral($valueNode, ?array $variables = null): string
9490
return $value;
9591
}
9692

97-
/**
98-
* Construct the error message that occurs when the given string does not match the regex.
99-
*/
93+
/** Construct the error message that occurs when the given string does not match the regex. */
10094
public static function unmatchedRegexMessage(string $value): string
10195
{
10296
$safeValue = GraphQLUtils::printSafeJson($value);

src/StringScalar.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ abstract class StringScalar extends ScalarType
1919
public static function make(string $name, ?string $description, callable $isValid): self
2020
{
2121
$concreteStringScalar = new class() extends StringScalar {
22-
/**
23-
* @var callable(string): bool
24-
*/
22+
/** @var callable(string): bool */
2523
public $isValid;
2624

2725
protected function isValid(string $stringValue): bool
@@ -37,9 +35,7 @@ protected function isValid(string $stringValue): bool
3735
return $concreteStringScalar;
3836
}
3937

40-
/**
41-
* Check if the given string is valid.
42-
*/
38+
/** Check if the given string is valid. */
4339
abstract protected function isValid(string $stringValue): bool;
4440

4541
public function serialize($value): string
@@ -55,9 +51,7 @@ public function serialize($value): string
5551
return $stringValue;
5652
}
5753

58-
/**
59-
* Construct an error message that occurs when an invalid string is passed.
60-
*/
54+
/** Construct an error message that occurs when an invalid string is passed. */
6155
public function invalidStringMessage(string $stringValue): string
6256
{
6357
$safeValue = GraphQLUtils::printSafeJson($stringValue);

src/Utils.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010

1111
final class Utils
1212
{
13-
/**
14-
* Check if a value can be serialized to a string.
15-
*/
13+
/** Check if a value can be serialized to a string. */
1614
public static function canBeString(mixed $value): bool
1715
{
1816
return null === $value

tests/BigIntTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace MLL\GraphQLScalars\Tests;
4+
5+
use GraphQL\Error\InvariantViolation;
6+
use MLL\GraphQLScalars\BigInt;
7+
use PHPUnit\Framework\TestCase;
8+
9+
final class BigIntTest extends TestCase
10+
{
11+
public function testSerializeThrowsIfBigIntIsInvalid(): void
12+
{
13+
$bigInt = new BigInt();
14+
15+
$this->expectExceptionObject(new InvariantViolation('The given value "foo" did not match the regex /\d+/.'));
16+
$bigInt->serialize('foo');
17+
}
18+
19+
public function testSerializePassesWhenBigIntIsValid(): void
20+
{
21+
$serializedResult = (new BigInt())->serialize(10000000000000);
22+
23+
self::assertSame('10000000000000', $serializedResult);
24+
}
25+
26+
public function testSerializePassesWhenBigIntIsValidAsString(): void
27+
{
28+
$serializedResult = (new BigInt())->serialize('10000000000000');
29+
30+
self::assertSame('10000000000000', $serializedResult);
31+
}
32+
33+
public function testParseBigIntIsValid(): void
34+
{
35+
$parsedResult = (new BigInt())->parseValue(10000000000000);
36+
37+
self::assertSame('10000000000000', $parsedResult);
38+
}
39+
}

tests/DateScalarTestBase.php

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@
1111

1212
abstract class DateScalarTestBase extends TestCase
1313
{
14-
/**
15-
* @dataProvider invalidDateValues
16-
*/
14+
/** @dataProvider invalidDateValues */
1715
public function testThrowsIfSerializingInvalidDates(mixed $value): void
1816
{
1917
$dateScalar = $this->scalarInstance();
@@ -22,9 +20,7 @@ public function testThrowsIfSerializingInvalidDates(mixed $value): void
2220
$dateScalar->serialize($value);
2321
}
2422

25-
/**
26-
* @dataProvider invalidDateValues
27-
*/
23+
/** @dataProvider invalidDateValues */
2824
public function testThrowsIfParseValueInvalidDate(mixed $value): void
2925
{
3026
$dateScalar = $this->scalarInstance();
@@ -46,19 +42,15 @@ public static function invalidDateValues(): iterable
4642
yield [''];
4743
}
4844

49-
/**
50-
* @dataProvider validDates
51-
*/
45+
/** @dataProvider validDates */
5246
public function testParsesValueString(string $value, string $expected): void
5347
{
5448
$parsedValue = $this->scalarInstance()->parseValue($value);
5549

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

59-
/**
60-
* @dataProvider validDates
61-
*/
53+
/** @dataProvider validDates */
6254
public function testParsesLiteral(string $value, string $expected): void
6355
{
6456
$dateLiteral = new StringValueNode(
@@ -86,9 +78,7 @@ public function testSerializesDateTimeInterfaceInstance(): void
8678
self::assertNotEmpty($result);
8779
}
8880

89-
/**
90-
* The specific instance under test.
91-
*/
81+
/** The specific instance under test. */
9282
abstract protected function scalarInstance(): DateScalar;
9383

9484
/**

tests/MixedScalarTest.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@ public function setUp(): void
4141
$this->schema = new Schema($schemaConfig);
4242
}
4343

44-
/**
45-
* @dataProvider singleValues
46-
*/
44+
/** @dataProvider singleValues */
4745
public function testSerializePassesThroughAnything(mixed $value): void
4846
{
4947
self::assertSame(
@@ -54,9 +52,7 @@ public function testSerializePassesThroughAnything(mixed $value): void
5452
);
5553
}
5654

57-
/**
58-
* @dataProvider singleValues
59-
*/
55+
/** @dataProvider singleValues */
6056
public function testParseValuePassesThroughAnything(mixed $value): void
6157
{
6258
self::assertSame(
@@ -81,9 +77,7 @@ public static function singleValues(): iterable
8177
yield [[self::class, 'singleValues']];
8278
}
8379

84-
/**
85-
* @dataProvider literalToPhpMap
86-
*/
80+
/** @dataProvider literalToPhpMap */
8781
public function testCastsValuesIntoAppropriatePhpValue(string $graphQLLiteral, string $jsonLiteral, mixed $expected): void
8882
{
8983
$graphqlResult = $this->executeQueryWithLiteral($graphQLLiteral);

tests/NullScalarTest.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ final class NullScalarTest extends TestCase
1414
{
1515
private Schema $schema;
1616

17-
/**
18-
* @var mixed will be returned by field mixed
19-
*/
17+
/** @var mixed will be returned by field mixed */
2018
private $return;
2119

2220
public function setUp(): void

tests/RegexTest.php

Lines changed: 11 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,14 @@ public static function regex(): string
4747
];
4848
}
4949

50-
/**
51-
* @dataProvider regexClassProvider
52-
*/
50+
/** @dataProvider regexClassProvider */
5351
public function testCreateNamedRegexClass(Regex $regex): void
5452
{
5553
self::assertSame('MyRegex', $regex->name);
5654
self::assertSame('Bar', $regex->description);
5755
}
5856

59-
/**
60-
* @dataProvider regexClassProvider
61-
*/
57+
/** @dataProvider regexClassProvider */
6258
public function testSerializeThrowsIfUnserializableValueIsGiven(Regex $regex): void
6359
{
6460
$object = new class() {};
@@ -67,9 +63,7 @@ public function testSerializeThrowsIfUnserializableValueIsGiven(Regex $regex): v
6763
$regex->serialize($object);
6864
}
6965

70-
/**
71-
* @dataProvider regexClassProvider
72-
*/
66+
/** @dataProvider regexClassProvider */
7367
public function testSerializeThrowsIfRegexIsNotMatched(Regex $regex): void
7468
{
7569
$this->expectExceptionObject(new InvariantViolation(
@@ -79,19 +73,15 @@ public function testSerializeThrowsIfRegexIsNotMatched(Regex $regex): void
7973
$regex->serialize('bar');
8074
}
8175

82-
/**
83-
* @dataProvider regexClassProvider
84-
*/
76+
/** @dataProvider regexClassProvider */
8577
public function testSerializePassesWhenRegexMatches(Regex $regex): void
8678
{
8779
$serializedResult = $regex->serialize('foo');
8880

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

92-
/**
93-
* @dataProvider regexClassProvider
94-
*/
84+
/** @dataProvider regexClassProvider */
9585
public function testSerializePassesForStringableObject(Regex $regex): void
9686
{
9787
$serializedResult = $regex->serialize(
@@ -106,9 +96,7 @@ public function __toString(): string
10696
self::assertSame('Contains foo right?', $serializedResult);
10797
}
10898

109-
/**
110-
* @dataProvider regexClassProvider
111-
*/
99+
/** @dataProvider regexClassProvider */
112100
public function testParseValueThrowsIfValueCantBeString(Regex $regex): void
113101
{
114102
$object = new class() {};
@@ -118,19 +106,15 @@ public function testParseValueThrowsIfValueCantBeString(Regex $regex): void
118106
$regex->parseValue($object);
119107
}
120108

121-
/**
122-
* @dataProvider regexClassProvider
123-
*/
109+
/** @dataProvider regexClassProvider */
124110
public function testParseValueThrowsIfValueDoesNotMatch(Regex $regex): void
125111
{
126112
$this->expectException(Error::class);
127113
$this->expectExceptionMessageMatches(/** @lang RegExp */ '/did not match the regex/');
128114
$regex->parseValue('');
129115
}
130116

131-
/**
132-
* @dataProvider regexClassProvider
133-
*/
117+
/** @dataProvider regexClassProvider */
134118
public function testParseValuePassesOnMatch(Regex $regex): void
135119
{
136120
self::assertSame(
@@ -139,9 +123,7 @@ public function testParseValuePassesOnMatch(Regex $regex): void
139123
);
140124
}
141125

142-
/**
143-
* @dataProvider regexClassProvider
144-
*/
126+
/** @dataProvider regexClassProvider */
145127
public function testParseLiteralThrowsIfNotString(Regex $regex): void
146128
{
147129
$intValueNode = new IntValueNode([]);
@@ -151,9 +133,7 @@ public function testParseLiteralThrowsIfNotString(Regex $regex): void
151133
$regex->parseLiteral($intValueNode);
152134
}
153135

154-
/**
155-
* @dataProvider regexClassProvider
156-
*/
136+
/** @dataProvider regexClassProvider */
157137
public function testParseLiteralThrowsIfValueDoesNotMatch(Regex $regex): void
158138
{
159139
$stringValueNode = new StringValueNode(['value' => 'asdf']);
@@ -163,9 +143,7 @@ public function testParseLiteralThrowsIfValueDoesNotMatch(Regex $regex): void
163143
$regex->parseLiteral($stringValueNode);
164144
}
165145

166-
/**
167-
* @dataProvider regexClassProvider
168-
*/
146+
/** @dataProvider regexClassProvider */
169147
public function testParseLiteralPassesOnMatch(Regex $regex): void
170148
{
171149
self::assertSame(

0 commit comments

Comments
 (0)