Completed
Push — master ( 5bf303...8cd23b )
by Portey
03:14
created

ParserTest::testDuplicationVariableException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
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
     * @expectedException Youshido\GraphQL\Parser\Exception\VariableTypeNotDefined
28
     */
29
    public function testTypeNotDefinedException($query)
0 ignored issues
show
Unused Code introduced by
The parameter $query 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...
30
    {
31
        $parser = new Parser();
32
        $parser->setSource('query getZuckProfile($devicePicSize: Int, $second: Int) {
33
                  user(id: 4) {
34
                    profilePic(size: $devicePicSize, test: $second, a: $c) {
35
                        url
36
                    }
37
                  }
38
                }');
39
40
        $parser->parse();
41
    }
42
43
    /**
44
     * @expectedException Youshido\GraphQL\Parser\Exception\UnusedVariableException
45
     */
46
    public function testTypeUnusedVariableException($query)
0 ignored issues
show
Unused Code introduced by
The parameter $query 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...
47
    {
48
        $parser = new Parser();
49
        $parser->setSource('query getZuckProfile($devicePicSize: Int, $second: Int) {
50
                  user(id: 4) {
51
                    profilePic(size: $devicePicSize) {
52
                        url
53
                    }
54
                  }
55
                }');
56
57
        $parser->parse();
58
    }
59
60
    /**
61
     * @expectedException Youshido\GraphQL\Parser\Exception\DuplicationVariableException
62
     */
63
    public function testDuplicationVariableException($query)
0 ignored issues
show
Unused Code introduced by
The parameter $query 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...
64
    {
65
        $parser = new Parser();
66
        $parser->setSource('query getZuckProfile($devicePicSize: Int, $second: Int, $second: Int) {
67
                  user(id: 4) {
68
                    profilePic(size: $devicePicSize, test: $second) {
69
                        url
70
                    }
71
                  }
72
                }');
73
74
        $parser->parse();
75
    }
76
77 View Code Duplication
    public function testComments()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
78
    {
79
        $query = '
80
            # asdasd "asdasdasd"
81
            # comment line 2
82
83
            query {
84
              authors (category: "#2") { #asda asd
85
                _id
86
              }
87
            }
88
        ';
89
90
91
        $parser = new Parser();
92
93
        $parser->setSource($query);
94
95
        $this->assertEquals($parser->parse(), [
96
            'queries'   => [
97
                new Query('authors', null,
98
                    [
99
                        new Argument('category', new Literal('#2'))
100
                    ],
101
                    [
102
                        new Field('_id', null),
103
                    ])
104
            ],
105
            'mutations' => [],
106
            'fragments' => []
107
        ]);
108
    }
109
110
111
    /**
112
     * @param $query string
113
     *
114
     * @dataProvider wrongQueriesProvider
115
     * @expectedException Youshido\GraphQL\Parser\Exception\SyntaxErrorException
116
     */
117
    public function testWrongQueries($query)
118
    {
119
        $parser = new Parser();
120
121
        $parser->setSource($query);
122
        $parser->parse();
123
    }
124
125
    public function testGoogleExtensionQuery()
