Completed
Push — master ( d52ee7...e7842c )
by Alexandr
03:54
created

IntrospectionTest::predefinedSchemaProvider()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 125
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 125
rs 8.2857
cc 1
eloc 45
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
* This file is a part of GraphQL project.
4
*
5
* @author Alexandr Viniychuk <[email protected]>
6
* created: 5/15/16 7:52 AM
7
*/
8
9
namespace Youshido\Tests\Schema;
10
11
12
use Youshido\GraphQL\Execution\Processor;
13
use Youshido\GraphQL\Field\Field;
14
use Youshido\GraphQL\Type\Enum\EnumType;
15
use Youshido\GraphQL\Type\InterfaceType\InterfaceType;
16
use Youshido\GraphQL\Type\Object\ObjectType;
17
use Youshido\GraphQL\Type\Scalar\IntType;
18
use Youshido\GraphQL\Type\TypeMap;
19
use Youshido\GraphQL\Type\Union\UnionType;
20
use Youshido\Tests\DataProvider\TestEmptySchema;
21
use Youshido\Tests\DataProvider\TestSchema;
22
23
class IntrospectionTest extends \PHPUnit_Framework_TestCase
24
{
25
    private $introspectionQuery = <<<TEXT
26
query IntrospectionQuery {
27
                __schema {
28
                    queryType { name }
29
                    mutationType { name }
30
                    types {
31
                        ...FullType
32
                    }
33
                    directives {
34
                        name
35
                        description
36
                        args {
37
                            ...InputValue
38
                        }
39
                        onOperation
40
                        onFragment
41
                        onField
42
                    }
43
                }
44
            }
45
46
            fragment FullType on __Type {
47
                kind
48
                name
49
                description
50
                fields {
51
                    name
52
                    description
53
                    args {
54
                        ...InputValue
55
                    }
56
                    type {
57
                        ...TypeRef
58
                    }
59
                    isDeprecated
60
                    deprecationReason
61
                }
62
                inputFields {
63
                    ...InputValue
64
                }
65
                interfaces {
66
                    ...TypeRef
67
                }
68
                enumValues {
69
                    name
70
                    description
71
                    isDeprecated
72
                    deprecationReason
73
                }
74
                possibleTypes {
75
                    ...TypeRef
76
                }
77
            }
78
79
            fragment InputValue on __InputValue {
80
                name
81
                description
82
                type { ...TypeRef }
83
                defaultValue
84
            }
85
86
            fragment TypeRef on __Type {
87
                kind
88
                name
89
                ofType {
90
                    kind
91
                    name
92
                    ofType {
93
                        kind
94
                        name
95
                        ofType {
96
                            kind
97
                            name
98
                        }
99
                    }
100
                }
101
            }
102
TEXT;
103
104
105
    public function testIntrospectionDirectiveRequest()
106
    {
107
        $processor = new Processor(new TestSchema());
108
109
        $processor->processPayload($this->introspectionQuery, []);
110
111
        $this->assertTrue(is_array($processor->getResponseData()));
112
    }
113
114
    /**
115
     * @param $query
116
     * @param $expectedResponse
117
     *
118
     * @dataProvider predefinedSchemaProvider
119
     */
120
    public function testPredefinedQueries($query, $expectedResponse)
121
    {
122
        $schema = new TestEmptySchema();
123
        $schema->addQueryField(new Field([
124
            'name'              => 'latest',
125
            'type'              => new ObjectType([
126
                'name'   => 'LatestType',
127
                'fields' => [
128
                    'id'   => ['type' => TypeMap::TYPE_INT],
129
                    'name' => ['type' => TypeMap::TYPE_STRING]
130
                ],
131
            ]),
132
            'args'              => [
133
                'id' => ['type' => TypeMap::TYPE_INT]
134
            ],
135
            'description'       => 'latest description',
136
            'deprecationReason' => 'for test',
137
            'isDeprecated'      => true,
138
            'resolve'           => function () {
139
                return [
140
                    'id'   => 1,
141
                    'name' => 'Alex'
142
                ];
143
            }
144
        ]));
145
146
        $processor = new Processor($schema);
147
148
        $processor->processPayload($query);
149
        $responseData = $processor->getResponseData();
150
151
        $this->assertEquals($expectedResponse, $responseData);
152
    }
153
154
    public function predefinedSchemaProvider()
155
    {
156
        return [
157
            [
158
                '{ __type { name } }',
159
                [
160
                    'errors' => [['message' => 'Require "name" arguments to query "__type"']]
161
                ]
162
            ],
163
            [
164
                '{ __type (name: "__Type") { name } }',
165
                [
166
                    'data' => [
167
                        '__type' => ['name' => '__Type']
168
                    ]
169
                ]
170
            ],
171
            [
172
                '{ __type (name: "InvalidName") { name } }',
173
                [
174
                    'data' => [
175
                        '__type' => null
176
                    ]
177
                ]
178
            ],
179
            [
180
                '{
181
                    __schema {
182
                        types {
183
                            name,
184
                            fields {
185
                                name
186
                            }
187
                        }
188
                    }
189
                }',
190
                [
191
                    'data' => [
192
                        '__schema' => [
193
                            'types' => [
194
                                ['name' => 'TestSchemaQuery', 'fields' => [['name' => 'latest']]],
195
                                ['name' => 'Int', 'fields' => null],
196
                                ['name' => 'LatestType', 'fields' => [['name' => 'id'], ['name' => 'name']]],
197
                                ['name' => 'String', 'fields' => null],
198
                                ['name' => '__Schema', 'fields' => [['name' => 'queryType'], ['name' => 'mutationType'], ['name' => 'subscriptionType'], ['name' => 'types'], ['name' => 'directives']]],
199
                                ['name' => '__Type', 'fields' => [['name' => 'name'], ['name' => 'kind'], ['name' => 'description'], ['name' => 'ofType'], ['name' => 'inputFields'], ['name' => 'enumValues'], ['name' => 'fields'], ['name' => 'interfaces'], ['name' => 'possibleTypes']]],
200
                                ['name' => '__InputValue', 'fields' => [['name' => 'name'], ['name' => 'description'], ['name' => 'type'], ['name' => 'defaultValue'],]],
201
                                ['name' => '__EnumValue', 'fields' => [['name' => 'name'], ['name' => 'description'], ['name' => 'deprecationReason'], ['name' => 'isDeprecated'],]],
202
                                ['name' => 'Boolean', 'fields' => null],
203
                                ['name' => '__Field', 'fields' => [['name' => 'name'], ['name' => 'description'], ['name' => 'isDeprecated'], ['name' => 'deprecationReason'], ['name' => 'type'], ['name' => 'args']]],
204
                                ['name' => '__Subscription', 'fields' => [['name' => 'name']]],
205
                                ['name' => '__Directive', 'fields' => [['name' => 'name'], ['name' => 'description'], ['name' => 'args'], ['name' => 'onOperation'], ['name' => 'onFragment'], ['name' => 'onField']]],
206
                            ]
207
                        ]
208
                    ]
209
                ]
210
            ],
211
            [
212
                '{
213
                  test : __schema {
214
                    queryType {
215
                      kind,
216
                      name,
217
                      fields {
218
                        name,
219
                        isDeprecated,
220
                        deprecationReason,
221
                        description,
222
                        type {
223
                          name
224
                        }
225
                      }
226
                    }
227
                  }
228
                }',
229
                ['data' => [
230
                    'test' => [
231
                        'queryType' => [
232
                            'name'   => 'TestSchemaQuery',
233
                            'kind'   => 'OBJECT',
234
                            'fields' => [
235
                                ['name' => 'latest', 'isDeprecated' => true, 'deprecationReason' => 'for test', 'description' => 'latest description', 'type' => ['name' => 'LatestType']]
236
                            ]
237
                        ]
238
                    ]
239
                ]]
240
            ],
241
            [
242
                '{
243
                  __schema {
244
                    queryType {
245
                      kind,
246
                      name,
247
                      description,
248
                      interfaces {
249
                        name
250
                      },
251
                      possibleTypes {
252
                        name
253
                      },
254
                      inputFields {
255
                        name
256
                      },
257
                      ofType{
258
                        name
259
                      }
260
                    }
261
                  }
262
                }',
263
                ['data' => [
264
                    '__schema' => [
265
                        'queryType' => [
266
                            'kind'          => 'OBJECT',
267
                            'name'          => 'TestSchemaQuery',
268
                            'description'   => null,
269
                            'interfaces'    => [],
270
                            'possibleTypes' => null,
271
                            'inputFields'   => null,
272
                            'ofType'        => null
273
                        ]
274
                    ]
275
                ]]
276
            ]
