Completed
Push — master ( af419f...577784 )
by Portey
06:50
created

ProcessorTest::testGoogleExtensionQuery()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 86
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 86
rs 8.6584
cc 1
eloc 5
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-youshido project.
4
*
5
* @author Alexandr Viniychuk <[email protected]>
6
* created: 11/28/15 2:02 AM
7
*/
8
9
namespace Youshido\Tests;
10
11
use Youshido\GraphQL\Schema;
12
use Youshido\GraphQL\Type\Object\ObjectType;
13
use Youshido\GraphQL\Processor;
14
use Youshido\GraphQL\Type\TypeMap;
15
use Youshido\GraphQL\Validator\ResolveValidator\ResolveValidator;
16
use Youshido\Tests\DataProvider\UserType;
17
18
class ProcessorTest extends \PHPUnit_Framework_TestCase
19
{
20
21
    public function testGoogleExtensionQuery()
22
    {
23
        $processor = new Processor(new ResolveValidator());
24
        $processor->setSchema(new Schema());
25
26
        $processor->processQuery('
27
            query IntrospectionQuery {
28
                __schema {
29
                    queryType { name }
30
                    mutationType { name }
31
                    types {
32
                        ...FullType
33
                    }
34
                    directives {
35
                        name
36
                        description
37
                        args {
38
                            ...InputValue
39
                        }
40
                        onOperation
41
                        onFragment
42
                        onField
43
                    }
44
                }
45
            }
46
47
            fragment FullType on __Type {
48
                kind
49
                name
50
                description
51
                fields {
52
                    name
53
                    description
54
                    args {
55
                        ...InputValue
56
                    }
57
                    type {
58
                        ...TypeRef
59
                    }
60
                    isDeprecated
61
                    deprecationReason
62
                }
63
                inputFields {
64
                    ...InputValue
65
                }
66
                interfaces {
67
                    ...TypeRef
68
                }
69
                enumValues {
70
                    name
71
                    description
72
                    isDeprecated
73
                    deprecationReason
74
                }
75
                possibleTypes {
76
                    ...TypeRef
77
                }
78
            }
79
80
            fragment InputValue on __InputValue {
81
                name
82
                description
83
                type { ...TypeRef }
84
                defaultValue
85
            }
86
87
            fragment TypeRef on __Type {
88
                kind
89
                name
90
                ofType {
91
                    kind
92
                    name
93
                    ofType {
94
                        kind
95
                        name
96
                        ofType {
97
                            kind
98
                            name
99
                        }
100
                    }
101
                }
102
            }
103
        ', []);
104
105
        $this->assertTrue(is_array($processor->getResponseData()));
106
    }
107
108
    /**
109
     * @param $query
110
     * @param $response
111
     *
112
     * @dataProvider predefinedSchemaProvider
113
     */
114
    public function testPredefinedQueries($query, $response)
115
    {
116
        $schema = new Schema([
117
            'query' => new ObjectType([
118
                'name'        => 'TestSchema',
119
                'description' => 'Root of TestSchema'
120
            ])
121
        ]);
122
        $schema->addQuery('latest',
123
            new ObjectType(
124
                [
125
                    'name'    => 'latest',
126
                    'args'    => [
127
                        'id' => ['type' => TypeMap::TYPE_INT]
128
                    ],
129
                    'fields'  => [
130
                        'id'   => ['type' => TypeMap::TYPE_INT],
131
                        'name' => ['type' => TypeMap::TYPE_STRING]
132
                    ],
133
                    'resolve' => function () {
134
                        return [
135
                            'id'   => 1,
136
                            'name' => 'Alex'
137
                        ];
138
                    }
139
                ]),
140
            [
141
                'description' => 'latest description',
142
                'deprecationReason' => 'for test',
143
                'isDeprecated' => true,
144
            ]
145
        );
146
147
        $validator = new ResolveValidator();
148
        $processor = new Processor($validator);
149
150
        $processor->setSchema($schema);
151
152
        $processor->processQuery($query);
153
154
        $this->assertEquals($processor->getResponseData(), $response);
155
    }
156
157
    public function predefinedSchemaProvider()
158
    {
159
        return [
160
            [
161
                '{ __type { name } }',
162
                [
163
                    'errors' => ['Require "name" arguments to query "__type"']
164
                ]
165
            ],
166
            [
167
                '{ __type (name: "__Type") { name } }',
168
                [
169
                    'data' => [
170
                        '__type' => ['name' => '__Type']
171
                    ]
172
                ]
173
            ],
174
            [
175
                '{
176
                    __schema {
177
                        types {
178
                            name,
179
                            fields {
180
                                name
181
                            }
182
                        }
183
                    }
184
                }',
185
                [
186
                    'data' => [
187
                        '__schema' => [
188
                            'types' => [
189
                                ['name' => 'latest', 'fields' => [['name' => 'id'], ['name' => 'name']]],
190
                                ['name' => 'Int', 'fields' => []],
191
                                ['name' => 'String', 'fields' => []],
192
                                ['name' => '__Schema', 'fields' => [['name' => 'queryType'], ['name' => 'mutationType'], ['name' => 'types'], ['name' => 'directives']]],
193
                                ['name' => '__Type', 'fields' => [['name' => 'name'], ['name' => 'kind'], ['name' => 'description'], ['name' => 'ofType'], ['name' => 'inputFields'], ['name' => 'enumValues'], ['name' => 'fields'], ['name' => 'interfaces'], ['name' => 'possibleTypes']]],
194
                                ['name' => '__InputValue', 'fields' => [['name' => 'name'],['name' => 'description'],['name' => 'type'],['name' => 'defaultValue'],]],
195
                                ['name' => '__EnumValue', 'fields' => [['name' => 'name'],['name' => 'description'],['name' => 'deprecationReason'],['name' => 'isDeprecated'],]],
196
                                ['name' => 'Boolean', 'fields' => []],
197
                                ['name' => '__Field', 'fields' => [['name' => 'name'], ['name' => 'description'], ['name' => 'isDeprecated'], ['name' => 'deprecationReason'], ['name' => 'type'], ['name' => 'args']]],
198
                                ['name' => '__Argument', 'fields' => [['name' => 'name'], ['name' => 'type'], ['name' => 'description']]],
199
                                ['name' => '__Interface', 'fields' => [['name' => 'name'], ['name' => 'kind'], ['name' => 'description'], ['name' => 'ofType'], ['name' => 'inputFields'], ['name' => 'enumValues'], ['name' => 'fields'], ['name' => 'interfaces'], ['name' => 'possibleTypes']]],
200
                                ['name' => '__PossibleOf', 'fields' => [['name' => 'name'], ['name' => 'kind'], ['name' => 'description'], ['name' => 'ofType'], ['name' => 'inputFields'], ['name' => 'enumValues'], ['name' => 'fields'], ['name' => 'interfaces'], ['name' => 'possibleTypes']]],
201
                                ['name' => '__Directive', 'fields' => [['name' => 'name'], ['name' => 'description'], ['name' => 'args'], ['name' => 'onOperation'], ['name' => 'onFragment'], ['name' => 'onField']]],
202
                            ]
203
                        ]
204
                    ]
205
                ]
206
            ],
207
            [
208
                '{
209
                  test : __schema {
210
                    queryType {
211
                      kind,
212
                      name,
213
                      fields {
214
                        name,
215
                        isDeprecated,
216
                        deprecationReason,
217
                        description,
218
                        type {
219
                          name
220
                        }
221
                      }
222
                    }
223
                  }
224
                }',
225
                ['data' => [
226
                    'test' => [
227
                        'queryType' => [
228
                            'name'   => 'TestSchema',
229
                            'kind'   => 'OBJECT',
230
                            'fields' => [
231
                                ['name' => 'latest', 'isDeprecated' => true, 'deprecationReason' => 'for test', 'description' => 'for test', 'type' => ['name' => 'latest']],
232
                                ['name' => '__schema', 'isDeprecated' => false, 'deprecationReason' => '', 'description' => '', 'type' => ['name' => '__Schema']],
233
                                ['name' => '__type', 'isDeprecated' => false, 'deprecationReason' => '', 'description' => '', 'type' => ['name' => '__Type']]
234
                            ]
235
                        ]
236
                    ]
237
                ]]
238
            ],
239
            [
240
                '{
241
                  __schema {
242
                    queryType {
243
                      kind,
244
                      name,
245
                      description,
246
                      interfaces {
247
                        name
248
                      },
249
                      possibleTypes {
250
                        name
251
                      },
252
                      inputFields {
253
                        name
254
                      },
255
                      ofType{
256
                        name
257
                      }
258
                    }
259
                  }
260
                }',
261
                ['data' => [
262
                    '__schema' => [
263
                        'queryType' => [
264
                            'kind'          => 'OBJECT',
265
                            'name'          => 'TestSchema',
266
                            'description'   => 'Root of TestSchema',
267
                            'interfaces'    => [],
268
                            'possibleTypes' => [],
269
                            'inputFields'   => [],
270
                            'ofType'        => []
271
                        ]
272
                    ]
273
                ]]
274
            ]
275
        ];
