Failed Conditions
Pull Request — master (#565)
by Šimon
10:25
created

testNullsANullableFieldThatThrowsInAPromise()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 23
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GraphQL\Tests\Executor;
6
7
use Exception;
8
use GraphQL\Deferred;
9
use GraphQL\Error\FormattedError;
10
use GraphQL\Error\UserError;
11
use GraphQL\Executor\Executor;
12
use GraphQL\Language\Parser;
13
use GraphQL\Language\SourceLocation;
14
use GraphQL\Type\Definition\ObjectType;
15
use GraphQL\Type\Definition\Type;
16
use GraphQL\Type\Schema;
17
use PHPUnit\Framework\ExpectationFailedException;
18
use PHPUnit\Framework\TestCase;
19
use function count;
20
use function is_string;
21
use function json_encode;
22
23
class NonNullTest extends TestCase
24
{
25
    /** @var Exception */
26
    public $syncError;
27
28
    /** @var Exception */
29
    public $syncNonNullError;
30
31
    /** @var  Exception */
32
    public $promiseError;
33
34
    /** @var  Exception */
35
    public $promiseNonNullError;
36
37
    /** @var callable[] */
38
    public $throwingData;
39
40
    /** @var callable[] */
41
    public $nullingData;
42
43
    /** @var Schema */
44
    public $schema;
45
46
    /** @var Schema */
47
    public $schemaWithNonNullArg;
48
49
    public function setUp()
50
    {
51
        $this->syncError           = new UserError('sync');
52
        $this->syncNonNullError    = new UserError('syncNonNull');
53
        $this->promiseError        = new UserError('promise');
54
        $this->promiseNonNullError = new UserError('promiseNonNull');
55
56
        $this->throwingData = [
57
            'sync'               => function () {
58
                throw $this->syncError;
59
            },
60
            'syncNonNull'        => function () {
61
                throw $this->syncNonNullError;
62
            },
63
            'promise'            => function () {
64
                return new Deferred(function () {
65
                    throw $this->promiseError;
66
                });
67
            },
68
            'promiseNonNull'     => function () {
69
                return new Deferred(function () {
70
                    throw $this->promiseNonNullError;
71
                });
72
            },
73
            'syncNest'           => function () {
74
                return $this->throwingData;
75
            },
76
            'syncNonNullNest'    => function () {
77
                return $this->throwingData;
78
            },
79
            'promiseNest'        => function () {
80
                return new Deferred(function () {
81
                    return $this->throwingData;
82
                });
83
            },
84
            'promiseNonNullNest' => function () {
85
                return new Deferred(function () {
86
                    return $this->throwingData;
87
                });
88
            },
89
        ];
90
91
        $this->nullingData = [
92
            'sync'               => static function () {
93
                return null;
94
            },
95
            'syncNonNull'        => static function () {
96
                return null;
97
            },
98
            'promise'            => static function () {
99
                return new Deferred(static function () {
100
                    return null;
101
                });
102
            },
103
            'promiseNonNull'     => static function () {
104
                return new Deferred(static function () {
105
                    return null;
106
                });
107
            },
108
            'syncNest'           => function () {
109
                return $this->nullingData;
110
            },
111
            'syncNonNullNest'    => function () {
112
                return $this->nullingData;
113
            },
114
            'promiseNest'        => function () {
115
                return new Deferred(function () {
116
                    return $this->nullingData;
117
                });
118
            },
119
            'promiseNonNullNest' => function () {
120
                return new Deferred(function () {
121
                    return $this->nullingData;
122
                });
123
            },
124
        ];
125
126
        $dataType = new ObjectType([
127
            'name'   => 'DataType',
128
            'fields' => static function () use (&$dataType) {
129
                return [
130
                    'sync'               => ['type' => Type::string()],
131
                    'syncNonNull'        => ['type' => Type::nonNull(Type::string())],
132
                    'promise'            => Type::string(),
133
                    'promiseNonNull'     => Type::nonNull(Type::string()),
134
                    'syncNest'           => $dataType,
135
                    'syncNonNullNest'    => Type::nonNull($dataType),
136
                    'promiseNest'        => $dataType,
137
                    'promiseNonNullNest' => Type::nonNull($dataType),
138
                ];
139
            },
140
        ]);
141
142
        $this->schema = new Schema(['query' => $dataType]);
143
144
        $this->schemaWithNonNullArg = new Schema([
145
            'query' => new ObjectType([
146
                'name' => 'Query',
147
                'fields' => [
148
                    'withNonNullArg' => [
149
                        'type' => Type::string(),
150
                        'args' => [
151
                            'cannotBeNull' => [
152
                                'type' => Type::nonNull(Type::string()),
153
                            ],
154
                        ],
155
                        'resolve' => static function ($value, $args) {
156
                            if (is_string($args['cannotBeNull'])) {
157
                                return 'Passed: ' . $args['cannotBeNull'];
158
                            }
159
                        },
160
                    ],
161
                ],
162
            ]),
163
        ]);
164
    }
165
166
    // Execute: handles non-nullable types
167
168
    /**
169
     * @see it('nulls a nullable field that throws synchronously')
170
     */
171
    public function testNullsANullableFieldThatThrowsSynchronously() : void
172
    {
173
        $doc = '
174
      query Q {
175
        sync
176
      }
177
        ';
178
179
        $ast = Parser::parse($doc);
180
181
        $expected = [
182
            'data'   => ['sync' => null],
183
            'errors' => [
184
                FormattedError::create(
0 ignored issues
show
Deprecated Code introduced by
The function GraphQL\Error\FormattedError::create() has been deprecated: as of v0.8.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

184
                /** @scrutinizer ignore-deprecated */ FormattedError::create(

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
185
                    $this->syncError->getMessage(),
186
                    [new SourceLocation(3, 9)]
187
                ),
188
            ],
189
        ];
190
        self::assertArraySubset(
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit\Framework\Assert::assertArraySubset() has been deprecated: https://github.com/sebastianbergmann/phpunit/issues/3494 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

190
        /** @scrutinizer ignore-deprecated */ self::assertArraySubset(

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
191
            $expected,
192
            Executor::execute($this->schema, $ast, $this->throwingData, null, [], 'Q')->toArray()
0 ignored issues
show
Bug introduced by
The method toArray() does not exist on GraphQL\Executor\Promise\Promise. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

192
            Executor::execute($this->schema, $ast, $this->throwingData, null, [], 'Q')->/** @scrutinizer ignore-call */ toArray()

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
193
        );
194
    }
195
196
    public function testNullsANullableFieldThatThrowsInAPromise() : void
197
    {
198
        $doc = '
199
      query Q {
200
        promise
201
      }
202
        ';
203
204
        $ast = Parser::parse($doc);
205
206
        $expected = [
207
            'data'   => ['promise' => null],
208
            'errors' => [
209
                FormattedError::create(
0 ignored issues
show
Deprecated Code introduced by
The function GraphQL\Error\FormattedError::create() has been deprecated: as of v0.8.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

209
                /** @scrutinizer ignore-deprecated */ FormattedError::create(

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
210
                    $this->promiseError->getMessage(),
211
                    [new SourceLocation(3, 9)]
212
                ),
213
            ],
214
        ];
215
216
        self::assertArraySubset(
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit\Framework\Assert::assertArraySubset() has been deprecated: https://github.com/sebastianbergmann/phpunit/issues/3494 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

216
        /** @scrutinizer ignore-deprecated */ self::assertArraySubset(

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
217
            $expected,
218
            Executor::execute($this->schema, $ast, $this->throwingData, null, [], 'Q')->toArray()
219
        );
220
    }
221
222
    public function testNullsASynchronouslyReturnedObjectThatContainsANonNullableFieldThatThrowsSynchronously() : void
223
    {
224
        // nulls a synchronously returned object that contains a non-nullable field that throws synchronously
225
        $doc = '
226
      query Q {
227
        syncNest {
228
          syncNonNull,
229
        }
230
      }
231
    ';
232
233
        $ast = Parser::parse($doc);
234
235
        $expected = [
236
            'data'   => ['syncNest' => null],
237
            'errors' => [
238
                FormattedError::create($this->syncNonNullError->getMessage(), [new SourceLocation(4, 11)]),
0 ignored issues
show
Deprecated Code introduced by
The function GraphQL\Error\FormattedError::create() has been deprecated: as of v0.8.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

238
                /** @scrutinizer ignore-deprecated */ FormattedError::create($this->syncNonNullError->getMessage(), [new SourceLocation(4, 11)]),

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
239
            ],
240
        ];
241
        self::assertArraySubset(
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit\Framework\Assert::assertArraySubset() has been deprecated: https://github.com/sebastianbergmann/phpunit/issues/3494 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

241
        /** @scrutinizer ignore-deprecated */ self::assertArraySubset(

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
242
            $expected,
243
            Executor::execute($this->schema, $ast, $this->throwingData, null, [], 'Q')->toArray()
244
        );
245
    }
246
247
    public function testNullsAsynchronouslyReturnedObjectThatContainsANonNullableFieldThatThrowsInAPromise() : void
248
    {
249
        $doc = '
250
      query Q {
251
        syncNest {
252
          promiseNonNull,
253
        }
254
      }
255
    ';
256
257
        $ast = Parser::parse($doc);
258
259
        $expected = [
260
            'data'   => ['syncNest' => null],
261
            'errors' => [
262
                FormattedError::create($this->promiseNonNullError->getMessage(), [new SourceLocation(4, 11)]),
0 ignored issues
show
Deprecated Code introduced by
The function GraphQL\Error\FormattedError::create() has been deprecated: as of v0.8.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

262
                /** @scrutinizer ignore-deprecated */ FormattedError::create($this->promiseNonNullError->getMessage(), [new SourceLocation(4, 11)]),

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
263
            ],
264
        ];
265
266
        self::assertArraySubset(
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit\Framework\Assert::assertArraySubset() has been deprecated: https://github.com/sebastianbergmann/phpunit/issues/3494 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

266
        /** @scrutinizer ignore-deprecated */ self::assertArraySubset(

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
267
            $expected,
268
            Executor::execute($this->schema, $ast, $this->throwingData, null, [], 'Q')->toArray()
269
        );
270
    }
271
272
    public function testNullsAnObjectReturnedInAPromiseThatContainsANonNullableFieldThatThrowsSynchronously() : void
273
    {
274
        $doc = '
275
      query Q {
276
        promiseNest {
277
          syncNonNull,
278
        }
279
      }
280
    ';
281
282
        $ast = Parser::parse($doc);
283
284
        $expected = [
285
            'data'   => ['promiseNest' => null],
286
            'errors' => [
287
                FormattedError::create($this->syncNonNullError->getMessage(), [new SourceLocation(4, 11)]),
0 ignored issues
show
Deprecated Code introduced by
The function GraphQL\Error\FormattedError::create() has been deprecated: as of v0.8.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

287
                /** @scrutinizer ignore-deprecated */ FormattedError::create($this->syncNonNullError->getMessage(), [new SourceLocation(4, 11)]),

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
288
            ],
289
        ];
290
291
        self::assertArraySubset(
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit\Framework\Assert::assertArraySubset() has been deprecated: https://github.com/sebastianbergmann/phpunit/issues/3494 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

291
        /** @scrutinizer ignore-deprecated */ self::assertArraySubset(

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
292
            $expected,
293
            Executor::execute($this->schema, $ast, $this->throwingData, null, [], 'Q')->toArray()
294
        );
295
    }
296
297
    public function testNullsAnObjectReturnedInAPromiseThatContainsANonNullableFieldThatThrowsInAPromise() : void
298
    {
299
        $doc = '
300
      query Q {
301
        promiseNest {
302
          promiseNonNull,
303
        }
304
      }
305
    ';
306
307
        $ast = Parser::parse($doc);
308
309
        $expected = [
310
            'data'   => ['promiseNest' => null],
311
            'errors' => [
312
                FormattedError::create($this->promiseNonNullError->getMessage(), [new SourceLocation(4, 11)]),
0 ignored issues
show
Deprecated Code introduced by
The function GraphQL\Error\FormattedError::create() has been deprecated: as of v0.8.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

312
                /** @scrutinizer ignore-deprecated */ FormattedError::create($this->promiseNonNullError->getMessage(), [new SourceLocation(4, 11)]),

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
313
            ],
314
        ];
315
316
        self::assertArraySubset(
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit\Framework\Assert::assertArraySubset() has been deprecated: https://github.com/sebastianbergmann/phpunit/issues/3494 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

316
        /** @scrutinizer ignore-deprecated */ self::assertArraySubset(

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
317
            $expected,
318
            Executor::execute($this->schema, $ast, $this->throwingData, null, [], 'Q')->toArray()
319
        );
320
    }
321
322
    /**
323
     * @see it('nulls a complex tree of nullable fields that throw')
324
     */
325
    public function testNullsAComplexTreeOfNullableFieldsThatThrow() : void
326
    {
327
        $doc = '
328
      query Q {
329
        syncNest {
330
          sync
331
          promise
332
          syncNest {
333
            sync
334
            promise
335
          }
336
          promiseNest {
337
            sync
338
            promise
339
          }
340
        }
341
        promiseNest {
342
          sync
343
          promise
344
          syncNest {
345
            sync
346
            promise
347
          }
348
          promiseNest {
349
            sync
350
            promise
351
          }
352
        }
353
      }
354
    ';
355
356
        $ast = Parser::parse($doc);
357
358
        $expected = [
359
            'data'   => [
360
                'syncNest'    => [
361
                    'sync'        => null,
362
                    'promise'     => null,
363
                    'syncNest'    => [
364
                        'sync'    => null,
365
                        'promise' => null,
366
                    ],
367
                    'promiseNest' => [
368
                        'sync'    => null,
369
                        'promise' => null,
370
                    ],
371
                ],
372
                'promiseNest' => [
373
                    'sync'        => null,
374
                    'promise'     => null,
375
                    'syncNest'    => [
376
                        'sync'    => null,
377
                        'promise' => null,
378
                    ],
379
                    'promiseNest' => [
380
                        'sync'    => null,
381
                        'promise' => null,
382
                    ],
383
                ],
384
            ],
385
            'errors' => [
386
                FormattedError::create($this->syncError->getMessage(), [new SourceLocation(4, 11)]),
0 ignored issues
show
Deprecated Code introduced by
The function GraphQL\Error\FormattedError::create() has been deprecated: as of v0.8.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

386
                /** @scrutinizer ignore-deprecated */ FormattedError::create($this->syncError->getMessage(), [new SourceLocation(4, 11)]),

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
387
                FormattedError::create($this->syncError->getMessage(), [new SourceLocation(7, 13)]),
0 ignored issues
show
Deprecated Code introduced by
The function GraphQL\Error\FormattedError::create() has been deprecated: as of v0.8.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

387
                /** @scrutinizer ignore-deprecated */ FormattedError::create($this->syncError->getMessage(), [new SourceLocation(7, 13)]),

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
388
                FormattedError::create($this->syncError->getMessage(), [new SourceLocation(11, 13)]),
0 ignored issues
show
Deprecated Code introduced by
The function GraphQL\Error\FormattedError::create() has been deprecated: as of v0.8.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

388
                /** @scrutinizer ignore-deprecated */ FormattedError::create($this->syncError->getMessage(), [new SourceLocation(11, 13)]),

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
389
                FormattedError::create($this->syncError->getMessage(), [new SourceLocation(16, 11)]),
0 ignored issues
show
Deprecated Code introduced by
The function GraphQL\Error\FormattedError::create() has been deprecated: as of v0.8.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

389
                /** @scrutinizer ignore-deprecated */ FormattedError::create($this->syncError->getMessage(), [new SourceLocation(16, 11)]),

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
390
                FormattedError::create($this->syncError->getMessage(), [new SourceLocation(19, 13)]),
0 ignored issues
show
Deprecated Code introduced by
The function GraphQL\Error\FormattedError::create() has been deprecated: as of v0.8.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

390
                /** @scrutinizer ignore-deprecated */ FormattedError::create($this->syncError->getMessage(), [new SourceLocation(19, 13)]),

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
391
                FormattedError::create($this->promiseError->getMessage(), [new SourceLocation(5, 11)]),
0 ignored issues
show
Deprecated Code introduced by
The function GraphQL\Error\FormattedError::create() has been deprecated: as of v0.8.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

391
                /** @scrutinizer ignore-deprecated */ FormattedError::create($this->promiseError->getMessage(), [new SourceLocation(5, 11)]),

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
392
                FormattedError::create($this->promiseError->getMessage(), [new SourceLocation(8, 13)]),
0 ignored issues
show
Deprecated Code introduced by
The function GraphQL\Error\FormattedError::create() has been deprecated: as of v0.8.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

392
                /** @scrutinizer ignore-deprecated */ FormattedError::create($this->promiseError->getMessage(), [new SourceLocation(8, 13)]),

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
393
                FormattedError::create($this->syncError->getMessage(), [new SourceLocation(23, 13)]),
0 ignored issues
show
Deprecated Code introduced by
The function GraphQL\Error\FormattedError::create() has been deprecated: as of v0.8.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

393
                /** @scrutinizer ignore-deprecated */ FormattedError::create($this->syncError->getMessage(), [new SourceLocation(23, 13)]),

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
394
                FormattedError::create($this->promiseError->getMessage(), [new SourceLocation(12, 13)]),
0 ignored issues
show
Deprecated Code introduced by
The function GraphQL\Error\FormattedError::create() has been deprecated: as of v0.8.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

394
                /** @scrutinizer ignore-deprecated */ FormattedError::create($this->promiseError->getMessage(), [new SourceLocation(12, 13)]),

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
395
                FormattedError::create($this->promiseError->getMessage(), [new SourceLocation(17, 11)]),
0 ignored issues
show
Deprecated Code introduced by
The function GraphQL\Error\FormattedError::create() has been deprecated: as of v0.8.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

395
                /** @scrutinizer ignore-deprecated */ FormattedError::create($this->promiseError->getMessage(), [new SourceLocation(17, 11)]),

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
396
                FormattedError::create($this->promiseError->getMessage(), [new SourceLocation(20, 13)]),
0 ignored issues
show
Deprecated Code introduced by
The function GraphQL\Error\FormattedError::create() has been deprecated: as of v0.8.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

396
                /** @scrutinizer ignore-deprecated */ FormattedError::create($this->promiseError->getMessage(), [new SourceLocation(20, 13)]),

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
397
                FormattedError::create($this->promiseError->getMessage(), [new SourceLocation(24, 13)]),
0 ignored issues
show
Deprecated Code introduced by
The function GraphQL\Error\FormattedError::create() has been deprecated: as of v0.8.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

397
                /** @scrutinizer ignore-deprecated */ FormattedError::create($this->promiseError->getMessage(), [new SourceLocation(24, 13)]),

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
398
            ],
399
        ];
400
401
        $result = Executor::execute($this->schema, $ast, $this->throwingData, null, [], 'Q')->toArray();
402
403
        self::assertEquals($expected['data'], $result['data']);
404
405
        self::assertCount(count($expected['errors']), $result['errors']);
406
        foreach ($expected['errors'] as $expectedError) {
407
            $found = false;
408
            foreach ($result['errors'] as $error) {
409
                try {
410
                    self::assertArraySubset($expectedError, $error);
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit\Framework\Assert::assertArraySubset() has been deprecated: https://github.com/sebastianbergmann/phpunit/issues/3494 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

410
                    /** @scrutinizer ignore-deprecated */ self::assertArraySubset($expectedError, $error);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
411
                    $found = true;
412
                    break;
413
                } catch (ExpectationFailedException $e) {
414
                    continue;
415
                }
416
            }
417
            self::assertTrue($found, 'Did not find error: ' . json_encode($expectedError));
418
        }
419
    }
420
421
    public function testNullsTheFirstNullableObjectAfterAFieldThrowsInALongChainOfFieldsThatAreNonNull() : void
422
    {
423
        $doc = '
424
      query Q {
425
        syncNest {
426
          syncNonNullNest {
427
            promiseNonNullNest {
428
              syncNonNullNest {
429
                promiseNonNullNest {
430
                  syncNonNull
431
                }
432
              }
433
            }
434
          }
435
        }
436
        promiseNest {
437
          syncNonNullNest {
438
            promiseNonNullNest {
439
              syncNonNullNest {
440
                promiseNonNullNest {
441
                  syncNonNull
442
                }
443
              }
444
            }
445
          }
446
        }
447
        anotherNest: syncNest {
448
          syncNonNullNest {
449
            promiseNonNullNest {
450
              syncNonNullNest {
451
                promiseNonNullNest {
452
                  promiseNonNull
453
                }
454
              }
455
            }
456
          }
457
        }
458
        anotherPromiseNest: promiseNest {
459
          syncNonNullNest {
460
            promiseNonNullNest {
461
              syncNonNullNest {
462
                promiseNonNullNest {
463
                  promiseNonNull
464
                }
465
              }
466
            }
467
          }
468
        }
469
      }
470
    ';
471
472
        $ast = Parser::parse($doc);
473
474
        $expected = [
475
            'data'   => [
476
                'syncNest'           => null,
477
                'promiseNest'        => null,
478
                'anotherNest'        => null,
479
                'anotherPromiseNest' => null,
480
            ],
481
            'errors' => [
482
                FormattedError::create($this->syncNonNullError->getMessage(), [new SourceLocation(8, 19)]),
0 ignored issues
show
Deprecated Code introduced by
The function GraphQL\Error\FormattedError::create() has been deprecated: as of v0.8.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

482
                /** @scrutinizer ignore-deprecated */ FormattedError::create($this->syncNonNullError->getMessage(), [new SourceLocation(8, 19)]),

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
483
                FormattedError::create($this->syncNonNullError->getMessage(), [new SourceLocation(19, 19)]),
0 ignored issues
show
Deprecated Code introduced by
The function GraphQL\Error\FormattedError::create() has been deprecated: as of v0.8.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

483
                /** @scrutinizer ignore-deprecated */ FormattedError::create($this->syncNonNullError->getMessage(), [new SourceLocation(19, 19)]),

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
484
                FormattedError::create($this->promiseNonNullError->getMessage(), [new SourceLocation(30, 19)]),
0 ignored issues
show
Deprecated Code introduced by
The function GraphQL\Error\FormattedError::create() has been deprecated: as of v0.8.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

484
                /** @scrutinizer ignore-deprecated */ FormattedError::create($this->promiseNonNullError->getMessage(), [new SourceLocation(30, 19)]),

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
485
                FormattedError::create($this->promiseNonNullError->getMessage(), [new SourceLocation(41, 19)]),
0 ignored issues
show
Deprecated Code introduced by
The function GraphQL\Error\FormattedError::create() has been deprecated: as of v0.8.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

485
                /** @scrutinizer ignore-deprecated */ FormattedError::create($this->promiseNonNullError->getMessage(), [new SourceLocation(41, 19)]),

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
486
            ],
487
        ];
488
489
        self::assertArraySubset(
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit\Framework\Assert::assertArraySubset() has been deprecated: https://github.com/sebastianbergmann/phpunit/issues/3494 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

489
        /** @scrutinizer ignore-deprecated */ self::assertArraySubset(

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
490
            $expected,
491
            Executor::execute($this->schema, $ast, $this->throwingData, null, [], 'Q')->toArray()
492
        );
493
    }
494
495
    public function testNullsANullableFieldThatSynchronouslyReturnsNull() : void
496
    {
497
        $doc = '
498
      query Q {
499
        sync
500
      }
501
        ';
502
503
        $ast = Parser::parse($doc);
504
505
        $expected = [
506
            'data' => ['sync' => null],
507
        ];
508
        self::assertEquals(
509
            $expected,
510
            Executor::execute($this->schema, $ast, $this->nullingData, null, [], 'Q')->toArray()
511
        );
512
    }
513
514
    public function testNullsANullableFieldThatReturnsNullInAPromise() : void
515
    {
516
        $doc = '
517
      query Q {
518
        promise
519
      }
520
    ';
521
522
        $ast = Parser::parse($doc);
523
524
        $expected = [
525
            'data' => ['promise' => null],
526
        ];
527
528
        self::assertArraySubset(
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit\Framework\Assert::assertArraySubset() has been deprecated: https://github.com/sebastianbergmann/phpunit/issues/3494 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

528
        /** @scrutinizer ignore-deprecated */ self::assertArraySubset(

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
529
            $expected,
530
            Executor::execute($this->schema, $ast, $this->nullingData, null, [], 'Q')->toArray()
531
        );
532
    }
533
534
    public function testNullsASynchronouslyReturnedObjectThatContainsANonNullableFieldThatReturnsNullSynchronously() : void
535
    {
536
        $doc = '
537
      query Q {
538
        syncNest {
539
          syncNonNull,
540
        }
541
      }
542
        ';
543
544
        $ast = Parser::parse($doc);
545
546
        $expected = [
547
            'data'   => ['syncNest' => null],
548
            'errors' => [
549
                [
550
                    'debugMessage' => 'Cannot return null for non-nullable field DataType.syncNonNull.',
551
                    'locations'    => [['line' => 4, 'column' => 11]],
552
                ],
553
            ],
554
        ];
555
        self::assertArraySubset(
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit\Framework\Assert::assertArraySubset() has been deprecated: https://github.com/sebastianbergmann/phpunit/issues/3494 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

555
        /** @scrutinizer ignore-deprecated */ self::assertArraySubset(

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
556
            $expected,
557
            Executor::execute($this->schema, $ast, $this->nullingData, null, [], 'Q')->toArray(true)
558
        );
559
    }
560
561
    public function testNullsASynchronouslyReturnedObjectThatContainsANonNullableFieldThatReturnsNullInAPromise() : void
562
    {
563
        $doc = '
564
      query Q {
565
        syncNest {
566
          promiseNonNull,
567
        }
568
      }
569
    ';
570
571
        $ast = Parser::parse($doc);
572
573
        $expected = [
574
            'data'   => ['syncNest' => null],
575
            'errors' => [
576
                [
577
                    'debugMessage' => 'Cannot return null for non-nullable field DataType.promiseNonNull.',
578
                    'locations'    => [['line' => 4, 'column' => 11]],
579
                ],
580
            ],
581
        ];
582
583
        self::assertArraySubset(
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit\Framework\Assert::assertArraySubset() has been deprecated: https://github.com/sebastianbergmann/phpunit/issues/3494 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

583
        /** @scrutinizer ignore-deprecated */ self::assertArraySubset(

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
584
            $expected,
585
            Executor::execute($this->schema, $ast, $this->nullingData, null, [], 'Q')->toArray(true)
586
        );
587
    }
588
589
    public function testNullsAnObjectReturnedInAPromiseThatContainsANonNullableFieldThatReturnsNullSynchronously() : void
590
    {
591
        $doc = '
592
      query Q {
593
        promiseNest {
594
          syncNonNull,
595
        }
596
      }
597
    ';
598
599
        $ast = Parser::parse($doc);
600
601
        $expected = [
602
            'data'   => ['promiseNest' => null],
603
            'errors' => [
604
                [
605
                    'debugMessage' => 'Cannot return null for non-nullable field DataType.syncNonNull.',
606
                    'locations'    => [['line' => 4, 'column' => 11]],
607
                ],
608
            ],
609
        ];
610
611
        self::assertArraySubset(
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit\Framework\Assert::assertArraySubset() has been deprecated: https://github.com/sebastianbergmann/phpunit/issues/3494 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

611
        /** @scrutinizer ignore-deprecated */ self::assertArraySubset(

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
612
            $expected,
613
            Executor::execute($this->schema, $ast, $this->nullingData, null, [], 'Q')->toArray(true)
614
        );
615
    }
616
617
    public function testNullsAnObjectReturnedInAPromiseThatContainsANonNullableFieldThatReturnsNullInaAPromise() : void
618
    {
619
        $doc = '
620
      query Q {
621
        promiseNest {
622
          promiseNonNull,
623
        }
624
      }
625
    ';
626
627
        $ast = Parser::parse($doc);
628
629
        $expected = [
630
            'data'   => ['promiseNest' => null],
631
            'errors' => [
632
                [
633
                    'debugMessage' => 'Cannot return null for non-nullable field DataType.promiseNonNull.',
634
                    'locations'    => [['line' => 4, 'column' => 11]],
635
                ],
636
            ],
637
        ];
638
639
        self::assertArraySubset(
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit\Framework\Assert::assertArraySubset() has been deprecated: https://github.com/sebastianbergmann/phpunit/issues/3494 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

639
        /** @scrutinizer ignore-deprecated */ self::assertArraySubset(

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
640
            $expected,
641
            Executor::execute($this->schema, $ast, $this->nullingData, null, [], 'Q')->toArray(true)
642
        );
643
    }
644
645
    public function testNullsAComplexTreeOfNullableFieldsThatReturnNull() : void
646
    {
647
        $doc = '
648
      query Q {
649
        syncNest {
650
          sync
651
          promise
652
          syncNest {
653
            sync
654
            promise
655
          }
656
          promiseNest {
657
            sync
658
            promise
659
          }
660
        }
661
        promiseNest {
662
          sync
663
          promise
664
          syncNest {
665
            sync
666
            promise
667
          }
668
          promiseNest {
669
            sync
670
            promise
671
          }
672
        }
673
      }
674
    ';
675
676
        $ast = Parser::parse($doc);
677
678
        $expected = [
679
            'data' => [
680
                'syncNest'    => [
681
                    'sync'        => null,
682
                    'promise'     => null,
683
                    'syncNest'    => [
684
                        'sync'    => null,
685
                        'promise' => null,
686
                    ],
687
                    'promiseNest' => [
688
                        'sync'    => null,
689
                        'promise' => null,
690
                    ],
691
                ],
692
                'promiseNest' => [
693
                    'sync'        => null,
694
                    'promise'     => null,
695
                    'syncNest'    => [
696
                        'sync'    => null,
697
                        'promise' => null,
698
                    ],
699
                    'promiseNest' => [
700
                        'sync'    => null,
701
                        'promise' => null,
702
                    ],
703
                ],
704
            ],
705
        ];
706
707
        $actual = Executor::execute($this->schema, $ast, $this->nullingData, null, [], 'Q')->toArray();
708
        self::assertEquals($expected, $actual);
709
    }
710
711
    /**
712
     * @see it('nulls the first nullable object after a field in a long chain of non-null fields')
713
     */
714
    public function testNullsTheFirstNullableObjectAfterAFieldReturnsNullInALongChainOfFieldsThatAreNonNull() : void
715
    {
716
        $doc = '
717
      query Q {
718
        syncNest {
719
          syncNonNullNest {
720
            promiseNonNullNest {
721
              syncNonNullNest {
722
                promiseNonNullNest {
723
                  syncNonNull
724
                }
725
              }
726
            }
727
          }
728
        }
729
        promiseNest {
730
          syncNonNullNest {
731
            promiseNonNullNest {
732
              syncNonNullNest {
733
                promiseNonNullNest {
734
                  syncNonNull
735
                }
736
              }
737
            }
738
          }
739
        }
740
        anotherNest: syncNest {
741
          syncNonNullNest {
742
            promiseNonNullNest {
743
              syncNonNullNest {
744
                promiseNonNullNest {
745
                  promiseNonNull
746
                }
747
              }
748
            }
749
          }
750
        }
751
        anotherPromiseNest: promiseNest {
752
          syncNonNullNest {
753
            promiseNonNullNest {
754
              syncNonNullNest {
755
                promiseNonNullNest {
756
                  promiseNonNull
757
                }
758
              }
759
            }
760
          }
761
        }
762
      }
763
    ';
764
765
        $ast = Parser::parse($doc);
766
767
        $expected = [
768
            'data'   => [
769
                'syncNest'           => null,
770
                'promiseNest'        => null,
771
                'anotherNest'        => null,
772
                'anotherPromiseNest' => null,
773
            ],
774
            'errors' => [
775
                ['debugMessage' => 'Cannot return null for non-nullable field DataType.syncNonNull.', 'locations' => [['line' => 8, 'column' => 19]]],
776
                ['debugMessage' => 'Cannot return null for non-nullable field DataType.syncNonNull.', 'locations' => [['line' => 19, 'column' => 19]]],
777
                ['debugMessage' => 'Cannot return null for non-nullable field DataType.promiseNonNull.', 'locations' => [['line' => 30, 'column' => 19]]],
778
                ['debugMessage' => 'Cannot return null for non-nullable field DataType.promiseNonNull.', 'locations' => [['line' => 41, 'column' => 19]]],
779
            ],
780
        ];
781
782
        self::assertArraySubset(
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit\Framework\Assert::assertArraySubset() has been deprecated: https://github.com/sebastianbergmann/phpunit/issues/3494 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

782
        /** @scrutinizer ignore-deprecated */ self::assertArraySubset(

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
783
            $expected,
784
            Executor::execute($this->schema, $ast, $this->nullingData, null, [], 'Q')->toArray(true)
785
        );
786
    }
787
788
    /**
789
     * @see it('nulls the top level if non-nullable field')
790
     */
791
    public function testNullsTheTopLevelIfSyncNonNullableFieldThrows() : void
792
    {
793
        $doc = '
794
      query Q { syncNonNull }
795
        ';
796
797
        $expected = [
798
            'errors' => [
799
                FormattedError::create($this->syncNonNullError->getMessage(), [new SourceLocation(2, 17)]),
0 ignored issues
show
Deprecated Code introduced by
The function GraphQL\Error\FormattedError::create() has been deprecated: as of v0.8.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

799
                /** @scrutinizer ignore-deprecated */ FormattedError::create($this->syncNonNullError->getMessage(), [new SourceLocation(2, 17)]),

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
800
            ],
801
        ];
802
        $actual   = Executor::execute($this->schema, Parser::parse($doc), $this->throwingData)->toArray();
803
        self::assertArraySubset($expected, $actual);
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit\Framework\Assert::assertArraySubset() has been deprecated: https://github.com/sebastianbergmann/phpunit/issues/3494 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

803
        /** @scrutinizer ignore-deprecated */ self::assertArraySubset($expected, $actual);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
804
    }
805
806
    /**
807
     * @see describe('Handles non-null argument')
808
     * @see it('succeeds when passed non-null literal value')
809
     */
810
    public function succeedsWhenPassedNonNullLiteralValue() : void
811
    {
812
        $result = Executor::execute(
813
            $this->schemaWithNonNullArg,
814
            Parser::parse('
815
          query {
816
            withNonNullArg (cannotBeNull: "literal value")
817
          }
818
        ')
819
        );
820
821
        $expected = ['data' => ['withNonNullArg' => 'Passed: literal value']];
822
        self::assertEquals($expected, $result->toArray());
823
    }
824
825
    /**
826
     * @see it('succeeds when passed non-null variable value')
827
     */
828
    public function succeedsWhenPassedNonNullVariableValue()
829
    {
830
        $result = Executor::execute(
831
            $this->schemaWithNonNullArg,
832
            Parser::parse('
833
          query ($testVar: String!) {
834
            withNonNullArg (cannotBeNull: $testVar)
835
          }
836
        '),
837
            null,
838
            null,
839
            ['testVar' => 'variable value']
840
        );
841
842
        $expected = ['data' => ['withNonNullArg' => 'Passed: variable value']];
843
        self::assertEquals($expected, $result->toArray());
844
    }
845
846
    /**
847
     * @see it('succeeds when missing variable has default value')
848
     */
849
    public function testSucceedsWhenMissingVariableHasDefaultValue()
850
    {
851
        $result = Executor::execute(
852
            $this->schemaWithNonNullArg,
853
            Parser::parse('
854
          query ($testVar: String = "default value") {
855
            withNonNullArg (cannotBeNull: $testVar)
856
          }
857
        '),
858
            null,
859
            null,
860
            [] // Intentionally missing variable
861
        );
862
863
        $expected = ['data' => ['withNonNullArg' => 'Passed: default value']];
864
        self::assertEquals($expected, $result->toArray());
865
    }
866
867
    /**
868
     * @see it('field error when missing non-null arg')
869
     */
870
    public function testFieldErrorWhenMissingNonNullArg()
871
    {
872
      // Note: validation should identify this issue first (missing args rule)
873
      // however execution should still protect against this.
874
        $result = Executor::execute(
875
            $this->schemaWithNonNullArg,
876
            Parser::parse('
877
          query {
878
            withNonNullArg
879
          }
880
        ')
881
        );
882
883
        $expected = [
884
            'data' => ['withNonNullArg' => null],
885
            'errors' => [
886
                [
887
                    'message' => 'Argument "cannotBeNull" of required type "String!" was not provided.',
888
                    'locations' => [['line' => 3, 'column' => 13]],
889
                    'path' => ['withNonNullArg'],
890
                    'extensions' => ['category' => 'graphql'],
891
                ],
892
            ],
893
        ];
894
        self::assertEquals($expected, $result->toArray());
895
    }
896
897
    /**
898
     * @see it('field error when non-null arg provided null')
899
     */
900
    public function testFieldErrorWhenNonNullArgProvidedNull()
901
    {
902
      // Note: validation should identify this issue first (values of correct
903
      // type rule) however execution should still protect against this.
904
        $result = Executor::execute(
905
            $this->schemaWithNonNullArg,
906
            Parser::parse('
907
          query {
908
            withNonNullArg(cannotBeNull: null)
909
          }
910
        ')
911
        );
912
913
        $expected = [
914
            'data' => ['withNonNullArg' => null],
915
            'errors' => [
916
                [
917
                    'message' => 'Argument "cannotBeNull" of non-null type "String!" must not be null.',
918
                    'locations' => [['line' => 3, 'column' => 13]],
919
                    'path' => ['withNonNullArg'],
920
                    'extensions' => ['category' => 'graphql'],
921
                ],
922
            ],
923
        ];
924
        self::assertEquals($expected, $result->toArray());
925
    }
926
927
    /**
928
     * @see it('field error when non-null arg not provided variable value')
929
     */
930
    public function testFieldErrorWhenNonNullArgNotProvidedVariableValue() : void
931
    {
932
      // Note: validation should identify this issue first (variables in allowed
933
      // position rule) however execution should still protect against this.
934
        $result = Executor::execute(
935
            $this->schemaWithNonNullArg,
936
            Parser::parse('
937
          query ($testVar: String) {
938
            withNonNullArg(cannotBeNull: $testVar)
939
          }
940
        '),
941
            null,
942
            null,
943
            [] // Intentionally missing variable
944
        );
945
946
        $expected = [
947
            'data' => ['withNonNullArg' => null],
948
            'errors' => [
949
                [
950
                    'message' => 'Argument "cannotBeNull" of required type "String!" was ' .
951
                      'provided the variable "$testVar" which was not provided a ' .
952
                      'runtime value.',
953
                    'locations' => [['line' => 3, 'column' => 42]],
954
                    'path' => ['withNonNullArg'],
955
                    'extensions' => ['category' => 'graphql'],
956
                ],
957
            ],
958
        ];
959
        self::assertEquals($expected, $result->toArray());
960
    }
961
962
    /**
963
     * @see it('field error when non-null arg provided variable with explicit null value')
964
     */
965
    public function testFieldErrorWhenNonNullArgProvidedVariableWithExplicitNullValue()
966
    {
967
        $result = Executor::execute(
968
            $this->schemaWithNonNullArg,
969
            Parser::parse('
970
          query ($testVar: String = "default value") {
971
            withNonNullArg (cannotBeNull: $testVar)
972
          }
973
        '),
974
            null,
975
            null,
976
            ['testVar' => null]
977
        );
978
979
        $expected = [
980
            'data' => ['withNonNullArg' => null],
981
            'errors' => [
982
                [
983
                    'message' => 'Argument "cannotBeNull" of non-null type "String!" must not be null.',
984
                    'locations' => [['line' => 3, 'column' => 13]],
985
                    'path' => ['withNonNullArg'],
986
                    'extensions' => ['category' => 'graphql'],
987
                ],
988
            ],
989
        ];
990
991
        self::assertEquals($expected, $result->toArray());
992
    }
993
994
    public function testNullsTheTopLevelIfAsyncNonNullableFieldErrors() : void
995
    {
996
        $doc = '
997
      query Q { promiseNonNull }
998
    ';
999
1000
        $ast = Parser::parse($doc);
1001
1002
        $expected = [
1003
            'errors' => [
1004
                FormattedError::create($this->promiseNonNullError->getMessage(), [new SourceLocation(2, 17)]),
0 ignored issues
show
Deprecated Code introduced by
The function GraphQL\Error\FormattedError::create() has been deprecated: as of v0.8.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

1004
                /** @scrutinizer ignore-deprecated */ FormattedError::create($this->promiseNonNullError->getMessage(), [new SourceLocation(2, 17)]),

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
1005
            ],
1006
        ];
1007
1008
        self::assertArraySubset(
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit\Framework\Assert::assertArraySubset() has been deprecated: https://github.com/sebastianbergmann/phpunit/issues/3494 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

1008
        /** @scrutinizer ignore-deprecated */ self::assertArraySubset(

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
1009
            $expected,
1010
            Executor::execute($this->schema, $ast, $this->throwingData, null, [], 'Q')->toArray()
1011
        );
1012
    }
1013
1014
    public function testNullsTheTopLevelIfSyncNonNullableFieldReturnsNull() : void
1015
    {
1016
        // nulls the top level if sync non-nullable field returns null
1017
        $doc = '
1018
      query Q { syncNonNull }
1019
        ';
1020
1021
        $expected = [
1022
            'errors' => [
1023
                [
1024
                    'debugMessage' => 'Cannot return null for non-nullable field DataType.syncNonNull.',
1025
                    'locations'    => [['line' => 2, 'column' => 17]],
1026
                ],
1027
            ],
1028
        ];
1029
        self::assertArraySubset(
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit\Framework\Assert::assertArraySubset() has been deprecated: https://github.com/sebastianbergmann/phpunit/issues/3494 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

1029
        /** @scrutinizer ignore-deprecated */ self::assertArraySubset(

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
1030
            $expected,
1031
            Executor::execute($this->schema, Parser::parse($doc), $this->nullingData)->toArray(true)
1032
        );
1033
    }
1034
1035
    public function testNullsTheTopLevelIfAsyncNonNullableFieldResolvesNull() : void
1036
    {
1037
        $doc = '
1038
      query Q { promiseNonNull }
1039
    ';
1040
1041
        $ast = Parser::parse($doc);
1042
1043
        $expected = [
1044
            'errors' => [
1045
                [
1046
                    'debugMessage' => 'Cannot return null for non-nullable field DataType.promiseNonNull.',
1047
                    'locations'    => [['line' => 2, 'column' => 17]],
1048
                ],
1049
            ],
1050
        ];
1051
1052
        self::assertArraySubset(
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit\Framework\Assert::assertArraySubset() has been deprecated: https://github.com/sebastianbergmann/phpunit/issues/3494 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

1052
        /** @scrutinizer ignore-deprecated */ self::assertArraySubset(

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
1053
            $expected,
1054
            Executor::execute($this->schema, $ast, $this->nullingData, null, [], 'Q')->toArray(true)
1055
        );
1056
    }
1057
}
1058