Passed
Pull Request — master (#1)
by Max
07:34 queued 02:12
created

testValidationInputObjectFieldFail()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 68
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 21
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 68
rs 9.584

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace GraphQL\Tests\Type\ValidatedFieldDefinition;
6
7
use GraphQL\GraphQL;
8
use GraphQL\Tests\Utils;
9
use GraphQL\Type\Definition\InputObjectType;
10
use GraphQL\Type\Definition\ObjectType;
11
use GraphQL\Type\Definition\Type;
12
use GraphQL\Type\Definition\UserErrorsType;
13
use GraphQL\Type\Definition\ValidatedFieldDefinition;
14
use GraphQL\Type\Schema;
15
use PHPUnit\Framework\TestCase;
16
use function strlen;
17
18
final class InputObjectValidationTest extends TestCase
19
{
20
    /** @var Type */
21
    protected $bookType;
22
23
    /** @var InputObjectType */
24
    protected $bookAttributesInputType;
25
26
    /** @var Type */
27
    protected $personType;
28
29
    /** @var mixed[] */
30
    protected $data = [
31
        'people' => [
32
            1 => ['firstName' => 'Wilson'],
33
            2 => ['firstName' => 'J.D.'],
34
            3 => ['firstName' => 'Diana'],
35
        ],
36
    ];
37
38
    /** @var ObjectType */
39
    protected $query;
40
41
    /** @var Schema */
42
    protected $schema;
43
44
    protected function setUp()
45
    {
46
        $this->personType = new ObjectType([
47
            'name' => 'Person',
48
            'fields' => [
49
                'firstName' => [
50
                    'type' => Type::string(),
51
                    'phoneNumbers' => [
52
                        'type' => Type::listOf(Type::string()),
53
                    ],
54
                ],
55
            ],
56
        ]);
57
58
        $this->bookAttributesInputType = new InputObjectType([
59
            'name' => 'BookAttributes',
60
            'fields' => [
61
                'title' => [
62
                    'type' => Type::string(),
63
                    'description' => 'Enter a book title, no more than 10 characters in length',
64
                    'validate' => static function (string $title) {
65
                        if (strlen($title) > 10) {
66
                            return [1, 'book title must be less than 10 chaacters'];
67
                        }
68
                        return 0;
69
                    },
70
                ],
71
                'author' => [
72
                    'type' => Type::id(),
73
                    'description' => 'Provide a valid author id',
74
                    'errorCodes' => [
75
                        'unknownAuthor',
76
                        'authorDeceased',
77
                    ],
78
                    'validate' => function (string $authorId) {
79
                        if (! isset($this->data['people'][$authorId])) {
80
                            return ['unknownAuthor', 'We have no record of that author'];
81
                        }
82
                        return 0;
83
                    },
84
                ],
85
            ],
86
        ]);
87
88
        $this->query = new ObjectType(['name' => 'Query']);
89
90
        $this->schema = new Schema([
91
            'query' => $this->query,
92
            'mutation' => new ObjectType([
93
                'name' => 'Mutation',
94
                'fields' => function () {
95
                    return [
96
                        'updateBook' => new ValidatedFieldDefinition([
97
                            'name' => 'updateBook',
98
                            'type' => Type::boolean(),
99
                            'args' => [
100
                                'bookAttributes' => [
101
                                    'type' => $this->bookAttributesInputType,
102
	                                'errorCodes' => [
103
										'titleOrIdRequired'
104
	                                ],
105
	                                'validate' => static function(?array $bookAttributes) {
106
                        	            if(is_null($bookAttributes)) {
0 ignored issues
show
introduced by
The condition is_null($bookAttributes) is always false.
Loading history...
107
                        	                return 0;
108
	                                    }
109
                        	            
110
                        	            return (isset($bookAttributes['title']) || isset($bookAttributes['author'])) ? 0 : [
111
                        	            	'titleOrIdRequired', 'You must supply at least one of title or author'
112
	                                    ];
113
	                                }
114
                                ],
115
                            ],
116
                            'resolve' => static function ($value, $args) : bool {
0 ignored issues
show
Unused Code introduced by
The parameter $value is not used and could be removed. ( Ignorable by Annotation )

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

116
                            'resolve' => static function (/** @scrutinizer ignore-unused */ $value, $args) : bool {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $args is not used and could be removed. ( Ignorable by Annotation )

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

116
                            'resolve' => static function ($value, /** @scrutinizer ignore-unused */ $args) : bool {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
117
                                // ...
118
                                // do update
119
                                // ...
120
121
                                return true;
122
                            },
123
                        ]),
124
                    ];
125
                },
126
            ]),
127
        ]);
128
    }
129
130
    public function testValidationInputObjectFieldFail()
131
    {
132
        $res = GraphQL::executeQuery(
133
            $this->schema,
134
            Utils::nowdoc('
135
				mutation UpdateBook(
136
						$bookAttributes: BookAttributes
137
					) {
138
					updateBook (
139
						bookAttributes: $bookAttributes
140
					) {
141
						valid
142
						suberrors {
143
							bookAttributes {
144
								suberrors {
145
									title {
146
										code
147
										msg
148
									}
149
									author {
150
										code
151
										msg
152
									}
153
								}
154
							}
155
						}
156
						result
157
					}
158
				}
159
			'),
160
            [],
161
            null,
162
            [
163
                'bookAttributes' => [
164
                    'title' => 'The Catcher in the Rye',
165
                    'author' => 4,
166
                ],
167
            ]
168
        );
169
170
        static::assertEquals(
171
            [
172
                'valid' => false,
173
                'suberrors' =>
174
                    [
175
                        'bookAttributes' =>
176
                            [
177
                                'suberrors' =>
178
                                    [
179
                                        'title' =>
180
                                            [
181
                                                'code' => 1,
182
                                                'msg' => 'book title must be less than 10 chaacters',
183
                                            ],
184
                                        'author' =>
185
                                            [
186
                                                'code' => 'unknownAuthor',
187
                                                'msg' => 'We have no record of that author',
188
                                            ],
189
                                    ],
190
                            ],
191
                    ],
192
                'result' => null,
193
            ],
194
            $res->data['updateBook']
195
        );
196
197
        static::assertFalse($res->data['updateBook']['valid']);
198
    }
199
200
	public function testValidationInputObjectSelfFail()
201
	{
202
		$res = GraphQL::executeQuery(
203
			$this->schema,
204
			Utils::nowdoc('
205
				mutation UpdateBook(
206
						$bookAttributes: BookAttributes
207
					) {
208
					updateBook (
209
						bookAttributes: $bookAttributes
210
					) {
211
						valid
212
						suberrors {
213
							bookAttributes {
214
								code
215
								msg
216
								suberrors {
217
									title {
218
										code
219
										msg
220
									}
221
									author {
222
										code
223
										msg
224
									}
225
								}
226
							}
227
						}
228
						result
229
					}
230
				}
231
			'),
232
			[],
233
			null,
234
			[
235
				'bookAttributes' => [
236
					'title' => null,
237
					'author' => null,
238
				],
239
			]
240
		);
241
242
		static::assertEquals(
243
			array (
244
				'valid' => false,
245
				'suberrors' =>
246
					array (
247
						'bookAttributes' =>
248
							array (
249
								'code' => 'titleOrIdRequired',
250
								'msg' => 'You must supply at least one of title or author',
251
								'suberrors' => NULL,
252
							),
253
					),
254
				'result' => NULL,
255
			),
256
			$res->data['updateBook']
257
		);
258
259
		static::assertFalse($res->data['updateBook']['valid']);
260
	}
261
262
    public function testValidationSuccess()
263
    {
264
        $res = GraphQL::executeQuery(
265
            $this->schema,
266
            Utils::nowdoc('
267
				mutation UpdateBook(
268
					$bookAttributes: BookAttributes
269
				) {
270
					updateBook (
271
						bookAttributes: $bookAttributes
272
					) {
273
						valid
274
						suberrors {
275
							bookAttributes {
276
								suberrors {
277
									title {
278
										code
279
										msg
280
									}
281
									author {
282
										code
283
										msg
284
									}
285
								}
286
							}
287
						}
288
						result
289
					}
290
				}
291
			'),
292
            [],
293
            null,
294
            [
295
                'bookAttributes' => [
296
                    'title' => 'Dogsbody',
297
                    'author' => 3,
298
                ],
299
            ]
300
        );
301
302
        static::assertEmpty($res->errors);
303
        static::assertEquals(
304
            [
305
                'valid' => true,
306
                'suberrors' => null,
307
                'result' => true,
308
            ],
309
            $res->data['updateBook']
310
        );
311
312
        static::assertTrue($res->data['updateBook']['valid']);
313
    }
314
315
    public function testValidationEmptyInput()
316
    {
317
        $res = GraphQL::executeQuery(
318
            $this->schema,
319
            Utils::nowdoc('
320
				mutation UpdateBook(
321
						$bookAttributes: BookAttributes
322
					) {
323
					updateBook (
324
						bookAttributes: $bookAttributes
325
					) {
326
						valid
327
						suberrors {
328
							bookAttributes {
329
								suberrors {
330
									title {
331
										code
332
										msg
333
									}
334
									author {
335
										code
336
										msg
337
									}
338
								}
339
							}
340
						}
341
						result
342
					}
343
				}
344
			'),
345
            [],
346
            null,
347
            [
348
                'bookAttributes' => null,
349
            ]
350
        );
351
352
        static::assertEmpty($res->errors);
353
        static::assertEquals(
354
	        array (
355
		        'valid' => true,
356
		        'suberrors' => NULL,
357
		        'result' => true,
358
	        ),
359
            $res->data['updateBook']
360
        );
361
362
        static::assertTrue($res->data['updateBook']['valid']);
363
    }
364
}
365