Completed
Pull Request — master (#204)
by Ryan
11:34
created

InputObjectTypeTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 274
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 12

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 7
lcom 1
cbo 12
dl 0
loc 274
rs 10
c 1
b 0
f 1
1
<?php
2
/**
3
 * Copyright (c) 2015–2018 Alexandr Viniychuk <http://youshido.com>.
4
 * Copyright (c) 2015–2018 Portey Vasil <https://github.com/portey>.
5
 * Copyright (c) 2018 Ryan Parman <https://github.com/skyzyx>.
6
 * Copyright (c) 2018 Ashley Hutson <https://github.com/asheliahut>.
7
 * Copyright (c) 2015–2018 Contributors.
8
 *
9
 * http://opensource.org/licenses/MIT
10
 */
11
12
declare(strict_types=1);
13
/*
14
 * This file is a part of GraphQL project.
15
 *
16
 * @author Alexandr Viniychuk <[email protected]>
17
 * created: 5/15/16 3:28 PM
18
 */
19
20
namespace Youshido\Tests\Library\Type;
21
22
use Youshido\GraphQL\Execution\Processor;
23
use Youshido\GraphQL\Schema\Schema;
24
use Youshido\GraphQL\Type\InputObject\InputObjectType;
25
use Youshido\GraphQL\Type\ListType\ListType;
26
use Youshido\GraphQL\Type\NonNullType;
27
use Youshido\GraphQL\Type\Object\ObjectType;
28
use Youshido\GraphQL\Type\Scalar\BooleanType;
29
use Youshido\GraphQL\Type\Scalar\IntType;
30
use Youshido\GraphQL\Type\Scalar\StringType;
31
use Youshido\GraphQL\Type\TypeMap;
32
use Youshido\Tests\DataProvider\TestInputObjectType;
33
34
class InputObjectTypeTest extends \PHPUnit_Framework_TestCase
35
{
36
    public function testInternal(): void
37
    {
38
        $inputObjectType = new InputObjectType([
39
            'name'   => 'PostData',
40
            'fields' => [
41
                'title' => new NonNullType(new StringType()),
42
            ],
43
        ]);
44
        $this->assertEquals(TypeMap::KIND_INPUT_OBJECT, $inputObjectType->getKind());
45
        $this->assertEquals('PostData', $inputObjectType->getName());
46
47
        $this->assertFalse($inputObjectType->isValidValue('invalid value'));
48
        $this->assertTrue($inputObjectType->isValidValue(['title' => 'Super ball!']));
49
        $this->assertFalse($inputObjectType->isValidValue(['title' => null]));
50
    }
51
52
    public function testStandaloneClass(): void
53
    {
54
        $inputObjectType = new TestInputObjectType();
55
        $this->assertEquals('TestInputObject', $inputObjectType->getName());
56
    }
57
58
    public function testListOfInputWithNonNull(): void
59
    {
60
        $processor = new Processor(new Schema([
61
            'query' => new ObjectType([
62
                'name'   => 'RootQuery',
63
                'fields' => [
64
                    'empty' => [
65
                        'type'    => new StringType(),
66
                        'resolve' => static function () {
67
                        },
68
                    ],
69
                ],
70
            ]),
71
            'mutation' => new ObjectType([
72
                'name'   => 'RootMutation',
73
                'fields' => [
74
                    'createList' => [
75
                        'args' => [
76
                            'posts' => new ListType(new InputObjectType([
77
                                'name'   => 'PostInputType',
78
                                'fields' => [
79
                                    'title' => new NonNullType(new StringType()),
80
                                ],
81
                            ])),
82
                        ],
83
                        'type'    => new BooleanType(),
84
                        'resolve' => static function ($object, $args) {
85
                            return true;
86
                        },
87
                    ],
88
                ],
89
            ]),
90
        ]));
91
92
        $processor->processPayload('mutation { createList(posts: [{title: null }, {}]) }');
93
        $this->assertEquals(
94
            [
95
                'data'   => ['createList' => null],
96
                'errors' => [[
97
                    'message'   => 'Not valid type for argument "posts" in query "createList": Not valid type for field "title" in input type "PostInputType": Field must not be NULL',
98
                    'locations' => [
99
                        [
100
                            'line'   => 1,
101
                            'column' => 23,
102
                        ],
103
                    ],
104
                ]],
105
            ],
106
            $processor->getResponseData()
107
        );
108
    }
109
110
    public function testNullableInputWithNonNull(): void
111
    {
112
        $processor = new Processor(new Schema([
113
            'query' => new ObjectType([
114
                'name'   => 'RootQuery',
115
                'fields' => [
116
                    'empty' => [
117
                        'type'    => new StringType(),
118
                        'resolve' => static function () {
119
                        },
120
                    ],
121
                ],
122
            ]),
123
            'mutation' => new ObjectType([
124
                'name'   => 'RootMutation',
125
                'fields' => [
126
                    'createAuthor' => [
127
                        'args' => [
128
                            'author' => new InputObjectType([
129
                                'name'   => 'AuthorInputType',
130
                                'fields' => [
131
                                    'name' => new NonNullType(new StringType()),
132
                                ],
133
                            ]),
134
                        ],
135
                        'type'    => new BooleanType(),
136
                        'resolve' => static function ($object, $args) {
137
                            return true;
138
                        },
139
                    ],
140
                ],
141
            ]),
142
        ]));
143
        $processor->processPayload('mutation { createAuthor(author: null) }');
144
        $this->assertEquals(
145
            [
146
                'data' => ['createAuthor' => true],
147
            ],
148
            $processor->getResponseData()
149
        );
150
    }
151
152
    public function testListInsideInputObject(): void
153
    {
154
        $processor = new Processor(new Schema([
155
            'query' => new ObjectType([
156
                'name'   => 'RootQueryType',
157
                'fields' => [
158
                    'empty' => [
159
                        'type'    => new StringType(),
160
                        'resolve' => static function (): void {
161
                        },
162
                    ],
163
                ],
164
            ]),
165
            'mutation' => new ObjectType([
166
                'name'   => 'RootMutation',
167
                'fields' => [
168
                    'createList' => [
169
                        'type' => new StringType(),
170
                        'args' => [
171
                            'topArgument' => new InputObjectType([
172
                                'name'   => 'topArgument',
173
                                'fields' => [
174
                                    'postObject' => new ListType(new InputObjectType([
175
                                        'name'   => 'postObject',
176
                                        'fields' => [
177
                                            'title' => new NonNullType(new StringType()),
178
                                        ],
179
                                    ])),
180
                                ],
181
                            ]),
182
                        ],
183
                        'resolve' => static function () {
184
                            return 'success message';
185
                        },
186
                    ],
187
                ],
188
            ]),
189
        ]));
190
        $processor->processPayload('mutation { createList(topArgument: { postObject:[ { title: null } ] })}');
191
        $this->assertEquals([
192
            'data'   => ['createList' => null],
193
            'errors' => [[
194
                'message'   => 'Not valid type for argument "topArgument" in query "createList": Not valid type for field "postObject" in input type "topArgument": Not valid type for field "title" in input type "postObject": Field must not be NULL',
195
                'locations' => [
196
                    [
197
                        'line'   => 1,
198
                        'column' => 23,
199
                    ],
200
                ],
201
            ]],
202
        ], $processor->getResponseData());
203
        $processor->getExecutionContext()->clearErrors();
204
        $processor->processPayload('mutation { createList(topArgument:{
205
                                        postObject:[{title: "not empty"}] })}');
206
        $this->assertEquals(['data' => ['createList' => 'success message']], $processor->getResponseData());
207
    }
