Completed
Push — master ( e13501...8c19ea )
by Alexandr
05:24
created

ParserTest::testFragmentWithFields()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 15

Duplication

Lines 26
Ratio 100 %

Importance

Changes 0
Metric Value
dl 26
loc 26
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 15
nc 1
nop 0
1
<?php
2
/**
3
 * Date: 01.12.15
4
 *
5
 * @author Portey Vasil <[email protected]>
6
 */
7
8
namespace Youshido\Tests\Parser;
9
10
use Youshido\GraphQL\Parser\Ast\Argument;
11
use Youshido\GraphQL\Parser\Ast\ArgumentValue\InputList;
12
use Youshido\GraphQL\Parser\Ast\ArgumentValue\InputObject;
13
use Youshido\GraphQL\Parser\Ast\ArgumentValue\Literal;
14
use Youshido\GraphQL\Parser\Ast\ArgumentValue\Variable;
15
use Youshido\GraphQL\Parser\Ast\ArgumentValue\VariableReference;
16
use Youshido\GraphQL\Parser\Ast\Field;
17
use Youshido\GraphQL\Parser\Ast\Fragment;
18
use Youshido\GraphQL\Parser\Ast\FragmentReference;
19
use Youshido\GraphQL\Parser\Ast\Mutation;
20
use Youshido\GraphQL\Parser\Ast\Query;
21
use Youshido\GraphQL\Parser\Ast\TypedFragmentReference;
22
use Youshido\GraphQL\Parser\Parser;
23
24
class ParserTest extends \PHPUnit_Framework_TestCase
25
{
26
27 View Code Duplication
    public function testEmptyParser()
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...
28
    {
29
        $parser = new Parser();
30
31
        $this->assertEquals([
32
            'queries'            => [],
33
            'mutations'          => [],
34
            'fragments'          => [],
35
            'fragmentReferences' => [],
36
            'variables'          => [],
37
            'variableReferences' => [],
38
        ], $parser->parse());
39
    }
40
41
    /**
42
     * @expectedException Youshido\GraphQL\Parser\Exception\SyntaxErrorException
43
     */
44
    public function testInvalidSelection()
45
    {
46
        $parser = new Parser();
47
        $data = $parser->parse('
0 ignored issues
show
Unused Code introduced by
$data is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
48
        {
49
            test {
50
                id
51
                image {
52
                    
53
                }
54
            }
55
        }
56
        ');
57
    }
58
59 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...
60
    {
61
        $query = '
62
            # asdasd "asdasdasd"
63
            # comment line 2
64
65
            query {
66
              authors (category: "#2") { #asda asd
67
                _id
68
              }
69
            }
70
        ';
71
72
73
        $parser = new Parser();
74
75
        $this->assertEquals($parser->parse($query), [
76
            'queries'            => [
77
                new Query('authors', null,
78
                    [
79
                        new Argument('category', new Literal('#2'))
80
                    ],
81
                    [
82
                        new Field('_id', null),
83
                    ])
84
            ],
85
            'mutations'          => [],
86
            'fragments'          => [],
87
            'fragmentReferences' => [],
88
            'variables'          => [],
89
            'variableReferences' => []
90
        ]);
91
    }
92
93
94
    /**
95
     * @param $query string
96
     *
97
     * @dataProvider wrongQueriesProvider
98
     * @expectedException Youshido\GraphQL\Parser\Exception\SyntaxErrorException
99
     */
100
    public function testWrongQueries($query)
101
    {
102
        $parser = new Parser();
103
104
        $parser->parse($query);
105
    }
106
107
    public function testQueryWithNoFields()
108
    {
109
        $parser = new Parser();
110
        $data   = $parser->parse('{ name }');
111
        $this->assertEquals([
112
            'queries'            => [
113
                new Query('name')
114
            ],
115
            'mutations'          => [],
116
            'fragments'          => [],
117
            'fragmentReferences' => [],
118
            'variables'          => [],
119
            'variableReferences' => [],
120
        ], $data);
121
    }
122
123 View Code Duplication
    public function testQueryWithFields()
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...
124
    {
125
        $parser = new Parser();
126
        $data   = $parser->parse('{ post, user { name } }');
127
        $this->assertEquals([
128
            'queries'            => [
129
                new Query('post'),
130
                new Query('user', null, [], [
131
                    new Field('name')
132
                ])
133
            ],
134
            'mutations'          => [],
135
            'fragments'          => [],
136
            'fragmentReferences' => [],
137
            'variables'          => [],
138
            'variableReferences' => [],
139
        ], $data);
140
    }
141
142 View Code Duplication
    public function testFragmentWithFields()
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...
143
    {
144
        $parser = new Parser();
145
        $data   = $parser->parse('
146
            fragment FullType on __Type {
147
                kind
148
                fields {
149
                    name
150
                }
151
            }');
152
        $this->assertEquals([
153
            'queries'            => [],
154
            'mutations'          => [],
155
            'fragments'          => [
156
                new Fragment('FullType', '__Type', [
157
                    new Field('kind'),
158
                    new Query('fields', null, [], [
159
                        new Field('name')
160
                    ])
161
                ])
162
            ],
163
            'fragmentReferences' => [],
164
            'variables'          => [],
165
            'variableReferences' => [],
166
        ], $data);
167
    }
168
169
    public function testInspectionQuery()
170
    {
171
        $parser = new Parser();
172
173
        $data = $parser->parse('
174
            query IntrospectionQuery {
175
                __schema {
176
                    queryType { name }
177
                    mutationType { name }
178
                    types {
179
                        ...FullType
180
                    }
181
                    directives {
182
                        name
183
                        description
184
                        args {
185
                            ...InputValue
186
                        }
187
                        onOperation
188
                        onFragment
189
                        onField
190
                    }
191
                }
192
            }
193
194
            fragment FullType on __Type {
195
                kind
196
                name
197
                description
198
                fields {
199
                    name
200
                    description
201
                    args {
202
                        ...InputValue
203
                    }
204
                    type {
205
                        ...TypeRef
206
                    }
207
                    isDeprecated
208
                    deprecationReason
209
                }
210
                inputFields {
211
                    ...InputValue
212
                }
213
                interfaces {
214
                    ...TypeRef
215
                }
216
                enumValues {
217
                    name
218
                    description
219
                    isDeprecated
220
                    deprecationReason
221
                }
222
                possibleTypes {
223
                    ...TypeRef
224
                }
225
            }
226
227
            fragment InputValue on __InputValue {
228
                name
229
                description
230
                type { ...TypeRef }
231
                defaultValue
232
            }
233
234
            fragment TypeRef on __Type {
235
                kind
236
                name
237
                ofType {
238
                    kind
239
                    name
240
                    ofType {
241
                        kind
242
                        name
243
                        ofType {
244
                            kind
245
                            name
246
                        }
247
                    }
248
                }
249
            }
250
        ');
251
252
        $this->assertEquals([
253
            'queries'            => [
254
                new Query('__schema', null, [], [
255
                    new Query('queryType', null, [], [
256
                        new Field('name')
257
                    ]),
258
                    new Query('mutationType', null, [], [
259
                        new Field('name')
260
                    ]),
261
                    new Query('types', null, [], [
262
                        new FragmentReference('FullType')
263
                    ]),
264
                    new Query('directives', null, [], [
265
                        new Field('name'),
266
                        new Field('description'),
267
                        new Query('args', null, [], [
268
                            new FragmentReference('InputValue'),
269
                        ]),
270
                        new Field('onOperation'),
271
                        new Field('onFragment'),
272
                        new Field('onField'),
273
                    ]),
274
                ])
275
            ],
276
            'mutations'          => [],
277
            'fragments'          => [
278
                new Fragment('FullType', '__Type', [
279
                    new Field('kind'),
280
                    new Field('name'),
281
                    new Field('description'),
282
                    new Query('fields', null, [], [
283
                        new Field('name'),
284
                        new Field('description'),
285
                        new Query('args', null, [], [
286
                            new FragmentReference('InputValue'),
287
                        ]),
288
                        new Query('type', null, [], [
289
                            new FragmentReference('TypeRef'),
290
                        ]),
291
                        new Field('isDeprecated'),
292
                        new Field('deprecationReason'),
293
                    ]),
294
                    new Query('inputFields', null, [], [
295
                        new FragmentReference('InputValue'),
296
                    ]),
297
                    new Query('interfaces', null, [], [
298
                        new FragmentReference('TypeRef'),
299
                    ]),
300
                    new Query('enumValues', null, [], [
301
                        new Field('name'),
302
                        new Field('description'),
303
304
                        new Field('isDeprecated'),
305
                        new Field('deprecationReason'),
306
                    ]),
307
                    new Query('possibleTypes', null, [], [
308
                        new FragmentReference('TypeRef'),
309
                    ]),
310
                ]),
311
                new Fragment('InputValue', '__InputValue', [
312
                    new Field('name'),
313
                    new Field('description'),
314
                    new Query('type', null, [], [
315
                        new FragmentReference('TypeRef'),
316
                    ]),
317
                    new Field('defaultValue'),
318
                ]),
319
                new Fragment('TypeRef', '__Type', [
320
                    new Field('kind'),
321
                    new Field('name'),
322
                    new Query('ofType', null, [], [
323
                        new Field('kind'),
324
                        new Field('name'),
325
                        new Query('ofType', null, [], [
326
                            new Field('kind'),
327
                            new Field('name'),
328
                            new Query('ofType', null, [], [
329
                                new Field('kind'),
330
                                new Field('name'),
331
                            ]),
332
                        ]),
333
                    ]),
334
                ]),
335
            ],
336
            'fragmentReferences' => [
337
                new FragmentReference('FullType'),
338
                new FragmentReference('InputValue'),
339
                new FragmentReference('InputValue'),
340
                new FragmentReference('TypeRef'),
341
                new FragmentReference('InputValue'),
342
                new FragmentReference('TypeRef'),
343
                new FragmentReference('TypeRef'),
344
                new FragmentReference('TypeRef'),
345
            ],
346
            'variables'          => [],
347
            'variableReferences' => []
348
        ], $data);
349
    }
350
351
    public function wrongQueriesProvider()
352
    {
353
        return [
354
            ['{ test { id,, asd } }'],
355
            ['{ test { id,, } }'],
356
            ['{ test (a: "asd", b: <basd>) { id }'],
357
            ['{ test (asd: [..., asd]) { id } }'],
358
            ['{ test (asd: { "a": 4, "m": null, "asd": false  "b": 5, "c" : { a }}) { id } }'],
359
            ['asdasd'],
360
            ['mutation { test(asd: ... ){ ...,asd, asd } }'],
361
            ['mutation { test( asd: $,as ){ ...,asd, asd } }'],
362
            ['mutation { test{ . test on Test { id } } }'],
363
            ['mutation { test( a: "asdd'],
364
            ['mutation { test( a: { "asd": 12 12'],
365
            ['mutation { test( a: { "asd": 12'],
366
        ];
367
    }
368
369
    /**
370
     * @dataProvider mutationProvider
371
     */
372
    public function testMutations($query, $structure)
373
    {
374
        $parser = new Parser();
375
376
        $parsedStructure = $parser->parse($query);
377
378
        $this->assertEquals($parsedStructure, $structure);
379
    }
380
381 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...
382
    {
383
        $parser          = new Parser();
384
        $parsedStructure = $parser->parse('
385
            {
386
                test: test {
387
                    name,
388
                    ... on UnionType {
389
                        unionName
390
                    }
391
                }
392
            }
393
        ');
394
395
        $this->assertEquals($parsedStructure, [
396
            'queries'            => [
397
                new Query('test', 'test', [],
398
                    [
399
                        new Field('name', null),
400
                        new TypedFragmentReference('UnionType', [
401
                            new Field('unionName')
402
                        ])
403
                    ])
404
            ],
405
            'mutations'          => [],
406
            'fragments'          => [],
407
            'fragmentReferences' => [],
408
            'variables'          => [],
409
            'variableReferences' => []
410
        ]);
411
    }
412
413
    public function mutationProvider()
414
    {
415
        return [
416
            [
417
                'query ($variable: Int){ query ( teas: $variable ) { alias: name } }',
418
                [
419
                    'queries'            => [
420
                        new Query('query', null,
421
                            [
422
                                new Argument('teas', new VariableReference('variable', (new Variable('variable', 'Int'))->setUsed(true)))
423
                            ],
424
                            [
425
                                new Field('name', 'alias')
426
                            ])
427
                    ],
428
                    'mutations'          => [],
429
                    'fragments'          => [],
430
                    'fragmentReferences' => [],
431
                    'variables'          => [
432
                        (new Variable('variable', 'Int'))->setUsed(true)
433
                    ],
434
                    'variableReferences' => [
435
                        new VariableReference('variable', (new Variable('variable', 'Int'))->setUsed(true))
436
                    ]
437
                ]
438
            ],
439
            [
440
                '{ query { alias: name } }',
441
                [
442
                    'queries'            => [
443
                        new Query('query', null, [], [new Field('name', 'alias')])
444
                    ],
445
                    'mutations'          => [],
446
                    'fragments'          => [],
447
                    'fragmentReferences' => [],
448
                    'variables'          => [],
449
                    'variableReferences' => []
450
                ]
451
            ],
452
            [
453
                'mutation { createUser ( email: "[email protected]", active: true ) { id } }',
454
                [
455
                    'queries'            => [],
456
                    'mutations'          => [
457
                        new Mutation(
458
                            'createUser',
459
                            null,
460
                            [
461
                                new Argument('email', new Literal('[email protected]')),
462
                                new Argument('active', new Literal(true)),
463
                            ],
464
                            [
465
                                new Field('id')
466
                            ]
467
                        )
468
                    ],
469
                    'fragments'          => [],
470
                    'fragmentReferences' => [],
471
                    'variables'          => [],
472
                    'variableReferences' => []
473
                ]
474
            ],
475
            [
476
                'mutation { test : createUser (id: 4) }',
477
                [
478
                    'queries'            => [],
479
                    'mutations'          => [
480
                        new Mutation(
481
                            'createUser',
482
                            'test',
483
                            [
484
                                new Argument('id', new Literal(4)),
485
                            ],
486
                            []
487
                        )
488
                    ],
489
                    'fragments'          => [],
490
                    'fragmentReferences' => [],
491
                    'variables'          => [],
492
                    'variableReferences' => []
493
                ]
494
            ]
495
        ];
496
    }
497
498
    /**
499
     * @dataProvider queryProvider
500
     */
501
    public function testParser($query, $structure)
502
    {
503
        $parser          = new Parser();
504
        $parsedStructure = $parser->parse($query);
505
506
        $this->assertEquals($structure, $parsedStructure);
507
    }
508
509
510
    public function queryProvider()
511
    {
512
        return [
513
            [
514
                '{ test (id: -5) { id } } ',
515
                [
516
                    'queries'            => [
517
                        new Query('test', null, [
518
                            new Argument('id', new Literal(-5))
519
                        ], [
520
                            new Field('id'),
521
                        ])
522
                    ],
523
                    'mutations'          => [],
524
                    'fragments'          => [],
525
                    'fragmentReferences' => [],
526
                    'variables'          => [],
527
                    'variableReferences' => []
528
                ]
529
            ],
530
            [
531
                "{ test (id: -5) \r\n { id } } ",
532
                [
533
                    'queries'            => [
534
                        new Query('test', null, [
535
                            new Argument('id', new Literal(-5))
536
                        ], [
537
                            new Field('id'),
538
                        ])
539
                    ],
540
                    'mutations'          => [],
541
                    'fragments'          => [],
542
                    'fragmentReferences' => [],
543
                    'variables'          => [],
544
                    'variableReferences' => []
545
                ]
546
            ],
547
            [
548
                'query CheckTypeOfLuke {
549
                  hero(episode: EMPIRE) {
550
                    __typename,
551
                    name
552
                  }
553
                }',
554
                [
555
                    'queries'            => [
556
                        new Query('hero', null, [
557
                            new Argument('episode', new Literal('EMPIRE'))
558
                        ], [
559
                            new Field('__typename'),
560
                            new Field('name'),
561
                        ])
562
                    ],
563
                    'mutations'          => [],
564
                    'fragments'          => [],
565
                    'fragmentReferences' => [],
566
                    'variables'          => [],
567
                    'variableReferences' => []
568
                ]
569
            ],
570
            [
571
                '{ test { __typename, id } }',
572
                [
573
                    'queries'            => [
574
                        new Query('test', null, [], [
575
                            new Field('__typename'),
576
                            new Field('id'),
577
                        ])
578
                    ],
579
                    'mutations'          => [],
580
                    'fragments'          => [],
581
                    'fragmentReferences' => [],
582
                    'variables'          => [],
583
                    'variableReferences' => []
584
                ]
585
            ],
586
            [
587
                '{}',
588
                [
589
                    'queries'            => [],
590
                    'mutations'          => [],
591
                    'fragments'          => [],
592
                    'fragmentReferences' => [],
593
                    'variables'          => [],
594
                    'variableReferences' => []
595
                ]
596
            ],
597
            [
598
                'query test {}',
599
                [
600
                    'queries'            => [],
601
                    'mutations'          => [],
602
                    'fragments'          => [],
603
                    'fragmentReferences' => [],
604
                    'variables'          => [],
605
                    'variableReferences' => []
606
                ]
607
            ],
608
            [
609
                'query {}',
610
                [
611
                    'queries'            => [],
612
                    'mutations'          => [],
613
                    'fragments'          => [],
614
                    'fragmentReferences' => [],
615
                    'variables'          => [],
616
                    'variableReferences' => []
617
                ]
618
            ],
619
            [
620
                'mutation setName { setUserName }',
621
                [
622
                    'queries'            => [],
623
                    'mutations'          => [new Mutation('setUserName')],
624
                    'fragments'          => [],
625
                    'fragmentReferences' => [],
626
                    'variables'          => [],
627
                    'variableReferences' => []
628
                ]
629
            ],
630
            [
631
                '{ test { ...userDataFragment } } fragment userDataFragment on User { id, name, email }',
632
                [
633
                    'queries'            => [
634
                        new Query('test', null, [], [new FragmentReference('userDataFragment')])
635
                    ],
636
                    'mutations'          => [],
637
                    'fragments'          => [
638
                        new Fragment('userDataFragment', 'User', [
639
                            new Field('id'),
640
                            new Field('name'),
641
                            new Field('email')
642
                        ])
643
                    ],
644
                    'fragmentReferences' => [
645
                        new FragmentReference('userDataFragment')
646
                    ],
647
                    'variables'          => [],
648
                    'variableReferences' => []
649
                ]
650
            ],
651
            [
652
                '{ user (id: 10, name: "max", float: 123.123 ) { id, name } }',
653
                [
654
                    'queries'            => [
655
                        new Query(
656
                            'user',
657
                            null,
658
                            [
659
                                new Argument('id', new Literal('10')),
660
                                new Argument('name', new Literal('max')),
661
                                new Argument('float', new Literal('123.123'))
662
                            ],
663
                            [
664
                                new Field('id'),
665
                                new Field('name')
666
                            ]
667
                        )
668
                    ],
669
                    'mutations'          => [],
670
                    'fragments'          => [],
671
                    'fragmentReferences' => [],
672
                    'variables'          => [],
673
                    'variableReferences' => []
674
                ]
675
            ],
676
            [
677
                '{ allUsers : users ( id: [ 1, 2, 3] ) { id } }',
678
                [
679
                    'queries'            => [
680
                        new Query(
681
                            'users',
682
                            'allUsers',
683
                            [
684
                                new Argument('id', new InputList([1, 2, 3]))
685
                            ],
686
                            [
687
                                new Field('id')
688
                            ]
689
                        )
690
                    ],
691
                    'mutations'          => [],
692
                    'fragments'          => [],
693
                    'fragmentReferences' => [],
694
                    'variables'          => [],
695
                    'variableReferences' => []
696
                ]
697
            ],
698
            [
699
                '{ allUsers : users ( id: [ 1, "2", true, null] ) { id } }',
700
                [
701
                    'queries'            => [
702
                        new Query(
703
                            'users',
704
                            'allUsers',
705
                            [
706
                                new Argument('id', new InputList([1, "2", true, null]))
707
                            ],
708
                            [
709
                                new Field('id')
710
                            ]
711
                        )
712
                    ],
713
                    'mutations'          => [],
714
                    'fragments'          => [],
715
                    'fragmentReferences' => [],
716
                    'variables'          => [],
717
                    'variableReferences' => []
718
                ]
719
            ],
720
            [
721
                '{ allUsers : users ( object: { "a": 123, "d": "asd",  "b" : [ 1, 2, 4 ], "c": { "a" : 123, "b":  "asd" } } ) { id } }',
722
                [
723
                    'queries'            => [
724
                        new Query(
725
                            'users',
726
                            'allUsers',
727
                            [
728
                                new Argument('object', new InputObject([
729
                                    'a' => 123,
730
                                    'd' => 'asd',
731
                                    'b' => [1, 2, 4],
732
                                    'c' => new InputObject([
733
                                        'a' => 123,
734
                                        'b' => 'asd'
735
                                    ])
736
                                ]))
737
                            ],
738
                            [
739
                                new Field('id')
740
                            ]
741
                        )
742
                    ],
743
                    'mutations'          => [],
744
                    'fragments'          => [],
745
                    'fragmentReferences' => [],
746
                    'variables'          => [],
747
                    'variableReferences' => []
748
                ]
749
            ]
750
        ];
751
    }
752
753
    public function testVariablesInQuery()
754
    {
755
        $parser = new Parser();
756
757
        $data = $parser->parse('
758
            query StarWarsAppHomeRoute($names_0:[String]!) {
759
              factions(names:$names_0) {
760
                id,
761
                ...F2
762
              }
763
            }
764
            fragment F0 on Ship {
765
              id,
766
              name
767
            }
768
            fragment F1 on Faction {
769
              id,
770
              factionId
771
            }
772
            fragment F2 on Faction {
773
              id,
774
              factionId,
775
              name,
776
              _shipsDRnzJ:ships(first:10) {
777
                edges {
778
                  node {
779
                    id,
780
                    ...F0
781
                  },
782
                  cursor
783
                },
784
                pageInfo {
785
                  hasNextPage,
786
                  hasPreviousPage
787
                }
788
              },
789
              ...F1
790
            }
791
        ');
792
793
        $this->assertArrayNotHasKey('errors', $data);
794
    }
795
796
}
797