276
    }
277
278
279
    /**
280
     * @dataProvider schemaProvider
281
     */
282
    public function testProcessor($query, $response)
283
    {
284
        $schema = new Schema();
285
        $schema->addQuery('latest',
286
            new ObjectType(
287
                [
288
                    'name'    => 'latest',
289
                    'fields'  => [
290
                        'id'   => ['type' => TypeMap::TYPE_INT],
291
                        'name' => ['type' => TypeMap::TYPE_STRING]
292
                    ],
293
                    'resolve' => function () {
294
                        return [
295
                            'id'   => 1,
296
                            'name' => 'Alex'
297
                        ];
298
                    }
299
                ]));
300
301
        $schema->addQuery('user', new UserType());
302
303
        $validator = new ResolveValidator();
304
        $processor = new Processor($validator);
305
306
        $processor->setSchema($schema);
307
        $processor->processQuery($query);
308
309
        $this->assertEquals(
310
            $processor->getResponseData(),
311
            $response
312
        );
313
    }
314
315
    public function schemaProvider()
316
    {
317
        return [
318
            [
319
                '{ latest { name } }',
320
                [
321
                    'data' => ['latest' => null]
322
                ]
323
            ],
324
            [
325
                '{ user(id:1) { id, name } }',
326
                [
327
                    'data' => ['user' => ['id' => 1, 'name' => 'John']]
328
                ]
329
            ]
330
        ];
331
    }
332
333
}
334