Passed
Pull Request — master (#64)
by David
01:38
created

AbstractQueryProviderTest   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 264
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 18
eloc 101
dl 0
loc 264
rs 10
c 0
b 0
f 0

33 Methods

Rating   Name   Duplication   Size   Complexity  
A hp$1 ➔ getTypeGenerator() 0 6 2
getRegistry() 0 10 ?
A hp$1 ➔ getRegistry() 0 10 2
A hp$1 ➔ getHydrator() 0 16 2
getInputTypeGenerator() 0 6 ?
getAnnotationReader() 0 6 ?
A hp$0 ➔ mapClassToInputType() 0 8 2
getHydrator() 0 16 ?
A hp$0 ➔ canMapClassToInputType() 0 3 2
buildAutoWiringContainer() 0 3 ?
A hp$1 ➔ canHydrate() 0 3 1
A hp$0 ➔ mapClassToType() 0 8 3
A hp$1 ➔ getInputTypeGenerator() 0 6 2
A hp$0 ➔ canMapNameToType() 0 3 2
getTypeMapper() 0 109 ?
getControllerQueryProviderFactory() 0 11 ?
A hp$1 ➔ buildAutoWiringContainer() 0 3 1
getTypeGenerator() 0 6 ?
A hp$0 ➔ mapNameToType() 0 9 3
A getInputTestObjectType() 0 11 2
A hp$1 ➔ buildControllerQueryProvider() 0 10 1
A getTestObjectType() 0 11 2
A hp$1 ➔ getInputTypeUtils() 0 6 2
B hp$0 ➔ getTypeMapper() 0 109 3
buildControllerQueryProvider() 0 10 ?
A getTestObjectType2() 0 11 2
A hp$1 ➔ getAnnotationReader() 0 6 2
A hp$1 ➔ getControllerQueryProviderFactory() 0 11 2
A hp$1 ➔ hydrate() 0 3 1
A hp$0 ➔ canMapClassToType() 0 3 2
A hp$0 ➔ __construct() 0 5 1
A hp$0 ➔ getSupportedClasses() 0 3 1
getInputTypeUtils() 0 6 ?
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\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
34
abstract class AbstractQueryProviderTest extends TestCase
35
{
36
    private $testObjectType;
37
    private $testObjectType2;
38
    private $inputTestObjectType;
39
    private $inputTestObjectType2;
0 ignored issues
show
introduced by
The private property $inputTestObjectType2 is not used, and could be removed.
Loading history...
40
    private $typeMapper;
41
    private $hydrator;
42
    private $registry;
43
    private $typeGenerator;
44
    private $inputTypeGenerator;
45
    private $inputTypeUtils;
46
    private $controllerQueryProviderFactory;
47
    private $annotationReader;
48
49
    protected function getTestObjectType()
50
    {
51
        if ($this->testObjectType === null) {
52
            $this->testObjectType = new ObjectType([
53
                'name'    => 'TestObject',
54
                'fields'  => [
55
                    'test'   => Type::string(),
56
                ],
57
            ]);
58
        }
59
        return $this->testObjectType;
60
    }
61
62
    protected function getTestObjectType2()
63
    {
64
        if ($this->testObjectType2 === null) {
65
            $this->testObjectType2 = new ObjectType([
66
                'name'    => 'TestObject2',
67
                'fields'  => [
68
                    'test'   => Type::string(),
69
                ],
70
            ]);
71
        }
72
        return $this->testObjectType2;
73
    }
74
75
    protected function getInputTestObjectType()
76
    {
77
        if ($this->inputTestObjectType === null) {
78
            $this->inputTestObjectType = new InputObjectType([
79
                'name'    => 'TestObjectInput',
80
                'fields'  => [
81
                    'test'   => Type::string(),
82
                ],
83
            ]);
84
        }
85
        return $this->inputTestObjectType;
86
    }
87
88
    /*protected function getInputTestObjectType2()
89
    {
90
        if ($this->inputTestObjectType2 === null) {
91
            $this->inputTestObjectType2 = new ResolvableInputObjectType('TestObjectInput2', $this->getControllerQueryProviderFactory(), $this->getTypeMapper(), new TestFactory(), 'myRecursiveFactory', $this->getHydrator(), null);
92
        }
93
        return $this->inputTestObjectType2;
94
    }*/
95
96
    protected function getTypeMapper()
97
    {
98
        if ($this->typeMapper === null) {
99
            $this->typeMapper = new RecursiveTypeMapper(new class($this->getTestObjectType(), $this->getTestObjectType2(), $this->getInputTestObjectType()/*, $this->getInputTestObjectType2()*/) implements TypeMapperInterface {
100
                /**
101
                 * @var ObjectType
102
                 */
103
                private $testObjectType;
104
                /**
105
                 * @var ObjectType
106
                 */
107
                private $testObjectType2;
108
                /**
109
                 * @var InputObjectType
110
                 */
111
                private $inputTestObjectType;
112
                /**
113
                 * @var InputObjectType
114
                 */
115
//                private $inputTestObjectType2;
116
117
                public function __construct(ObjectType $testObjectType, ObjectType $testObjectType2, InputObjectType $inputTestObjectType/*, InputObjectType $inputTestObjectType2*/)
118
                {
119
                    $this->testObjectType = $testObjectType;
120
                    $this->testObjectType2 = $testObjectType2;
121
                    $this->inputTestObjectType = $inputTestObjectType;
122
                    //$this->inputTestObjectType2 = $inputTestObjectType2;
123
                }
124
125
                public function mapClassToType(string $className, RecursiveTypeMapperInterface $recursiveTypeMapper): ObjectType
126
                {
127
                    if ($className === TestObject::class) {
128
                        return $this->testObjectType;
129
                    } elseif ($className === TestObject2::class) {
130
                        return $this->testObjectType2;
131
                    } else {
132
                        throw CannotMapTypeException::createForType($className);
133
                    }
134
                }
135
136
                public function mapClassToInputType(string $className, RecursiveTypeMapperInterface $recursiveTypeMapper): InputObjectType
137
                {
138
                    if ($className === TestObject::class) {
139
                        return $this->inputTestObjectType;
140
                    } /*elseif ($className === TestObjectWithRecursiveList::class) {
141
                        return $this->inputTestObjectType2;
142
                    } */else {
143
                        throw CannotMapTypeException::createForInputType($className);
144
                    }
145
                }
146
147
                public function canMapClassToType(string $className): bool
148
                {
149
                    return $className === TestObject::class || $className === TestObject2::class;
150
                }
151
152
                /**
153
                 * Returns true if this type mapper can map the $className FQCN to a GraphQL input type.
154
                 *
155
                 * @param string $className
156
                 * @return bool
157
                 */
158
                public function canMapClassToInputType(string $className): bool
159
                {
160
                    return $className === TestObject::class || $className === TestObject2::class;
161
                }
162
163
                /**
164
                 * Returns the list of classes that have matching input GraphQL types.
165
                 *
166
                 * @return string[]
167
                 */
168
                public function getSupportedClasses(): array
169
                {
170
                    return [TestObject::class, TestObject2::class];
171
                }
172
173
                /**
174
                 * Returns a GraphQL type by name (can be either an input or output type)
175
                 *
176
                 * @param string $typeName The name of the GraphQL type
177
                 * @return Type&(InputType|OutputType)
178
                 * @throws CannotMapTypeException
179
                 */
180
                public function mapNameToType(string $typeName, RecursiveTypeMapperInterface $recursiveTypeMapper): Type
181
                {
182
                    switch ($typeName) {
183
                        case 'TestObject':
184
                            return $this->testObjectType;
185
                        case 'TestObject2':
186
                            return $this->testObjectType2;
187
                        default:
188
                            throw CannotMapTypeException::createForName($typeName);
189
                    }
190
                }
191
192
                /**
193
                 * Returns true if this type mapper can map the $typeName GraphQL name to a GraphQL type.
194
                 *
195
                 * @param string $typeName The name of the GraphQL type
196
                 * @return bool
197
                 */
198
                public function canMapNameToType(string $typeName): bool
199
                {
200
                    return $typeName === 'TestObject' || $typeName === 'TestObject2';
201
                }
202
            }, new NamingStrategy(), new ArrayCache());
203
        }
204
        return $this->typeMapper;
205
    }
206
207
    protected function getHydrator(): HydratorInterface
208
    {
209
        if ($this->hydrator === null) {
210
            $this->hydrator = new class implements HydratorInterface {
211
                public function hydrate(array $data, InputObjectType $type)
212
                {
213
                    return new TestObject($data['test']);
214
                }
215
216
                public function canHydrate(array $data, InputObjectType $type): bool
217
                {
218
                    return true;
219
                }
220
            };
221
        }
222
        return $this->hydrator;
223
    }
224
225
    protected function getRegistry()
226
    {
227
        if ($this->registry === null) {
228
            $this->registry = $this->buildAutoWiringContainer(new Picotainer([
229
                'customOutput' => function() {
230
                    return new StringType();
231
                }
232
            ]));
233
        }
234
        return $this->registry;
235
    }
236
237
    protected function buildAutoWiringContainer(ContainerInterface $container): BasicAutoWiringContainer
238
    {
239
        return new BasicAutoWiringContainer($container);
240
    }
241
242
    protected function getAnnotationReader(): AnnotationReader
243
    {
244
        if ($this->annotationReader === null) {
245
            $this->annotationReader = new AnnotationReader(new DoctrineAnnotationReader());
246
        }
247
        return $this->annotationReader;
248
    }
249
250
    protected function buildControllerQueryProvider()
251
    {
252
        return new ControllerQueryProvider(
253
            $this->getAnnotationReader(),
254
            $this->getTypeMapper(),
255
            $this->getHydrator(),
256
            new VoidAuthenticationService(),
257
            new VoidAuthorizationService(),
258
            $this->getRegistry(),
259
            new CachedDocBlockFactory(new ArrayCache())
260
        );
261
    }
262
263
    protected function getTypeGenerator(): TypeGenerator
264
    {
265
        if ($this->typeGenerator === null) {
266
            $this->typeGenerator = new TypeGenerator($this->getAnnotationReader(), $this->getControllerQueryProviderFactory(), new NamingStrategy());
267
        }
268
        return $this->typeGenerator;
269
    }
270
271
    protected function getInputTypeGenerator(): InputTypeGenerator
272
    {
273
        if ($this->inputTypeGenerator === null) {
274
            $this->inputTypeGenerator = new InputTypeGenerator($this->getInputTypeUtils(), $this->getControllerQueryProviderFactory(), $this->getHydrator());
275
        }
276
        return $this->inputTypeGenerator;
277
    }
278
279
    protected function getInputTypeUtils(): InputTypeUtils
280
    {
281
        if ($this->inputTypeUtils === null) {
282
            $this->inputTypeUtils = new InputTypeUtils($this->getAnnotationReader(), new NamingStrategy());
283
        }
284
        return $this->inputTypeUtils;
285
    }
286
287
    protected function getControllerQueryProviderFactory(): ControllerQueryProviderFactory
288
    {
289
        if ($this->controllerQueryProviderFactory === null) {
290
            $this->controllerQueryProviderFactory = new ControllerQueryProviderFactory($this->getAnnotationReader(),
291
                $this->getHydrator(),
292
                new VoidAuthenticationService(),
293
                new VoidAuthorizationService(),
294
                $this->getRegistry(),
295
                new CachedDocBlockFactory(new ArrayCache()));
296
        }
297
        return $this->controllerQueryProviderFactory;
298
    }
299
}
300