126
    {
127
        $parser = new Parser();
128
129
        $parser->setSource('
130
            query IntrospectionQuery {
131
                __schema {
132
                    queryType { name }
133
                    mutationType { name }
134
                    types {
135
                        ...FullType
136
                    }
137
                    directives {
138
                        name
139
                        description
140
                        args {
141
                            ...InputValue
142
                        }
143
                        onOperation
144
                        onFragment
145
                        onField
146
                    }
147
                }
148
            }
149
150
            fragment FullType on __Type {
151
                kind
152
                name
153
                description
154
                fields {
155
                    name
156
                    description
157
                    args {
158
                        ...InputValue
159
                    }
160
                    type {
161
                        ...TypeRef
162
                    }
163
                    isDeprecated
164
                    deprecationReason
165
                }
166
                inputFields {
167
                    ...InputValue
168
                }
169
                interfaces {
170
                    ...TypeRef
171
                }
172
                enumValues {
173
                    name
174
                    description
175
                    isDeprecated
176
                    deprecationReason
177
                }
178
                possibleTypes {
179
                    ...TypeRef
180
                }
181
            }
182
183
            fragment InputValue on __InputValue {
184
                name
185
                description
186
                type { ...TypeRef }
187
                defaultValue
188
            }
189
190
            fragment TypeRef on __Type {
191
                kind
192
                name
193
                ofType {
194
                    kind
195
                    name
196
                    ofType {
197
                        kind
198
                        name
199
                        ofType {
200
                            kind
201
                            name
202
                        }
203
                    }
204
                }
205
            }
206
        ');
207
208
        $this->assertTrue(is_array($parser->parse()));
209
    }
210
211
212
    public function wrongQueriesProvider()
213
    {
214
        return [
215
            ['{ test { id,, asd } }'],
216
            ['{ test { id,, } }'],
217
            ['{ test (a: "asd", b: <basd>) { id }'],
218
            ['{ test (asd: [..., asd]) { id } }'],
219
            ['{ test (asd: { "a": 4, "m": null, "asd": false  "b": 5, "c" : { a }}) { id } }'],
220
            ['asdasd'],
221
            ['mutation { test(asd: ... ){ ...,asd, asd } }'],
222
            ['mutation { test( asd: $,as ){ ...,asd, asd } }']
223
        ];
224
    }
225
226
    /**
227
     * @dataProvider mutationProvider
228
     */
229
    public function testMutations($query, $structure)
230
    {
231
        $parser = new Parser();
232
        $parser->setSource($query);
233
234
        $parsedStructure = $parser->parse();
235
236
        $this->assertEquals($parsedStructure, $structure);
237
    }
238
239 View Code Duplication
    public function testTypedFragment()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
240
    {
241
        $parser = new Parser();
242
        $parser->setSource('
243
            {
244
                test: test {
245
                    name,
246
                    ... on UnionType {
247
                        unionName
248
                    }
249
                }
250
            }
251
        ');
252
253
        $parsedStructure = $parser->parse();
254
255
        $this->assertEquals($parsedStructure, [
256
            'queries'   => [
257
                new Query('test', 'test', [],
258
                    [
259
                        new Field('name', null),
260
                        new TypedFragmentReference('UnionType', [
261
                            new Field('unionName')
262
                        ])
263
                    ])
264
            ],
265
            'mutations' => [],
266
            'fragments' => []
267
        ]);
268
    }
269
270
    public function mutationProvider()
271
    {
272
        return [
273
            [
274
                'query ($variable: Int){ query ( teas: $variable ) { alias: name } }',
275
                [
276
                    'queries'   => [
277
                        new Query('query', null,
278
                            [
279
                                new Argument('teas', new Variable('variable', 'Int'))
280
                            ],
281
                            [
282
                                new Field('name', 'alias')
283
                            ])
284
                    ],
285
                    'mutations' => [],
286
                    'fragments' => []
287
                ]
288
            ],
289
            [
290
                '{ query { alias: name } }',
291
                [
292
                    'queries'   => [
293
                        new Query('query', null, [], [new Field('name', 'alias')])
294
                    ],
295
                    'mutations' => [],
296
                    'fragments' => []
297
                ]
298
            ],
299
            [
300
                'mutation { createUser ( email: "[email protected]", active: true ) { id } }',
301
                [
302
                    'queries'   => [],
303
                    'mutations' => [
304
                        new Mutation(
305
                            'createUser',
306
                            null,
307
                            [
308
                                new Argument('email', new Literal('[email protected]')),
309
                                new Argument('active', new Literal(true)),
310
                            ],
311
                            [
312
                                new Field('id')
313
                            ]
314
                        )
315
                    ],
316
                    'fragments' => []
317
                ]
318
            ],
319
            [
320
                'mutation { test : createUser (id: 4) }',
321
                [
322
                    'queries'   => [],
323
                    'mutations' => [
324
                        new Mutation(
325
                            'createUser',
326
                            'test',
327
                            [
328
                                new Argument('id', new Literal(4)),
329
                            ],
330
                            []
331
                        )
332
                    ],
333
                    'fragments' => []
334
                ]
335
            ]
336
        ];
337
    }
338
339
    /**
340
     * @dataProvider queryProvider
341
     */
342
    public function testParser($query, $structure)
343
    {
344
        $parser = new Parser();
345
        $parser->setSource($query);
346
347
        $parsedStructure = $parser->parse();
348
349
        $this->assertEquals($parsedStructure, $structure);
350
    }
351
352
353
    public function queryProvider()
354
    {
355
        return [
356
            [
357
                '{ test (id: -5) { id } } ',
358
                [
359
                    'queries'   => [
360
                        new Query('test', null, [
361
                            new Argument('id', new Literal(-5))
362
                        ], [
363
                            new Field('id'),
364
                        ])
365
                    ],
366
                    'mutations' => [],
367
                    'fragments' => []
368
                ]
369
            ],
370
            [
371
                'query CheckTypeOfLuke {
372
                  hero(episode: EMPIRE) {
373
                    __typename,
374
                    name
375
                  }
376
                }',
377
                [
378
                    'queries'   => [
379
                        new Query('hero', null, [
380
                            new Argument('episode', new Literal('EMPIRE'))
381
                        ], [
382
                            new Field('__typename'),
383
                            new Field('name'),
384
                        ])
385
                    ],
386
                    'mutations' => [],
387
                    'fragments' => []
388
                ]
389
            ],
390
            [
391
                '{ test { __typename, id } }',
392
                [
393
                    'queries'   => [
394
                        new Query('test', null, [], [
395
                            new Field('__typename'),
396
                            new Field('id'),
397
                        ])
398
                    ],
399
                    'mutations' => [],
400
                    'fragments' => []
401
                ]
402
            ],
403
            [
404
                '{}',
405
                [
406
                    'queries'   => [],
407
                    'mutations' => [],
408
                    'fragments' => []
409
                ]
410
            ],
411
            [
412
                'query test {}',
413
                [
414
                    'queries'   => [],
415
                    'mutations' => [],
416
                    'fragments' => []
417
                ]
418
            ],
419
            [
420
                'query {}',
421
                [
422
                    'queries'   => [],
423
                    'mutations' => [],
424
                    'fragments' => []
425
                ]
426
            ],
427
            [
428
                'mutation setName { setUserName }',
429
                [
430
                    'queries'   => [],
431
                    'mutations' => [new Mutation('setUserName')],
432
                    'fragments' => []
433
                ]
434
            ],
435
            [
436
                '{ test { ...userDataFragment } } fragment userDataFragment on User { id, name, email }',
437
                [
438
                    'queries'   => [
439
                        new Query('test', null, [], [new FragmentReference('userDataFragment')])
440
                    ],
441
                    'mutations' => [],
442
                    'fragments' => [
443
                        new Fragment('userDataFragment', 'User', [
444
                            new Field('id'),
445
                            new Field('name'),
446
                            new Field('email')
447
                        ])
448
                    ]
449
                ]
450
            ],
451
            [
452
                '{ user (id: 10, name: "max", float: 123.123 ) { id, name } }',
453
                [
454
                    'queries'   => [
455
                        new Query(
456
                            'user',
457
                            null,
458
                            [
459
                                new Argument('id', new Literal('10')),
460
                                new Argument('name', new Literal('max')),
461
                                new Argument('float', new Literal('123.123'))
462
                            ],
463
                            [
464
                                new Field('id'),
465
                                new Field('name')
466
                            ]
467
                        )
468
                    ],
469
                    'mutations' => [],
470
                    'fragments' => []
471
                ]
472
            ],
473
            [
474
                '{ allUsers : users ( id: [ 1, 2, 3] ) { id } }',
475
                [
476
                    'queries'   => [
477
                        new Query(
478
                            'users',
479
                            'allUsers',
480
                            [
481
                                new Argument('id', new InputList([1, 2, 3]))
482
                            ],
483
                            [
484
                                new Field('id')
485
                            ]
486
                        )
487
                    ],
488
                    'mutations' => [],
489
                    'fragments' => []
490
                ]
491
            ],
492
            [
493
                '{ allUsers : users ( id: [ 1, "2", true, null] ) { id } }',
494
                [
495
                    'queries'   => [
496
                        new Query(
497
                            'users',
498
                            'allUsers',
499
                            [
500
                                new Argument('id', new InputList([1, "2", true, null]))
501
                            ],
502
                            [
503
                                new Field('id')
504
                            ]
505
                        )
506
                    ],
507
                    'mutations' => [],
508
                    'fragments' => []
509
                ]
510
            ],
511
            [
512
                '{ allUsers : users ( object: { "a": 123, "d": "asd",  "b" : [ 1, 2, 4 ], "c": { "a" : 123, "b":  "asd" } } ) { id } }',
513
                [
514
                    'queries'   => [
515
                        new Query(
516
                            'users',
517
                            'allUsers',
518
                            [
519
                                new Argument('object', new InputObject([
520
                                    'a' => 123,
521
                                    'd' => 'asd',
522
                                    'b' => [1, 2, 4],
523
                                    'c' => [
524
                                        'a' => 123,
525
                                        'b' => 'asd'
526
                                    ]
527
                                ]))
528
                            ],
529
                            [
530
                                new Field('id')
531
                            ]
532
                        )
533
                    ],
534
                    'mutations' => [],
535
                    'fragments' => []
536
                ]
537
            ]
538
        ];
539
    }
540
541
}
542