Completed
Push — master ( e40fed...ed70fa )
by Portey
02:55
created

ParserTest::mutationProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 68
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 2 Features 2
Metric Value
c 5
b 2
f 2
dl 0
loc 68
rs 9.2448
cc 1
eloc 32
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\Value\InputList;
16
use Youshido\GraphQL\Parser\Value\InputObject;
17
use Youshido\GraphQL\Parser\Value\Literal;
18
use Youshido\GraphQL\Parser\Ast\Query;
19
use Youshido\GraphQL\Parser\Parser;
20
use Youshido\GraphQL\Parser\Value\Variable;
21
22
class ParserTest extends \PHPUnit_Framework_TestCase
23
{
24
25
    /**
26
     * @param $query string
27
     *
28
     * @dataProvider wrongQueriesProvider
29
     * @expectedException Youshido\GraphQL\Parser\SyntaxErrorException
30
     */
31
    public function testWrongQueries($query)
32
    {
33
        $parser = new Parser();
34
35
        $parser->setSource($query);
36
        $parser->parse();
37
    }
38
39
    public function wrongQueriesProvider()
40
    {
41
        return [
42
            ['{ test { id,, asd } }'],
43
            ['{ test { id,, } }'],
44
            ['{ test (a: $a, b: <basd>) { id }'],
45
            ['{ test (asd: [..., asd]) { id } }'],
46
            ['{ test (asd: { "a": 4, b: 5}) { id } }'],
47
            ['{ test (asd: { "a": 4, "m": null, "asd": false  "b": 5, "c" : { a }}) { id } }'],
48
            ['asdasd'],
49
            ['mutation { test(asd: ... ){ ...,asd, asd } }'],
50
            ['mutation { test( asd: $,as ){ ...,asd, asd } }']
51
        ];
52
    }
53
54
    /**
55
     * @dataProvider mutationProvider
56
     */
57
    public function testMutations($query, $structure)
58
    {
59
        $parser = new Parser();
60
        $parser->setSource($query);
61
62
        $parsedStructure = $parser->parse();
63
64
        $this->assertEquals($parsedStructure, $structure);
65
    }
66
67
    public function mutationProvider()
68
    {
69
        return [
70
            [
71
                '{ query ( teas: $variable ) { alias: name } }',
72
                [
73
                    'queries' => [
74
                        new Query('query', null,
75
                            [
76
                                new Argument('teas', new Variable('variable'))
77
                            ],
78
                            [
79
                                new Field('name', 'alias')
80
                            ])
81
                    ],
82
                    'mutations' => [],
83
                    'fragments' => []
84
                ]
85
            ],
86
            [
87
                '{ query { alias: name } }',
88
                [
89
                    'queries' => [
90
                        new Query('query', null, [], [new Field('name', 'alias')])
91
                    ],
92
                    'mutations' => [],
93
                    'fragments' => []
94
                ]
95
            ],
96
            [
97
                'mutation { createUser ( email: "[email protected]", active: true ) { id } }',
98
                [
99
                    'queries'   => [],
100
                    'mutations' => [
101
                        new Mutation(
102
                            'createUser',
103
                            null,
104
                            [
105
                                new Argument('email', new Literal('[email protected]')),
106
                                new Argument('active', new Literal(true)),
107
                            ],
108
                            [
109
                                new Field('id')
110
                            ]
111
                        )
112
                    ],
113
                    'fragments' => []
114
                ]
115
            ],
116
            [
117
                'mutation { test : createUser (id: 4) }',
118
                [
119
                    'queries'   => [],
120
                    'mutations' => [
121
                        new Mutation(
122
                            'createUser',
123
                            'test',
124
                            [
125
                                new Argument('id', new Literal(4)),
126
                            ],
127
                            []
128
                        )
129
                    ],
130
                    'fragments' => []
131
                ]
132
            ]
133
        ];
134
    }
135
136
    /**
137
     * @dataProvider queryProvider
138
     */
139
    public function testParser($query, $structure)
140
    {
141
        $parser = new Parser();
142
        $parser->setSource($query);
143
144
        $parsedStructure = $parser->parse();
145
146
        $this->assertEquals($parsedStructure, $structure);
147
    }
148
149
150
    public function queryProvider()
151
    {
152
        return [
153
            [
154
                '{}',
155
                [
156
                    'queries'   => [],
157
                    'mutations' => [],
158
                    'fragments' => []
159
                ]
160
            ],
161
            [
162
                '{ test { ...userDataFragment } } fragment userDataFragment on User { id, name, email }',
163
                [
164
                    'queries'   => [
165
                        new Query('test', null, [], [new FragmentReference('userDataFragment')])
166
                    ],
167
                    'mutations' => [],
168
                    'fragments' => [
169
                        new Fragment('userDataFragment', 'User', [
170
                            new Field('id'),
171
                            new Field('name'),
172
                            new Field('email')
173
                        ])
174
                    ]
175
                ]
176
            ],
177
            [
178
                '{ user (id: 10, name: "max", float: 123.123 ) { id, name } }',
179
                [
180
                    'queries'   => [
181
                        new Query(
182
                            'user',
183
                            null,
184
                            [
185
                                new Argument('id', new Literal('10')),
186
                                new Argument('name', new Literal('max')),
187
                                new Argument('float', new Literal('123.123'))
188
                            ],
189
                            [
190
                                new Field('id'),
191
                                new Field('name')
192
                            ]
193
                        )
194
                    ],
195
                    'mutations' => [],
196
                    'fragments' => []
197
                ]
198
            ],
199
            [
200
                '{ allUsers : users ( id: [ 1, 2, 3] ) { id } }',
201
                [
202
                    'queries'   => [
203
                        new Query(
204
                            'users',
205
                            'allUsers',
206
                            [
207
                                new Argument('id', new InputList([1, 2, 3]))
208
                            ],
209
                            [
210
                                new Field('id')
211
                            ]
212
                        )
213
                    ],
214
                    'mutations' => [],
215
                    'fragments' => []
216
                ]
217
            ],
218
            [
219
                '{ allUsers : users ( id: [ 1, "2", true, null] ) { id } }',
220
                [
221
                    'queries'   => [
222
                        new Query(
223
                            'users',
224
                            'allUsers',
225
                            [
226
                                new Argument('id', new InputList([1, "2", true, null]))
227
                            ],
228
                            [
229
                                new Field('id')
230
                            ]
231
                        )
232
                    ],
233
                    'mutations' => [],
234
                    'fragments' => []
235
                ]
236
            ],
237
            [
238
                '{ allUsers : users ( object: { "a": 123, "d": "asd",  "b" : [ 1, 2, 4 ], "c": { "a" : 123, "b":  "asd" } } ) { id } }',
239
                [
240
                    'queries'   => [
241
                        new Query(
242
                            'users',
243
                            'allUsers',
244
                            [
245
                                new Argument('object', new InputObject([
246
                                    'a' => 123,
247
                                    'd' => 'asd',
248
                                    'b' => [1, 2, 4],
249
                                    'c' => [
250
                                        'a' => 123,
251
                                        'b' => 'asd'
252
                                    ]
253
                                ]))
254
                            ],
255
                            [
256
                                new Field('id')
257
                            ]
258
                        )
259
                    ],
260
                    'mutations' => [],
261
                    'fragments' => []
262
                ]
263
            ]
264
        ];
265
    }
266
267
}