1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tests\Fnash\GraphQL; |
4
|
|
|
|
5
|
|
|
use Fnash\GraphQL\Fragment; |
6
|
|
|
use Fnash\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 = Query::create('article', [ |
17
|
|
|
'id' => 999, |
18
|
|
|
'title' => 'Hello World', |
19
|
|
|
'note' => 3.5, |
20
|
|
|
], [ |
21
|
|
|
'id', |
22
|
|
|
'title', |
23
|
|
|
'body', |
24
|
|
|
]); |
25
|
|
|
|
26
|
|
|
$query2 = Query::create('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 = Query::create('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 = Query::create('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 = Query::create('article') |
89
|
|
|
->fields([ |
90
|
|
|
'articleId' => 'id', |
91
|
|
|
'articleTitle' => 'title', |
92
|
|
|
'body', |
93
|
|
|
]); |
94
|
|
|
|
95
|
|
|
$expected = |
96
|
|
|
'{ |
97
|
|
|
article { |
98
|
|
|
articleId: id |
99
|
|
|
articleTitle: title |
100
|
|
|
body |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
'; |
104
|
|
|
$this->assertEquals($expected, (string) $query); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Tests operation name generation and printing. |
109
|
|
|
*/ |
110
|
|
|
public function testOperationName() |
111
|
|
|
{ |
112
|
|
|
// query with variables but no operation name |
113
|
|
|
$query1 = Query::create('article') |
114
|
|
|
->variables([ |
115
|
|
|
'$id' => 'Integer', |
116
|
|
|
]) |
117
|
|
|
->arguments([ |
118
|
|
|
'id' => '$id', |
119
|
|
|
]) |
120
|
|
|
->fields([ |
121
|
|
|
'id', |
122
|
|
|
'title', |
123
|
|
|
'body', |
124
|
|
|
]); |
125
|
|
|
|
126
|
|
|
$expected1 = |
127
|
|
|
'query query_a6fa4442880a206cf86fc5c24e0a384637ab885d($id: Integer) { |
128
|
|
|
article(id: $id) { |
129
|
|
|
body |
130
|
|
|
id |
131
|
|
|
title |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
'; |
135
|
|
|
$this->assertEquals($expected1, (string) $query1); |
136
|
|
|
|
137
|
|
|
// query with variables and operation name |
138
|
|
|
$query2 = Query::create('article') |
139
|
|
|
->operationName('articlesQuery') |
140
|
|
|
->variables([ |
141
|
|
|
'$id' => 'Integer', |
142
|
|
|
]) |
143
|
|
|
->arguments([ |
144
|
|
|
'id' => '$id', |
145
|
|
|
]) |
146
|
|
|
->fields([ |
147
|
|
|
'id', |
148
|
|
|
'title', |
149
|
|
|
'body', |
150
|
|
|
]); |
151
|
|
|
|
152
|
|
|
$expected2 = |
153
|
|
|
'query articlesQuery($id: Integer) { |
154
|
|
|
article(id: $id) { |
155
|
|
|
body |
156
|
|
|
id |
157
|
|
|
title |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
'; |
161
|
|
|
$this->assertEquals($expected2, (string) $query2); |
162
|
|
|
|
163
|
|
|
// query with only operation name |
164
|
|
|
$query3 = Query::create('article') |
165
|
|
|
->operationName('articlesQuery') |
166
|
|
|
->fields([ |
167
|
|
|
'id', |
168
|
|
|
'title', |
169
|
|
|
'body', |
170
|
|
|
]); |
171
|
|
|
|
172
|
|
|
$expected3 = |
173
|
|
|
'query articlesQuery { |
174
|
|
|
article { |
175
|
|
|
body |
176
|
|
|
id |
177
|
|
|
title |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
'; |
181
|
|
|
$this->assertEquals($expected3, (string) $query3); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Tests directives printing. |
186
|
|
|
*/ |
187
|
|
|
public function testDirective() |
188
|
|
|
{ |
189
|
|
|
// skip if directive |
190
|
|
|
$query1 = Query::create('article') |
191
|
|
|
->operationName('articlesQuery') |
192
|
|
|
->variables([ |
193
|
|
|
'$withoutTags' => 'Boolean', |
194
|
|
|
]) |
195
|
|
|
->fields([ |
196
|
|
|
'id', |
197
|
|
|
'title', |
198
|
|
|
'body', |
199
|
|
|
'tags', |
200
|
|
|
]) |
201
|
|
|
->skipIf([ |
202
|
|
|
'tags' => '$withoutTags', |
203
|
|
|
]) |
204
|
|
|
; |
205
|
|
|
|
206
|
|
|
$expected1 = |
207
|
|
|
'query articlesQuery($withoutTags: Boolean) { |
208
|
|
|
article { |
209
|
|
|
body |
210
|
|
|
id |
211
|
|
|
tags @skip(if: $withoutTags) |
212
|
|
|
title |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
'; |
216
|
|
|
$this->assertEquals($expected1, (string) $query1); |
217
|
|
|
|
218
|
|
|
// include if directive |
219
|
|
|
$query2 = Query::create('article') |
220
|
|
|
->operationName('articlesQuery') |
221
|
|
|
->variables([ |
222
|
|
|
'$withTags' => 'Boolean!', |
223
|
|
|
]) |
224
|
|
|
->fields([ |
225
|
|
|
'id', |
226
|
|
|
'title', |
227
|
|
|
'body', |
228
|
|
|
'tags', |
229
|
|
|
]) |
230
|
|
|
->includeIf([ |
231
|
|
|
'tags' => '$withTags', |
232
|
|
|
]) |
233
|
|
|
; |
234
|
|
|
|
235
|
|
|
$expected2 = |
236
|
|
|
'query articlesQuery($withTags: Boolean!) { |
237
|
|
|
article { |
238
|
|
|
body |
239
|
|
|
id |
240
|
|
|
tags @include(if: $withTags) |
241
|
|
|
title |
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
'; |
245
|
|
|
$this->assertEquals($expected2, (string) $query2); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Tests sub query, with directive and alias. |
250
|
|
|
*/ |
251
|
|
|
public function testSubqueryWithAlias() |
252
|
|
|
{ |
253
|
|
|
$query = Query::create('article') |
254
|
|
|
->operationName('articlesQuery') |
255
|
|
|
->variables([ |
256
|
|
|
'$withTags' => 'Boolean!', |
257
|
|
|
]) |
258
|
|
|
->fields([ |
259
|
|
|
'id', |
260
|
|
|
'title', |
261
|
|
|
'body', |
262
|
|
|
// sub query with type and alias |
263
|
|
|
'articleTags' => Query::create('tags')->fields([ |
264
|
|
|
'id', |
265
|
|
|
'tagTitle' => 'title', |
266
|
|
|
]), |
267
|
|
|
]) |
268
|
|
|
->includeIf([ |
269
|
|
|
'articleTags' => '$withTags', |
270
|
|
|
]) |
271
|
|
|
; |
272
|
|
|
|
273
|
|
|
$expected = |
274
|
|
|
'query articlesQuery($withTags: Boolean!) { |
275
|
|
|
article { |
276
|
|
|
articleTags: tags @include(if: $withTags) { |
277
|
|
|
id |
278
|
|
|
tagTitle: title |
279
|
|
|
} |
280
|
|
|
body |
281
|
|
|
id |
282
|
|
|
title |
283
|
|
|
} |
284
|
|
|
} |
285
|
|
|
'; |
286
|
|
|
$this->assertEquals($expected, (string) $query); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
public function testSubqueryWithoutAlias() |
290
|
|
|
{ |
291
|
|
|
$query = Query::create('article') |
292
|
|
|
->operationName('articlesQuery') |
293
|
|
|
->variables([ |
294
|
|
|
'$withTags' => 'Boolean!', |
295
|
|
|
]) |
296
|
|
|
->fields([ |
297
|
|
|
'id', |
298
|
|
|
'title', |
299
|
|
|
'body', |
300
|
|
|
// sub query without type parameter, so we take the alias |
301
|
|
|
'tags' => Query::create()->fields([ |
302
|
|
|
'id', |
303
|
|
|
'tagTitle' => 'title', |
304
|
|
|
]), |
305
|
|
|
]) |
306
|
|
|
->includeIf([ |
307
|
|
|
'tags' => '$withTags', |
308
|
|
|
]) |
309
|
|
|
; |
310
|
|
|
|
311
|
|
|
$expected = |
312
|
|
|
'query articlesQuery($withTags: Boolean!) { |
313
|
|
|
article { |
314
|
|
|
body |
315
|
|
|
id |
316
|
|
|
tags @include(if: $withTags) { |
317
|
|
|
id |
318
|
|
|
tagTitle: title |
319
|
|
|
} |
320
|
|
|
title |
321
|
|
|
} |
322
|
|
|
} |
323
|
|
|
'; |
324
|
|
|
$this->assertEquals($expected, (string) $query); |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
public function testQueryWithFragment() |
328
|
|
|
{ |
329
|
|
|
$query = Query::create('article') |
330
|
|
|
->operationName('articlesQuery') |
331
|
|
|
->fields([ |
332
|
|
|
'id', |
333
|
|
|
'title', |
334
|
|
|
'body', |
335
|
|
|
'...imageFragment', |
336
|
|
|
]) |
337
|
|
|
->addFragment(Fragment::create('imageFragment', 'image', [ |
338
|
|
|
'height', |
339
|
|
|
'width', |
340
|
|
|
'filename', |
341
|
|
|
'size', |
342
|
|
|
'formats' => Query::create()->fields([ |
343
|
|
|
'id', |
344
|
|
|
'name', |
345
|
|
|
'url', |
346
|
|
|
]), |
347
|
|
|
])) |
348
|
|
|
; |
349
|
|
|
|
350
|
|
|
$expected = |
351
|
|
|
'query articlesQuery { |
352
|
|
|
article { |
353
|
|
|
...imageFragment |
354
|
|
|
body |
355
|
|
|
id |
356
|
|
|
title |
357
|
|
|
} |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
fragment imageFragment on image { |
361
|
|
|
filename |
362
|
|
|
formats { |
363
|
|
|
id |
364
|
|
|
name |
365
|
|
|
url |
366
|
|
|
} |
367
|
|
|
height |
368
|
|
|
size |
369
|
|
|
width |
370
|
|
|
} |
371
|
|
|
'; |
372
|
|
|
$this->assertEquals($expected, (string) $query); |
373
|
|
|
} |
374
|
|
|
} |
375
|
|
|
|