1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace TheCodingMachine\GraphQL\Controllers; |
5
|
|
|
|
6
|
|
|
use Doctrine\Common\Annotations\AnnotationReader; |
7
|
|
|
use GraphQL\Type\Definition\InputObjectType; |
8
|
|
|
use GraphQL\Type\Definition\InputType; |
9
|
|
|
use GraphQL\Type\Definition\ObjectType; |
10
|
|
|
use GraphQL\Type\Definition\OutputType; |
11
|
|
|
use GraphQL\Type\Definition\Type; |
12
|
|
|
use PHPUnit\Framework\TestCase; |
13
|
|
|
use Psr\Container\ContainerInterface; |
14
|
|
|
use TheCodingMachine\GraphQL\Controllers\Fixtures\TestObject; |
15
|
|
|
use TheCodingMachine\GraphQL\Controllers\Fixtures\TestObject2; |
16
|
|
|
use TheCodingMachine\GraphQL\Controllers\Mappers\CannotMapTypeException; |
17
|
|
|
use TheCodingMachine\GraphQL\Controllers\Mappers\TypeMapperInterface; |
18
|
|
|
use TheCodingMachine\GraphQL\Controllers\Registry\EmptyContainer; |
19
|
|
|
use TheCodingMachine\GraphQL\Controllers\Registry\Registry; |
20
|
|
|
use TheCodingMachine\GraphQL\Controllers\Security\VoidAuthenticationService; |
21
|
|
|
use TheCodingMachine\GraphQL\Controllers\Security\VoidAuthorizationService; |
22
|
|
|
|
23
|
|
|
abstract class AbstractQueryProviderTest extends TestCase |
24
|
|
|
{ |
25
|
|
|
private $testObjectType; |
26
|
|
|
private $testObjectType2; |
27
|
|
|
private $inputTestObjectType; |
28
|
|
|
private $typeMapper; |
29
|
|
|
private $hydrator; |
30
|
|
|
private $registry; |
31
|
|
|
|
32
|
|
|
protected function getTestObjectType() |
33
|
|
|
{ |
34
|
|
|
if ($this->testObjectType === null) { |
35
|
|
|
$this->testObjectType = new ObjectType([ |
36
|
|
|
'name' => 'TestObject', |
37
|
|
|
'fields' => [ |
38
|
|
|
'test' => Type::string(), |
39
|
|
|
], |
40
|
|
|
]); |
41
|
|
|
} |
42
|
|
|
return $this->testObjectType; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
protected function getTestObjectType2() |
46
|
|
|
{ |
47
|
|
|
if ($this->testObjectType2 === null) { |
48
|
|
|
$this->testObjectType2 = new ObjectType([ |
49
|
|
|
'name' => 'TestObject2', |
50
|
|
|
'fields' => [ |
51
|
|
|
'test' => Type::string(), |
52
|
|
|
], |
53
|
|
|
]); |
54
|
|
|
} |
55
|
|
|
return $this->testObjectType2; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
protected function getInputTestObjectType() |
59
|
|
|
{ |
60
|
|
|
if ($this->inputTestObjectType === null) { |
61
|
|
|
$this->inputTestObjectType = new InputObjectType([ |
62
|
|
|
'name' => 'TestObject', |
63
|
|
|
'fields' => [ |
64
|
|
|
'test' => Type::string(), |
65
|
|
|
], |
66
|
|
|
]); |
67
|
|
|
} |
68
|
|
|
return $this->inputTestObjectType; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
protected function getTypeMapper() |
72
|
|
|
{ |
73
|
|
|
if ($this->typeMapper === null) { |
74
|
|
|
$this->typeMapper = new class($this->getTestObjectType(), $this->getTestObjectType2(), $this->getInputTestObjectType()) implements TypeMapperInterface { |
75
|
|
|
/** |
76
|
|
|
* @var ObjectType |
77
|
|
|
*/ |
78
|
|
|
private $testObjectType; |
79
|
|
|
/** |
80
|
|
|
* @var ObjectType |
81
|
|
|
*/ |
82
|
|
|
private $testObjectType2; |
83
|
|
|
/** |
84
|
|
|
* @var InputObjectType |
85
|
|
|
*/ |
86
|
|
|
private $inputTestObjectType; |
87
|
|
|
|
88
|
|
|
public function __construct(ObjectType $testObjectType, ObjectType $testObjectType2, InputObjectType $inputTestObjectType) |
89
|
|
|
{ |
90
|
|
|
$this->testObjectType = $testObjectType; |
91
|
|
|
$this->testObjectType2 = $testObjectType2; |
92
|
|
|
$this->inputTestObjectType = $inputTestObjectType; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function mapClassToType(string $className): OutputType |
96
|
|
|
{ |
97
|
|
|
if ($className === TestObject::class) { |
98
|
|
|
return $this->testObjectType; |
99
|
|
|
} elseif ($className === TestObject2::class) { |
100
|
|
|
return $this->testObjectType2; |
101
|
|
|
} else { |
102
|
|
|
throw CannotMapTypeException::createForType($className); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function mapClassToInputType(string $className): InputType |
107
|
|
|
{ |
108
|
|
|
if ($className === TestObject::class) { |
109
|
|
|
return $this->inputTestObjectType; |
110
|
|
|
} else { |
111
|
|
|
throw CannotMapTypeException::createForInputType($className); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function canMapClassToType(string $className): bool |
116
|
|
|
{ |
117
|
|
|
return $className === TestObject::class; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Returns true if this type mapper can map the $className FQCN to a GraphQL input type. |
122
|
|
|
* |
123
|
|
|
* @param string $className |
124
|
|
|
* @return bool |
125
|
|
|
*/ |
126
|
|
|
public function canMapClassToInputType(string $className): bool |
127
|
|
|
{ |
128
|
|
|
return $className === TestObject::class; |
129
|
|
|
} |
130
|
|
|
}; |
131
|
|
|
} |
132
|
|
|
return $this->typeMapper; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
protected function getHydrator() |
136
|
|
|
{ |
137
|
|
|
if ($this->hydrator === null) { |
138
|
|
|
$this->hydrator = new class implements HydratorInterface { |
139
|
|
|
public function hydrate(array $data, InputType $type) |
140
|
|
|
{ |
141
|
|
|
return new TestObject($data['test']); |
142
|
|
|
} |
143
|
|
|
}; |
144
|
|
|
} |
145
|
|
|
return $this->hydrator; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
protected function getRegistry() |
149
|
|
|
{ |
150
|
|
|
if ($this->registry === null) { |
151
|
|
|
$this->registry = $this->buildRegistry(new EmptyContainer()); |
152
|
|
|
} |
153
|
|
|
return $this->registry; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
protected function buildRegistry(ContainerInterface $container) |
157
|
|
|
{ |
158
|
|
|
$reader = new AnnotationReader(); |
159
|
|
|
return new Registry($container, |
160
|
|
|
new VoidAuthorizationService(), |
161
|
|
|
new VoidAuthenticationService(), |
162
|
|
|
$reader, |
163
|
|
|
$this->getTypeMapper(), |
164
|
|
|
$this->getHydrator()); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|