Passed
Pull Request — master (#3)
by
unknown
05:56 queued 02:23
created

QueryTest::testSubqueryWithAlias()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 36
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 27
nc 1
nop 0
dl 0
loc 36
rs 9.488
c 0
b 0
f 0
1
<?php
2
3
namespace Tests\Commadore\GraphQL;
4
5
use Commadore\GraphQL\Fragment;
6
use Commadore\GraphQL\Query;
7
use PHPUnit\Framework\TestCase;
8
9
class QueryTest extends TestCase
10
{
11
    /**
12
     * Tests adding fields and args using constructor parameters or method call.
13
     */
14
    public function testAddFields()
15
    {
16
        $query1 = new Query('article', [
17
            'id' => 999,
18
            'title' => 'Hello World',
19
            'note' => 3.5,
20
        ], [
21
            'id',
22
            'title',
23
            'body',
24
        ]);
25
26
        $query2 = (new Query('article'))
27
            ->arguments([
28
                'id' => 999,
29
                'title' => 'Hello World',
30
                'note' => 3.5,
31
            ])
32
            ->fields([
33
                'id',
34
                'title',
35
                'body',
36
            ]);
37
38
        $expected =
39
'{
40
  article(id: 999, note: 3.5, title: "Hello World") {
41
    body
42
    id
43
    title
44
  }
45
}
46
';
47
        $this->assertEquals($expected, (string) $query1);
48
        $this->assertEquals($expected, (string) $query2);
49
    }
50
51
    /**
52
     * Tests the order of fields and arguments.
53
     */
54
    public function testSortFields()
55
    {
56
        $query1 = (new Query('article'))
57
            ->arguments([
58
                'title' => 'Hello World',
59
                'note' => 3.5,
60
                'id' => 999,
61
            ])
62
            ->fields([
63
                'id',
64
                'title',
65
                'body',
66
            ]);
67
68
        $query2 = (new Query('article'))
69
            ->arguments([
70
                'id' => 999,
71
                'title' => 'Hello World',
72
                'note' => 3.5,
73
            ])
74
            ->fields([
75
                'title',
76
                'id',
77
                'body',
78
            ]);
79
80
        $this->assertEquals((string) $query1, (string) $query2);
81
    }
82
83
    /**
84
     * Tests field alias.
85
     */
86
    public function testAlias()
87
    {
88
        $query = (new Query('article'))->fields([
89
                'articleId' => 'id',
90
                'articleTitle' => 'title',
91
                'body',
92
            ]);
93
94
        $expected =
95
'{
96
  article {
97
    articleId: id
98
    articleTitle: title
99
    body
100
  }
101
}
102
';
103
        $this->assertEquals($expected, (string) $query);
104
    }
105
106
    /**
107
     * Tests operation name generation and printing.
108
     */
109
    public function testOperationName()
110
    {
111
        // query with variables but no operation name
112
        $query1 = (new Query('article'))
113
            ->variables([
114
                '$id' => 'Integer',
115
            ])
116
            ->arguments([
117
                'id' => '$id',
118
            ])
119
            ->fields([
120
                'id',
121
                'title',
122
                'body',
123
            ]);
124
125
        $expected1 =
126
'query query_a6fa4442880a206cf86fc5c24e0a384637ab885d($id: Integer) {
127
  article(id: $id) {
128
    body
129
    id
130
    title
131
  }
132
}
133
';
134
        $this->assertEquals($expected1, (string) $query1);
135
136
        // query with variables and operation name
137
        $query2 = (new Query('article'))
138
            ->operationName('articlesQuery')
139
            ->variables([
140
                '$id' => 'Integer',
141
            ])
142
            ->arguments([
143
                'id' => '$id',
144
            ])
145
            ->fields([
146
                'id',
147
                'title',
148
                'body',
149
            ]);
150
151
        $expected2 =
152
'query articlesQuery($id: Integer) {
153
  article(id: $id) {
154
    body
155
    id
156
    title
157
  }
158
}
159
';
160
        $this->assertEquals($expected2, (string) $query2);
161
162
        // query with only operation name
163
        $query3 = (new Query('article'))
164
            ->operationName('articlesQuery')
165
            ->fields([
166
                'id',
167
                'title',
168
                'body',
169
            ]);
170
171
        $expected3 =
172
'query articlesQuery {
173
  article {
174
    body
175
    id
176
    title
177
  }
178
}
179
';
180
        $this->assertEquals($expected3, (string) $query3);
