Completed
Push — master ( 6bfa1f...af419f )
by Portey
05:37
created

ParserTest::testGoogleExtensionQuery()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 85
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 85
rs 8.6875
cc 1
eloc 4
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
 * Date: 01.12.15
4
 *
5
 * @author Portey Vasil <[email protected]>
6
 */
7
8
namespace Youshido\tests\GraphQL\Parser;
9
10
use Youshido\GraphQL\Parser\Ast\Argument;
11
use Youshido\GraphQL\Parser\Ast\Field;
12
use Youshido\GraphQL\Parser\Ast\Fragment;
13
use Youshido\GraphQL\Parser\Ast\FragmentReference;
14
use Youshido\GraphQL\Parser\Ast\Mutation;
15
use Youshido\GraphQL\Parser\Ast\TypedFragmentReference;
16
use Youshido\GraphQL\Parser\Value\InputList;
17
use Youshido\GraphQL\Parser\Value\InputObject;
18
use Youshido\GraphQL\Parser\Value\Literal;
19
use Youshido\GraphQL\Parser\Ast\Query;
20
use Youshido\GraphQL\Parser\Parser;
21
use Youshido\GraphQL\Parser\Value\Variable;
22
23
class ParserTest extends \PHPUnit_Framework_TestCase
24
{
25
26
    /**
27
     * @param $query string
28
     *
29
     * @dataProvider wrongQueriesProvider
30
     * @expectedException Youshido\GraphQL\Parser\SyntaxErrorException
31
     */
32
    public function testWrongQueries($query)
33
    {
34
        $parser = new Parser();
35
36
        $parser->setSource($query);
37
        $parser->parse();
38
    }
39
40
    public function testGoogleExtensionQuery()
41
    {
42
        $parser = new Parser();
43
44
        $parser->setSource('
45
            query IntrospectionQuery {
46
                __schema {
47
                    queryType { name }
48
                    mutationType { name }
49
                    types {
50
                        ...FullType
51
                    }
52
                    directives {
53
                        name
54
                        description
55
                        args {
56
                            ...InputValue
57
                        }
58
                        onOperation
59
                        onFragment
60
                        onField
61
                    }
62
                }
63
            }
64
65
            fragment FullType on __Type {
66
                kind
67
                name
68
                description
69
                fields {
70
                    name
71
                    description
72
                    args {
73
                        ...InputValue
74
                    }
75
                    type {
76
                        ...TypeRef
77
                    }
78
                    isDeprecated
79
                    deprecationReason
80
                }
81
                inputFields {
82
                    ...InputValue
83
                }
84
                interfaces {
85
                    ...TypeRef
86
                }
87
                enumValues {
88
                    name
89
                    description
90
                    isDeprecated
91
                    deprecationReason
92
                }
93
                possibleTypes {
94
                    ...TypeRef
95
                }
96
            }
97
98
            fragment InputValue on __InputValue {
99
                name
100
                description
101
                type { ...TypeRef }
102
                defaultValue
103
            }
104
105
            fragment TypeRef on __Type {
106
                kind
107
                name
108
                ofType {
109
                    kind
110
                    name
111
                    ofType {
112
                        kind
113
                        name
114
                        ofType {
115
                            kind
116
                            name
117
                        }
118
                    }
119
                }
120
            }
121
        ');
122
123
        $this->assertTrue(is_array($parser->parse()));
124
    }
125
126
127
    public function wrongQueriesProvider()
128
    {
129
        return [
130
            ['{ test { id,, asd } }'],
131
            ['{ test { id,, } }'],
132
            ['{ test (a: $a, b: <basd>) { id }'],
133
            ['{ test (asd: [..., asd]) { id } }'],
134
            ['{ test (asd: { "a": 4, b: 5}) { id } }'],
135
            ['{ test (asd: { "a": 4, "m": null, "asd": false  "b": 5, "c" : { a }}) { id } }'],
136
            ['asdasd'],
137
            ['mutation { test(asd: ... ){ ...,asd, asd } }'],
138
            ['mutation { test( asd: $,as ){ ...,asd, asd } }']
139
        ];
140
    }
141
142
    /**
143
     * @dataProvider mutationProvider
144
     */
145
    public function testMutations($query, $structure)
146
    {
147
        $parser = new Parser();
148
        $parser->setSource($query);
149
150
        $parsedStructure = $parser->parse();
151
152
        $this->assertEquals($parsedStructure, $structure);
153
    }
154
155
    public function testTypedFragment()
156
    {
157
        $parser = new Parser();
158
        $parser->setSource('
159
            {
160
                test: test {
161
                    name,
162
                    ... on UnionType {
163
                        unionName
164
                    }
165
                }
166
            }
167
        ');
168
169
        $parsedStructure = $parser->parse();
170
171
        $this->assertEquals($parsedStructure, [
172
            'queries' => [
173
                new Query('test', 'test', [],
174
                    [
175
                        new Field('name', null),
176
                        new TypedFragmentReference('UnionType', [
177
                            new Field('unionName')
178
                        ])
179
                    ])
180
            ],
181
            'mutations' => [],
182
            'fragments' => []
183
        ]);
184
    }
185
186
    public function mutationProvider()
187
    {
188
        return [
189
            [
190
                '{ query ( teas: $variable ) { alias: name } }',
191
                [
192
                    'queries' => [
193
                        new Query('query', null,
194
                            [
195
                                new Argument('teas', new Variable('variable'))
196
                            ],
197
                            [
198
                                new Field('name', 'alias')
199
                            ])
200
                    ],
201
                    'mutations' => [],
202
                    'fragments' => []
203
                ]
204
            ],
205
            [
206
                '{ query { alias: name } }',
207
                [
208
                    'queries' => [
209
                        new Query('query', null, [], [new Field('name', 'alias')])
210
                    ],
211
                    'mutations' => [],
212
                    'fragments' => []
213
                ]
214
            ],
215
            [
216
                'mutation { createUser ( email: "[email protected]", active: true ) { id } }',
217
                [
218
                    'queries'   => [],
219
                    'mutations' => [
220
                        new Mutation(
221
                            'createUser',
222
                            null,
223
                            [
224
                                new Argument('email', new Literal('[email protected]')),
225
                                new Argument('active', new Literal(true)),
226
                            ],
227
                            [
228
                                new Field('id')
229
                            ]
230
                        )
231
                    ],
232
                    'fragments' => []
233
                ]
234
            ],
235
            [
236
                'mutation { test : createUser (id: 4) }',
237
                [
238
                    'queries'   => [],
239
                    'mutations' => [
240
                        new Mutation(
241
                            'createUser',
242
                            'test',
243
                            [
244
                                new Argument('id', new Literal(4)),
245
                            ],
246
                            []
247
                        )
248
                    ],
249
                    'fragments' => []
250
                ]
251
            ]
252
        ];
253
    }
254
255
    /**
256
     * @dataProvider queryProvider
257
     */
258
    public function testParser($query, $structure)
259
    {
260
        $parser = new Parser();
261
        $parser->setSource($query);
262
263
        $parsedStructure = $parser->parse();
264
265
        $this->assertEquals($parsedStructure, $structure);
266
    }
267
268
269
    public function queryProvider()
270
    {
271
        return [
272
            [
273
                'query CheckTypeOfLuke {
274
                  hero(episode: EMPIRE) {
275
                    __typename,
276
                    name
277
                  }
278
                }',
279
                [
280
                    'queries'   => [
281
                        new Query('hero', null, [
282
                            new Argument('episode', new Literal('EMPIRE'))
283
                        ], [
284
                            new Field('__typename'),
285
                            new Field('name'),
286
                        ])
287
                    ],
288
                    'mutations' => [],
289
                    'fragments' => []
290
                ]
291
            ],
292
            [
293
                '{ test { __typename, id } }',
294
                [
295
                    'queries'   => [
296
                        new Query('test', null, [], [
297
                            new Field('__typename'),
298
                            new Field('id'),
299
                        ])
300
                    ],
301
                    'mutations' => [],
302
                    'fragments' => []
303
                ]
304
            ],
305
            [
306
                '{}',
307
                [
308
                    'queries'   => [],
309
                    'mutations' => [],
310
                    'fragments' => []
311
                ]
312
            ],
313
            [
314
                'query test {}',
315
                [
316
                    'queries'   => [],
317
                    'mutations' => [],
318
                    'fragments' => []
319
                ]
320
            ],
321
            [
322
                'query {}',
323
                [
324
                    'queries'   => [],
325
                    'mutations' => [],
326
                    'fragments' => []
327
                ]
328
            ],
329
            [
330
                'mutation setName { setUserName }',
331
                [
332
                    'queries'   => [],
333
                    'mutations' => [new Mutation('setUserName')],
334
                    'fragments' => []
335
                ]
336
            ],
337
            [
338
                '{ test { ...userDataFragment } } fragment userDataFragment on User { id, name, email }',
339
                [
340
                    'queries'   => [
341
                        new Query('test', null, [], [new FragmentReference('userDataFragment')])
342
                    ],
343
                    'mutations' => [],
344
                    'fragments' => [
345
                        new Fragment('userDataFragment', 'User', [
346
                            new Field('id'),
347
                            new Field('name'),
348
                            new Field('email')
349
                        ])
350
                    ]
351
                ]
352
            ],
353
            [
354
                '{ user (id: 10, name: "max", float: 123.123 ) { id, name } }',
355
                [
356
                    'queries'   => [
357
                        new Query(
358
                            'user',
359
                            null,
360
                            [
361
                                new Argument('id', new Literal('10')),
362
                                new Argument('name', new Literal('max')),
363
                                new Argument('float', new Literal('123.123'))
364
                            ],
365
                            [
366
                                new Field('id'),
367
                                new Field('name')
368
                            ]
369
                        )
370
                    ],
371
                    'mutations' => [],
372
                    'fragments' => []
373
                ]
374
            ],
375
            [
376
                '{ allUsers : users ( id: [ 1, 2, 3] ) { id } }',
377
                [
378
                    'queries'   => [
379
                        new Query(
380
                            'users',
381
                            'allUsers',
382
                            [
383
                                new Argument('id', new InputList([1, 2, 3]))
384
                            ],
385
                            [
386
                                new Field('id')
387
                            ]
388
                        )
389
                    ],
390
                    'mutations' => [],
391
                    'fragments' => []
392
                ]
393
            ],
394
            [
395
                '{ allUsers : users ( id: [ 1, "2", true, null] ) { id } }',
396
                [
397
                    'queries'   => [
398
                        new Query(
399
                            'users',
400
                            'allUsers',
401
                            [
402
                                new Argument('id', new InputList([1, "2", true, null]))
403
                            ],
404
                            [
405
                                new Field('id')
406
                            ]
407
                        )
408
                    ],
409
                    'mutations' => [],
410
                    'fragments' => []
411
                ]
412
            ],
413
            [
414
                '{ allUsers : users ( object: { "a": 123, "d": "asd",  "b" : [ 1, 2, 4 ], "c": { "a" : 123, "b":  "asd" } } ) { id } }',
415
                [
416
                    'queries'   => [
417
                        new Query(
418
                            'users',
419
                            'allUsers',
420
                            [
421
                                new Argument('object', new InputObject([
422
                                    'a' => 123,
423
                                    'd' => 'asd',
424
                                    'b' => [1, 2, 4],
425
                                    'c' => [
426
                                        'a' => 123,
427
                                        'b' => 'asd'
428
                                    ]
429
                                ]))
430
                            ],
431
                            [
432
                                new Field('id')
433
                            ]
434
                        )
435
                    ],
436
                    'mutations' => [],
437
                    'fragments' => []
438
                ]
439
            ]
440
        ];
441
    }
442
443
}