Skip to content

Commit 5c0180d

Browse files
committed
Improve data providers
1 parent 49dd575 commit 5c0180d

20 files changed

+247
-306
lines changed

tests/Error/FormattedErrorTest.php

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,19 @@ public function testPrintVar($var, string $printed): void
2323
/**
2424
* @throws InvariantViolation
2525
*
26-
* @return array<int, array{mixed, string}>
26+
* @return iterable<array{mixed, string}>
2727
*/
28-
public function printVar(): array
28+
public static function printVar(): iterable
2929
{
30-
return [
31-
[Type::string(), 'GraphQLType: String'],
32-
[new NodeList([]), 'instance of GraphQL\Language\AST\NodeList(0)'],
33-
[[2], 'array(1)'],
34-
['', '(empty string)'],
35-
["'", "'\\''"],
36-
[true, 'true'],
37-
[false, 'false'],
38-
[1, '1'],
39-
[2.3, '2.3'],
40-
[null, 'null'],
41-
];
30+
yield [Type::string(), 'GraphQLType: String'];
31+
yield [new NodeList([]), 'instance of GraphQL\Language\AST\NodeList(0)'];
32+
yield [[2], 'array(1)'];
33+
yield ['', '(empty string)'];
34+
yield ["'", "'\\''"];
35+
yield [true, 'true'];
36+
yield [false, 'false'];
37+
yield [1, '1'];
38+
yield [2.3, '2.3'];
39+
yield [null, 'null'];
4240
}
4341
}

tests/Executor/AbstractTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ public function testResolveUnionTypeUsingTypenameOnSourceObject($dog, $cat): voi
432432
*
433433
* @return iterable<array{mixed, mixed}>
434434
*/
435-
public function dogCatRootValues(): iterable
435+
public static function dogCatRootValues(): iterable
436436
{
437437
yield [
438438
[

tests/Executor/Promise/SyncPromiseTest.php

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ final class SyncPromiseTest extends TestCaseBase
1717
* string,
1818
* }>
1919
*/
20-
public function fulfilledPromiseResolveData(): iterable
20+
public static function fulfilledPromiseResolveData(): iterable
2121
{
2222
$onFulfilledReturnsNull = static fn () => null;
2323

@@ -29,13 +29,11 @@ public function fulfilledPromiseResolveData(): iterable
2929
throw new \Exception('onFulfilled throws this!');
3030
};
3131

32-
return [
33-
['test-value', null, 'test-value', null, SyncPromise::FULFILLED],
34-
[\uniqid(), $onFulfilledReturnsNull, null, null, SyncPromise::FULFILLED],
35-
['test-value', $onFulfilledReturnsSameValue, 'test-value', null, SyncPromise::FULFILLED],
36-
['test-value-2', $onFulfilledReturnsOtherValue, 'other-test-value-2', null, SyncPromise::FULFILLED],
37-
['test-value-3', $onFulfilledThrows, null, 'onFulfilled throws this!', SyncPromise::REJECTED],
38-
];
32+
yield ['test-value', null, 'test-value', null, SyncPromise::FULFILLED];
33+
yield [\uniqid(), $onFulfilledReturnsNull, null, null, SyncPromise::FULFILLED];
34+
yield ['test-value', $onFulfilledReturnsSameValue, 'test-value', null, SyncPromise::FULFILLED];
35+
yield ['test-value-2', $onFulfilledReturnsOtherValue, 'other-test-value-2', null, SyncPromise::FULFILLED];
36+
yield ['test-value-3', $onFulfilledThrows, null, 'onFulfilled throws this!', SyncPromise::REJECTED];
3937
}
4038

4139
/** @dataProvider fulfilledPromiseResolveData */
@@ -179,7 +177,7 @@ static function (\Throwable $reason) use (&$actualNextReason, &$onRejectedCalled
179177
}
180178

181179
/** @return iterable<array{\Exception, ?callable, ?string, ?string, string}> */
182-
public function rejectedPromiseData(): iterable
180+
public static function rejectedPromiseData(): iterable
183181
{
184182
$onRejectedReturnsNull = static fn () => null;
185183

@@ -193,14 +191,12 @@ public function rejectedPromiseData(): iterable
193191
throw new \Exception('onRejected throws other!');
194192
};
195193

196-
return [
197-
// $rejectedReason, $onRejected, $expectedNextValue, $expectedNextReason, $expectedNextState
198-
[new \Exception('test-reason'), null, null, 'test-reason', SyncPromise::REJECTED],
199-
[new \Exception('test-reason-2'), $onRejectedReturnsNull, null, null, SyncPromise::FULFILLED],
200-
[new \Exception('test-reason-3'), $onRejectedReturnsSomeValue, 'some-value', null, SyncPromise::FULFILLED],
201-
[new \Exception('test-reason-4'), $onRejectedThrowsSameReason, null, 'test-reason-4', SyncPromise::REJECTED],
202-
[new \Exception('test-reason-5'), $onRejectedThrowsOtherReason, null, 'onRejected throws other!', SyncPromise::REJECTED],
203-
];
194+
// $rejectedReason, $onRejected, $expectedNextValue, $expectedNextReason, $expectedNextState
195+
yield [new \Exception('test-reason'), null, null, 'test-reason', SyncPromise::REJECTED];
196+
yield [new \Exception('test-reason-2'), $onRejectedReturnsNull, null, null, SyncPromise::FULFILLED];
197+
yield [new \Exception('test-reason-3'), $onRejectedReturnsSomeValue, 'some-value', null, SyncPromise::FULFILLED];
198+
yield [new \Exception('test-reason-4'), $onRejectedThrowsSameReason, null, 'test-reason-4', SyncPromise::REJECTED];
199+
yield [new \Exception('test-reason-5'), $onRejectedThrowsOtherReason, null, 'onRejected throws other!', SyncPromise::REJECTED];
204200
}
205201

