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\ControllerQueryProviderFactory; |
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\Mappers\GlobTypeMapper; |
27
|
|
|
use TheCodingMachine\GraphQL\Controllers\Mappers\RecursiveTypeMapper; |
28
|
|
|
use TheCodingMachine\GraphQL\Controllers\Mappers\RecursiveTypeMapperInterface; |
29
|
|
|
use TheCodingMachine\GraphQL\Controllers\Mappers\TypeMapperInterface; |
30
|
|
|
use TheCodingMachine\GraphQL\Controllers\NamingStrategy; |
31
|
|
|
use TheCodingMachine\GraphQL\Controllers\NamingStrategyInterface; |
32
|
|
|
use TheCodingMachine\GraphQL\Controllers\QueryProviderInterface; |
33
|
|
|
use TheCodingMachine\GraphQL\Controllers\Containers\BasicAutoWiringContainer; |
34
|
|
|
use TheCodingMachine\GraphQL\Controllers\Containers\EmptyContainer; |
35
|
|
|
use TheCodingMachine\GraphQL\Controllers\Reflection\CachedDocBlockFactory; |
36
|
|
|
use TheCodingMachine\GraphQL\Controllers\Schema; |
37
|
|
|
use TheCodingMachine\GraphQL\Controllers\Security\AuthenticationServiceInterface; |
38
|
|
|
use TheCodingMachine\GraphQL\Controllers\Security\AuthorizationServiceInterface; |
39
|
|
|
use TheCodingMachine\GraphQL\Controllers\Security\VoidAuthenticationService; |
40
|
|
|
use TheCodingMachine\GraphQL\Controllers\Security\VoidAuthorizationService; |
41
|
|
|
use TheCodingMachine\GraphQL\Controllers\TypeGenerator; |
42
|
|
|
|
43
|
|
|
class EndToEndTest extends TestCase |
44
|
|
|
{ |
45
|
|
|
/** |
46
|
|
|
* @var ContainerInterface |
47
|
|
|
*/ |
48
|
|
|
private $mainContainer; |
49
|
|
|
|
50
|
|
|
public function setUp() |
51
|
|
|
{ |
52
|
|
|
$this->mainContainer = new Picotainer([ |
53
|
|
|
Schema::class => function(ContainerInterface $container) { |
54
|
|
|
return new Schema($container->get(QueryProviderInterface::class), $container->get(RecursiveTypeMapperInterface::class)); |
55
|
|
|
}, |
56
|
|
|
QueryProviderInterface::class => function(ContainerInterface $container) { |
57
|
|
|
return new GlobControllerQueryProvider('TheCodingMachine\\GraphQL\\Controllers\\Fixtures\\Integration\\Controllers', $container->get(ControllerQueryProviderFactory::class), |
58
|
|
|
$container->get(RecursiveTypeMapperInterface::class), $container->get(BasicAutoWiringContainer::class), new ArrayCache()); |
59
|
|
|
}, |
60
|
|
|
ControllerQueryProviderFactory::class => function(ContainerInterface $container) { |
61
|
|
|
return new ControllerQueryProviderFactory( |
62
|
|
|
$container->get(AnnotationReader::class), |
63
|
|
|
$container->get(HydratorInterface::class), |
64
|
|
|
$container->get(AuthenticationServiceInterface::class), |
65
|
|
|
$container->get(AuthorizationServiceInterface::class), |
66
|
|
|
$container->get(BasicAutoWiringContainer::class), |
67
|
|
|
$container->get(CachedDocBlockFactory::class) |
68
|
|
|
); |
69
|
|
|
}, |
70
|
|
|
BasicAutoWiringContainer::class => function(ContainerInterface $container) { |
|
|
|
|
71
|
|
|
return new BasicAutoWiringContainer(new EmptyContainer()); |
72
|
|
|
}, |
73
|
|
|
AuthorizationServiceInterface::class => function(ContainerInterface $container) { |
|
|
|
|
74
|
|
|
return new VoidAuthorizationService(); |
75
|
|
|
}, |
76
|
|
|
AuthenticationServiceInterface::class => function(ContainerInterface $container) { |
|
|
|
|
77
|
|
|
return new VoidAuthenticationService(); |
78
|
|
|
}, |
79
|
|
|
RecursiveTypeMapperInterface::class => function(ContainerInterface $container) { |
80
|
|
|
return new RecursiveTypeMapper($container->get(TypeMapperInterface::class), $container->get(NamingStrategyInterface::class), new ArrayCache()); |
81
|
|
|
}, |
82
|
|
|
TypeMapperInterface::class => function(ContainerInterface $container) { |
83
|
|
|
return new GlobTypeMapper('TheCodingMachine\\GraphQL\\Controllers\\Fixtures\\Integration\\Types', |
84
|
|
|
$container->get(TypeGenerator::class), |
85
|
|
|
$container->get(InputTypeGenerator::class), |
86
|
|
|
$container->get(BasicAutoWiringContainer::class), |
87
|
|
|
$container->get(AnnotationReader::class), |
88
|
|
|
$container->get(NamingStrategyInterface::class), |
89
|
|
|
new ArrayCache() |
90
|
|
|
); |
91
|
|
|
}, |
92
|
|
|
TypeGenerator::class => function(ContainerInterface $container) { |
93
|
|
|
return new TypeGenerator( |
94
|
|
|
$container->get(AnnotationReader::class), |
95
|
|
|
$container->get(ControllerQueryProviderFactory::class), |
96
|
|
|
$container->get(NamingStrategyInterface::class) |
97
|
|
|
); |
98
|
|
|
}, |
99
|
|
|
InputTypeGenerator::class => function(ContainerInterface $container) { |
100
|
|
|
return new InputTypeGenerator( |
101
|
|
|
$container->get(AnnotationReader::class), |
102
|
|
|
$container->get(ControllerQueryProviderFactory::class), |
103
|
|
|
$container->get(NamingStrategyInterface::class), |
104
|
|
|
$container->get(HydratorInterface::class) |
105
|
|
|
); |
106
|
|
|
}, |
107
|
|
|
AnnotationReader::class => function(ContainerInterface $container) { |
|
|
|
|
108
|
|
|
return new AnnotationReader(new DoctrineAnnotationReader()); |
109
|
|
|
}, |
110
|
|
|
HydratorInterface::class => function(ContainerInterface $container) { |
|
|
|
|
111
|
|
|
return new FactoryHydrator(); |
112
|
|
|
}, |
113
|
|
|
NamingStrategyInterface::class => function() { |
114
|
|
|
return new NamingStrategy(); |
115
|
|
|
}, |
116
|
|
|
CachedDocBlockFactory::class => function() { |
117
|
|
|
return new CachedDocBlockFactory(new ArrayCache()); |
118
|
|
|
} |
119
|
|
|
]); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function testEndToEnd() |
123
|
|
|
{ |
124
|
|
|
/** |
125
|
|
|
* @var Schema $schema |
126
|
|
|
*/ |
127
|
|
|
$schema = $this->mainContainer->get(Schema::class); |
128
|
|
|
|
129
|
|
|
$schema->assertValid(); |
130
|
|
|
|
131
|
|
|
$queryString = ' |
132
|
|
|
query { |
133
|
|
|
getContacts { |
134
|
|
|
name |
135
|
|
|
... on User { |
136
|
|
|
email |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
'; |
141
|
|
|
|
142
|
|
|
$result = GraphQL::executeQuery( |
143
|
|
|
$schema, |
144
|
|
|
$queryString |
145
|
|
|
); |
146
|
|
|
|
147
|
|
|
$this->assertSame([ |
148
|
|
|
'getContacts' => [ |
149
|
|
|
[ |
150
|
|
|
'name' => 'Joe' |
151
|
|
|
], |
152
|
|
|
[ |
153
|
|
|
'name' => 'Bill', |
154
|
|
|
'email' => '[email protected]' |
155
|
|
|
] |
156
|
|
|
|
157
|
|
|
] |
158
|
|
|
], $result->toArray(Debug::RETHROW_INTERNAL_EXCEPTIONS)['data']); |
159
|
|
|
|
160
|
|
|
// Let's redo this to test cache. |
161
|
|
|
$result = GraphQL::executeQuery( |
162
|
|
|
$schema, |
163
|
|
|
$queryString |
164
|
|
|
); |
165
|
|
|
|
166
|
|
|
$this->assertSame([ |
167
|
|
|
'getContacts' => [ |
168
|
|
|
[ |
169
|
|
|
'name' => 'Joe' |
170
|
|
|
], |
171
|
|
|
[ |
172
|
|
|
'name' => 'Bill', |
173
|
|
|
'email' => '[email protected]' |
174
|
|
|
] |
175
|
|
|
|
176
|
|
|
] |
177
|
|
|
], $result->toArray(Debug::RETHROW_INTERNAL_EXCEPTIONS)['data']); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
public function testEndToEndInputType() |
181
|
|
|
{ |
182
|
|
|
/** |
183
|
|
|
* @var Schema $schema |
184
|
|
|
*/ |
185
|
|
|
$schema = $this->mainContainer->get(Schema::class); |
186
|
|
|
$queryString = ' |
187
|
|
|
mutation { |
188
|
|
|
saveContact( |
189
|
|
|
contact: { |
190
|
|
|
name: "foo" |
191
|
|
|
relations: [ |
192
|
|
|
{ |
193
|
|
|
name: "bar" |
194
|
|
|
} |
195
|
|
|
] |
196
|
|
|
} |
197
|
|
|
) { |
198
|
|
|
name, |
199
|
|
|
relations { |
200
|
|
|
name |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
'; |
205
|
|
|
|
206
|
|
|
$result = GraphQL::executeQuery( |
207
|
|
|
$schema, |
208
|
|
|
$queryString |
209
|
|
|
); |
210
|
|
|
|
211
|
|
|
$this->assertSame([ |
212
|
|
|
'saveContact' => [ |
213
|
|
|
'name' => 'foo', |
214
|
|
|
'relations' => [ |
215
|
|
|
[ |
216
|
|
|
'name' => 'bar' |
217
|
|
|
] |
218
|
|
|
] |
219
|
|
|
] |
220
|
|
|
], $result->toArray(Debug::RETHROW_INTERNAL_EXCEPTIONS)['data']); |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.