anonymous//tests/AbstractQueryProviderTest.php$0   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 22
dl 0
loc 100
rs 10
c 0
b 0
f 0
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;
0 ignored issues
show
Bug introduced by
The type TheCodingMachine\GraphQL...ObjectWithRecursiveList was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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\CannotMapTypeExceptionInterface;
25
use TheCodingMachine\GraphQL\Controllers\Mappers\RecursiveTypeMapper;
26
use TheCodingMachine\GraphQL\Controllers\Mappers\RecursiveTypeMapperInterface;
27
use TheCodingMachine\GraphQL\Controllers\Mappers\TypeMapperInterface;
28
use TheCodingMachine\GraphQL\Controllers\Containers\EmptyContainer;
29
use TheCodingMachine\GraphQL\Controllers\Containers\BasicAutoWiringContainer;
30
use TheCodingMachine\GraphQL\Controllers\Reflection\CachedDocBlockFactory;
31
use TheCodingMachine\GraphQL\Controllers\Security\VoidAuthenticationService;
32
use TheCodingMachine\GraphQL\Controllers\Security\VoidAuthorizationService;
33
use TheCodingMachine\GraphQL\Controllers\Types\MutableObjectType;
34
use TheCodingMachine\GraphQL\Controllers\Types\ResolvableInputObjectType;
35
use TheCodingMachine\GraphQL\Controllers\Types\TypeResolver;
36
37
abstract class AbstractQueryProviderTest extends TestCase
38
{
39
    private $testObjectType;
40
    private $testObjectType2;
41
    private $inputTestObjectType;
42
    private $inputTestObjectType2;
0 ignored issues
show
introduced by
The private property $inputTestObjectType2 is not used, and could be removed.
Loading history...
43
    private $typeMapper;
44
    private $hydrator;
45
    private $registry;
46
    private $typeGenerator;
47
    private $inputTypeGenerator;
48
    private $inputTypeUtils;
49
    private $controllerQueryProviderFactory;
50
    private $annotationReader;
51
    private $typeResolver;
52
    private $typeRegistry;
53
54
    protected function getTestObjectType(): MutableObjectType
55
    {
56
        if ($this->testObjectType === null) {
57
            $this->testObjectType = new MutableObjectType([
58
                'name'    => 'TestObject',
59
                'fields'  => [
60
                    'test'   => Type::string(),
61
                ],
62
            ]);
63
        }
64
        return $this->testObjectType;
65
    }
66
67
    protected function getTestObjectType2(): MutableObjectType
68
    {
69
        if ($this->testObjectType2 === null) {
70
            $this->testObjectType2 = new MutableObjectType([
71
                'name'    => 'TestObject2',
72
                'fields'  => [
73
                    'test'   => Type::string(),
74
                ],
75
            ]);
76
        }
77
        return $this->testObjectType2;
78
    }
79
80
    protected function getInputTestObjectType(): InputObjectType
81
    {
82
        if ($this->inputTestObjectType === null) {
83
            $this->inputTestObjectType = new InputObjectType([
84
                'name'    => 'TestObjectInput',
85
                'fields'  => [
86
                    'test'   => Type::string(),
87
                ],
88
            ]);
89
        }
90
        return $this->inputTestObjectType;
91
    }
92
93
    /*protected function getInputTestObjectType2()
94
    {
95
        if ($this->inputTestObjectType2 === null) {
96
            $this->inputTestObjectType2 = new ResolvableInputObjectType('TestObjectInput2', $this->getControllerQueryProviderFactory(), $this->getTypeMapper(), new TestFactory(), 'myRecursiveFactory', $this->getHydrator(), null);
97
        }
98
        return $this->inputTestObjectType2;
99
    }*/
100
101
    protected function getTypeMapper()
102
    {
103
        if ($this->typeMapper === null) {
104
            $this->typeMapper = new RecursiveTypeMapper(new class($this->getTestObjectType(), $this->getTestObjectType2(), $this->getInputTestObjectType()/*, $this->getInputTestObjectType2()*/) implements TypeMapperInterface {
105
                /**
106
                 * @var ObjectType
107
                 */
108
                private $testObjectType;
109
                /**
110
                 * @var ObjectType
111
                 */
112
                private $testObjectType2;
113
                /**
114
                 * @var InputObjectType
115
                 */
116
                private $inputTestObjectType;
117
                /**
118
                 * @var InputObjectType
119
                 */
120
//                private $inputTestObjectType2;
121
122
                public function __construct(ObjectType $testObjectType, ObjectType $testObjectType2, InputObjectType $inputTestObjectType/*, InputObjectType $inputTestObjectType2*/)
123
                {
124
                    $this->testObjectType = $testObjectType;
125
                    $this->testObjectType2 = $testObjectType2;
126
                    $this->inputTestObjectType = $inputTestObjectType;
127
                    //$this->inputTestObjectType2 = $inputTestObjectType2;
128
                }
129
130
                public function mapClassToType(string $className, ?OutputType $subType, RecursiveTypeMapperInterface $recursiveTypeMapper): MutableObjectType
131
                {
132
                    if ($className === TestObject::class) {
133
                        return $this->testObjectType;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->testObjectType returns the type GraphQL\Type\Definition\ObjectType which includes types incompatible with the type-hinted return TheCodingMachine\GraphQL...Types\MutableObjectType.
Loading history...
134
                    } elseif ($className === TestObject2::class) {
135
                        return $this->testObjectType2;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->testObjectType2 returns the type GraphQL\Type\Definition\ObjectType which includes types incompatible with the type-hinted return TheCodingMachine\GraphQL...Types\MutableObjectType.
Loading history...
136
                    } else {
137
                        throw CannotMapTypeException::createForType($className);
138
                    }
139
                }
140
141
                public function mapClassToInputType(string $className, RecursiveTypeMapperInterface $recursiveTypeMapper): InputObjectType
142
                {
143
                    if ($className === TestObject::class) {
144
                        return $this->inputTestObjectType;
145
                    } /*elseif ($className === TestObjectWithRecursiveList::class) {
146
                        return $this->inputTestObjectType2;
147
                    } */else {
148
                        throw CannotMapTypeException::createForInputType($className);
149
                    }
150
                }
151
152
                public function canMapClassToType(string $className): bool
153
                {
154
                    return $className === TestObject::class || $className === TestObject2::class;
155
                }
156
157
                public function canMapClassToInputType(string $className): bool
158
                {
159
                    return $className === TestObject::class || $className === TestObject2::class;
160
                }
161
162
                public function getSupportedClasses(): array
163
                {
164
                    return [TestObject::class, TestObject2::class];
165
                }
166
167
                public function mapNameToType(string $typeName, RecursiveTypeMapperInterface $recursiveTypeMapper): Type
168
                {
169
                    switch ($typeName) {
170
                        case 'TestObject':
171
                            return $this->testObjectType;
172
                        case 'TestObject2':
173
                            return $this->testObjectType2;
174
                        case 'TestObjectInput':
175
                            return $this->inputTestObjectType;
176
                        default:
177
                            throw CannotMapTypeException::createForName($typeName);
178
                    }
179
                }
180
181
                public function canMapNameToType(string $typeName): bool
182
                {
183
                    return $typeName === 'TestObject' || $typeName === 'TestObject2' || $typeName === 'TestObjectInput';
184
                }
185
186
                public function canExtendTypeForClass(string $className, MutableObjectType $type, RecursiveTypeMapperInterface $recursiveTypeMapper): bool
187
                {
188
                    return false;
189
                }
190
191
                public function extendTypeForClass(string $className, MutableObjectType $type, RecursiveTypeMapperInterface $recursiveTypeMapper): void
192
                {
193
                    throw CannotMapTypeException::createForExtendType($className, $type);
194
                }
195
196
                public function canExtendTypeForName(string $typeName, MutableObjectType $type, RecursiveTypeMapperInterface $recursiveTypeMapper): bool
197
                {
198
                    return false;
199
                }
200
201
                public function extendTypeForName(string $typeName, MutableObjectType $type, RecursiveTypeMapperInterface $recursiveTypeMapper): void
202
                {
203
                    throw CannotMapTypeException::createForExtendName($typeName, $type);
204
                }
205
            }, new NamingStrategy(), new ArrayCache(), $this->getTypeRegistry());
206
        }
207
        return $this->typeMapper;
208
    }
209
210
    protected function getHydrator(): HydratorInterface
211
    {
212
        if ($this->hydrator === null) {
213
            $this->hydrator = new class implements HydratorInterface {
214
                public function hydrate(array $data, InputObjectType $type)
215
                {
216
                    return new TestObject($data['test']);
217
                }
218
219
                public function canHydrate(array $data, InputObjectType $type): bool
220
                {
221
                    return true;
222
                }
223
            };
224
        }
225
        return $this->hydrator;
226
    }
227
228
    protected function getRegistry()
229
    {
230
        if ($this->registry === null) {
231
            $this->registry = $this->buildAutoWiringContainer(new Picotainer([
232
                /*'customOutput' => function() {
233
                    return new StringType();
234
                }*/
235
            ]));
236
        }
237
        return $this->registry;
238
    }
239
240
    protected function buildAutoWiringContainer(ContainerInterface $container): BasicAutoWiringContainer
241
    {
242
        return new BasicAutoWiringContainer($container);
243
    }
244
245
    protected function getAnnotationReader(): AnnotationReader
246
    {
247
        if ($this->annotationReader === null) {
248
            $this->annotationReader = new AnnotationReader(new DoctrineAnnotationReader());
249
        }
250
        return $this->annotationReader;
251
    }
252
253
    protected function buildFieldsBuilder(): FieldsBuilder
254
    {
255
        return new FieldsBuilder(
256
            $this->getAnnotationReader(),
257
            $this->getTypeMapper(),
258
            $this->getHydrator(),
259
            new VoidAuthenticationService(),
260
            new VoidAuthorizationService(),
261
            $this->getTypeResolver(),
262
            new CachedDocBlockFactory(new ArrayCache()),
263
            new NamingStrategy()
264
        );
265
    }
266
267
    protected function getTypeGenerator(): TypeGenerator
268
    {
269
        if ($this->typeGenerator === null) {
270
            $this->typeGenerator = new TypeGenerator($this->getAnnotationReader(), $this->getControllerQueryProviderFactory(), new NamingStrategy(), $this->getTypeRegistry(), $this->getRegistry());
271
        }
272
        return $this->typeGenerator;
273
    }
274
275
    protected function getInputTypeGenerator(): InputTypeGenerator
276
    {
277
        if ($this->inputTypeGenerator === null) {
278
            $this->inputTypeGenerator = new InputTypeGenerator($this->getInputTypeUtils(), $this->getControllerQueryProviderFactory(), $this->getHydrator());
279
        }
280
        return $this->inputTypeGenerator;
281
    }
282
283
    protected function getInputTypeUtils(): InputTypeUtils
284
    {
285
        if ($this->inputTypeUtils === null) {
286
            $this->inputTypeUtils = new InputTypeUtils($this->getAnnotationReader(), new NamingStrategy());
287
        }
288
        return $this->inputTypeUtils;
289
    }
290
291
    protected function getTypeResolver(): TypeResolver
292
    {
293
        if ($this->typeResolver === null) {
294
            $this->typeResolver = new TypeResolver();
295
            $this->typeResolver->registerSchema(new \GraphQL\Type\Schema([]));
296
        }
297
        return $this->typeResolver;
298
    }
299
300
    protected function getControllerQueryProviderFactory(): FieldsBuilderFactory
301
    {
302
        if ($this->controllerQueryProviderFactory === null) {
303
            $this->controllerQueryProviderFactory = new FieldsBuilderFactory($this->getAnnotationReader(),
304
                $this->getHydrator(),
305
                new VoidAuthenticationService(),
306
                new VoidAuthorizationService(),
307
                $this->getTypeResolver(),
308
                new CachedDocBlockFactory(new ArrayCache()),
309
                new NamingStrategy());
310
        }
311
        return $this->controllerQueryProviderFactory;
312
    }
313
314
    protected function getTypeRegistry(): TypeRegistry
315
    {
316
        if ($this->typeRegistry === null) {
317
            $this->typeRegistry = new TypeRegistry();
318
        }
319
        return $this->typeRegistry;
320
    }
321
}
322