1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace TheCodingMachine\GraphQL\Controllers; |
5
|
|
|
|
6
|
|
|
use Doctrine\Common\Annotations\Annotation; |
7
|
|
|
use Doctrine\Common\Annotations\AnnotationReader as DoctrineAnnotationReader; |
8
|
|
|
use GraphQL\Type\Definition\InputObjectType; |
9
|
|
|
use GraphQL\Type\Definition\InputType; |
10
|
|
|
use GraphQL\Type\Definition\ObjectType; |
11
|
|
|
use GraphQL\Type\Definition\OutputType; |
12
|
|
|
use GraphQL\Type\Definition\StringType; |
13
|
|
|
use GraphQL\Type\Definition\Type; |
14
|
|
|
use Mouf\Picotainer\Picotainer; |
15
|
|
|
use PHPUnit\Framework\TestCase; |
16
|
|
|
use Psr\Container\ContainerInterface; |
17
|
|
|
use Symfony\Component\Cache\Simple\ArrayCache; |
18
|
|
|
use TheCodingMachine\GraphQL\Controllers\Fixtures\TestObject; |
19
|
|
|
use TheCodingMachine\GraphQL\Controllers\Fixtures\TestObject2; |
20
|
|
|
use TheCodingMachine\GraphQL\Controllers\Fixtures\TestObjectWithRecursiveList; |
|
|
|
|
21
|
|
|
use TheCodingMachine\GraphQL\Controllers\Fixtures\Types\TestFactory; |
22
|
|
|
use TheCodingMachine\GraphQL\Controllers\Hydrators\HydratorInterface; |
23
|
|
|
use TheCodingMachine\GraphQL\Controllers\Mappers\CannotMapTypeException; |
24
|
|
|
use TheCodingMachine\GraphQL\Controllers\Mappers\RecursiveTypeMapper; |
25
|
|
|
use TheCodingMachine\GraphQL\Controllers\Mappers\RecursiveTypeMapperInterface; |
26
|
|
|
use TheCodingMachine\GraphQL\Controllers\Mappers\TypeMapperInterface; |
27
|
|
|
use TheCodingMachine\GraphQL\Controllers\Containers\EmptyContainer; |
28
|
|
|
use TheCodingMachine\GraphQL\Controllers\Containers\BasicAutoWiringContainer; |
29
|
|
|
use TheCodingMachine\GraphQL\Controllers\Reflection\CachedDocBlockFactory; |
30
|
|
|
use TheCodingMachine\GraphQL\Controllers\Security\VoidAuthenticationService; |
31
|
|
|
use TheCodingMachine\GraphQL\Controllers\Security\VoidAuthorizationService; |
32
|
|
|
use TheCodingMachine\GraphQL\Controllers\Types\ResolvableInputObjectType; |
33
|
|
|
use TheCodingMachine\GraphQL\Controllers\Types\TypeResolver; |
34
|
|
|
|
35
|
|
|
abstract class AbstractQueryProviderTest extends TestCase |
36
|
|
|
{ |
37
|
|
|
private $testObjectType; |
38
|
|
|
private $testObjectType2; |
39
|
|
|
private $inputTestObjectType; |
40
|
|
|
private $inputTestObjectType2; |
|
|
|
|
41
|
|
|
private $typeMapper; |
42
|
|
|
private $hydrator; |
43
|
|
|
private $registry; |
44
|
|
|
private $typeGenerator; |
45
|
|
|
private $inputTypeGenerator; |
46
|
|
|
private $inputTypeUtils; |
47
|
|
|
private $controllerQueryProviderFactory; |
48
|
|
|
private $annotationReader; |
49
|
|
|
private $typeResolver; |
50
|
|
|
|
51
|
|
|
protected function getTestObjectType() |
52
|
|
|
{ |
53
|
|
|
if ($this->testObjectType === null) { |
54
|
|
|
$this->testObjectType = new ObjectType([ |
55
|
|
|
'name' => 'TestObject', |
56
|
|
|
'fields' => [ |
57
|
|
|
'test' => Type::string(), |
58
|
|
|
], |
59
|
|
|
]); |
60
|
|
|
} |
61
|
|
|
return $this->testObjectType; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
protected function getTestObjectType2() |
65
|
|
|
{ |
66
|
|
|
if ($this->testObjectType2 === null) { |
67
|
|
|
$this->testObjectType2 = new ObjectType([ |
68
|
|
|
'name' => 'TestObject2', |
69
|
|
|
'fields' => [ |
70
|
|
|
'test' => Type::string(), |
71
|
|
|
], |
72
|
|
|
]); |
73
|
|
|
} |
74
|
|
|
return $this->testObjectType2; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
protected function getInputTestObjectType() |
78
|
|
|
{ |
79
|
|
|
if ($this->inputTestObjectType === null) { |
80
|
|
|
$this->inputTestObjectType = new InputObjectType([ |
81
|
|
|
'name' => 'TestObjectInput', |
82
|
|
|
'fields' => [ |
83
|
|
|
'test' => Type::string(), |
84
|
|
|
], |
85
|
|
|
]); |
86
|
|
|
} |
87
|
|
|
return $this->inputTestObjectType; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/*protected function getInputTestObjectType2() |
91
|
|
|
{ |
92
|
|
|
if ($this->inputTestObjectType2 === null) { |
93
|
|
|
$this->inputTestObjectType2 = new ResolvableInputObjectType('TestObjectInput2', $this->getControllerQueryProviderFactory(), $this->getTypeMapper(), new TestFactory(), 'myRecursiveFactory', $this->getHydrator(), null); |
94
|
|
|
} |
95
|
|
|
return $this->inputTestObjectType2; |
96
|
|
|
}*/ |
97
|
|
|
|
98
|
|
|
protected function getTypeMapper() |
99
|
|
|
{ |
100
|
|
|
if ($this->typeMapper === null) { |
101
|
|
|
$this->typeMapper = new RecursiveTypeMapper(new class($this->getTestObjectType(), $this->getTestObjectType2(), $this->getInputTestObjectType()/*, $this->getInputTestObjectType2()*/) implements TypeMapperInterface { |
102
|
|
|
/** |
103
|
|
|
* @var ObjectType |
104
|
|
|
*/ |
105
|
|
|
private $testObjectType; |
106
|
|
|
/** |
107
|
|
|
* @var ObjectType |
108
|
|
|
*/ |
109
|
|
|
private $testObjectType2; |
110
|
|
|
/** |
111
|
|
|
* @var InputObjectType |
112
|
|
|
*/ |
113
|
|
|
private $inputTestObjectType; |
114
|
|
|
/** |
115
|
|
|
* @var InputObjectType |
116
|
|
|
*/ |
117
|
|
|
// private $inputTestObjectType2; |
118
|
|
|
|
119
|
|
|
public function __construct(ObjectType $testObjectType, ObjectType $testObjectType2, InputObjectType $inputTestObjectType/*, InputObjectType $inputTestObjectType2*/) |
120
|
|
|
{ |
121
|
|
|
$this->testObjectType = $testObjectType; |
122
|
|
|
$this->testObjectType2 = $testObjectType2; |
123
|
|
|
$this->inputTestObjectType = $inputTestObjectType; |
124
|
|
|
//$this->inputTestObjectType2 = $inputTestObjectType2; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function mapClassToType(string $className, RecursiveTypeMapperInterface $recursiveTypeMapper): ObjectType |
128
|
|
|
{ |
129
|
|
|
if ($className === TestObject::class) { |
130
|
|
|
return $this->testObjectType; |
131
|
|
|
} elseif ($className === TestObject2::class) { |
132
|
|
|
return $this->testObjectType2; |
133
|
|
|
} else { |
134
|
|
|
throw CannotMapTypeException::createForType($className); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function mapClassToInputType(string $className, RecursiveTypeMapperInterface $recursiveTypeMapper): InputObjectType |
139
|
|
|
{ |
140
|
|
|
if ($className === TestObject::class) { |
141
|
|
|
return $this->inputTestObjectType; |
142
|
|
|
} /*elseif ($className === TestObjectWithRecursiveList::class) { |
143
|
|
|
return $this->inputTestObjectType2; |
144
|
|
|
} */else { |
145
|
|
|
throw CannotMapTypeException::createForInputType($className); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
public function canMapClassToType(string $className): bool |
150
|
|
|
{ |
151
|
|
|
return $className === TestObject::class || $className === TestObject2::class; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Returns true if this type mapper can map the $className FQCN to a GraphQL input type. |
156
|
|
|
* |
157
|
|
|
* @param string $className |
158
|
|
|
* @return bool |
159
|
|
|
*/ |
160
|
|
|
public function canMapClassToInputType(string $className): bool |
161
|
|
|
{ |
162
|
|
|
return $className === TestObject::class || $className === TestObject2::class; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Returns the list of classes that have matching input GraphQL types. |
167
|
|
|
* |
168
|
|
|
* @return string[] |
169
|
|
|
*/ |
170
|
|
|
public function getSupportedClasses(): array |
171
|
|
|
{ |
172
|
|
|
return [TestObject::class, TestObject2::class]; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Returns a GraphQL type by name (can be either an input or output type) |
177
|
|
|
* |
178
|
|
|
* @param string $typeName The name of the GraphQL type |
179
|
|
|
* @return Type&(InputType|OutputType) |
180
|
|
|
* @throws CannotMapTypeException |
181
|
|
|
*/ |
182
|
|
|
public function mapNameToType(string $typeName, RecursiveTypeMapperInterface $recursiveTypeMapper): Type |
183
|
|
|
{ |
184
|
|
|
switch ($typeName) { |
185
|
|
|
case 'TestObject': |
186
|
|
|
return $this->testObjectType; |
187
|
|
|
case 'TestObject2': |
188
|
|
|
return $this->testObjectType2; |
189
|
|
|
default: |
190
|
|
|
throw CannotMapTypeException::createForName($typeName); |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Returns true if this type mapper can map the $typeName GraphQL name to a GraphQL type. |
196
|
|
|
* |
197
|
|
|
* @param string $typeName The name of the GraphQL type |
198
|
|
|
* @return bool |
199
|
|
|
*/ |
200
|
|
|
public function canMapNameToType(string $typeName): bool |
201
|
|
|
{ |
202
|
|
|
return $typeName === 'TestObject' || $typeName === 'TestObject2'; |
203
|
|
|
} |
204
|
|
|
}, new NamingStrategy(), new ArrayCache()); |
205
|
|
|
} |
206
|
|
|
return $this->typeMapper; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
protected function getHydrator(): HydratorInterface |
210
|
|
|
{ |
211
|
|
|
if ($this->hydrator === null) { |
212
|
|
|
$this->hydrator = new class implements HydratorInterface { |
213
|
|
|
public function hydrate(array $data, InputObjectType $type) |
214
|
|
|
{ |
215
|
|
|
return new TestObject($data['test']); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
public function canHydrate(array $data, InputObjectType $type): bool |
219
|
|
|
{ |
220
|
|
|
return true; |
221
|
|
|
} |
222
|
|
|
}; |
223
|
|
|
} |
224
|
|
|
return $this->hydrator; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
protected function getRegistry() |
228
|
|
|
{ |
229
|
|
|
if ($this->registry === null) { |
230
|
|
|
$this->registry = $this->buildAutoWiringContainer(new Picotainer([ |
231
|
|
|
/*'customOutput' => function() { |
232
|
|
|
return new StringType(); |
233
|
|
|
}*/ |
234
|
|
|
])); |
235
|
|
|
} |
236
|
|
|
return $this->registry; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
protected function buildAutoWiringContainer(ContainerInterface $container): BasicAutoWiringContainer |
240
|
|
|
{ |
241
|
|
|
return new BasicAutoWiringContainer($container); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
protected function getAnnotationReader(): AnnotationReader |
245
|
|
|
{ |
246
|
|
|
if ($this->annotationReader === null) { |
247
|
|
|
$this->annotationReader = new AnnotationReader(new DoctrineAnnotationReader()); |
248
|
|
|
} |
249
|
|
|
return $this->annotationReader; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
protected function buildFieldsBuilder(): FieldsBuilder |
253
|
|
|
{ |
254
|
|
|
return new FieldsBuilder( |
255
|
|
|
$this->getAnnotationReader(), |
256
|
|
|
$this->getTypeMapper(), |
257
|
|
|
$this->getHydrator(), |
258
|
|
|
new VoidAuthenticationService(), |
259
|
|
|
new VoidAuthorizationService(), |
260
|
|
|
$this->getTypeResolver(), |
261
|
|
|
new CachedDocBlockFactory(new ArrayCache()) |
262
|
|
|
); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
protected function getTypeGenerator(): TypeGenerator |
266
|
|
|
{ |
267
|
|
|
if ($this->typeGenerator === null) { |
268
|
|
|
$this->typeGenerator = new TypeGenerator($this->getAnnotationReader(), $this->getControllerQueryProviderFactory(), new NamingStrategy()); |
269
|
|
|
} |
270
|
|
|
return $this->typeGenerator; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
protected function getInputTypeGenerator(): InputTypeGenerator |
274
|
|
|
{ |
275
|
|
|
if ($this->inputTypeGenerator === null) { |
276
|
|
|
$this->inputTypeGenerator = new InputTypeGenerator($this->getInputTypeUtils(), $this->getControllerQueryProviderFactory(), $this->getHydrator()); |
277
|
|
|
} |
278
|
|
|
return $this->inputTypeGenerator; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
protected function getInputTypeUtils(): InputTypeUtils |
282
|
|
|
{ |
283
|
|
|
if ($this->inputTypeUtils === null) { |
284
|
|
|
$this->inputTypeUtils = new InputTypeUtils($this->getAnnotationReader(), new NamingStrategy()); |
285
|
|
|
} |
286
|
|
|
return $this->inputTypeUtils; |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
protected function getTypeResolver(): TypeResolver |
290
|
|
|
{ |
291
|
|
|
if ($this->typeResolver === null) { |
292
|
|
|
$this->typeResolver = new TypeResolver(); |
293
|
|
|
$this->typeResolver->registerSchema(new \GraphQL\Type\Schema([])); |
294
|
|
|
} |
295
|
|
|
return $this->typeResolver; |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
protected function getControllerQueryProviderFactory(): FieldsBuilderFactory |
299
|
|
|
{ |
300
|
|
|
if ($this->controllerQueryProviderFactory === null) { |
301
|
|
|
$this->controllerQueryProviderFactory = new FieldsBuilderFactory($this->getAnnotationReader(), |
302
|
|
|
$this->getHydrator(), |
303
|
|
|
new VoidAuthenticationService(), |
304
|
|
|
new VoidAuthorizationService(), |
305
|
|
|
$this->getTypeResolver(), |
306
|
|
|
new CachedDocBlockFactory(new ArrayCache())); |
307
|
|
|
} |
308
|
|
|
return $this->controllerQueryProviderFactory; |
309
|
|
|
} |
310
|
|
|
} |
311
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths