-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy pathIntegrationTestCase.php
363 lines (346 loc) · 19.9 KB
/
IntegrationTestCase.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
<?php
namespace TheCodingMachine\GraphQLite\Integration;
use GraphQL\Error\DebugFlag;
use GraphQL\Executor\ExecutionResult;
use Kcs\ClassFinder\Finder\ComposerFinder;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use stdClass;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\Cache\Adapter\Psr16Adapter;
use Symfony\Component\Cache\Psr16Cache;
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
use TheCodingMachine\GraphQLite\AggregateQueryProvider;
use TheCodingMachine\GraphQLite\AnnotationReader;
use TheCodingMachine\GraphQLite\Cache\ClassBoundCache;
use TheCodingMachine\GraphQLite\Cache\FilesSnapshot;
use TheCodingMachine\GraphQLite\Cache\SnapshotClassBoundCache;
use TheCodingMachine\GraphQLite\Containers\BasicAutoWiringContainer;
use TheCodingMachine\GraphQLite\Containers\EmptyContainer;
use TheCodingMachine\GraphQLite\Containers\LazyContainer;
use TheCodingMachine\GraphQLite\Discovery\Cache\ClassFinderComputedCache;
use TheCodingMachine\GraphQLite\Discovery\Cache\HardClassFinderComputedCache;
use TheCodingMachine\GraphQLite\Discovery\ClassFinder;
use TheCodingMachine\GraphQLite\Discovery\KcsClassFinder;
use TheCodingMachine\GraphQLite\FieldsBuilder;
use TheCodingMachine\GraphQLite\GlobControllerQueryProvider;
use TheCodingMachine\GraphQLite\InputTypeGenerator;
use TheCodingMachine\GraphQLite\InputTypeUtils;
use TheCodingMachine\GraphQLite\Loggers\ExceptionLogger;
use TheCodingMachine\GraphQLite\Mappers\ClassFinderTypeMapper;
use TheCodingMachine\GraphQLite\Mappers\CompositeTypeMapper;
use TheCodingMachine\GraphQLite\Mappers\Parameters\ContainerParameterHandler;
use TheCodingMachine\GraphQLite\Mappers\Parameters\InjectUserParameterHandler;
use TheCodingMachine\GraphQLite\Mappers\Parameters\ParameterMiddlewareInterface;
use TheCodingMachine\GraphQLite\Mappers\Parameters\ParameterMiddlewarePipe;
use TheCodingMachine\GraphQLite\Mappers\Parameters\PrefetchParameterMiddleware;
use TheCodingMachine\GraphQLite\Mappers\Parameters\ResolveInfoParameterHandler;
use TheCodingMachine\GraphQLite\Mappers\PorpaginasTypeMapper;
use TheCodingMachine\GraphQLite\Mappers\RecursiveTypeMapper;
use TheCodingMachine\GraphQLite\Mappers\RecursiveTypeMapperInterface;
use TheCodingMachine\GraphQLite\Mappers\Root\BaseTypeMapper;
use TheCodingMachine\GraphQLite\Mappers\Root\CallableTypeMapper;
use TheCodingMachine\GraphQLite\Mappers\Root\CompoundTypeMapper;
use TheCodingMachine\GraphQLite\Mappers\Root\EnumTypeMapper;
use TheCodingMachine\GraphQLite\Mappers\Root\FinalRootTypeMapper;
use TheCodingMachine\GraphQLite\Mappers\Root\IteratorTypeMapper;
use TheCodingMachine\GraphQLite\Mappers\Root\LastDelegatingTypeMapper;
use TheCodingMachine\GraphQLite\Mappers\Root\MyCLabsEnumTypeMapper;
use TheCodingMachine\GraphQLite\Mappers\Root\NullableTypeMapperAdapter;
use TheCodingMachine\GraphQLite\Mappers\Root\RootTypeMapperInterface;
use TheCodingMachine\GraphQLite\Mappers\Root\VoidTypeMapper;
use TheCodingMachine\GraphQLite\Mappers\TypeMapperInterface;
use TheCodingMachine\GraphQLite\Middlewares\AuthorizationFieldMiddleware;
use TheCodingMachine\GraphQLite\Middlewares\AuthorizationInputFieldMiddleware;
use TheCodingMachine\GraphQLite\Middlewares\CostFieldMiddleware;
use TheCodingMachine\GraphQLite\Middlewares\FieldMiddlewareInterface;
use TheCodingMachine\GraphQLite\Middlewares\FieldMiddlewarePipe;
use TheCodingMachine\GraphQLite\Middlewares\InputFieldMiddlewareInterface;
use TheCodingMachine\GraphQLite\Middlewares\InputFieldMiddlewarePipe;
use TheCodingMachine\GraphQLite\Middlewares\SecurityFieldMiddleware;
use TheCodingMachine\GraphQLite\Middlewares\SecurityInputFieldMiddleware;
use TheCodingMachine\GraphQLite\NamingStrategy;
use TheCodingMachine\GraphQLite\NamingStrategyInterface;
use TheCodingMachine\GraphQLite\ParameterizedCallableResolver;
use TheCodingMachine\GraphQLite\QueryProviderInterface;
use TheCodingMachine\GraphQLite\Reflection\DocBlock\CachedDocBlockFactory;
use TheCodingMachine\GraphQLite\Reflection\DocBlock\DocBlockFactory;
use TheCodingMachine\GraphQLite\Reflection\DocBlock\PhpDocumentorDocBlockFactory;
use TheCodingMachine\GraphQLite\Schema;
use TheCodingMachine\GraphQLite\Security\AuthenticationServiceInterface;
use TheCodingMachine\GraphQLite\Security\AuthorizationServiceInterface;
use TheCodingMachine\GraphQLite\Security\SecurityExpressionLanguageProvider;
use TheCodingMachine\GraphQLite\Security\VoidAuthenticationService;
use TheCodingMachine\GraphQLite\Security\VoidAuthorizationService;
use TheCodingMachine\GraphQLite\TypeGenerator;
use TheCodingMachine\GraphQLite\TypeRegistry;
use TheCodingMachine\GraphQLite\Types\ArgumentResolver;
use TheCodingMachine\GraphQLite\Types\TypeResolver;
class IntegrationTestCase extends TestCase
{
protected ContainerInterface $mainContainer;
public function setUp(): void
{
$this->mainContainer = $this->createContainer();
}
/** @param array<string, callable> $overloadedServices */
public function createContainer(array $overloadedServices = []): ContainerInterface
{
$services = [
Schema::class => static function (ContainerInterface $container) {
return new Schema($container->get(QueryProviderInterface::class), $container->get(RecursiveTypeMapperInterface::class), $container->get(TypeResolver::class), $container->get(RootTypeMapperInterface::class));
},
ClassFinder::class => function () {
$namespaces = [
'TheCodingMachine\\GraphQLite\\Fixtures\\Integration\\Types',
'TheCodingMachine\\GraphQLite\\Fixtures\\Integration\\Models',
'TheCodingMachine\\GraphQLite\\Fixtures\\Integration\\Controllers',
];
$hash = md5(implode(',', $namespaces));
$composerFinder = new ComposerFinder();
foreach ($namespaces as $namespace) {
$composerFinder->inNamespace($namespace);
}
return new KcsClassFinder($composerFinder, $hash);
},
ClassFinderComputedCache::class => function () {
return new HardClassFinderComputedCache(
new Psr16Cache(new ArrayAdapter()),
);
},
QueryProviderInterface::class => static function (ContainerInterface $container) {
$queryProvider = new GlobControllerQueryProvider(
$container->get(FieldsBuilder::class),
$container->get(BasicAutoWiringContainer::class),
$container->get(AnnotationReader::class),
$container->get(ClassFinder::class),
$container->get(ClassFinderComputedCache::class),
);
$queryProvider = new AggregateQueryProvider([
$queryProvider,
new GlobControllerQueryProvider(
$container->get(FieldsBuilder::class),
$container->get(BasicAutoWiringContainer::class),
$container->get(AnnotationReader::class),
$container->get(ClassFinder::class),
$container->get(ClassFinderComputedCache::class),
),
]);
return $queryProvider;
},
FieldsBuilder::class => static function (ContainerInterface $container) {
$parameterMiddlewarePipe = $container->get(ParameterMiddlewareInterface::class);
$fieldsBuilder = new FieldsBuilder(
$container->get(AnnotationReader::class),
$container->get(RecursiveTypeMapperInterface::class),
$container->get(ArgumentResolver::class),
$container->get(TypeResolver::class),
$container->get(DocBlockFactory::class),
$container->get(NamingStrategyInterface::class),
$container->get(RootTypeMapperInterface::class),
$parameterMiddlewarePipe,
$container->get(FieldMiddlewareInterface::class),
$container->get(InputFieldMiddlewareInterface::class),
);
$parameterizedCallableResolver = new ParameterizedCallableResolver($fieldsBuilder, $container);
$parameterMiddlewarePipe->pipe(new PrefetchParameterMiddleware($parameterizedCallableResolver));
return $fieldsBuilder;
},
FieldMiddlewareInterface::class => static function (ContainerInterface $container) {
$pipe = new FieldMiddlewarePipe();
$pipe->pipe($container->get(AuthorizationFieldMiddleware::class));
$pipe->pipe($container->get(SecurityFieldMiddleware::class));
$pipe->pipe($container->get(CostFieldMiddleware::class));
return $pipe;
},
InputFieldMiddlewareInterface::class => static function (ContainerInterface $container) {
$pipe = new InputFieldMiddlewarePipe();
$pipe->pipe($container->get(AuthorizationInputFieldMiddleware::class));
$pipe->pipe($container->get(SecurityInputFieldMiddleware::class));
return $pipe;
},
AuthorizationInputFieldMiddleware::class => static function (ContainerInterface $container) {
return new AuthorizationInputFieldMiddleware(
$container->get(AuthenticationServiceInterface::class),
$container->get(AuthorizationServiceInterface::class),
);
},
SecurityInputFieldMiddleware::class => static function (ContainerInterface $container) {
return new SecurityInputFieldMiddleware(
new ExpressionLanguage(new Psr16Adapter(new Psr16Cache(new ArrayAdapter())), [new SecurityExpressionLanguageProvider()]),
$container->get(AuthenticationServiceInterface::class),
$container->get(AuthorizationServiceInterface::class),
);
},
AuthorizationFieldMiddleware::class => static function (ContainerInterface $container) {
return new AuthorizationFieldMiddleware(
$container->get(AuthenticationServiceInterface::class),
$container->get(AuthorizationServiceInterface::class),
);
},
SecurityFieldMiddleware::class => static function (ContainerInterface $container) {
return new SecurityFieldMiddleware(
new ExpressionLanguage(new Psr16Adapter(new Psr16Cache(new ArrayAdapter())), [new SecurityExpressionLanguageProvider()]),
$container->get(AuthenticationServiceInterface::class),
$container->get(AuthorizationServiceInterface::class),
);
},
CostFieldMiddleware::class => fn () => new CostFieldMiddleware(),
ArgumentResolver::class => static function (ContainerInterface $container) {
return new ArgumentResolver();
},
TypeResolver::class => static function (ContainerInterface $container) {
return new TypeResolver();
},
BasicAutoWiringContainer::class => static function (ContainerInterface $container) {
return new BasicAutoWiringContainer(new EmptyContainer());
},
AuthorizationServiceInterface::class => static function (ContainerInterface $container) {
return new VoidAuthorizationService();
},
AuthenticationServiceInterface::class => static function (ContainerInterface $container) {
return new VoidAuthenticationService();
},
RecursiveTypeMapperInterface::class => static function (ContainerInterface $container) {
$arrayAdapter = new ArrayAdapter();
$arrayAdapter->setLogger(new ExceptionLogger());
return new RecursiveTypeMapper(
$container->get(TypeMapperInterface::class),
$container->get(NamingStrategyInterface::class),
new Psr16Cache($arrayAdapter),
$container->get(TypeRegistry::class),
$container->get(AnnotationReader::class),
);
},
TypeMapperInterface::class => static function (ContainerInterface $container) {
return new CompositeTypeMapper();
},
ClassFinderTypeMapper::class => static function (ContainerInterface $container) {
$arrayAdapter = new ArrayAdapter();
$arrayAdapter->setLogger(new ExceptionLogger());
return new ClassFinderTypeMapper(
$container->get(ClassFinder::class),
$container->get(TypeGenerator::class),
$container->get(InputTypeGenerator::class),
$container->get(InputTypeUtils::class),
$container->get(BasicAutoWiringContainer::class),
$container->get(AnnotationReader::class),
$container->get(NamingStrategyInterface::class),
$container->get(RecursiveTypeMapperInterface::class),
$container->get(ClassFinderComputedCache::class),
);
},
PorpaginasTypeMapper::class => static function (ContainerInterface $container) {
return new PorpaginasTypeMapper($container->get(RecursiveTypeMapperInterface::class));
},
EnumTypeMapper::class => static function (ContainerInterface $container) {
return new EnumTypeMapper(
$container->get(RootTypeMapperInterface::class),
$container->get(AnnotationReader::class),
$container->get(ClassFinder::class),
$container->get(ClassFinderComputedCache::class),
);
},
TypeGenerator::class => static function (ContainerInterface $container) {
return new TypeGenerator(
$container->get(AnnotationReader::class),
$container->get(NamingStrategyInterface::class),
$container->get(TypeRegistry::class),
$container->get(BasicAutoWiringContainer::class),
$container->get(RecursiveTypeMapperInterface::class),
$container->get(FieldsBuilder::class),
);
},
TypeRegistry::class => static function () {
return new TypeRegistry();
},
InputTypeGenerator::class => static function (ContainerInterface $container) {
return new InputTypeGenerator(
$container->get(InputTypeUtils::class),
$container->get(FieldsBuilder::class),
);
},
InputTypeUtils::class => static function (ContainerInterface $container) {
return new InputTypeUtils(
$container->get(AnnotationReader::class),
$container->get(NamingStrategyInterface::class),
);
},
AnnotationReader::class => static function (ContainerInterface $container) {
return new AnnotationReader();
},
NamingStrategyInterface::class => static function () {
return new NamingStrategy();
},
ClassBoundCache::class => static function () {
$arrayAdapter = new ArrayAdapter();
$arrayAdapter->setLogger(new ExceptionLogger());
$psr16Cache = new Psr16Cache($arrayAdapter);
return new SnapshotClassBoundCache($psr16Cache, FilesSnapshot::alwaysUnchanged(...));
},
DocBlockFactory::class => static function (ContainerInterface $container) {
return new CachedDocBlockFactory(
$container->get(ClassBoundCache::class),
PhpDocumentorDocBlockFactory::default(),
);
},
RootTypeMapperInterface::class => static function (ContainerInterface $container) {
return new CallableTypeMapper(
new VoidTypeMapper(
new NullableTypeMapperAdapter(
$container->get('topRootTypeMapper')
)
),
$container->get('topRootTypeMapper')
);
},
'topRootTypeMapper' => static function () {
return new LastDelegatingTypeMapper();
},
'rootTypeMapper' => static function (ContainerInterface $container) {
// These are in reverse order of execution
$errorRootTypeMapper = new FinalRootTypeMapper($container->get(RecursiveTypeMapperInterface::class));
$rootTypeMapper = new BaseTypeMapper($errorRootTypeMapper, $container->get(RecursiveTypeMapperInterface::class), $container->get(RootTypeMapperInterface::class));
$rootTypeMapper = new MyCLabsEnumTypeMapper($rootTypeMapper, $container->get(AnnotationReader::class), $container->get(ClassFinder::class), $container->get(ClassFinderComputedCache::class));
$rootTypeMapper = new EnumTypeMapper($rootTypeMapper, $container->get(AnnotationReader::class), $container->get(DocBlockFactory::class), $container->get(ClassFinder::class), $container->get(ClassFinderComputedCache::class));
$rootTypeMapper = new CompoundTypeMapper($rootTypeMapper, $container->get(RootTypeMapperInterface::class), $container->get(NamingStrategyInterface::class), $container->get(TypeRegistry::class), $container->get(RecursiveTypeMapperInterface::class));
$rootTypeMapper = new IteratorTypeMapper($rootTypeMapper, $container->get(RootTypeMapperInterface::class));
return $rootTypeMapper;
},
ContainerParameterHandler::class => static function (ContainerInterface $container) {
return new ContainerParameterHandler($container, true, true);
},
InjectUserParameterHandler::class => static function (ContainerInterface $container) {
return new InjectUserParameterHandler($container->get(AuthenticationServiceInterface::class));
},
'testService' => static function () {
return 'foo';
},
stdClass::class => static function () {
// Empty test service for autowiring
return new stdClass();
},
ParameterMiddlewareInterface::class => static function (ContainerInterface $container) {
$parameterMiddlewarePipe = new ParameterMiddlewarePipe();
$parameterMiddlewarePipe->pipe(new ResolveInfoParameterHandler());
$parameterMiddlewarePipe->pipe($container->get(ContainerParameterHandler::class));
$parameterMiddlewarePipe->pipe($container->get(InjectUserParameterHandler::class));
return $parameterMiddlewarePipe;
},
];
$container = new LazyContainer($overloadedServices + $services);
$container->get(TypeResolver::class)->registerSchema($container->get(Schema::class));
$container->get(TypeMapperInterface::class)->addTypeMapper($container->get(ClassFinderTypeMapper::class));
$container->get(TypeMapperInterface::class)->addTypeMapper($container->get(PorpaginasTypeMapper::class));
$container->get('topRootTypeMapper')->setNext($container->get('rootTypeMapper'));
return $container;
}
protected function getSuccessResult(ExecutionResult $result, int $debugFlag = DebugFlag::RETHROW_INTERNAL_EXCEPTIONS): mixed
{
$array = $result->toArray($debugFlag);
if (isset($array['errors']) || ! isset($array['data'])) {
$this->fail('Expected a successful answer. Got ' . json_encode($array, JSON_PRETTY_PRINT));
}
return $array['data'];
}
}