Test Failed
Pull Request — master (#19)
by David
02:44
created

AbstractQueryProviderTest::getRegistry()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
3
4
namespace TheCodingMachine\GraphQL\Controllers;
5
6
use Doctrine\Common\Annotations\AnnotationReader;
7
use PHPUnit\Framework\TestCase;
8
use Psr\Container\ContainerInterface;
9
use TheCodingMachine\GraphQL\Controllers\Fixtures\TestObject;
10
use TheCodingMachine\GraphQL\Controllers\Registry\EmptyContainer;
11
use TheCodingMachine\GraphQL\Controllers\Registry\Registry;
12
use TheCodingMachine\GraphQL\Controllers\Security\VoidAuthenticationService;
13
use TheCodingMachine\GraphQL\Controllers\Security\VoidAuthorizationService;
14
use Youshido\GraphQL\Type\InputObject\InputObjectType;
15
use Youshido\GraphQL\Type\InputTypeInterface;
16
use Youshido\GraphQL\Type\Object\ObjectType;
17
use Youshido\GraphQL\Type\Scalar\StringType;
18
use Youshido\GraphQL\Type\TypeInterface;
19
20
abstract class AbstractQueryProviderTest extends TestCase
21
{
22
    private $testObjectType;
23
    private $inputTestObjectType;
24
    private $typeMapper;
25
    private $hydrator;
26
    private $registry;
27
28 View Code Duplication
    protected function getTestObjectType()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
29
    {
30
        if ($this->testObjectType === null) {
31
            $this->testObjectType = new ObjectType([
32
                'name'    => 'TestObject',
33
                'fields'  => [
34
                    'test'   => new StringType(),
35
                ],
36
            ]);
37
        }
38
        return $this->testObjectType;
39
    }
40
41 View Code Duplication
    protected function getInputTestObjectType()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
42
    {
43
        if ($this->inputTestObjectType === null) {
44
            $this->inputTestObjectType = new InputObjectType([
45
                'name'    => 'TestObject',
46
                'fields'  => [
47
                    'test'   => new StringType(),
48
                ],
49
            ]);
50
        }
51
        return $this->inputTestObjectType;
52
    }
53
54
    protected function getTypeMapper()
55
    {
56
        if ($this->typeMapper === null) {
57
            $this->typeMapper = new class($this->getTestObjectType(), $this->getInputTestObjectType()) implements TypeMapperInterface {
58
                /**
59
                 * @var ObjectType
60
                 */
61
                private $testObjectType;
62
                /**
63
                 * @var InputObjectType
64
                 */
65
                private $inputTestObjectType;
66
67
                public function __construct(ObjectType $testObjectType, InputObjectType $inputTestObjectType)
68
                {
69
                    $this->testObjectType = $testObjectType;
70
                    $this->inputTestObjectType = $inputTestObjectType;
71
                }
72
73
                public function mapClassToType(string $className): TypeInterface
74
                {
75
                    if ($className === TestObject::class) {
76
                        return $this->testObjectType;
77
                    } else {
78
                        throw new \RuntimeException('Unexpected type');
79
                    }
80
                }
81
82
                public function mapClassToInputType(string $className): InputTypeInterface
83
                {
84
                    if ($className === TestObject::class) {
85
                        return $this->inputTestObjectType;
86
                    } else {
87
                        throw new \RuntimeException('Unexpected type');
88
                    }
89
                }
90
            };
91
        }
92
        return $this->typeMapper;
93
    }
94
95
    protected function getHydrator()
96
    {
97
        if ($this->hydrator === null) {
98
            $this->hydrator = new class implements HydratorInterface {
99
                public function hydrate(array $data, TypeInterface $type)
100
                {
101
                    return new TestObject($data['test']);
102
                }
103
            };
104
        }
105
        return $this->hydrator;
106
    }
107
108
    protected function getRegistry()
109
    {
110
        if ($this->registry === null) {
111
            $this->registry = $this->buildRegistry(new EmptyContainer());
112
        }
113
        return $this->registry;
114
    }
115
116
    protected function buildRegistry(ContainerInterface $container)
117
    {
118
        $reader = new AnnotationReader();
119
        return new Registry($container,
120
                new VoidAuthorizationService(),
121
                new VoidAuthenticationService(),
122
                $reader,
123
                $this->getTypeMapper(),
124
                $this->getHydrator());
125
    }
126
}
127