1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace GraphQLTests\Doctrine\Definition\Operator; |
6
|
|
|
|
7
|
|
|
use GraphQL\Doctrine\Definition\Operator\AbstractOperator; |
8
|
|
|
use GraphQL\Doctrine\Definition\Operator\BetweenOperatorType; |
9
|
|
|
use GraphQL\Doctrine\Definition\Operator\EmptyOperatorType; |
10
|
|
|
use GraphQL\Doctrine\Definition\Operator\EqualOperatorType; |
11
|
|
|
use GraphQL\Doctrine\Definition\Operator\GreaterOperatorType; |
12
|
|
|
use GraphQL\Doctrine\Definition\Operator\GreaterOrEqualOperatorType; |
13
|
|
|
use GraphQL\Doctrine\Definition\Operator\GroupOperatorType; |
14
|
|
|
use GraphQL\Doctrine\Definition\Operator\HaveOperatorType; |
15
|
|
|
use GraphQL\Doctrine\Definition\Operator\InOperatorType; |
16
|
|
|
use GraphQL\Doctrine\Definition\Operator\LessOperatorType; |
17
|
|
|
use GraphQL\Doctrine\Definition\Operator\LessOrEqualOperatorType; |
18
|
|
|
use GraphQL\Doctrine\Definition\Operator\LikeOperatorType; |
19
|
|
|
use GraphQL\Doctrine\Definition\Operator\NullOperatorType; |
20
|
|
|
use GraphQL\Doctrine\Factory\UniqueNameFactory; |
21
|
|
|
use GraphQL\Type\Definition\Type; |
22
|
|
|
use GraphQLTests\Doctrine\Blog\Model\User; |
23
|
|
|
use GraphQLTests\Doctrine\TypesTrait; |
24
|
|
|
|
25
|
|
|
final class OperatorsTest extends \PHPUnit\Framework\TestCase |
26
|
|
|
{ |
27
|
|
|
use TypesTrait; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @dataProvider providerOperator |
31
|
|
|
* |
32
|
|
|
* @param string $expected |
33
|
|
|
* @param string $className |
34
|
|
|
* @param array $args |
35
|
|
|
* @param string $field |
36
|
|
|
*/ |
37
|
|
|
public function testOperator(?string $expected, string $className, ?array $args, string $field = 'field'): void |
38
|
|
|
{ |
39
|
|
|
/** @var AbstractOperator $operator */ |
40
|
|
|
$operator = new $className($this->types, Type::string()); |
41
|
|
|
$uniqueNameFactory = new UniqueNameFactory(); |
42
|
|
|
$metadata = $this->entityManager->getClassMetadata(User::class); |
43
|
|
|
$queryBuilder = $this->entityManager->createQueryBuilder(); |
44
|
|
|
$alias = 'alias'; |
45
|
|
|
|
46
|
|
|
$actual = $operator->getDqlCondition($uniqueNameFactory, $metadata, $queryBuilder, $alias, $field, $args); |
47
|
|
|
|
48
|
|
|
self::assertSame($expected, $actual, 'DQL condition should match'); |
49
|
|
|
|
50
|
|
|
if (is_string($expected)) { |
51
|
|
|
self::assertSame(mb_substr_count($expected, ':'), $queryBuilder->getParameters()->count(), 'should declare the same number of parameters that are actually used'); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if ($className === GroupOperatorType::class) { |
55
|
|
|
self::assertCount(1, $queryBuilder->getDQLPart('groupBy')); |
56
|
|
|
self::assertSame(['alias.field'], $queryBuilder->getDQLPart('groupBy')[0]->getParts()); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function providerOperator(): array |
61
|
|
|
{ |
62
|
|
|
return [ |
63
|
|
|
[ |
64
|
|
|
null, |
65
|
|
|
BetweenOperatorType::class, |
66
|
|
|
null, |
67
|
|
|
], |
68
|
|
|
['alias.field BETWEEN :filter1 AND :filter2', |
69
|
|
|
BetweenOperatorType::class, |
70
|
|
|
[ |
71
|
|
|
'from' => 123, |
72
|
|
|
'to' => 456, |
73
|
|
|
'not' => false, |
74
|
|
|
], |
75
|
|
|
], |
76
|
|
|
[ |
77
|
|
|
'alias.field NOT BETWEEN :filter1 AND :filter2', |
78
|
|
|
BetweenOperatorType::class, |
79
|
|
|
[ |
80
|
|
|
'from' => 123, |
81
|
|
|
'to' => 456, |
82
|
|
|
'not' => true, |
83
|
|
|
], |
84
|
|
|
], |
85
|
|
|
[ |
86
|
|
|
null, |
87
|
|
|
HaveOperatorType::class, |
88
|
|
|
null, |
89
|
|
|
'posts', |
90
|
|
|
], |
91
|
|
|
[ |
92
|
|
|
':filter1 MEMBER OF alias.posts', |
93
|
|
|
HaveOperatorType::class, |
94
|
|
|
[ |
95
|
|
|
'values' => [123, 456], |
96
|
|
|
'not' => false, |
97
|
|
|
], |
98
|
|
|
'posts', |
99
|
|
|
], |
100
|
|
|
[ |
101
|
|
|
':filter1 NOT MEMBER OF alias.posts', |
102
|
|
|
HaveOperatorType::class, |
103
|
|
|
[ |
104
|
|
|
'values' => [123, 456], |
105
|
|
|
'not' => true, |
106
|
|
|
], |
107
|
|
|
'posts', |
108
|
|
|
], |
109
|
|
|
[ |
110
|
|
|
null, |
111
|
|
|
HaveOperatorType::class, |
112
|
|
|
null, |
113
|
|
|
'manager', |
114
|
|
|
], |
115
|
|
|
[ |
116
|
|
|
'alias.manager IN (:filter1)', |
117
|
|
|
HaveOperatorType::class, |
118
|
|
|
[ |
119
|
|
|
'values' => [123, 456], |
120
|
|
|
'not' => false, |
121
|
|
|
], |
122
|
|
|
'manager', |
123
|
|
|
], |
124
|
|
|
[ |
125
|
|
|
'alias.manager NOT IN (:filter1)', |
126
|
|
|
HaveOperatorType::class, |
127
|
|
|
[ |
128
|
|
|
'values' => [123, 456], |
129
|
|
|
'not' => true, |
130
|
|
|
], |
131
|
|
|
'manager', |
132
|
|
|
], |
133
|
|
|
[ |
134
|
|
|
null, |
135
|
|
|
EmptyOperatorType::class, |
136
|
|
|
null, |
137
|
|
|
'posts', |
138
|
|
|
], |
139
|
|
|
[ |
140
|
|
|
'alias.posts IS EMPTY', |
141
|
|
|
EmptyOperatorType::class, |
142
|
|
|
[ |
143
|
|
|
'not' => false, |
144
|
|
|
], |
145
|
|
|
'posts', |
146
|
|
|
], |
147
|
|
|
[ |
148
|
|
|
'alias.posts IS NOT EMPTY', |
149
|
|
|
EmptyOperatorType::class, |
150
|
|
|
[ |
151
|
|
|
'not' => true, |
152
|
|
|
], |
153
|
|
|
'posts', |
154
|
|
|
], |
155
|
|
|
[ |
156
|
|
|
null, |
157
|
|
|
EmptyOperatorType::class, |
158
|
|
|
null, |
159
|
|
|
'manager', |
160
|
|
|
], |
161
|
|
|
[ |
162
|
|
|
'alias.manager IS NULL', |
163
|
|
|
EmptyOperatorType::class, |
164
|
|
|
[ |
165
|
|
|
'not' => false, |
166
|
|
|
], |
167
|
|
|
'manager', |
168
|
|
|
], |
169
|
|
|
[ |
170
|
|
|
'alias.manager IS NOT NULL', |
171
|
|
|
EmptyOperatorType::class, |
172
|
|
|
[ |
173
|
|
|
'not' => true, |
174
|
|
|
], |
175
|
|
|
'manager', |
176
|
|
|
], |
177
|
|
|
[ |
178
|
|
|
null, |
179
|
|
|
EqualOperatorType::class, |
180
|
|
|
null, |
181
|
|
|
], |
182
|
|
|
[ |
183
|
|
|
'alias.field = :filter1', |
184
|
|
|
EqualOperatorType::class, |
185
|
|
|
[ |
186
|
|
|
'value' => 123, |
187
|
|
|
'not' => false, |
188
|
|
|
], |
189
|
|
|
], |
190
|
|
|
[ |
191
|
|
|
'alias.field != :filter1', |
192
|
|
|
EqualOperatorType::class, |
193
|
|
|
[ |
194
|
|
|
'value' => 123, |
195
|
|
|
'not' => true, |
196
|
|
|
], |
197
|
|
|
], |
198
|
|
|
[ |
199
|
|
|
null, |
200
|
|
|
GreaterOperatorType::class, |
201
|
|
|
null, |
202
|
|
|
], |
203
|
|
|
[ |
204
|
|
|
'alias.field > :filter1', |
205
|
|
|
GreaterOperatorType::class, |
206
|
|
|
[ |
207
|
|
|
'value' => 123, |
208
|
|
|
'not' => false, |
209
|
|
|
], |
210
|
|
|
], |
211
|
|
|
[ |
212
|
|
|
'alias.field <= :filter1', |
213
|
|
|
GreaterOperatorType::class, |
214
|
|
|
[ |
215
|
|
|
'value' => 123, |
216
|
|
|
'not' => true, |
217
|
|
|
], |
218
|
|
|
], |
219
|
|
|
[ |
220
|
|
|
null, |
221
|
|
|
GreaterOrEqualOperatorType::class, |
222
|
|
|
null, |
223
|
|
|
], |
224
|
|
|
[ |
225
|
|
|
'alias.field >= :filter1', |
226
|
|
|
GreaterOrEqualOperatorType::class, |
227
|
|
|
[ |
228
|
|
|
'value' => 123, |
229
|
|
|
'not' => false, |
230
|
|
|
], |
231
|
|
|
], |
232
|
|
|
[ |
233
|
|
|
'alias.field < :filter1', |
234
|
|
|
GreaterOrEqualOperatorType::class, |
235
|
|
|
[ |
236
|
|
|
'value' => 123, |
237
|
|
|
'not' => true, |
238
|
|
|
], |
239
|
|
|
], |
240
|
|
|
[ |
241
|
|
|
null, |
242
|
|
|
InOperatorType::class, |
243
|
|
|
null, |
244
|
|
|
], |
245
|
|
|
[ |
246
|
|
|
'alias.field IN (:filter1)', |
247
|
|
|
InOperatorType::class, |
248
|
|
|
[ |
249
|
|
|
'values' => [123, 456], |
250
|
|
|
'not' => false, |
251
|
|
|
], |
252
|
|
|
], |
253
|
|
|
[ |
254
|
|
|
'alias.field NOT IN (:filter1)', |
255
|
|
|
InOperatorType::class, |
256
|
|
|
[ |
257
|
|
|
'values' => [123, 456], |
258
|
|
|
'not' => true, |
259
|
|
|
], |
260
|
|
|
], |
261
|
|
|
[ |
262
|
|
|
null, |
263
|
|
|
LessOperatorType::class, |
264
|
|
|
null, |
265
|
|
|
], |
266
|
|
|
[ |
267
|
|
|
'alias.field < :filter1', |
268
|
|
|
LessOperatorType::class, |
269
|
|
|
[ |
270
|
|
|
'value' => 123, |
271
|
|
|
'not' => false, |
272
|
|
|
], |
273
|
|
|
], |
274
|
|
|
[ |
275
|
|
|
'alias.field >= :filter1', |
276
|
|
|
LessOperatorType::class, |
277
|
|
|
[ |
278
|
|
|
'value' => 123, |
279
|
|
|
'not' => true, |
280
|
|
|
], |
281
|
|
|
], |
282
|
|
|
[ |
283
|
|
|
null, |
284
|
|
|
LessOrEqualOperatorType::class, |
285
|
|
|
null, |
286
|
|
|
], |
287
|
|
|
[ |
288
|
|
|
'alias.field <= :filter1', |
289
|
|
|
LessOrEqualOperatorType::class, |
290
|
|
|
[ |
291
|
|
|
'value' => 123, |
292
|
|
|
'not' => false, |
293
|
|
|
], |
294
|
|
|
], |
295
|
|
|
[ |
296
|
|
|
'alias.field > :filter1', |
297
|
|
|
LessOrEqualOperatorType::class, |
298
|
|
|
[ |
299
|
|
|
'value' => 123, |
300
|
|
|
'not' => true, |
301
|
|
|
], |
302
|
|
|
], |
303
|
|
|
[ |
304
|
|
|
null, |
305
|
|
|
LikeOperatorType::class, |
306
|
|
|
null, |
307
|
|
|
], |
308
|
|
|
[ |
309
|
|
|
'alias.field LIKE :filter1', |
310
|
|
|
LikeOperatorType::class, |
311
|
|
|
[ |
312
|
|
|
'value' => 123, |
313
|
|
|
'not' => false, |
314
|
|
|
], |
315
|
|
|
], |
316
|
|
|
[ |
317
|
|
|
'alias.field NOT LIKE :filter1', |
318
|
|
|
LikeOperatorType::class, |
319
|
|
|
[ |
320
|
|
|
'value' => 123, |
321
|
|
|
'not' => true, |
322
|
|
|
], |
323
|
|
|
], |
324
|
|
|
[ |
325
|
|
|
null, |
326
|
|
|
NullOperatorType::class, |
327
|
|
|
null, |
328
|
|
|
], |
329
|
|
|
[ |
330
|
|
|
'alias.field IS NULL', |
331
|
|
|
NullOperatorType::class, |
332
|
|
|
[ |
333
|
|
|
'value' => 123, |
334
|
|
|
'not' => false, |
335
|
|
|
], |
336
|
|
|
], |
337
|
|
|
[ |
338
|
|
|
'alias.field IS NOT NULL', |
339
|
|
|
NullOperatorType::class, |
340
|
|
|
[ |
341
|
|
|
'value' => 123, |
342
|
|
|
'not' => true, |
343
|
|
|
], |
344
|
|
|
], |
345
|
|
|
[ |
346
|
|
|
null, |
347
|
|
|
GroupOperatorType::class, |
348
|
|
|
null, |
349
|
|
|
], |
350
|
|
|
[ |
351
|
|
|
null, |
352
|
|
|
GroupOperatorType::class, |
353
|
|
|
[ |
354
|
|
|
'value' => null, |
355
|
|
|
], |
356
|
|
|
], |
357
|
|
|
[ |
358
|
|
|
null, |
359
|
|
|
GroupOperatorType::class, |
360
|
|
|
[ |
361
|
|
|
'value' => true, |
362
|
|
|
], |
363
|
|
|
], |
364
|
|
|
]; |
365
|
|
|
} |
366
|
|
|
} |
367
|
|
|
|