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

ParserTest::testEmptyParser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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