Failed Conditions
Push — master ( 4e43a2...d70c8e )
by Vladimir
15:58 queued 04:47
created

ExtractTypesTest::setUp()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 190
Code Lines 132

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 132
dl 0
loc 190
rs 8
c 0
b 0
f 0
cc 1
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
declare(strict_types=1);
4
5
namespace GraphQL\Tests\Utils;
6
7
use GraphQL\Error\InvariantViolation;
8
use GraphQL\Type\Definition\InputObjectType;
9
use GraphQL\Type\Definition\InterfaceType;
10
use GraphQL\Type\Definition\ObjectType;
11
use GraphQL\Type\Definition\Type;
12
use GraphQL\Type\Definition\UnionType;
13
use GraphQL\Utils\TypeInfo;
14
use PHPUnit\Framework\TestCase;
15
16
class ExtractTypesTest extends TestCase
17
{
18
    /** @var ObjectType */
19
    private $query;
20
21
    /** @var ObjectType */
22
    private $mutation;
23
24
    /** @var InterfaceType */
25
    private $node;
26
27
    /** @var InterfaceType */
28
    private $content;
29
30
    /** @var ObjectType */
31
    private $blogStory;
32
33
    /** @var ObjectType */
34
    private $comment;
35
36
    /** @var ObjectType */
37
    private $user;
38
39
    /** @var ObjectType */
40
    private $category;
41
42
    /** @var UnionType */
43
    private $mention;
44
45
    /** @var ObjectType */
46
    private $postStoryMutation;
47
48
    /** @var InputObjectType */
49
    private $postStoryMutationInput;
50
51
    /** @var ObjectType */
52
    private $postCommentMutation;
53
54
    /** @var InputObjectType */
55
    private $postCommentMutationInput;
56
57
    public function setUp()
58
    {
59
        $this->node = new InterfaceType([
60
            'name'   => 'Node',
61
            'fields' => [
62
                'id' => Type::string(),
63
            ],
64
        ]);
65
66
        $this->content = new InterfaceType([
67
            'name'   => 'Content',
68
            'fields' => function () {
69
                return [
70
                    'title'      => Type::string(),
71
                    'body'       => Type::string(),
72
                    'author'     => $this->user,
73
                    'comments'   => Type::listOf($this->comment),
74
                    'categories' => Type::listOf($this->category),
75
                ];
76
            },
77
        ]);
78
79
        $this->blogStory = new ObjectType([
80
            'name'       => 'BlogStory',
81
            'interfaces' => [
82
                $this->node,
83
                $this->content,
84
            ],
85
            'fields'     => function () {
86
                return [
87
                    $this->node->getField('id'),
88
                    $this->content->getField('title'),
89
                    $this->content->getField('body'),
90
                    $this->content->getField('author'),
91
                    $this->content->getField('comments'),
92
                    $this->content->getField('categories'),
93
                ];
94
            },
95
        ]);
96
97
        new ObjectType([
98
            'name'       => 'Link',
99
            'interfaces' => [
100
                $this->node,
101
                $this->content,
102
            ],
103
            'fields'     => function () {
104
                return [
105
                    'id'         => $this->node->getField('id'),
106
                    'title'      => $this->content->getField('title'),
107
                    'body'       => $this->content->getField('body'),
108
                    'author'     => $this->content->getField('author'),
109
                    'comments'   => $this->content->getField('comments'),
110
                    'categories' => $this->content->getField('categories'),
111
                    'url'        => Type::string(),
112
                ];
113
            },
114
        ]);
115
116
        new ObjectType([
117
            'name'       => 'Video',
118
            'interfaces' => [
119
                $this->node,
120
                $this->content,
121
            ],
122
            'fields'     => function () {
123
                return [
124
                    'id'          => $this->node->getField('id'),
125
                    'title'       => $this->content->getField('title'),
126
                    'body'        => $this->content->getField('body'),
127
                    'author'      => $this->content->getField('author'),
128
                    'comments'    => $this->content->getField('comments'),
129
                    'categories'  => $this->content->getField('categories'),
130
                    'streamUrl'   => Type::string(),
131
                    'downloadUrl' => Type::string(),
132
                    'metadata'    => new ObjectType([
133
                        'name'   => 'VideoMetadata',
134
                        'fields' => [
135
                            'lat' => Type::float(),
136
                            'lng' => Type::float(),
137
                        ],
138
                    ]),
139
                ];
140
            },
141
        ]);
142
143
        $this->comment = new ObjectType([
144
            'name'       => 'Comment',
145
            'interfaces' => [
146
                $this->node,
147
            ],
148
            'fields'     => function () {
149
                return [
150
                    'id'      => $this->node->getField('id'),
151
                    'author'  => $this->user,
152
                    'text'    => Type::string(),
153
                    'replies' => Type::listOf($this->comment),
154
                    'parent'  => $this->comment,
155
                    'content' => $this->content,
156
                ];
157
            },
158
        ]);
159
160
        $this->user = new ObjectType([
161
            'name'       => 'User',
162
            'interfaces' => [
163
                $this->node,
164
            ],
165
            'fields'     => function () {
166
                return [
167
                    'id'   => $this->node->getField('id'),
168
                    'name' => Type::string(),
169
                ];
170
            },
171
        ]);
172
173
        $this->category = new ObjectType([
174
            'name'       => 'Category',
175
            'interfaces' => [
176
                $this->node,
177
            ],
178
            'fields'     => function () {
179
                return [
180
                    'id'   => $this->node->getField('id'),
181
                    'name' => Type::string(),
182
                ];
183
            },
184
        ]);
185
186
        $this->mention = new UnionType([
187
            'name'  => 'Mention',
188
            'types' => [
189
                $this->user,
190
                $this->category,
191
            ],
192
        ]);
193
194
        $this->query = new ObjectType([
195
            'name'   => 'Query',
196
            'fields' => [
197
                'viewer'        => $this->user,
198
                'latestContent' => $this->content,
199
                'node'          => $this->node,
200
                'mentions'      => Type::listOf($this->mention),
201
            ],
202
        ]);
203
204
        $this->postStoryMutationInput = new InputObjectType([
205
            'name'   => 'PostStoryMutationInput',
206
            'fields' => [
207
                'title'    => Type::string(),
208
                'body'     => Type::string(),
209
                'author'   => Type::id(),
210
                'category' => Type::id(),
211
            ],
212
        ]);
213
214
        $this->mutation = new ObjectType([
215
            'name'   => 'Mutation',
216
            'fields' => [
217
                'postStory'   => [
218
                    'type' => $this->postStoryMutation = new ObjectType([
219
                        'name'   => 'PostStoryMutation',
220
                        'fields' => [
221
                            'story' => $this->blogStory,
222
                        ],
223
                    ]),
224
                    'args' => [
225
                        'input'           => Type::nonNull($this->postStoryMutationInput),
226
                        'clientRequestId' => Type::string(),
227
                    ],
228
                ],
229
                'postComment' => [
230
                    'type' => $this->postCommentMutation = new ObjectType([
231
                        'name'   => 'PostCommentMutation',
232
                        'fields' => [
233
                            'comment' => $this->comment,
234
                        ],
235
                    ]),
236
                    'args' => [
237
                        'input'           => Type::nonNull($this->postCommentMutationInput = new InputObjectType([
238
                            'name'   => 'PostCommentMutationInput',
239
                            'fields' => [
240
                                'text'    => Type::nonNull(Type::string()),
241
                                'author'  => Type::nonNull(Type::id()),
242
                                'content' => Type::id(),
243
                                'parent'  => Type::id(),
244
                            ],
245
                        ])),
246
                        'clientRequestId' => Type::string(),
247
                    ],
248
                ],
249
            ],
250
        ]);
251
    }
252
253
    public function testExtractTypesFromQuery() : void
254
    {
255
        $expectedTypeMap = [
256
            'Query'    => $this->query,
257
            'User'     => $this->user,
258
            'Node'     => $this->node,
259
            'String'   => Type::string(),
260
            'Content'  => $this->content,
261
            'Comment'  => $this->comment,
262
            'Mention'  => $this->mention,
263
            'Category' => $this->category,
264
        ];
265
266
        $actualTypeMap = TypeInfo::extractTypes($this->query);
267
        $this->assertEquals($expectedTypeMap, $actualTypeMap);
268
    }
269
270
    public function testExtractTypesFromMutation() : void
271
    {
272
        $expectedTypeMap = [
273
            'Mutation'                 => $this->mutation,
274
            'User'                     => $this->user,
275
            'Node'                     => $this->node,
276
            'String'                   => Type::string(),
277
            'Content'                  => $this->content,
278
            'Comment'                  => $this->comment,
279
            'BlogStory'                => $this->blogStory,
280
            'Category'                 => $this->category,
281
            'PostStoryMutationInput'   => $this->postStoryMutationInput,
282
            'ID'                       => Type::id(),
283
            'PostStoryMutation'        => $this->postStoryMutation,
284
            'PostCommentMutationInput' => $this->postCommentMutationInput,
285
            'PostCommentMutation'      => $this->postCommentMutation,
286
        ];
287
288
        $actualTypeMap = TypeInfo::extractTypes($this->mutation);
289
        $this->assertEquals($expectedTypeMap, $actualTypeMap);
290
    }
291
292
    public function testThrowsOnMultipleTypesWithSameName() : void
293
    {
294
        $otherUserType = new ObjectType([
295
            'name'   => 'User',
296
            'fields' => ['a' => Type::string()],
297
        ]);
298
299
        $queryType = new ObjectType([
300
            'name'   => 'Test',
301
            'fields' => [
302
                'otherUser' => $otherUserType,
303
                'user'      => $this->user,
304
            ],
305
        ]);
306
307
        $this->expectException(InvariantViolation::class);
308
        $this->expectExceptionMessage('Schema must contain unique named types but contains multiple types named "User"');
309
        TypeInfo::extractTypes($queryType);
310
    }
311
}
312