Passed
Pull Request — master (#7)
by Max
05:23
created

InputObjectValidationTest::setUp()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 47
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 29
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 47
rs 9.456
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 mixed[] */
21
    protected $data = [
22
        'people' => [
23
            1 => ['firstName' => 'Wilson'],
24
            2 => ['firstName' => 'J.D.'],
25
            3 => ['firstName' => 'Diana'],
26
        ],
27
    ];
28
29
    /** @var ObjectType */
30
    protected $query;
31
32
    /** @var Schema */
33
    protected $schema;
34
35
    protected function setUp(): void
36
    {
37
        $this->query = new ObjectType(['name' => 'Query']);
38
39
        $this->schema = $this->_createSchema();
40
    }
41
42
    protected function _createSchema() {
43
        return new Schema([
44
            'query' => $this->query,
45
            'mutation' => new ObjectType([
46
                'name' => 'Mutation',
47
                'fields' => function () {
48
                    return [
49
                        'updateBook' => new ValidatedFieldDefinition([
50
                            'name' => 'updateBook',
51
                            'type' => Type::boolean(),
52
                            'args' => [
53
                                'bookAttributes' => [
54
                                    'type' => new InputObjectType([
55
                                        'name' => 'BookAttributes',
56
                                        'fields' => [
57
                                            'title' => [
58
                                                'type' => Type::string(),
59
                                                'description' => 'Enter a book title, no more than 10 characters in length',
60
                                                'validate' => static function (string $title) {
61
                                                    if (strlen($title) > 10) {
62
                                                        return [1, 'book title must be less than 10 chaacters'];
63
                                                    }
64
                                                    return 0;
65
                                                },
66
                                            ],
67
                                            'author' => [
68
                                                'type' => Type::id(),
69
                                                'description' => 'Provide a valid author id',
70
                                                'errorCodes' => [
71
                                                    'unknownAuthor',
72
                                                    'authorDeceased',
73
                                                ],
74
                                                'validate' => function (string $authorId) {
75
                                                    if (!isset($this->data['people'][$authorId])) {
76
                                                        return ['unknownAuthor', 'We have no record of that author'];
77
                                                    }
78
                                                    return 0;
79
                                                },
80
                                            ],
81
                                        ],
82
                                    ]),
83
                                    'errorCodes' => ['titleOrIdRequired'],
84
                                    'validate' => static function (?array $bookAttributes) {
85
                                        if (empty($bookAttributes)) {
86
                                            return 0;
87
                                        }
88
89
                                        return isset($bookAttributes['title']) || isset($bookAttributes['author']) ? 0 : [
90
                                            'titleOrIdRequired',
91
                                            'You must supply at least one of title or author',
92
                                        ];
93
                                    },
94
                                ],
95
                            ],
96
                            'resolve' => static function ($value) : bool {
97
                                // ...
98
                                // do update
99
                                // ...
100
101
                                return !$value;
102
                            },
103
                        ]),
104
                    ];
105
                },
106
            ]),
107
        ]);
108
    }
109
    
110
    public function testValidationInputObjectFieldFail(): void
111
    {
112
        $res = GraphQL::executeQuery(
113
            $this->schema,
114
            Utils::nowdoc('
115
                mutation UpdateBook(
116
                        $bookAttributes: BookAttributes
117
                    ) {
118
                    updateBook (
119
                        bookAttributes: $bookAttributes
120
                    ) {
121
                        valid
122
                        suberrors {
123
                            bookAttributes {
124
                                suberrors {
125
                                    title {
126
                                        code
127
                                        msg
128
                                    }
129
                                    author {
130
                                        code
131
                                        msg
132
                                    }
133
                                }
134
                            }
135
                        }
136
                        result
137
                    }
138
                }
139
            '),
140
            [],
141
            null,
142
            [
143
                'bookAttributes' => [
144
                    'title' => 'The Catcher in the Rye',
145
                    'author' => 4,
146
                ],
147
            ]
148
        );
149
150
        static::assertEquals(
151
            [
152
                'valid' => false,
153
                'suberrors' =>
154
                    [
155
                        'bookAttributes' =>
156
                            [
157
                                'suberrors' =>
158
                                    [
159
                                        'title' =>
160
                                            [
161
                                                'code' => 1,
162
                                                'msg' => 'book title must be less than 10 chaacters',
163
                                            ],
164
                                        'author' =>
165
                                            [
166
                                                'code' => 'unknownAuthor',
167
                                                'msg' => 'We have no record of that author',
168
                                            ],
169
                                    ],
170
                            ],
171
                    ],
172
                'result' => null,
173
            ],
174
            $res->data['updateBook']
175
        );
176
177
        static::assertFalse($res->data['updateBook']['valid']);
178
    }
179
180
    public function testValidationInputObjectSelfFail(): void
181
    {
182
        $res = GraphQL::executeQuery(
183
            $this->schema,
184
            Utils::nowdoc('
185
                mutation UpdateBook(
186
                        $bookAttributes: BookAttributes
187
                    ) {
188
                    updateBook (
189
                        bookAttributes: $bookAttributes
190
                    ) {
191
                        valid
192
                        suberrors {
193
                            bookAttributes {
194
                                code
195
                                msg
196
                                suberrors {
197
                                    title {
198
                                        code
199
                                        msg
200
                                    }
201
                                    author {
202
                                        code
203
                                        msg
204
                                    }
205
                                }
206
                            }
207
                        }
208
                        result
209
                    }
210
                }
211
            '),
212
            [],
213
            null,
214
            [
215
                'bookAttributes' => [
216
                    'title' => null,
217
                    'author' => null,
218
                ],
219
            ]
220
        );
221
222
        static::assertEquals(
223
            array (
224
                'valid' => false,
225
                'suberrors' =>
226
                    [
227
                        'bookAttributes' =>
228
                            [
229
                                'code' => 'titleOrIdRequired',
230
                                'msg' => 'You must supply at least one of title or author',
231
                                'suberrors' => NULL,
232
                            ],
233
                    ],
234
                'result' => null,
235
            ),
236
            $res->data['updateBook']
237
        );
238
239
        static::assertFalse($res->data['updateBook']['valid']);
240
    }
241
242
    public function testValidationSuccess(): void
243
    {
244
        $res = GraphQL::executeQuery(
245
            $this->schema,
246
            Utils::nowdoc('
247
                mutation UpdateBook(
248
                    $bookAttributes: BookAttributes
249
                ) {
250
                    updateBook (
251
                        bookAttributes: $bookAttributes
252
                    ) {
253
                        valid
254
                        suberrors {
255
                            bookAttributes {
256
                                suberrors {
257
                                    title {
258
                                        code
259
                                        msg
260
                                    }
261
                                    author {
262
                                        code
263
                                        msg
264
                                    }
265
                                }
266
                            }
267
                        }
268
                        result
269
                    }
270
                }
271
            '),
272
            [],
273
            null,
274
            [
275
                'bookAttributes' => [
276
                    'title' => 'Dogsbody',
277
                    'author' => 3,
278
                ],
279
            ]
280
        );
281
282
        static::assertEmpty($res->errors);
283
        static::assertEquals(
284
            [
285
                'valid' => true,
286
                'suberrors' => null,
287
                'result' => true,
288
            ],
289
            $res->data['updateBook']
290
        );
291
292
        static::assertTrue($res->data['updateBook']['valid']);
293
    }
294
295
    public function testValidationEmptyInput(): void
296
    {
297
        $res = GraphQL::executeQuery(
298
            $this->schema,
299
            Utils::nowdoc('
300
                mutation UpdateBook(
301
                        $bookAttributes: BookAttributes
302
                    ) {
303
                    updateBook (
304
                        bookAttributes: $bookAttributes
305
                    ) {
306
                        valid
307
                        suberrors {
308
                            bookAttributes {
309
                                suberrors {
310
                                    title {
311
                                        code
312
                                        msg
313
                                    }
314
                                    author {
315
                                        code
316
                                        msg
317
                                    }
318
                                }
319
                            }
320
                        }
321
                        result
322
                    }
323
                }
324
            '),
325
            [],
326
            null,
327
            ['bookAttributes' => null]
328
        );
329
330
        static::assertEmpty($res->errors);
331
        static::assertEquals(
332
            [
333
                'valid' => true,
334
                'suberrors' => null,
335
                'result' => true,
336
            ],
337
            $res->data['updateBook']
338
        );
339
340
        static::assertTrue($res->data['updateBook']['valid']);
341
    }
342
}
343