277
        ];
278
    }
279
280
    public function testCombinedFields()
281
    {
282
        $schema = new TestEmptySchema();
283
284
        $interface = new InterfaceType([
285
            'name'        => 'TestInterface',
286
            'fields'      => [
287
                'id'   => ['type' => new IntType()],
288
                'name' => ['type' => new IntType()],
289
            ],
290
            'resolveType' => function ($type) {
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed.

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

Loading history...
291
292
            }
293
        ]);
294
295
        $object1 = new ObjectType([
296
            'name'       => 'Test1',
297
            'fields'     => [
298
                'id'       => ['type' => new IntType()],
299
                'name'     => ['type' => new IntType()],
300
                'lastName' => ['type' => new IntType()],
301
            ],
302
            'interfaces' => [$interface]
303
        ]);
304
305
        $object2 = new ObjectType([
306
            'name'       => 'Test2',
307
            'fields'     => [
308
                'id'        => ['type' => new IntType()],
309
                'name'      => ['type' => new IntType()],
310
                'thirdName' => ['type' => new IntType()],
311
            ],
312
            'interfaces' => [$interface]
313
        ]);
314
315
        $unionType = new UnionType([
316
            'name'        => 'UnionType',
317
            'types'       => [$object1, $object2],
318
            'resolveType' => function () {
319
320
            }
321
        ]);
322
323
        $schema->addQueryField(new Field([
324
            'name'    => 'union',
325
            'type'    => $unionType,
326
            'args'    => [
327
                'id' => ['type' => TypeMap::TYPE_INT]
328
            ],
329
            'resolve' => function () {
330
                return [
331
                    'id'   => 1,
332
                    'name' => 'Alex'
333
                ];
334
            }
335
        ]));
336
337
        $schema->addMutationField(new Field([
338
            'name'    => 'mutation',
339
            'type'    => $unionType,
340
            'args'    => [
341
                'type' => new EnumType([
342
                    'name'   => 'MutationType',
343
                    'values' => [
344
                        [
345
                            'name'  => 'Type1',
346
                            'value' => 'type_1'
347
                        ],
348
                        [
349
                            'name'  => 'Type2',
350
                            'value' => 'type_2'
351
                        ]
352
                    ]
353
                ])
354
            ],
355
            'resolve' => function () {
356
                return null;
357
            }
358
        ]));
359
360
        $processor = new Processor($schema);
361
362
        $processor->processPayload($this->introspectionQuery);
363
        $responseData = $processor->getResponseData();
364
365
        /** strange that this test got broken after I fixed the field resolve behavior */
366
        $this->assertArrayNotHasKey('errors', $responseData);
367
    }
368
369
}
370