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

IntrospectionTest::predefinedSchemaProvider()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 129
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 129
rs 8.2857
c 0
b 0
f 0
cc 1
eloc 46
nc 1
nop 0

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
 * 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 7:52 AM
18
 */
19
20
namespace Youshido\Tests\Schema;
21
22
use Youshido\GraphQL\Execution\Processor;
23
use Youshido\GraphQL\Field\Field;
24
use Youshido\GraphQL\Type\Enum\EnumType;
25
use Youshido\GraphQL\Type\InterfaceType\InterfaceType;
26
use Youshido\GraphQL\Type\Object\ObjectType;
27
use Youshido\GraphQL\Type\Scalar\IntType;
28
use Youshido\GraphQL\Type\TypeMap;
29
use Youshido\GraphQL\Type\Union\UnionType;
30
use Youshido\Tests\DataProvider\TestEmptySchema;
31
use Youshido\Tests\DataProvider\TestSchema;
32
33
class IntrospectionTest extends \PHPUnit_Framework_TestCase
34
{
35
    private $introspectionQuery = <<<'TEXT'
36
query IntrospectionQuery {
37
                __schema {
38
                    queryType { name }
39
                    mutationType { name }
40
                    types {
41
                        ...FullType
42
                    }
43
                    directives {
44
                        name
45
                        description
46
                        args {
47
                            ...InputValue
48
                        }
49
                        onOperation
50
                        onFragment
51
                        onField
52
                    }
53
                }
54
            }
55
56
            fragment FullType on __Type {
57
                kind
58
                name
59
                description
60
                fields {
61
                    name
62
                    description
63
                    args {
64
                        ...InputValue
65
                    }
66
                    type {
67
                        ...TypeRef
68
                    }
69
                    isDeprecated
70
                    deprecationReason
71
                }
72
                inputFields {
73
                    ...InputValue
74
                }
75
                interfaces {
76
                    ...TypeRef
77
                }
78
                enumValues {
79
                    name
80
                    description
81
                    isDeprecated
82
                    deprecationReason
83
                }
84
                possibleTypes {
85
                    ...TypeRef
86
                }
87
            }
88
89
            fragment InputValue on __InputValue {
90
                name
91
                description
92
                type { ...TypeRef }
93
                defaultValue
94
            }
95
96
            fragment TypeRef on __Type {
97
                kind
98
                name
99
                ofType {
100
                    kind
101
                    name
102
                    ofType {
103
                        kind
104
                        name
105
                        ofType {
106
                            kind
107
                            name
108
                        }
109
                    }
110
                }
111
            }
112
TEXT;
113
114
    public function testIntrospectionDirectiveRequest(): void
115
    {
116
        $processor = new Processor(new TestSchema());
117
118
        $processor->processPayload($this->introspectionQuery, []);
119
120
        $this->assertInternalType('array', $processor->getResponseData());
121
    }
122
123
    /**
124
     * @param $query
125
     * @param $expectedResponse
126
     *
127
     * @dataProvider predefinedSchemaProvider
128
     */
129
    public function testPredefinedQueries($query, $expectedResponse): void
130
    {
131
        $schema = new TestEmptySchema();
132
        $schema->addQueryField(new Field([
133
            'name' => 'latest',
134
            'type' => new ObjectType([
135
                'name'   => 'LatestType',
136
                'fields' => [
137
                    'id'   => ['type' => TypeMap::TYPE_INT],
138
                    'name' => ['type' => TypeMap::TYPE_STRING],
139
                ],
140
            ]),
141
            'args' => [
142
                'id'  => ['type' => TypeMap::TYPE_INT, 'defaultValue' => 'test'],
143
                'id2' => ['type' => TypeMap::TYPE_INT],
144
            ],
145
            'description'       => 'latest description',
146
            'deprecationReason' => 'for test',
147
            'isDeprecated'      => true,
148
            'resolve'           => static function () {
149
                return [
150
                    'id'   => 1,
151
                    'name' => 'Alex',
152
                ];
153
            },
154
        ]));
155
156
        $processor = new Processor($schema);
157
158
        $processor->processPayload($query);
159
        $responseData = $processor->getResponseData();
160
161
        $this->assertEquals($expectedResponse, $responseData);
162
    }
163
164
    public function predefinedSchemaProvider()
165
    {
166
        return [
167
            [
168
                '{ __type { name } }',
169
                [
170
                    'data'   => ['__type' => null],
171
                    'errors' => [['message' => 'Require "name" arguments to query "__type"']],
172
                ],
173
            ],
174
            [
175
                '{ __type (name: "__Type") { name } }',
176
                [
177
                    'data' => [
178
                        '__type' => ['name' => '__Type'],
179
                    ],
180
                ],
181
            ],
182
            [
183
                '{ __type (name: "InvalidName") { name } }',
184
                [
185
                    'data' => [
186
                        '__type' => null,
187
                    ],
188
                ],
189
            ],
190
            [
191
                '{
192
                    __schema {
193
                        types {
194
                            name,
195
                            fields (includeDeprecated: true) {
196
                                name
197
                                args {
198
                                    defaultValue
199
                                }
200
                            }
201
                        }
202
                    }
203
                }',
204
                [
205
                    'data' => [
206
                        '__schema' => [
207
                            'types' => [
208
                                ['name' => 'TestSchemaQuery', 'fields' => [['name' => 'latest', 'args' => [['defaultValue' => 'test'], ['defaultValue' => null]]]]],
209
                                ['name' => 'Int', 'fields' => null],
210
                                ['name' => 'LatestType', 'fields' => [['name' => 'id', 'args' => []], ['name' => 'name', 'args' => []]]],
211
                                ['name' => 'String', 'fields' => null],
212
                                ['name' => '__Schema', 'fields' => [['name' => 'queryType', 'args' => []], ['name' => 'mutationType', 'args' => []], ['name' => 'subscriptionType', 'args' => []], ['name' => 'types', 'args' => []], ['name' => 'directives', 'args' => []]]],
213
                                ['name' => '__Type', 'fields' => [['name' => 'name', 'args' => []], ['name' => 'kind', 'args' => []], ['name' => 'description', 'args' => []], ['name' => 'ofType', 'args' => []], ['name' => 'inputFields', 'args' => []], ['name' => 'enumValues', 'args' => [['defaultValue' => 'false']]], ['name' => 'fields', 'args' => [['defaultValue' => 'false']]], ['name' => 'interfaces', 'args' => []], ['name' => 'possibleTypes', 'args' => []]]],
214
                                ['name' => '__InputValue', 'fields' => [['name' => 'name', 'args' => []], ['name' => 'description', 'args' => []], ['name' => 'isDeprecated', 'args' => []], ['name' => 'deprecationReason', 'args' => []], ['name' => 'type', 'args' => []], ['name' => 'defaultValue', 'args' => []]]],
215
                                ['name' => 'Boolean', 'fields' => null],
216
                                ['name' => '__EnumValue', 'fields' => [['name' => 'name', 'args' => []], ['name' => 'description', 'args' => []], ['name' => 'deprecationReason', 'args' => []], ['name' => 'isDeprecated', 'args' => []]]],
217
                                ['name' => '__Field', 'fields' => [['name' => 'name', 'args' => []], ['name' => 'description', 'args' => []], ['name' => 'isDeprecated', 'args' => []], ['name' => 'deprecationReason', 'args' => []], ['name' => 'type', 'args' => []], ['name' => 'args', 'args' => []]]],
218
                                ['name' => '__Directive', 'fields' => [['name' => 'name', 'args' => []], ['name' => 'description', 'args' => []], ['name' => 'args', 'args' => []], ['name' => 'locations', 'args' => []]]],
219
                                ['name' => '__DirectiveLocation', 'fields' => null],
220
                            ],
221
                        ],
222
                    ],
223
                ],
224
            ],
225
            [
226
                '{
227
                  test : __schema {
228
                    queryType {
229
                      kind,
230
                      name,
231
                      fields (includeDeprecated: true) {
232
                        name,
233
                        isDeprecated,
234
                        deprecationReason,
235
                        description,
236
                        type {
237
                          name
238
                        }
239
                      }
240
                    }
241
                  }
242
                }',
243
                ['data' => [
244
                    'test' => [
245
                        'queryType' => [
246
                            'name'   => 'TestSchemaQuery',
247
                            'kind'   => 'OBJECT',
248
                            'fields' => [
249
                                ['name' => 'latest', 'isDeprecated' => true, 'deprecationReason' => 'for test', 'description' => 'latest description', 'type' => ['name' => 'LatestType']],
250
                            ],
251
                        ],
252
                    ],
253
                ]],
254
            ],
255
            [
256
                '{
257
                  __schema {
258
                    queryType {
259
                      kind,
260
                      name,
261
                      description,
262
                      interfaces {
263
                        name
264
                      },
265
                      possibleTypes {
266
                        name
267
                      },
268
                      inputFields {
269
                        name
270
                      },
271
                      ofType{
272
                        name
273
                      }
274
                    }
275
                  }
276
                }',
277
                ['data' => [
278
                    '__schema' => [
279
                        'queryType' => [
280
                            'kind'          => 'OBJECT',
281
                            'name'          => 'TestSchemaQuery',
282
                            'description'   => null,
283
                            'interfaces'    => [],
284
                            'possibleTypes' => null,
285
                            'inputFields'   => null,
286
                            'ofType'        => null,
287
                        ],
288
                    ],
289
                ]],
290
            ],