208
209
    public function testInputObjectDefaultValue(): void
210
    {
211
        $processor = new Processor(new Schema([
212
            'query' => new ObjectType([
213
                'name'   => 'RootQuery',
214
                'fields' => [
215
                    'cities' => [
216
                        'type' => new ListType(new StringType()),
217
                        'args' => [
218
                            'paging' => [
219
                                'type' => new InputObjectType([
220
                                    'name'   => 'paging',
221
                                    'fields' => [
222
                                        'limit'  => new IntType(),
223
                                        'offset' => new IntType(),
224
                                    ],
225
                                ]),
226
                                'defaultValue' => [
227
                                    'limit'  => 10,
228
                                    'offset' => 0,
229
                                ],
230
                            ],
231
                        ],
232
                        'resolve' => static function ($source, $args) {
233
                            return [
234
                                'limit is ' . $args['paging']['limit'],
235
                                'offset is ' . $args['paging']['offset'],
236
                            ];
237
                        },
238
                    ],
239
240
                ],
241
            ]),
242
        ]));
243
        $processor->processPayload('{ cities }');
244
        $response = $processor->getResponseData();
245
        $this->assertEquals(
246
            [
247
                'data' => ['cities' => [
248
                    'limit is 10',
249
                    'offset is 0',
250
                ]],
251
            ],
252
            $response
253
        );
254
    }
255
256
    public function testInvalidTypeErrors(): void
257
    {
258
        $processor = new Processor(new Schema([
259
            'query' => new ObjectType([
260
                'name'   => 'RootQuery',
261
                'fields' => [
262
                    'hello' => new StringType(),
263
                ],
264
            ]),
265
            'mutation' => new ObjectType([
266
                'name'   => 'RootMutation',
267
                'fields' => [
268
                    'createPost' => [
269
                        'type' => new StringType(),
270
                        'args' => [
271
                            'object' => new InputObjectType([
272
                                'name'   => 'InputPostType',
273
                                'fields' => [
274
                                    'title'  => new NonNullType(new StringType()),
275
                                    'userId' => new NonNullType(new IntType()),
276
                                ],
277
                            ]),
278
                        ],
279
                        'resolve' => static function ($source, $args) {
280
                            return \sprintf('%s by %s', $args['title'], $args['userId']);
281
                        },
282
                    ],
283
284
                ],
285
            ]),
286
        ]));
287
        $processor->processPayload('mutation { createPost(object: {title: "Hello world"}) }');
288
        $response = $processor->getResponseData();
289
        $this->assertEquals(
290
            [
291
                'data'   => ['createPost' => null],
292
                'errors' => [[
293
                    'message'   => 'Not valid type for argument "object" in query "createPost": userId is required on InputPostType',
294
                    'locations' => [
295
                        ['line' => 1, 'column' => 23],
296
                    ],
297
                ]],
298
            ],
299
            $response
300
        );
301
    }
302
}
303