Passed
Push — master ( 5d51a4...0ae335 )
by Max
09:21
created

testValidationEmptyInput()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 48
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 12
nc 1
nop 0
dl 0
loc 48
rs 9.8666
c 0
b 0
f 0
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\ValidatedFieldDefinition;
13
use GraphQL\Type\Schema;
14
use PHPUnit\Framework\TestCase;
15
use function strlen;
16
17
final class InputObjectValidationTest extends TestCase
18
{
19
    /** @var Type */
20
    protected $bookType;
21
22
    /** @var InputObjectType */
23
    protected $bookAttributesInputType;
24
25
    /** @var Type */
26
    protected $personType;
27
28
    /** @var mixed[] */
29
    protected $data = [
30
        'people' => [
31
            1 => ['firstName' => 'Wilson'],
32
            2 => ['firstName' => 'J.D.'],
33
            3 => ['firstName' => 'Diana'],
34
        ],
35
    ];
36
37
    /** @var ObjectType */
38
    protected $query;
39
40
    /** @var Schema */
41
    protected $schema;
42
43
    protected function setUp()
44
    {
45
        $this->personType = new ObjectType([
46
            'name' => 'Person',
47
            'fields' => [
48
                'firstName' => [
49
                    'type' => Type::string(),
50
                    'phoneNumbers' => [
51
                        'type' => Type::listOf(Type::string()),
52
                    ],
53
                ],
54
            ],
55
        ]);
56
57
        $this->bookAttributesInputType = new InputObjectType([
58
            'name' => 'BookAttributes',
59
            'fields' => [
60
                'title' => [
61
                    'type' => Type::string(),
62
                    'description' => 'Enter a book title, no more than 10 characters in length',
63
                    'validate' => static function (string $title) {
64
                        if (strlen($title) > 10) {
65
                            return [1, 'book title must be less than 10 chaacters'];
66
                        }
67
                        return 0;
68
                    },
69
                ],
70
                'author' => [
71
                    'type' => Type::id(),
72
                    'description' => 'Provide a valid author id',
73
                    'errorCodes' => [
74
                        'unknownAuthor',
75
                        'authorDeceased',
76
                    ],
77
                    'validate' => function (string $authorId) {
78
                        if (! isset($this->data['people'][$authorId])) {
79
                            return ['unknownAuthor', 'We have no record of that author'];
80
                        }
81
                        return 0;
82
                    },
83
                ],
84
            ],
85
        ]);
86
87
        $this->query = new ObjectType(['name' => 'Query']);
88
89
        $this->schema = new Schema([
90
            'query' => $this->query,
91
            'mutation' => new ObjectType([
92
                'name' => 'Mutation',
93
                'fields' => function () {
94
                    return [
95
                        'updateBook' => new ValidatedFieldDefinition([
96
                            'name' => 'updateBook',
97
                            'type' => Type::boolean(),
98
                            'args' => [
99
                                'bookAttributes' => [
100
                                    'type' => $this->bookAttributesInputType,
101
                                ],
102
                            ],
103
                            '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

103
                            '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

103
                            '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...
104
                                // ...
105
                                // do update
106
                                // ...
107
108
                                return true;
109
                            },
110
                        ]),
111
                    ];
112
                },
113
            ]),
114
        ]);
115
    }
116
117
    public function testValidationFail()
118
    {
119
        $res = GraphQL::executeQuery(
120
            $this->schema,
121
            Utils::nowdoc('
122
				mutation UpdateBook(
123
                        $bookAttributes: BookAttributes
124
                    ) {
125
                        updateBook (
126
                            bookAttributes: $bookAttributes
127
                        ) {
128
                            valid
129
                            suberrors {
130
                                bookAttributes {
131
                                    suberrors {
132
                                        title {
133
                                            code
134
                                            msg
135
                                        }
136
                                        author {
137
                                            code
138
                                            msg
139
                                        }
140
                                    }
141
                                }
142
                            }
143
                            result
144
                        }
145
                    }
146
			'),
147
            [],
148
            null,
149
            [
150
                'bookAttributes' => [
151
                    'title' => 'The Catcher in the Rye',
152
                    'author' => 4,
153
                ],
154
            ]
155
        );
156
157
        static::assertEquals(
158
            [
159
                'valid' => false,
160
                'suberrors' =>
161
                    [
162
                        'bookAttributes' =>
163
                            [
164
                                'suberrors' =>
165
                                    [
166
                                        'title' =>
167
                                            [
168
                                                'code' => 1,
169
                                                'msg' => 'book title must be less than 10 chaacters',
170
                                            ],
171
                                        'author' =>
172
                                            [
173
                                                'code' => 'unknownAuthor',
174
                                                'msg' => 'We have no record of that author',
175
                                            ],
176
                                    ],
177
                            ],
178
                    ],
179
                'result' => null,
180
            ],
181
            $res->data['updateBook']
182
        );
183
184
        static::assertFalse($res->data['updateBook']['valid']);
185
    }
186
187
    public function testValidationSuccess()
188
    {
189
        $res = GraphQL::executeQuery(
190
            $this->schema,
191
            Utils::nowdoc('
192
				mutation UpdateBook(
193
                        $bookAttributes: BookAttributes
194
                    ) {
195
                        updateBook (
196
                            bookAttributes: $bookAttributes
197
                        ) {
198
                            valid
199
                            suberrors {
200
                                bookAttributes {
201
                                    suberrors {
202
                                        title {
203
                                            code
204
                                            msg
205
                                        }
206
                                        author {
207
                                            code
208
                                            msg
209
                                        }
210
                                    }
211
                                }
212
                            }
213
                            result
214
                        }
215
                    }
216
			'),
217
            [],
218
            null,
219
            [
220
                'bookAttributes' => [
221
                    'title' => 'Dogsbody',
222
                    'author' => 3,
223
                ],
224
            ]
225
        );
226
227
        static::assertEmpty($res->errors);
228
        static::assertEquals(
229
            [
230
                'valid' => true,
231
                'suberrors' => null,
232
                'result' => true,
233
            ],
234
            $res->data['updateBook']
235
        );
236
237
        static::assertTrue($res->data['updateBook']['valid']);
238
    }
239
240
    public function testValidationEmptyInput()
241
    {
242
        $res = GraphQL::executeQuery(
243
            $this->schema,
244
            Utils::nowdoc('
245
				mutation UpdateBook(
246
                        $bookAttributes: BookAttributes
247
                    ) {
248
                        updateBook (
249
                            bookAttributes: $bookAttributes
250
                        ) {
251
                            valid
252
                            suberrors {
253
                                bookAttributes {
254
                                    suberrors {
255
                                        title {
256
                                            code
257
                                            msg
258
                                        }
259
                                        author {
260
                                            code
261
                                            msg
262
                                        }
263
                                    }
264
                                }
265
                            }
266
                            result
267
                        }
268
                    }
269
			'),
270
            [],
271
            null,
272
            [
273
                'bookAttributes' => [],
274
            ]
275
        );
276
277
        static::assertEmpty($res->errors);
278
        static::assertEquals(
279
            [
280
                'valid' => true,
281
                'suberrors' => null,
282
                'result' => true,
283
            ],
284
            $res->data['updateBook']
285
        );
286
287
        static::assertTrue($res->data['updateBook']['valid']);
288
    }
289
}
290