291
        ];
292
    }
293
294
    public function testCombinedFields(): void
295
    {
296
        $schema = new TestEmptySchema();
297
298
        $interface = new InterfaceType([
299
            'name'   => 'TestInterface',
300
            'fields' => [
301
                'id'   => ['type' => new IntType()],
302
                'name' => ['type' => new IntType()],
303
            ],
304
            'resolveType' => static function ($type): void {
305
            },
306
        ]);
307
308
        $object1 = new ObjectType([
309
            'name'   => 'Test1',
310
            'fields' => [
311
                'id'       => ['type' => new IntType()],
312
                'name'     => ['type' => new IntType()],
313
                'lastName' => ['type' => new IntType()],
314
            ],
315
            'interfaces' => [$interface],
316
        ]);
317
318
        $object2 = new ObjectType([
319
            'name'   => 'Test2',
320
            'fields' => [
321
                'id'        => ['type' => new IntType()],
322
                'name'      => ['type' => new IntType()],
323
                'thirdName' => ['type' => new IntType()],
324
            ],
325
            'interfaces' => [$interface],
326
        ]);
327
328
        $unionType = new UnionType([
329
            'name'        => 'UnionType',
330
            'types'       => [$object1, $object2],
331
            'resolveType' => static function (): void {
332
            },
333
        ]);
334
335
        $schema->addQueryField(new Field([
336
            'name' => 'union',
337
            'type' => $unionType,
338
            'args' => [
339
                'id' => ['type' => TypeMap::TYPE_INT],
340
            ],
341
            'resolve' => static function () {
342
                return [
343
                    'id'   => 1,
344
                    'name' => 'Alex',
345
                ];
346
            },
347
        ]));
348
349
        $schema->addMutationField(new Field([
350
            'name' => 'mutation',
351
            'type' => $unionType,
352
            'args' => [
353
                'type' => new EnumType([
354
                    'name'   => 'MutationType',
355
                    'values' => [
356
                        [
357
                            'name'  => 'Type1',
358
                            'value' => 'type_1',
359
                        ],
360
                        [
361
                            'name'  => 'Type2',
362
                            'value' => 'type_2',
363
                        ],
364
                    ],
365
                ]),
366
            ],
367
            'resolve' => static function () {
368
            },
369
        ]));
370
371
        $processor = new Processor($schema);
372
373
        $processor->processPayload($this->introspectionQuery);
374
        $responseData = $processor->getResponseData();
375
376
        /* strange that this test got broken after I fixed the field resolve behavior */
377
        $this->assertArrayNotHasKey('errors', $responseData);
378
    }
379
}
380