206202
/** @dataProvider rejectedPromiseData */

tests/Executor/TestClasses/ComplexScalar.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace GraphQL\Tests\Executor\TestClasses;
44

55
use GraphQL\Error\Error;
6-
use GraphQL\Error\InvariantViolation;
76
use GraphQL\Error\SerializationError;
87
use GraphQL\Language\AST\Node;
98
use GraphQL\Language\Printer;
@@ -14,12 +13,6 @@ final class ComplexScalar extends ScalarType
1413
{
1514
public string $name = 'ComplexScalar';
1615

17-
/** @throws InvariantViolation */
18-
public static function create(): self
19-
{
20-
return new self();
21-
}
22-
2316
/** @throws SerializationError */
2417
public function serialize($value): string
2518
{

tests/Executor/VariablesTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ private function executeQuery(string $query, ?array $variableValues = null): Exe
136136
/** @throws InvariantViolation */
137137
public function schema(): Schema
138138
{
139-
$ComplexScalarType = ComplexScalar::create();
139+
$ComplexScalarType = new ComplexScalar();
140140

141141
$TestInputObject = new InputObjectType([
142142
'name' => 'TestInputObject',

tests/Language/LexerTest.php

Lines changed: 55 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function testDisallowsUncommonControlCharacters(): void
2121
$this->expectSyntaxError(
2222
Utils::chr(0x0007),
2323
'Cannot contain the invalid character "\u0007"',
24-
$this->loc(1, 1)
24+
self::loc(1, 1)
2525
);
2626
}
2727

@@ -53,7 +53,7 @@ private function lexOne(string $body): Token
5353
return $lexer->advance();
5454
}
5555

56-
private function loc(int $line, int $column): SourceLocation
56+
private static function loc(int $line, int $column): SourceLocation
5757
{
5858
return new SourceLocation($line, $column);
5959
}
@@ -397,47 +397,32 @@ public function testLexesBlockString(): void
397397
}
398398

399399
/** @return iterable<array{string, string, SourceLocation}> */
400-
public function reportsUsefulStringErrors(): iterable
400+
public static function reportsUsefulStringErrors(): iterable
401401
{
402-
return [
403-
['"', 'Unterminated string.', $this->loc(1, 2)],
404-
['"no end quote', 'Unterminated string.', $this->loc(1, 14)],
405-
[
406-
"'single quotes'",
407-
"Unexpected single quote character ('), did you mean to use a double quote (\")?",
408-
$this->loc(
409-
1,
410-
1
411-
),
412-
],
413-
[
414-
'"contains unescaped \u0007 control char"',
415-
'Invalid character within String: "\\u0007"',
416-
$this->loc(
417-
1,
418-
21
419-
),
420-
],
421-
['"null-byte is not \u0000 end of file"', 'Invalid character within String: "\\u0000"', $this->loc(1, 19)],
422-
['"multi' . "\n" . 'line"', 'Unterminated string.', $this->loc(1, 7)],
423-
['"multiline"', 'Unterminated string.', $this->loc(1, 7)],
424-
['"bad esc \\', 'Unterminated string.', $this->loc(1, 11)],
425-
['"bad \\z esc"', 'Invalid character escape sequence: \\z', $this->loc(1, 7)],
426-
['"bad \\x esc"', 'Invalid character escape sequence: \\x', $this->loc(1, 7)],
427-
['"bad \\u1 esc"', 'Invalid character escape sequence: \\u1 es', $this->loc(1, 7)],
428-
['"bad \\u0XX1 esc"', 'Invalid character escape sequence: \\u0XX1', $this->loc(1, 7)],
429-
['"bad \\uXXXX esc"', 'Invalid character escape sequence: \\uXXXX', $this->loc(1, 7)],
430-
['"bad \\uFXXX esc"', 'Invalid character escape sequence: \\uFXXX', $this->loc(1, 7)],
431-
['"bad \\uXXXF esc"', 'Invalid character escape sequence: \\uXXXF', $this->loc(1, 7)],
432-
['"bad \\uD835"', 'Invalid UTF-16 trailing surrogate: ', $this->loc(1, 13)],
433-
['"bad \\uD835\\u1"', 'Invalid UTF-16 trailing surrogate: \\u1', $this->loc(1, 13)],
434-
['"bad \\uD835\\u1 esc"', 'Invalid UTF-16 trailing surrogate: \\u1 es', $this->loc(1, 13)],
435-
['"bad \\uD835uuFFFF esc"', 'Invalid UTF-16 trailing surrogate: uuFFFF', $this->loc(1, 13)],
436-
['"bad \\uD835\\u0XX1 esc"', 'Invalid UTF-16 trailing surrogate: \\u0XX1', $this->loc(1, 13)],
437-
['"bad \\uD835\\uXXXX esc"', 'Invalid UTF-16 trailing surrogate: \\uXXXX', $this->loc(1, 13)],
438-
['"bad \\uD835\\uFXXX esc"', 'Invalid UTF-16 trailing surrogate: \\uFXXX', $this->loc(1, 13)],
439-
['"bad \\uD835\\uXXXF esc"', 'Invalid UTF-16 trailing surrogate: \\uXXXF', $this->loc(1, 13)],
440-
];
402+
yield ['"', 'Unterminated string.', self::loc(1, 2)];
403+
yield ['"no end quote', 'Unterminated string.', self::loc(1, 14)];
404+
yield ["'single quotes'", "Unexpected single quote character ('), did you mean to use a double quote (\")?", self::loc(1, 1)];
405+
yield ['"contains unescaped \u0007 control char"', 'Invalid character within String: "\\u0007"', self::loc(1, 21)];
406+
yield ['"null-byte is not \u0000 end of file"', 'Invalid character within String: "\\u0000"', self::loc(1, 19)];
407+
yield ['"multi' . "\n" . 'line"', 'Unterminated string.', self::loc(1, 7)];
408+
yield ['"multi
409+
line"', 'Unterminated string.', self::loc(1, 7)];
410+
yield ['"bad esc \\', 'Unterminated string.', self::loc(1, 11)];
411+
yield ['"bad \\z esc"', 'Invalid character escape sequence: \\z', self::loc(1, 7)];
412+
yield ['"bad \\x esc"', 'Invalid character escape sequence: \\x', self::loc(1, 7)];
413+
yield ['"bad \\u1 esc"', 'Invalid character escape sequence: \\u1 es', self::loc(1, 7)];
414+
yield ['"bad \\u0XX1 esc"', 'Invalid character escape sequence: \\u0XX1', self::loc(1, 7)];
415+
yield ['"bad \\uXXXX esc"', 'Invalid character escape sequence: \\uXXXX', self::loc(1, 7)];
416+
yield ['"bad \\uFXXX esc"', 'Invalid character escape sequence: \\uFXXX', self::loc(1, 7)];
417+
yield ['"bad \\uXXXF esc"', 'Invalid character escape sequence: \\uXXXF', self::loc(1, 7)];
418+
yield ['"bad \\uD835"', 'Invalid UTF-16 trailing surrogate: ', self::loc(1, 13)];
419+
yield ['"bad \\uD835\\u1"', 'Invalid UTF-16 trailing surrogate: \\u1', self::loc(1, 13)];
420+
yield ['"bad \\uD835\\u1 esc"', 'Invalid UTF-16 trailing surrogate: \\u1 es', self::loc(1, 13)];
421+
yield ['"bad \\uD835uuFFFF esc"', 'Invalid UTF-16 trailing surrogate: uuFFFF', self::loc(1, 13)];
422+
yield ['"bad \\uD835\\u0XX1 esc"', 'Invalid UTF-16 trailing surrogate: \\u0XX1', self::loc(1, 13)];
423+
yield ['"bad \\uD835\\uXXXX esc"', 'Invalid UTF-16 trailing surrogate: \\uXXXX', self::loc(1, 13)];
424+
yield ['"bad \\uD835\\uFXXX esc"', 'Invalid UTF-16 trailing surrogate: \\uFXXX', self::loc(1, 13)];
425+
yield ['"bad \\uD835\\uXXXF esc"', 'Invalid UTF-16 trailing surrogate: \\uXXXF', self::loc(1, 13)];
441426
}
442427

443428
/**
@@ -451,27 +436,19 @@ public function testLexReportsUsefulStringErrors(string $str, string $expectedMe
451436
}
452437

453438
/** @return iterable<array{string, string, SourceLocation}> */
454-
public function reportsUsefulBlockStringErrors(): iterable
439+
public static function reportsUsefulBlockStringErrors(): iterable
455440
{
456-
return [
457-
['"""', 'Unterminated string.', $this->loc(1, 4)],
458-
['"""no end quote', 'Unterminated string.', $this->loc(1, 16)],
459-
[
460-
'"""contains unescaped ' . \json_decode('"\u0007"') . ' control char"""',
461-
'Invalid character within String: "\\u0007"',
462-
$this->loc(
463-
1,
464-
23
465-
),
466-
],
467-
[
468-
'"""null-byte is not ' . \json_decode('"\u0000"') . ' end of file"""',
469-
'Invalid character within String: "\\u0000"',
470-
$this->loc(
471-
1,
472-
21
473-
),
474-
],
441+
yield ['"""', 'Unterminated string.', self::loc(1, 4)];
442+
yield ['"""no end quote', 'Unterminated string.', self::loc(1, 16)];
443+
yield [
444+
'"""contains unescaped ' . \json_decode('"\u0007"') . ' control char"""',
445+
'Invalid character within String: "\\u0007"',
446+
self::loc(1, 23),
447+
];
448+
yield [
449+
'"""null-byte is not ' . \json_decode('"\u0000"') . ' end of file"""',
450+
'Invalid character within String: "\\u0000"',
451+
self::loc(1, 21),
475452
];
476453
}
477454

@@ -555,19 +532,17 @@ public function testLexesNumbers(): void
555532
}
556533

557534
/** @return iterable<array{string, string, SourceLocation}> */
558-
public function reportsUsefulNumberErrors(): iterable
535+
public static function reportsUsefulNumberErrors(): iterable
559536
{
560-
return [
561-
['00', 'Invalid number, unexpected digit after 0: "0"', $this->loc(1, 2)],
562-
['+1', 'Cannot parse the unexpected character "+".', $this->loc(1, 1)],
563-
['1.', 'Invalid number, expected digit but got: <EOF>', $this->loc(1, 3)],
564-
['1.e1', 'Invalid number, expected digit but got: "e"', $this->loc(1, 3)],
565-
['.123', 'Cannot parse the unexpected character ".".', $this->loc(1, 1)],
566-
['1.A', 'Invalid number, expected digit but got: "A"', $this->loc(1, 3)],
567-
['-A', 'Invalid number, expected digit but got: "A"', $this->loc(1, 2)],
568-
['1.0e', 'Invalid number, expected digit but got: <EOF>', $this->loc(1, 5)],
569-
['1.0eA', 'Invalid number, expected digit but got: "A"', $this->loc(1, 5)],
570-
];
537+
yield ['00', 'Invalid number, unexpected digit after 0: "0"', self::loc(1, 2)];
538+
yield ['+1', 'Cannot parse the unexpected character "+".', self::loc(1, 1)];
539+
yield ['1.', 'Invalid number, expected digit but got: <EOF>', self::loc(1, 3)];
540+
yield ['1.e1', 'Invalid number, expected digit but got: "e"', self::loc(1, 3)];
541+
yield ['.123', 'Cannot parse the unexpected character ".".', self::loc(1, 1)];
542+
yield ['1.A', 'Invalid number, expected digit but got: "A"', self::loc(1, 3)];
543+
yield ['-A', 'Invalid number, expected digit but got: "A"', self::loc(1, 2)];
544+
yield ['1.0e', 'Invalid number, expected digit but got: <EOF>', self::loc(1, 5)];
545+
yield ['1.0eA', 'Invalid number, expected digit but got: "A"', self::loc(1, 5)];
571546
}
572547

573548
/**
@@ -638,14 +613,12 @@ public function testLexesPunctuation(): void
638613
}
639614

640615
/** @return iterable<array{string, string, SourceLocation}> */
641-
public function reportsUsefulUnknownCharErrors(): iterable
616+
public static function reportsUsefulUnknownCharErrors(): iterable
642617
{
643-
return [
644-
['..', 'Cannot parse the unexpected character ".".', $this->loc(1, 1)],
645-
['?', 'Cannot parse the unexpected character "?".', $this->loc(1, 1)],
646-
[\json_decode('"\u203B"'), 'Cannot parse the unexpected character "\\u203b".', $this->loc(1, 1)],
647-
[\json_decode('"\u200b"'), 'Cannot parse the unexpected character "\\u200b".', $this->loc(1, 1)],
648-
];
618+
yield ['..', 'Cannot parse the unexpected character ".".', self::loc(1, 1)];
619+
yield ['?', 'Cannot parse the unexpected character "?".', self::loc(1, 1)];
620+
yield [\json_decode('"\u203B"'), 'Cannot parse the unexpected character "\\u203b".', self::loc(1, 1)];
621+
yield [\json_decode('"\u200b"'), 'Cannot parse the unexpected character "\\u200b".', self::loc(1, 1)];
649622
}
650623

651624
/**
@@ -674,7 +647,7 @@ public function testReportsUsefulDashesInfo(): void
674647
$lexer->advance();
675648
self::fail('Expected exception not thrown');
676649
} catch (SyntaxError $error) {
677-
self::assertEquals([$this->loc(1, 3)], $error->getLocations());
650+
self::assertEquals([self::loc(1, 3)], $error->getLocations());
678651

679652
throw $error;
680653
}

tests/Language/ParserTest.php

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -24,44 +24,42 @@
2424

2525
final class ParserTest extends TestCaseBase
2626
{
27-
/** @return array<int, array{0: string, 1: string, 2: string, 3?: list<int>, 4?: list<SourceLocation>}> */
28-
public function parseProvidesUsefulErrors(): array
27+
/** @return iterable<array{0: string, 1: string, 2: string, 3?: list<int>, 4?: list<SourceLocation>}> */
28+
public static function parseProvidesUsefulErrors(): iterable
2929
{
30-
return [
30+
yield [
31+
'{',
32+
'Syntax Error: Expected Name, found <EOF>',
33+
"Syntax Error: Expected Name, found <EOF>\n\nGraphQL request (1:2)\n1: {\n ^\n",
34+
[1],
3135
[
32-
'{',
33-
'Syntax Error: Expected Name, found <EOF>',
34-
"Syntax Error: Expected Name, found <EOF>\n\nGraphQL request (1:2)\n1: {\n ^\n",
35-
[1],
36-
[
37-
new SourceLocation(
38-
1,
39-
2
40-
),
41-
],
36+
new SourceLocation(
37+
1,
38+
2
39+
),
4240
],
43-
[
44-
'{ ...MissingOn }
41+
];
42+
yield [
43+
'{ ...MissingOn }
4544
fragment MissingOn Type
4645
',
47-
'Syntax Error: Expected "on", found Name "Type"',
48-
"Syntax Error: Expected \"on\", found Name \"Type\"\n\nGraphQL request (2:20)\n1: { ...MissingOn }\n2: fragment MissingOn Type\n ^\n3: \n",
49-
],
50-
[
51-
'{ field: {} }',
52-
'Syntax Error: Expected Name, found {',
53-
"Syntax Error: Expected Name, found {\n\nGraphQL request (1:10)\n1: { field: {} }\n ^\n",
54-
],
55-
[
56-
'notanoperation Foo { field }',
57-
'Syntax Error: Unexpected Name "notanoperation"',
58-
"Syntax Error: Unexpected Name \"notanoperation\"\n\nGraphQL request (1:1)\n1: notanoperation Foo { field }\n ^\n",
59-
],
60-
[
61-
'...',
62-
'Syntax Error: Unexpected ...',
63-
"Syntax Error: Unexpected ...\n\nGraphQL request (1:1)\n1: ...\n ^\n",
64-
],
46+
'Syntax Error: Expected "on", found Name "Type"',
47+
"Syntax Error: Expected \"on\", found Name \"Type\"\n\nGraphQL request (2:20)\n1: { ...MissingOn }\n2: fragment MissingOn Type\n ^\n3: \n",
48+
];
49+
yield [
50+
'{ field: {} }',
51+
'Syntax Error: Expected Name, found {',
52+
"Syntax Error: Expected Name, found {\n\nGraphQL request (1:10)\n1: { field: {} }\n ^\n",
53+
];
54+
yield [
55+
'notanoperation Foo { field }',
56+
'Syntax Error: Unexpected Name "notanoperation"',
57+
"Syntax Error: Unexpected Name \"notanoperation\"\n\nGraphQL request (1:1)\n1: notanoperation Foo { field }\n ^\n",
58+
];
59+
yield [
60+
'...',
61+
'Syntax Error: Unexpected ...',
62+
"Syntax Error: Unexpected ...\n\nGraphQL request (1:1)\n1: ...\n ^\n",
6563
];
6664
}
6765

0 commit comments

Comments
 (0)