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
|
|
|
protected function getTestObjectType() |
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
|
|
|
protected function getInputTestObjectType() |
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
|
|
|
|