1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TheCodingMachine\GraphQL\Controllers; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Annotations\AnnotationReader; |
6
|
|
|
use GraphQL\Type\Definition\BooleanType; |
7
|
|
|
use GraphQL\Type\Definition\FloatType; |
8
|
|
|
use GraphQL\Type\Definition\IDType; |
9
|
|
|
use GraphQL\Type\Definition\InputObjectType; |
10
|
|
|
use GraphQL\Type\Definition\IntType; |
11
|
|
|
use GraphQL\Type\Definition\ListOfType; |
12
|
|
|
use GraphQL\Type\Definition\NonNull; |
13
|
|
|
use GraphQL\Type\Definition\StringType; |
14
|
|
|
use GraphQL\Type\Definition\ObjectType; |
15
|
|
|
use GraphQL\Type\Definition\UnionType; |
16
|
|
|
use TheCodingMachine\GraphQL\Controllers\Fixtures\TestController; |
17
|
|
|
use TheCodingMachine\GraphQL\Controllers\Fixtures\TestControllerNoReturnType; |
18
|
|
|
use TheCodingMachine\GraphQL\Controllers\Fixtures\TestObject; |
19
|
|
|
use TheCodingMachine\GraphQL\Controllers\Fixtures\TestType; |
20
|
|
|
use TheCodingMachine\GraphQL\Controllers\Fixtures\TestTypeId; |
21
|
|
|
use TheCodingMachine\GraphQL\Controllers\Fixtures\TestTypeMissingAnnotation; |
22
|
|
|
use TheCodingMachine\GraphQL\Controllers\Fixtures\TestTypeMissingField; |
23
|
|
|
use TheCodingMachine\GraphQL\Controllers\Fixtures\TestTypeWithSourceFieldInterface; |
24
|
|
|
use TheCodingMachine\GraphQL\Controllers\Registry\EmptyContainer; |
25
|
|
|
use TheCodingMachine\GraphQL\Controllers\Registry\Registry; |
26
|
|
|
use TheCodingMachine\GraphQL\Controllers\Security\AuthenticationServiceInterface; |
27
|
|
|
use TheCodingMachine\GraphQL\Controllers\Security\AuthorizationServiceInterface; |
28
|
|
|
use TheCodingMachine\GraphQL\Controllers\Security\VoidAuthenticationService; |
29
|
|
|
use TheCodingMachine\GraphQL\Controllers\Security\VoidAuthorizationService; |
30
|
|
|
use TheCodingMachine\GraphQL\Controllers\Annotations\Query; |
31
|
|
|
use TheCodingMachine\GraphQL\Controllers\Types\DateTimeType; |
32
|
|
|
|
33
|
|
|
class ControllerQueryProviderTest extends AbstractQueryProviderTest |
34
|
|
|
{ |
35
|
|
|
public function testQueryProvider() |
36
|
|
|
{ |
37
|
|
|
$controller = new TestController(); |
38
|
|
|
|
39
|
|
|
$queryProvider = new ControllerQueryProvider($controller, $this->getRegistry()); |
40
|
|
|
|
41
|
|
|
$queries = $queryProvider->getQueries(); |
42
|
|
|
|
43
|
|
|
$this->assertCount(6, $queries); |
44
|
|
|
$usersQuery = $queries[0]; |
45
|
|
|
$this->assertSame('test', $usersQuery->name); |
46
|
|
|
|
47
|
|
|
$this->assertCount(8, $usersQuery->args); |
48
|
|
|
$this->assertSame('int', $usersQuery->args[0]->name); |
49
|
|
|
$this->assertInstanceOf(NonNull::class, $usersQuery->args[0]->getType()); |
50
|
|
|
$this->assertInstanceOf(IntType::class, $usersQuery->args[0]->getType()->getWrappedType()); |
51
|
|
|
$this->assertInstanceOf(StringType::class, $usersQuery->args[7]->getType()); |
52
|
|
|
$this->assertInstanceOf(NonNull::class, $usersQuery->args[1]->getType()); |
53
|
|
|
$this->assertInstanceOf(ListOfType::class, $usersQuery->args[1]->getType()->getWrappedType()); |
54
|
|
|
$this->assertInstanceOf(NonNull::class, $usersQuery->args[1]->getType()->getWrappedType()->getWrappedType()); |
55
|
|
|
$this->assertInstanceOf(InputObjectType::class, $usersQuery->args[1]->getType()->getWrappedType()->getWrappedType()->getWrappedType()); |
56
|
|
|
$this->assertInstanceOf(BooleanType::class, $usersQuery->args[2]->getType()); |
57
|
|
|
$this->assertInstanceOf(FloatType::class, $usersQuery->args[3]->getType()); |
58
|
|
|
$this->assertInstanceOf(DateTimeType::class, $usersQuery->args[4]->getType()); |
59
|
|
|
$this->assertInstanceOf(DateTimeType::class, $usersQuery->args[5]->getType()); |
60
|
|
|
$this->assertInstanceOf(StringType::class, $usersQuery->args[6]->getType()); |
61
|
|
|
$this->assertSame('TestObject', $usersQuery->args[1]->getType()->getWrappedType()->getWrappedType()->getWrappedType()->name); |
62
|
|
|
|
63
|
|
|
$context = ['int' => 42, 'string' => 'foo', 'list' => [ |
64
|
|
|
['test' => 42], |
65
|
|
|
['test' => 12], |
66
|
|
|
], |
67
|
|
|
'boolean' => true, |
68
|
|
|
'float' => 4.2, |
69
|
|
|
'dateTimeImmutable' => '2017-01-01 01:01:01', |
70
|
|
|
'dateTime' => '2017-01-01 01:01:01' |
71
|
|
|
]; |
72
|
|
|
|
73
|
|
|
$resolve = $usersQuery->resolveFn; |
74
|
|
|
$result = $resolve('foo', $context); |
75
|
|
|
|
76
|
|
|
$this->assertInstanceOf(TestObject::class, $result); |
77
|
|
|
$this->assertSame('foo424212true4.22017010101010120170101010101default', $result->getTest()); |
78
|
|
|
|
79
|
|
|
unset($context['string']); // Testing null default value |
80
|
|
|
$result = $resolve('foo', $context); |
81
|
|
|
|
82
|
|
|
$this->assertSame('424212true4.22017010101010120170101010101default', $result->getTest()); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function testMutations() |
86
|
|
|
{ |
87
|
|
|
$controller = new TestController(); |
88
|
|
|
|
89
|
|
|
$queryProvider = new ControllerQueryProvider($controller, $this->getRegistry()); |
90
|
|
|
|
91
|
|
|
$mutations = $queryProvider->getMutations(); |
92
|
|
|
|
93
|
|
|
$this->assertCount(1, $mutations); |
94
|
|
|
$mutation = $mutations[0]; |
95
|
|
|
$this->assertSame('mutation', $mutation->name); |
96
|
|
|
|
97
|
|
|
$resolve = $mutation->resolveFn; |
98
|
|
|
$result = $resolve('foo', ['testObject' => ['test' => 42]]); |
99
|
|
|
|
100
|
|
|
$this->assertInstanceOf(TestObject::class, $result); |
101
|
|
|
$this->assertEquals('42', $result->getTest()); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function testErrors() |
105
|
|
|
{ |
106
|
|
|
$controller = new class |
107
|
|
|
{ |
108
|
|
|
/** |
109
|
|
|
* @Query |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
|
|
public function test($noTypeHint): string |
|
|
|
|
113
|
|
|
{ |
114
|
|
|
return 'foo'; |
115
|
|
|
} |
116
|
|
|
}; |
117
|
|
|
|
118
|
|
|
$queryProvider = new ControllerQueryProvider($controller, $this->getRegistry()); |
119
|
|
|
|
120
|
|
|
$this->expectException(MissingTypeHintException::class); |
121
|
|
|
$queryProvider->getQueries(); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function testQueryProviderWithFixedReturnType() |
125
|
|
|
{ |
126
|
|
|
$controller = new TestController(); |
127
|
|
|
|
128
|
|
|
$queryProvider = new ControllerQueryProvider($controller, $this->getRegistry()); |
129
|
|
|
|
130
|
|
|
$queries = $queryProvider->getQueries(); |
131
|
|
|
|
132
|
|
|
$this->assertCount(6, $queries); |
133
|
|
|
$fixedQuery = $queries[1]; |
134
|
|
|
|
135
|
|
|
$this->assertInstanceOf(ObjectType::class, $fixedQuery->getType()); |
136
|
|
|
$this->assertSame('Test', $fixedQuery->getType()->name); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function testNameFromAnnotation() |
140
|
|
|
{ |
141
|
|
|
$controller = new TestController(); |
142
|
|
|
|
143
|
|
|
$queryProvider = new ControllerQueryProvider($controller, $this->getRegistry()); |
144
|
|
|
|
145
|
|
|
$queries = $queryProvider->getQueries(); |
146
|
|
|
|
147
|
|
|
$query = $queries[2]; |
148
|
|
|
|
149
|
|
|
$this->assertSame('nameFromAnnotation', $query->name); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function testSourceField() |
153
|
|
|
{ |
154
|
|
|
$controller = new TestType($this->getRegistry()); |
|
|
|
|
155
|
|
|
|
156
|
|
|
$queryProvider = new ControllerQueryProvider($controller, $this->getRegistry()); |
157
|
|
|
|
158
|
|
|
$fields = $queryProvider->getFields(); |
159
|
|
|
|
160
|
|
|
$this->assertCount(2, $fields); |
161
|
|
|
|
162
|
|
|
$this->assertSame('customField', $fields[0]->name); |
163
|
|
|
$this->assertSame('test', $fields[1]->name); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
public function testLoggedInSourceField() |
167
|
|
|
{ |
168
|
|
|
$registry = new Registry(new EmptyContainer(), |
169
|
|
|
new VoidAuthorizationService(), |
170
|
|
|
new class implements AuthenticationServiceInterface { |
171
|
|
|
public function isLogged(): bool |
172
|
|
|
{ |
173
|
|
|
return true; |
174
|
|
|
} |
175
|
|
|
}, |
176
|
|
|
new AnnotationReader(), |
177
|
|
|
$this->getTypeMapper(), |
178
|
|
|
$this->getHydrator()); |
179
|
|
|
|
180
|
|
|
$queryProvider = new ControllerQueryProvider(new TestType($this->getRegistry()), $registry); |
|
|
|
|
181
|
|
|
$fields = $queryProvider->getFields(); |
182
|
|
|
$this->assertCount(3, $fields); |
183
|
|
|
|
184
|
|
|
$this->assertSame('testBool', $fields[2]->name); |
185
|
|
|
|
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
public function testRightInSourceField() |
189
|
|
|
{ |
190
|
|
|
$registry = new Registry(new EmptyContainer(), |
191
|
|
|
new class implements AuthorizationServiceInterface { |
192
|
|
|
public function isAllowed(string $right): bool |
193
|
|
|
{ |
194
|
|
|
return true; |
195
|
|
|
} |
196
|
|
|
}, |
197
|
|
|
new VoidAuthenticationService(), |
198
|
|
|
new AnnotationReader(), |
199
|
|
|
$this->getTypeMapper(), |
200
|
|
|
$this->getHydrator()); |
201
|
|
|
|
202
|
|
|
$queryProvider = new ControllerQueryProvider(new TestType(), $registry); |
203
|
|
|
$fields = $queryProvider->getFields(); |
204
|
|
|
$this->assertCount(3, $fields); |
205
|
|
|
|
206
|
|
|
$this->assertSame('testRight', $fields[2]->name); |
207
|
|
|
|
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
public function testMissingTypeAnnotation() |
211
|
|
|
{ |
212
|
|
|
$queryProvider = new ControllerQueryProvider(new TestTypeMissingAnnotation(), $this->getRegistry()); |
213
|
|
|
|
214
|
|
|
$this->expectException(MissingAnnotationException::class); |
215
|
|
|
$queryProvider->getFields(); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
public function testSourceFieldDoesNotExists() |
219
|
|
|
{ |
220
|
|
|
$queryProvider = new ControllerQueryProvider(new TestTypeMissingField(), $this->getRegistry()); |
221
|
|
|
|
222
|
|
|
$this->expectException(FieldNotFoundException::class); |
223
|
|
|
$this->expectExceptionMessage("There is an issue with a @SourceField annotation in class \"TheCodingMachine\GraphQL\Controllers\Fixtures\TestTypeMissingField\": Could not find a getter or a isser for field \"notExists\". Looked for: \"TheCodingMachine\GraphQL\Controllers\Fixtures\TestObject::getNotExists()\", \"TheCodingMachine\GraphQL\Controllers\Fixtures\TestObject::isNotExists()"); |
224
|
|
|
$queryProvider->getFields(); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
public function testSourceFieldIsId() |
228
|
|
|
{ |
229
|
|
|
$queryProvider = new ControllerQueryProvider(new TestTypeId(), $this->getRegistry()); |
230
|
|
|
$fields = $queryProvider->getFields(); |
231
|
|
|
$this->assertCount(1, $fields); |
232
|
|
|
|
233
|
|
|
$this->assertSame('test', $fields[0]->name); |
234
|
|
|
$this->assertInstanceOf(NonNull::class, $fields[0]->getType()); |
235
|
|
|
$this->assertInstanceOf(IDType::class, $fields[0]->getType()->getWrappedType()); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
public function testFromSourceFieldsInterface() |
239
|
|
|
{ |
240
|
|
|
$registry = new Registry(new EmptyContainer(), |
241
|
|
|
new class implements AuthorizationServiceInterface { |
242
|
|
|
public function isAllowed(string $right): bool |
243
|
|
|
{ |
244
|
|
|
return true; |
245
|
|
|
} |
246
|
|
|
}, |
247
|
|
|
new VoidAuthenticationService(), |
248
|
|
|
new AnnotationReader(), |
249
|
|
|
$this->getTypeMapper(), |
250
|
|
|
$this->getHydrator()); |
251
|
|
|
|
252
|
|
|
$queryProvider = new ControllerQueryProvider(new TestTypeWithSourceFieldInterface(), $registry); |
253
|
|
|
$fields = $queryProvider->getFields(); |
254
|
|
|
$this->assertCount(1, $fields); |
255
|
|
|
|
256
|
|
|
$this->assertSame('test', $fields[0]->name); |
257
|
|
|
|
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
public function testQueryProviderWithIterableClass() |
261
|
|
|
{ |
262
|
|
|
$controller = new TestController(); |
263
|
|
|
|
264
|
|
|
$queryProvider = new ControllerQueryProvider($controller, $this->getRegistry()); |
265
|
|
|
|
266
|
|
|
$queries = $queryProvider->getQueries(); |
267
|
|
|
|
268
|
|
|
$this->assertCount(6, $queries); |
269
|
|
|
$iterableQuery = $queries[3]; |
270
|
|
|
|
271
|
|
|
$this->assertInstanceOf(NonNull::class, $iterableQuery->getType()); |
272
|
|
|
$this->assertInstanceOf(ListOfType::class, $iterableQuery->getType()->getWrappedType()); |
273
|
|
|
$this->assertInstanceOf(NonNull::class, $iterableQuery->getType()->getWrappedType()->getWrappedType()); |
274
|
|
|
$this->assertInstanceOf(ObjectType::class, $iterableQuery->getType()->getWrappedType()->getWrappedType()->getWrappedType()); |
275
|
|
|
$this->assertSame('TestObject', $iterableQuery->getType()->getWrappedType()->getWrappedType()->getWrappedType()->name); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
public function testQueryProviderWithIterable() |
279
|
|
|
{ |
280
|
|
|
$controller = new TestController(); |
281
|
|
|
|
282
|
|
|
$queryProvider = new ControllerQueryProvider($controller, $this->getRegistry()); |
283
|
|
|
|
284
|
|
|
$queries = $queryProvider->getQueries(); |
285
|
|
|
|
286
|
|
|
$this->assertCount(6, $queries); |
287
|
|
|
$iterableQuery = $queries[4]; |
288
|
|
|
|
289
|
|
|
$this->assertInstanceOf(NonNull::class, $iterableQuery->getType()); |
290
|
|
|
$this->assertInstanceOf(ListOfType::class, $iterableQuery->getType()->getWrappedType()); |
291
|
|
|
$this->assertInstanceOf(NonNull::class, $iterableQuery->getType()->getWrappedType()->getWrappedType()); |
292
|
|
|
$this->assertInstanceOf(ObjectType::class, $iterableQuery->getType()->getWrappedType()->getWrappedType()->getWrappedType()); |
293
|
|
|
$this->assertSame('TestObject', $iterableQuery->getType()->getWrappedType()->getWrappedType()->getWrappedType()->name); |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
public function testNoReturnTypeError() |
297
|
|
|
{ |
298
|
|
|
$queryProvider = new ControllerQueryProvider(new TestControllerNoReturnType(), $this->getRegistry()); |
299
|
|
|
$this->expectException(TypeMappingException::class); |
300
|
|
|
$queryProvider->getQueries(); |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
public function testQueryProviderWithUnion() |
304
|
|
|
{ |
305
|
|
|
$controller = new TestController(); |
306
|
|
|
|
307
|
|
|
$queryProvider = new ControllerQueryProvider($controller, $this->getRegistry()); |
308
|
|
|
|
309
|
|
|
$queries = $queryProvider->getQueries(); |
310
|
|
|
|
311
|
|
|
$this->assertCount(6, $queries); |
312
|
|
|
$unionQuery = $queries[5]; |
313
|
|
|
|
314
|
|
|
$this->assertInstanceOf(NonNull::class, $unionQuery->getType()); |
315
|
|
|
$this->assertInstanceOf(UnionType::class, $unionQuery->getType()->getWrappedType()); |
316
|
|
|
$this->assertInstanceOf(ObjectType::class, $unionQuery->getType()->getWrappedType()->getTypes()[0]); |
317
|
|
|
$this->assertSame('TestObject', $unionQuery->getType()->getWrappedType()->getTypes()[0]->name); |
318
|
|
|
$this->assertInstanceOf(ObjectType::class, $unionQuery->getType()->getWrappedType()->getTypes()[1]); |
319
|
|
|
$this->assertSame('TestObject2', $unionQuery->getType()->getWrappedType()->getTypes()[1]->name); |
320
|
|
|
} |
321
|
|
|
} |
322
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.