1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TheCodingMachine\GraphQL\Controllers\Integration; |
4
|
|
|
|
5
|
|
|
use function class_exists; |
6
|
|
|
use Doctrine\Common\Annotations\AnnotationReader as DoctrineAnnotationReader; |
7
|
|
|
use Exception; |
8
|
|
|
use GraphQL\Error\Debug; |
9
|
|
|
use GraphQL\GraphQL; |
10
|
|
|
use GraphQL\Type\Definition\InputType; |
11
|
|
|
use Mouf\Picotainer\Picotainer; |
12
|
|
|
use PhpParser\Comment\Doc; |
13
|
|
|
use PHPUnit\Framework\TestCase; |
14
|
|
|
use PHPUnit\Util\Type; |
15
|
|
|
use function print_r; |
16
|
|
|
use Psr\Container\ContainerInterface; |
17
|
|
|
use Psr\Container\NotFoundExceptionInterface; |
18
|
|
|
use Symfony\Component\Cache\Simple\ArrayCache; |
19
|
|
|
use TheCodingMachine\GraphQL\Controllers\AnnotationReader; |
20
|
|
|
use TheCodingMachine\GraphQL\Controllers\FieldsBuilderFactory; |
21
|
|
|
use TheCodingMachine\GraphQL\Controllers\Fixtures\Integration\Models\Contact; |
22
|
|
|
use TheCodingMachine\GraphQL\Controllers\GlobControllerQueryProvider; |
23
|
|
|
use TheCodingMachine\GraphQL\Controllers\Hydrators\HydratorInterface; |
24
|
|
|
use TheCodingMachine\GraphQL\Controllers\Hydrators\FactoryHydrator; |
25
|
|
|
use TheCodingMachine\GraphQL\Controllers\InputTypeGenerator; |
26
|
|
|
use TheCodingMachine\GraphQL\Controllers\InputTypeUtils; |
27
|
|
|
use TheCodingMachine\GraphQL\Controllers\Mappers\GlobTypeMapper; |
28
|
|
|
use TheCodingMachine\GraphQL\Controllers\Mappers\RecursiveTypeMapper; |
29
|
|
|
use TheCodingMachine\GraphQL\Controllers\Mappers\RecursiveTypeMapperInterface; |
30
|
|
|
use TheCodingMachine\GraphQL\Controllers\Mappers\TypeMapperInterface; |
31
|
|
|
use TheCodingMachine\GraphQL\Controllers\NamingStrategy; |
32
|
|
|
use TheCodingMachine\GraphQL\Controllers\NamingStrategyInterface; |
33
|
|
|
use TheCodingMachine\GraphQL\Controllers\QueryProviderInterface; |
34
|
|
|
use TheCodingMachine\GraphQL\Controllers\Containers\BasicAutoWiringContainer; |
35
|
|
|
use TheCodingMachine\GraphQL\Controllers\Containers\EmptyContainer; |
36
|
|
|
use TheCodingMachine\GraphQL\Controllers\Reflection\CachedDocBlockFactory; |
37
|
|
|
use TheCodingMachine\GraphQL\Controllers\Schema; |
38
|
|
|
use TheCodingMachine\GraphQL\Controllers\Security\AuthenticationServiceInterface; |
39
|
|
|
use TheCodingMachine\GraphQL\Controllers\Security\AuthorizationServiceInterface; |
40
|
|
|
use TheCodingMachine\GraphQL\Controllers\Security\VoidAuthenticationService; |
41
|
|
|
use TheCodingMachine\GraphQL\Controllers\Security\VoidAuthorizationService; |
42
|
|
|
use TheCodingMachine\GraphQL\Controllers\TypeGenerator; |
43
|
|
|
use TheCodingMachine\GraphQL\Controllers\Types\TypeResolver; |
44
|
|
|
use function var_export; |
45
|
|
|
|
46
|
|
|
class EndToEndTest extends TestCase |
47
|
|
|
{ |
48
|
|
|
/** |
49
|
|
|
* @var ContainerInterface |
50
|
|
|
*/ |
51
|
|
|
private $mainContainer; |
52
|
|
|
|
53
|
|
|
public function setUp() |
54
|
|
|
{ |
55
|
|
|
$this->mainContainer = new Picotainer([ |
56
|
|
|
Schema::class => function(ContainerInterface $container) { |
57
|
|
|
return new Schema($container->get(QueryProviderInterface::class), $container->get(RecursiveTypeMapperInterface::class), $container->get(TypeResolver::class)); |
58
|
|
|
}, |
59
|
|
|
QueryProviderInterface::class => function(ContainerInterface $container) { |
60
|
|
|
return new GlobControllerQueryProvider('TheCodingMachine\\GraphQL\\Controllers\\Fixtures\\Integration\\Controllers', $container->get(FieldsBuilderFactory::class), |
61
|
|
|
$container->get(RecursiveTypeMapperInterface::class), $container->get(BasicAutoWiringContainer::class), new ArrayCache()); |
62
|
|
|
}, |
63
|
|
|
FieldsBuilderFactory::class => function(ContainerInterface $container) { |
64
|
|
|
return new FieldsBuilderFactory( |
65
|
|
|
$container->get(AnnotationReader::class), |
66
|
|
|
$container->get(HydratorInterface::class), |
67
|
|
|
$container->get(AuthenticationServiceInterface::class), |
68
|
|
|
$container->get(AuthorizationServiceInterface::class), |
69
|
|
|
$container->get(TypeResolver::class), |
70
|
|
|
$container->get(CachedDocBlockFactory::class) |
71
|
|
|
); |
72
|
|
|
}, |
73
|
|
|
TypeResolver::class => function(ContainerInterface $container) { |
|
|
|
|
74
|
|
|
return new TypeResolver(); |
75
|
|
|
}, |
76
|
|
|
BasicAutoWiringContainer::class => function(ContainerInterface $container) { |
|
|
|
|
77
|
|
|
return new BasicAutoWiringContainer(new EmptyContainer()); |
78
|
|
|
}, |
79
|
|
|
AuthorizationServiceInterface::class => function(ContainerInterface $container) { |
|
|
|
|
80
|
|
|
return new VoidAuthorizationService(); |
81
|
|
|
}, |
82
|
|
|
AuthenticationServiceInterface::class => function(ContainerInterface $container) { |
|
|
|
|
83
|
|
|
return new VoidAuthenticationService(); |
84
|
|
|
}, |
85
|
|
|
RecursiveTypeMapperInterface::class => function(ContainerInterface $container) { |
86
|
|
|
return new RecursiveTypeMapper($container->get(TypeMapperInterface::class), $container->get(NamingStrategyInterface::class), new ArrayCache()); |
87
|
|
|
}, |
88
|
|
|
TypeMapperInterface::class => function(ContainerInterface $container) { |
89
|
|
|
return new GlobTypeMapper('TheCodingMachine\\GraphQL\\Controllers\\Fixtures\\Integration\\Types', |
90
|
|
|
$container->get(TypeGenerator::class), |
91
|
|
|
$container->get(InputTypeGenerator::class), |
92
|
|
|
$container->get(InputTypeUtils::class), |
93
|
|
|
$container->get(BasicAutoWiringContainer::class), |
94
|
|
|
$container->get(AnnotationReader::class), |
95
|
|
|
$container->get(NamingStrategyInterface::class), |
96
|
|
|
new ArrayCache() |
97
|
|
|
); |
98
|
|
|
}, |
99
|
|
|
TypeGenerator::class => function(ContainerInterface $container) { |
100
|
|
|
return new TypeGenerator( |
101
|
|
|
$container->get(AnnotationReader::class), |
102
|
|
|
$container->get(FieldsBuilderFactory::class), |
103
|
|
|
$container->get(NamingStrategyInterface::class) |
104
|
|
|
); |
105
|
|
|
}, |
106
|
|
|
InputTypeGenerator::class => function(ContainerInterface $container) { |
107
|
|
|
return new InputTypeGenerator( |
108
|
|
|
$container->get(InputTypeUtils::class), |
109
|
|
|
$container->get(FieldsBuilderFactory::class), |
110
|
|
|
$container->get(HydratorInterface::class) |
111
|
|
|
); |
112
|
|
|
}, |
113
|
|
|
InputTypeUtils::class => function(ContainerInterface $container) { |
114
|
|
|
return new InputTypeUtils( |
115
|
|
|
$container->get(AnnotationReader::class), |
116
|
|
|
$container->get(NamingStrategyInterface::class) |
117
|
|
|
); |
118
|
|
|
}, |
119
|
|
|
AnnotationReader::class => function(ContainerInterface $container) { |
|
|
|
|
120
|
|
|
return new AnnotationReader(new DoctrineAnnotationReader()); |
121
|
|
|
}, |
122
|
|
|
HydratorInterface::class => function(ContainerInterface $container) { |
|
|
|
|
123
|
|
|
return new FactoryHydrator(); |
124
|
|
|
}, |
125
|
|
|
NamingStrategyInterface::class => function() { |
126
|
|
|
return new NamingStrategy(); |
127
|
|
|
}, |
128
|
|
|
CachedDocBlockFactory::class => function() { |
129
|
|
|
return new CachedDocBlockFactory(new ArrayCache()); |
130
|
|
|
} |
131
|
|
|
]); |
132
|
|
|
|
133
|
|
|
$this->mainContainer->get(TypeResolver::class)->registerSchema($this->mainContainer->get(Schema::class)); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function testEndToEnd() |
137
|
|
|
{ |
138
|
|
|
/** |
139
|
|
|
* @var Schema $schema |
140
|
|
|
*/ |
141
|
|
|
$schema = $this->mainContainer->get(Schema::class); |
142
|
|
|
|
143
|
|
|
$schema->assertValid(); |
144
|
|
|
|
145
|
|
|
$queryString = ' |
146
|
|
|
query { |
147
|
|
|
getContacts { |
148
|
|
|
name |
149
|
|
|
... on User { |
150
|
|
|
email |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
'; |
155
|
|
|
|
156
|
|
|
$result = GraphQL::executeQuery( |
157
|
|
|
$schema, |
158
|
|
|
$queryString |
159
|
|
|
); |
160
|
|
|
|
161
|
|
|
$this->assertSame([ |
162
|
|
|
'getContacts' => [ |
163
|
|
|
[ |
164
|
|
|
'name' => 'Joe' |
165
|
|
|
], |
166
|
|
|
[ |
167
|
|
|
'name' => 'Bill', |
168
|
|
|
'email' => '[email protected]' |
169
|
|
|
] |
170
|
|
|
|
171
|
|
|
] |
172
|
|
|
], $result->toArray(Debug::RETHROW_INTERNAL_EXCEPTIONS)['data']); |
173
|
|
|
|
174
|
|
|
// Let's redo this to test cache. |
175
|
|
|
$result = GraphQL::executeQuery( |
176
|
|
|
$schema, |
177
|
|
|
$queryString |
178
|
|
|
); |
179
|
|
|
|
180
|
|
|
$this->assertSame([ |
181
|
|
|
'getContacts' => [ |
182
|
|
|
[ |
183
|
|
|
'name' => 'Joe' |
184
|
|
|
], |
185
|
|
|
[ |
186
|
|
|
'name' => 'Bill', |
187
|
|
|
'email' => '[email protected]' |
188
|
|
|
] |
189
|
|
|
|
190
|
|
|
] |
191
|
|
|
], $result->toArray(Debug::RETHROW_INTERNAL_EXCEPTIONS)['data']); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
public function testEndToEndInputType() |
195
|
|
|
{ |
196
|
|
|
/** |
197
|
|
|
* @var Schema $schema |
198
|
|
|
*/ |
199
|
|
|
$schema = $this->mainContainer->get(Schema::class); |
200
|
|
|
$queryString = ' |
201
|
|
|
mutation { |
202
|
|
|
saveContact( |
203
|
|
|
contact: { |
204
|
|
|
name: "foo", |
205
|
|
|
birthDate: "1942-12-24 00:00:00", |
206
|
|
|
relations: [ |
207
|
|
|
{ |
208
|
|
|
name: "bar" |
209
|
|
|
birthDate: "1942-12-24 00:00:00", |
210
|
|
|
} |
211
|
|
|
] |
212
|
|
|
} |
213
|
|
|
) { |
214
|
|
|
name, |
215
|
|
|
birthDate, |
216
|
|
|
relations { |
217
|
|
|
name, |
218
|
|
|
birthDate |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
'; |
223
|
|
|
|
224
|
|
|
$result = GraphQL::executeQuery( |
225
|
|
|
$schema, |
226
|
|
|
$queryString |
227
|
|
|
); |
228
|
|
|
|
229
|
|
|
$this->assertSame([ |
230
|
|
|
'saveContact' => [ |
231
|
|
|
'name' => 'foo', |
232
|
|
|
'birthDate' => '1942-12-24T00:00:00+00:00', |
233
|
|
|
'relations' => [ |
234
|
|
|
[ |
235
|
|
|
'name' => 'bar', |
236
|
|
|
'birthDate' => '1942-12-24T00:00:00+00:00' |
237
|
|
|
] |
238
|
|
|
] |
239
|
|
|
] |
240
|
|
|
], $result->toArray(Debug::RETHROW_INTERNAL_EXCEPTIONS)['data']); |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.