Passed
Pull Request — master (#586)
by Šimon
12:52
created

ExecutorSchemaTest::testExecutesUsingASchema()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 188
Code Lines 100

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 100
c 4
b 0
f 0
dl 0
loc 188
rs 8
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\Executor;
6
7
use GraphQL\Executor\Executor;
8
use GraphQL\Language\Parser;
9
use GraphQL\Type\Definition\CustomScalarType;
10
use GraphQL\Type\Definition\ObjectType;
11
use GraphQL\Type\Definition\Type;
12
use GraphQL\Type\Schema;
13
use PHPUnit\Framework\TestCase;
14
use function sprintf;
15
16
class ExecutorSchemaTest extends TestCase
17
{
18
    // Execute: Handles execution with a complex schema
19
    /**
20
     * @see it('executes using a schema')
21
     */
22
    public function testExecutesUsingASchema() : void
23
    {
24
        $BlogSerializableValueType = new CustomScalarType([
25
            'name'         => 'JsonSerializableValueScalar',
26
            'serialize'    => static function ($value) {
27
                return $value;
28
            },
29
        ]);
30
31
        $BlogArticle = null;
32
        $BlogImage   = new ObjectType([
33
            'name'   => 'Image',
34
            'fields' => [
35
                'url'    => ['type' => Type::string()],
36
                'width'  => ['type' => Type::int()],
37
                'height' => ['type' => Type::int()],
38
            ],
39
        ]);
40
41
        $BlogAuthor = new ObjectType([
42
            'name'   => 'Author',
43
            'fields' => static function () use (&$BlogArticle, &$BlogImage) {
44
                return [
45
                    'id'            => ['type' => Type::string()],
46
                    'name'          => ['type' => Type::string()],
47
                    'pic'           => [
48
                        'args'    => ['width' => ['type' => Type::int()], 'height' => ['type' => Type::int()]],
49
                        'type'    => $BlogImage,
50
                        'resolve' => static function ($obj, $args) {
51
                            return $obj['pic']($args['width'], $args['height']);
52
                        },
53
                    ],
54
                    'recentArticle' => $BlogArticle,
55
                ];
56
            },
57
        ]);
58
59
        $BlogArticle = new ObjectType([
60
            'name'   => 'Article',
61
            'fields' => [
62
                'id'          => ['type' => Type::nonNull(Type::string())],
63
                'isPublished' => ['type' => Type::boolean()],
64
                'author'      => ['type' => $BlogAuthor],
65
                'title'       => ['type' => Type::string()],
66
                'body'        => ['type' => Type::string()],
67
                'keywords'    => ['type' => Type::listOf(Type::string())],
68
                'meta'        => ['type' => $BlogSerializableValueType],
69
            ],
70
        ]);
71
72
        $BlogQuery = new ObjectType([
73
            'name'   => 'Query',
74
            'fields' => [
75
                'article' => [
76
                    'type'    => $BlogArticle,
77
                    'args'    => ['id' => ['type' => Type::id()]],
78
                    'resolve' => function ($rootValue, $args) {
79
                        return $this->article($args['id']);
80
                    },
81
                ],
82
                'feed'    => [
83
                    'type'    => Type::listOf($BlogArticle),
84
                    'resolve' => function () {
85
                        return [
86
                            $this->article(1),
87
                            $this->article(2),
88
                            $this->article(3),
89
                            $this->article(4),
90
                            $this->article(5),
91
                            $this->article(6),
92
                            $this->article(7),
93
                            $this->article(8),
94
                            $this->article(9),
95
                            $this->article(10),
96
                        ];
97
                    },
98
                ],
99
            ],
100
        ]);
101
102
        $BlogSchema = new Schema(['query' => $BlogQuery]);
103
104
        $request = '
105
      {
106
        feed {
107
          id,
108
          title
109
        },
110
        article(id: "1") {
111
          ...articleFields,
112
          author {
113
            id,
114
            name,
115
            pic(width: 640, height: 480) {
116
              url,
117
              width,
118
              height
119
            },
120
            recentArticle {
121
              ...articleFields,
122
              keywords
123
            }
124
          }
125
          meta
126
        }
127
      }
128
129
      fragment articleFields on Article {
130
        id,
131
        isPublished,
132
        title,
133
        body,
134
        hidden,
135
        notdefined
136
      }
137
    ';
138
139
        $expected = [
140
            'data' => [
141
                'feed'    => [
142
                    [
143
                        'id'    => '1',
144
                        'title' => 'My Article 1',
145
                    ],
146
                    [
147
                        'id'    => '2',
148
                        'title' => 'My Article 2',
149
                    ],
150
                    [
151
                        'id'    => '3',
152
                        'title' => 'My Article 3',
153
                    ],
154
                    [
155
                        'id'    => '4',
156
                        'title' => 'My Article 4',
157
                    ],
158
                    [
159
                        'id'    => '5',
160
                        'title' => 'My Article 5',
161
                    ],
162
                    [
163
                        'id'    => '6',
164
                        'title' => 'My Article 6',
165
                    ],
166
                    [
167
                        'id'    => '7',
168
                        'title' => 'My Article 7',
169
                    ],
170
                    [
171
                        'id'    => '8',
172
                        'title' => 'My Article 8',
173
                    ],
174
                    [
175
                        'id'    => '9',
176
                        'title' => 'My Article 9',
177
                    ],
178
                    [
179
                        'id'    => '10',
180
                        'title' => 'My Article 10',
181
                    ],
182
                ],
183
                'article' => [
184
                    'id'          => '1',
185
                    'isPublished' => true,
186
                    'title'       => 'My Article 1',
187
                    'body'        => 'This is a post',
188
                    'author'      => [
189
                        'id'            => '123',
190
                        'name'          => 'John Smith',
191
                        'pic'           => [
192
                            'url'    => 'cdn://123',
193
                            'width'  => 640,
194
                            'height' => 480,
195
                        ],
196
                        'recentArticle' => [
197
                            'id'          => '1',
198
                            'isPublished' => true,
199
                            'title'       => 'My Article 1',
200
                            'body'        => 'This is a post',
201
                            'keywords'    => ['foo', 'bar', '1', '1', null],
202
                        ],
203
                    ],
204
                    'meta' => [ 'title' => 'My Article 1 | My Blog' ],
205
                ],
206
            ],
207
        ];
208
209
        self::assertEquals($expected, Executor::execute($BlogSchema, Parser::parse($request))->toArray());
210
    }
211
212
    private function article($id)
213
    {
214
        $johnSmith = null;
215
        $article   = static function ($id) use (&$johnSmith) {
216
            return [
217
                'id'          => $id,
218
                'isPublished' => 'true',
219
                'author'      => $johnSmith,
220
                'title'       => 'My Article ' . $id,
221
                'body'        => 'This is a post',
222
                'hidden'      => 'This data is not exposed in the schema',
223
                'keywords'    => ['foo', 'bar', 1, true, null],
224
                'meta'        => ['title' => 'My Article 1 | My Blog'],
225
            ];
226
        };
227
228
        $getPic = static function ($uid, $width, $height) {
229
            return [
230
                'url'    => sprintf('cdn://%s', $uid),
231
                'width'  => $width,
232
                'height' => $height,
233
            ];
234
        };
235
236
        $johnSmith = [
237
            'id'            => 123,
238
            'name'          => 'John Smith',
239
            'pic'           => static function ($width, $height) use ($getPic) {
240
                return $getPic(123, $width, $height);
241
            },
242
            'recentArticle' => $article(1),
243
        ];
244
245
        return $article($id);
246
    }
247
}
248