-
-
Notifications
You must be signed in to change notification settings - Fork 576
Expand file tree
/
Copy pathCoerceInputValueTest.php
More file actions
517 lines (455 loc) · 17.1 KB
/
CoerceInputValueTest.php
File metadata and controls
517 lines (455 loc) · 17.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
<?php declare(strict_types=1);
namespace GraphQL\Tests\Utils;
use GraphQL\Error\ClientAware;
use GraphQL\Error\CoercionError;
use GraphQL\Error\InvariantViolation;
use GraphQL\Type\Definition\CustomScalarType;
use GraphQL\Type\Definition\EnumType;
use GraphQL\Type\Definition\InputObjectType;
use GraphQL\Type\Definition\ListOfType;
use GraphQL\Type\Definition\NonNull;
use GraphQL\Type\Definition\ScalarType;
use GraphQL\Type\Definition\Type;
use GraphQL\Utils\Value;
use PHPUnit\Framework\TestCase;
/**
* @see coerceInputValue-test.ts
*
* @phpstan-import-type InputPath from CoercionError
*/
final class CoerceInputValueTest extends TestCase
{
private NonNull $testNonNull;
private CustomScalarType $testScalar;
private EnumType $testEnum;
private InputObjectType $testInputObject;
/** @var ListOfType<ScalarType> */
private ListOfType $testList;
/** @var ListOfType<ListOfType<ScalarType>> */
private ListOfType $testNestedList;
public function setUp(): void
{
$this->testNonNull = Type::nonNull(Type::int());
$this->testScalar = new CustomScalarType([
'name' => 'TestScalar',
'parseValue' => function ($input) {
if (isset($input['error'])) {
throw new \Exception($input['error']);
}
return $input['value'];
},
'parseLiteral' => fn () => null,
]);
$this->testEnum = new EnumType([
'name' => 'TestEnum',
'values' => [
'FOO' => 'InternalFoo',
'BAR' => 123456789,
],
]);
$nestedObject = new InputObjectType([
'name' => 'TestInputObject',
'fields' => [
'foobar' => Type::nonNull(Type::int()),
],
]);
$this->testInputObject = new InputObjectType([
'name' => 'TestInputObject',
'fields' => [
'foo' => Type::nonNull(Type::int()),
'bar' => Type::int(),
'nested' => $nestedObject,
],
]);
$this->testList = new ListOfType(Type::int());
$this->testNestedList = new ListOfType($this->testList);
}
/**
* @see describe('coerceInputValue', () => {
* @see describe('for GraphQLNonNull', () => {
* @see it('returns no error for non-null value', () => {
*/
public function testReturnsNoErrorForNonNullValue(): void
{
$result = Value::coerceInputValue(1, $this->testNonNull);
$this->expectGraphQLValue($result, 1);
}
/**
* @see it('returns an error for undefined value', () => {
* @see it('returns an error for null value', () => {
*/
public function testReturnsNoErrorForNullValue(): void
{
$result = Value::coerceInputValue(null, $this->testNonNull);
$this->expectGraphQLError($result, [CoercionError::make('Expected non-nullable type "Int!" not to be null.', null, null)]);
}
/**
* @see describe('for GraphQLScalar', () => {
* @see it('returns no error for valid input', () => {
*/
public function testReturnsNoErrorForValidInput(): void
{
$result = Value::coerceInputValue(['value' => 1], $this->testScalar);
$this->expectGraphQLValue($result, 1);
}
/**
* @see it('returns no error for null result', () => {
* @see it('returns no error for NaN result', () => {
* @see it('returns an error for undefined result', () => {
*/
public function testReturnsNoErrorForNullResult(): void
{
$result = Value::coerceInputValue(['value' => null], $this->testScalar);
$this->expectGraphQLValue($result, null);
}
/** @see it('it('returns a thrown error', () => {', () => { */
public function testReturnsAThrownError(): void
{
$result = Value::coerceInputValue(['error' => 'Some error message'], $this->testScalar);
$this->expectGraphQLError($result, [CoercionError::make(
'Expected type "TestScalar".',
null,
['error' => 'Some error message'],
new \Exception('Some error message'),
)]);
}
/**
* @see describe('for GraphQLEnum', () => {
* @see it('returns no error for a known enum name', () => {
*/
public function testReturnsNoErrorForAKnownEnumName(): void
{
$fooResult = Value::coerceInputValue('FOO', $this->testEnum);
$this->expectGraphQLValue($fooResult, 'InternalFoo');
$barResult = Value::coerceInputValue('BAR', $this->testEnum);
$this->expectGraphQLValue($barResult, 123456789);
}
/** @see it('returns an error for misspelled enum value', () => { */
public function testReturnsAnErrorForMisspelledEnumValue(): void
{
$result = Value::coerceInputValue('foo', $this->testEnum);
$this->expectGraphQLError($result, [CoercionError::make(
'Value "foo" does not exist in "TestEnum" enum. Did you mean the enum value "FOO"?',
null,
'foo',
)]);
}
/** @see it('returns an error for incorrect value type', () => { */
public function testReturnsErrorForIncorrectValueType(): void
{
$result1 = Value::coerceInputValue(123, $this->testEnum);
$this->expectGraphQLError($result1, [CoercionError::make(
'Enum "TestEnum" cannot represent non-string value: 123.',
null,
123
)]);
$result2 = Value::coerceInputValue(['field' => 'value'], $this->testEnum);
$this->expectGraphQLError($result2, [CoercionError::make(
// Differs from graphql-js due to JSON serialization
'Enum "TestEnum" cannot represent non-string value: {"field":"value"}.',
null,
['field' => 'value'],
)]);
}
public function testPropagatesClientSafeError(): void
{
$message = 'message';
$value = ['value' => 1];
$clientSafeException = new class($message) extends \Exception implements ClientAware {
public function isClientSafe(): bool
{
return true;
}
};
$scalar = new CustomScalarType([
'name' => 'TestScalar',
'parseValue' => function () use ($clientSafeException) {
throw $clientSafeException;
},
'parseLiteral' => fn () => null,
]);
$result = Value::coerceInputValue($value, $scalar);
$this->expectGraphQLError($result, [CoercionError::make(
$message,
null,
$value,
)]);
}
/**
* @see describe('for GraphQLInputObject', () => {
* @see it('returns no error for a valid input')
*
* @param mixed $input
*
* @dataProvider validInputObjects
*/
public function testReturnsNoErrorForAValidInput($input): void
{
$result = Value::coerceInputValue($input, $this->testInputObject);
$this->expectGraphQLValue($result, ['foo' => 123]);
}
/** @return iterable<array{mixed}> */
public static function validInputObjects(): iterable
{
yield [['foo' => 123]];
yield [(object) ['foo' => 123]];
}
/** @see it('returns an error for a non-object type', () => { */
public function testReturnsAnErrorForANonObjectType(): void
{
$result = Value::coerceInputValue(123, $this->testInputObject);
$this->expectGraphQLError($result, [CoercionError::make(
'Expected type "TestInputObject" to be an object.',
null,
123
)]);
}
/** @see it('returns an error for an invalid field', () => { */
public function testReturnsAnErrorForAnInvalidField(): void
{
$notInt = new \stdClass();
$result = Value::coerceInputValue(['foo' => $notInt], $this->testInputObject);
$this->expectGraphQLError($result, [CoercionError::make(
'Int cannot represent non-integer value: {}',
['foo'],
$notInt
)]);
}
/** @see it('returns multiple errors for multiple invalid fields', () => { */
public function testReturnsMultipleErrorsForMultipleInvalidFields(): void
{
$result = Value::coerceInputValue(['foo' => 'abc', 'bar' => 'def'], $this->testInputObject);
$this->expectGraphQLError($result, [
CoercionError::make(
'Int cannot represent non-integer value: "abc"',
['foo'],
'abc'
),
CoercionError::make(
'Int cannot represent non-integer value: "def"',
['bar'],
'def'
),
]);
}
/** @see it('returns error for a missing required field', () => { */
public function testReturnsErrorForAMissingRequiredField(): void
{
$result = Value::coerceInputValue(['bar' => 123], $this->testInputObject);
$this->expectGraphQLError($result, [CoercionError::make(
'Field "foo" of required type "Int!" was not provided.',
null,
['bar' => 123]
)]);
}
/** @see it('returns error for an unknown field', () => { */
public function testReturnsErrorForAnUnknownField(): void
{
$result = Value::coerceInputValue(['foo' => 123, 'unknownField' => 123], $this->testInputObject);
$this->expectGraphQLError($result, [CoercionError::make(
'Field "unknownField" is not defined by type "TestInputObject".',
null,
['foo' => 123, 'unknownField' => 123],
)]);
}
/** @see it('returns error for a misspelled field', () => { */
public function testReturnsErrorForAMisspelledField(): void
{
$result = Value::coerceInputValue(['foo' => 123, 'bart' => 123], $this->testInputObject);
$this->expectGraphQLError($result, [CoercionError::make(
'Field "bart" is not defined by type "TestInputObject". Did you mean "bar"?',
null,
['foo' => 123, 'bart' => 123],
)]);
}
/**
* @param mixed $defaultValue Anything goes
*
* @throws InvariantViolation
*/
private function makeTestInputObject($defaultValue): InputObjectType
{
return new InputObjectType([
'name' => 'TestInputObject',
'fields' => [
'foo' => [
'type' => new CustomScalarType([
'name' => 'TestScalar',
'parseValue' => static fn ($value) => $value,
'parseLiteral' => static fn () => null,
]),
'defaultValue' => $defaultValue,
],
],
]);
}
/**
* @see describe('for GraphQLInputObject with default value', () => {
* @see it('returns no errors for valid input value', () => {
*/
public function testReturnsNoErrorsForValidInputValue(): void
{
$result = Value::coerceInputValue(['foo' => 5], $this->makeTestInputObject(7));
$this->expectGraphQLValue($result, ['foo' => 5]);
}
/** @see it('returns object with default value', () => { */
public function testReturnsObjectWithDefaultValue(): void
{
$result = Value::coerceInputValue([], $this->makeTestInputObject(7));
$this->expectGraphQLValue($result, ['foo' => 7]);
}
/**
* @see it('returns null as value', () => {
* @see it('returns NaN as value', () => {
*/
public function testReturnsNullAsValue(): void
{
$result = Value::coerceInputValue([], $this->makeTestInputObject(null));
$this->expectGraphQLValue($result, ['foo' => null]);
}
/**
* @see describe('for GraphQLList', () => {
* @see it('returns no error for a valid input', () => {
*/
public function testGraphQLListReturnsNoErrorForAValidInput(): void
{
$result = Value::coerceInputValue([1, 2, 3], $this->testList);
$this->expectGraphQLValue($result, [1, 2, 3]);
}
/** @see it('returns no error for a valid iterable input', () => { */
public function testReturnsNoErrorForAValidIterableInput(): void
{
$listGenerator = function (): iterable {
yield 1;
yield 2;
yield 3;
};
$result = Value::coerceInputValue($listGenerator(), $this->testList);
$this->expectGraphQLValue($result, [1, 2, 3]);
}
/** @see it('returns an error for an invalid input', () => { */
public function testReturnsAnErrorForAnInvalidInput(): void
{
$result = Value::coerceInputValue([1, 'b', true, 4], $this->testList);
$this->expectGraphQLError($result, [
CoercionError::make(
'Int cannot represent non-integer value: "b"',
[1],
'b',
),
CoercionError::make(
'Int cannot represent non-integer value: true',
[2],
true,
),
]);
}
/** @see it('returns a list for a non-list value', () => { */
public function testReturnsAListForANonListValue(): void
{
$result = Value::coerceInputValue(42, $this->testList);
$this->expectGraphQLValue($result, [42]);
}
/** @see it('returns a list for a non-list object value', () => { */
public function testReturnsAListForANonListObjectValue(): void
{
$TestListOfObjects = new ListOfType(
new InputObjectType([
'name' => 'TestObject',
'fields' => [
'length' => [
'type' => Type::int(),
],
],
])
);
$result = Value::coerceInputValue((object) ['length' => 100500], $TestListOfObjects);
$this->expectGraphQLValue($result, [['length' => 100500]]);
}
/** @see it('returns an error for a non-list invalid value', () => { */
public function testReturnsAnErrorForANonListInvalidValue(): void
{
$result = Value::coerceInputValue('INVALID', $this->testList);
$this->expectGraphQLError($result, [
CoercionError::make(
'Int cannot represent non-integer value: "INVALID"',
null,
'INVALID',
),
]);
}
/** @see it('returns null for a null value', () => { */
public function testReturnsNullForANullValue(): void
{
$result = Value::coerceInputValue(null, $this->testList);
$this->expectGraphQLValue($result, null);
}
/**
* @see describe('for nested GraphQLList', () => {
* @see it('returns no error for a valid input', () => {
*/
public function testNestedGraphQLListReturnsNoErrorForAValidInput(): void
{
$result = Value::coerceInputValue([[1], [2, 3]], $this->testNestedList);
$this->expectGraphQLValue($result, [[1], [2, 3]]);
}
/** @see it('returns a list for a non-list value', () => { */
public function testNestedGraphQLListReturnsAListForANonListValue(): void
{
$result = Value::coerceInputValue(42, $this->testNestedList);
$this->expectGraphQLValue($result, [[42]]);
}
/** @see it('returns null for a null value', () => { */
public function testNestedGraphQLListReturnsNullForANullValue(): void
{
$result = Value::coerceInputValue(null, $this->testNestedList);
$this->expectGraphQLValue($result, null);
}
/** @see it('returns nested lists for nested non-list values', () => { */
public function testReturnsNestedListsForNestedNonListValue(): void
{
$result = Value::coerceInputValue([1, 2, 3], $this->testNestedList);
$this->expectGraphQLValue($result, [[1], [2], [3]]);
}
/** @see it('returns nested null for nested null values', () => { */
public function testReturnsNestedNullForNestedNullValues(): void
{
$result = Value::coerceInputValue([42, [null], null], $this->testNestedList);
$this->expectGraphQLValue($result, [[42], [null], null]);
}
/*
* @see describe('with default onError', () => {
* @see it('throw error without path', () => {
* @see it('throw error with path', () => {
*
* Unnecessary because we do not implement the callback variant coerceInputValue.
*/
/**
* @param mixed $result returned result
* @param list<CoercionError> $coercionErrors
*/
private function expectGraphQLError($result, array $coercionErrors): void
{
self::assertIsArray($result);
$errors = $result['errors'];
self::assertIsArray($errors);
self::assertCount(count($coercionErrors), $errors);
foreach ($errors as $i => $error) {
$expected = $coercionErrors[$i];
self::assertSame($expected->getMessage(), $error->getMessage());
self::assertSame($expected->inputPath, $error->inputPath);
self::assertSame($expected->invalidValue, $error->invalidValue);
}
self::assertNull($result['value']);
}
/**
* @param mixed $result
* @param mixed $expected
*/
private function expectGraphQLValue($result, $expected): void
{
self::assertIsArray($result);
self::assertNull($result['errors']);
self::assertSame($expected, $result['value']);
}
}