181
    }
182
183
    /**
184
     * Tests directives printing.
185
     */
186
    public function testDirective()
187
    {
188
        // skip if directive
189
        $query1 = (new Query('article'))
190
            ->operationName('articlesQuery')
191
            ->variables([
192
                '$withoutTags' => 'Boolean',
193
            ])
194
            ->fields([
195
                'id',
196
                'title',
197
                'body',
198
                'tags',
199
            ])
200
            ->skipIf([
201
                'tags' => '$withoutTags',
202
            ])
203
        ;
204
205
        $expected1 =
206
'query articlesQuery($withoutTags: Boolean) {
207
  article {
208
    body
209
    id
210
    tags @skip(if: $withoutTags)
211
    title
212
  }
213
}
214
';
215
        $this->assertEquals($expected1, (string) $query1);
216
217
        // include if directive
218
        $query2 = (new Query('article'))
219
            ->operationName('articlesQuery')
220
            ->variables([
221
                '$withTags' => 'Boolean!',
222
            ])
223
            ->fields([
224
                'id',
225
                'title',
226
                'body',
227
                'tags',
228
            ])
229
            ->includeIf([
230
                'tags' => '$withTags',
231
            ])
232
        ;
233
234
        $expected2 =
235
'query articlesQuery($withTags: Boolean!) {
236
  article {
237
    body
238
    id
239
    tags @include(if: $withTags)
240
    title
241
  }
242
}
243
';
244
        $this->assertEquals($expected2, (string) $query2);
245
    }
246
247
    /**
248
     * Tests sub query, with directive and alias.
249
     */
250
    public function testSubqueryWithAlias()
251
    {
252
        $query = (new Query('article'))
253
            ->operationName('articlesQuery')
254
            ->variables([
255
                '$withTags' => 'Boolean!',
256
            ])
257
            ->fields([
258
                'id',
259
                'title',
260
                'body',
261
                // sub query with type and alias
262
                'articleTags' => (new Query('tags'))->fields([
263
                    'id',
264
                    'tagTitle' => 'title',
265
                ]),
266
            ])
267
            ->includeIf([
268
                'articleTags' => '$withTags',
269
            ])
270
        ;
271
272
        $expected =
273
'query articlesQuery($withTags: Boolean!) {
274
  article {
275
    articleTags: tags @include(if: $withTags) {
276
      id
277
      tagTitle: title
278
    }
279
    body
280
    id
281
    title
282
  }
283
}
284
';
285
        $this->assertEquals($expected, (string) $query);
286
    }
287
288
    public function testSubqueryWithoutAlias()
289
    {
290
        $query = (new Query('article'))
291
            ->operationName('articlesQuery')
292
            ->variables([
293
                '$withTags' => 'Boolean!',
294
            ])
295
            ->fields([
296
                'id',
297
                'title',
298
                'body',
299
                // sub query without type parameter, so we take the alias
300
                'tags' => (new Query())->fields([
301
                    'id',
302
                    'tagTitle' => 'title',
303
                ]),
304
            ])
305
            ->includeIf([
306
                'tags' => '$withTags',
307
            ])
308
        ;
309
310
        $expected =
311
'query articlesQuery($withTags: Boolean!) {
312
  article {
313
    body
314
    id
315
    tags @include(if: $withTags) {
316
      id
317
      tagTitle: title
318
    }
319
    title
320
  }
321
}
322
';
323
        $this->assertEquals($expected, (string) $query);
324
    }
325
326
    public function testQueryWithFragment()
327
    {
328
        $query = (new Query('article'))
329
            ->operationName('articlesQuery')
330
            ->fields([
331
                'id',
332
                'title',
333
                'body',
334
                '...imageFragment',
335
            ])
336
            ->addFragment(new Fragment('imageFragment', 'image', [
337
                'height',
338
                'width',
339
                'filename',
340
                'size',
341
                'formats' => (new Query())->fields([
342
                    'id',
343
                    'name',
344
                    'url',
345
                ]),
346
            ]))
347
        ;
348
349
        $expected =
350
            'query articlesQuery {
351
  article {
352
    ...imageFragment
353
    body
354
    id
355
    title
356
  }
357
}
358
359
fragment imageFragment on image {
360
  filename
361
  formats {
362
    id
363
    name
364
    url
365
  }
366
  height
367
  size
368
  width
369
}
370
';
371
        $this->assertEquals($expected, (string) $query);
372
    }
373
}
374