1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the OverblogGraphQLBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) Overblog <http://github.com/overblog/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Overblog\GraphQLBundle\Tests\DependencyInjection; |
13
|
|
|
|
14
|
|
|
use Overblog\GraphQLBundle\DependencyInjection\OverblogGraphQLExtension; |
15
|
|
|
use Overblog\GraphQLBundle\DependencyInjection\OverblogGraphQLTypesExtension; |
16
|
|
|
use Overblog\GraphQLBundle\Error\UserError; |
17
|
|
|
use Overblog\GraphQLBundle\Error\UserWarning; |
18
|
|
|
use Overblog\GraphQLBundle\Tests\DependencyInjection\Builder\PagerArgs; |
19
|
|
|
use Overblog\GraphQLBundle\Tests\DependencyInjection\Builder\RawIdField; |
20
|
|
|
use PHPUnit\Framework\TestCase; |
21
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
22
|
|
|
use Symfony\Component\Routing\Exception\ResourceNotFoundException; |
23
|
|
|
|
24
|
|
|
class OverblogGraphQLTypesExtensionTest extends TestCase |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var ContainerBuilder |
28
|
|
|
*/ |
29
|
|
|
private $container; |
30
|
|
|
/** |
31
|
|
|
* @var OverblogGraphQLTypesExtension |
32
|
|
|
*/ |
33
|
|
|
private $extension; |
34
|
|
|
|
35
|
|
|
public function setUp() |
36
|
|
|
{ |
37
|
|
|
$this->container = new ContainerBuilder(); |
38
|
|
|
$this->container->setParameter('kernel.bundles', []); |
39
|
|
|
$this->container->setParameter('kernel.debug', false); |
40
|
|
|
$this->extension = new OverblogGraphQLTypesExtension(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function tearDown() |
44
|
|
|
{ |
45
|
|
|
unset($this->container, $this->extension); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @expectedException \Symfony\Component\Config\Definition\Exception\ForbiddenOverwriteException |
50
|
|
|
*/ |
51
|
|
|
public function testDuplicatedType() |
52
|
|
|
{ |
53
|
|
|
$type = ['foo' => []]; |
54
|
|
|
$configs = [$type, $type]; |
55
|
|
|
$this->extension->load($configs, $this->container); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException |
60
|
|
|
* @expectedExceptionMessageRegExp #The file "(.*)/broken.types.yml" does not contain valid YAML\.# |
61
|
|
|
*/ |
62
|
|
|
public function testBrokenYmlOnPrepend() |
63
|
|
|
{ |
64
|
|
|
$this->extension->containerPrependExtensionConfig($this->getMappingConfig('yaml'), $this->container); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException |
69
|
|
|
* @expectedExceptionMessageRegExp #Unable to parse file "(.*)/broken.types.xml"\.# |
70
|
|
|
*/ |
71
|
|
|
public function testBrokenXmlOnPrepend() |
72
|
|
|
{ |
73
|
|
|
$this->extension->containerPrependExtensionConfig($this->getMappingConfig('xml'), $this->container); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param $internalConfigKey |
78
|
|
|
* @dataProvider internalConfigKeys |
79
|
|
|
* @expectedException \InvalidArgumentException |
80
|
|
|
* @expectedExceptionMessage Don't use internal config keys _object_config, _enum_config, _interface_config, _union_config, _input_object_config, _custom_scalar_config, replace it by "config" instead. |
81
|
|
|
*/ |
82
|
|
|
public function testInternalConfigKeysShouldNotBeUsed($internalConfigKey) |
83
|
|
|
{ |
84
|
|
|
$configs = [ |
85
|
|
|
['bar' => [$internalConfigKey => []]], |
86
|
|
|
]; |
87
|
|
|
|
88
|
|
|
$this->extension->load($configs, $this->container); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @runInSeparateProcess |
93
|
|
|
*/ |
94
|
|
|
public function testCustomExceptions() |
95
|
|
|
{ |
96
|
|
|
$ext = new OverblogGraphQLExtension(); |
97
|
|
|
$ext->load( |
98
|
|
|
[ |
99
|
|
|
[ |
100
|
|
|
'definitions' => [ |
101
|
|
|
'exceptions' => [ |
102
|
|
|
'warnings' => [ |
103
|
|
|
ResourceNotFoundException::class, |
104
|
|
|
], |
105
|
|
|
'errors' => [ |
106
|
|
|
\InvalidArgumentException::class, |
107
|
|
|
], |
108
|
|
|
], |
109
|
|
|
], |
110
|
|
|
], |
111
|
|
|
], |
112
|
|
|
$this->container |
113
|
|
|
); |
114
|
|
|
|
115
|
|
|
$expectedExceptionMap = [ |
116
|
|
|
ResourceNotFoundException::class => UserWarning::class, |
117
|
|
|
\InvalidArgumentException::class => UserError::class, |
118
|
|
|
]; |
119
|
|
|
|
120
|
|
|
$definition = $this->container->getDefinition('overblog_graphql.error_handler'); |
121
|
|
|
$this->assertEquals($expectedExceptionMap, $definition->getArgument(2)); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @runInSeparateProcess |
126
|
|
|
*/ |
127
|
|
|
public function testCustomBuilders() |
128
|
|
|
{ |
129
|
|
|
$ext = new OverblogGraphQLExtension(); |
130
|
|
|
$ext->load( |
131
|
|
|
[ |
132
|
|
|
[ |
133
|
|
|
'definitions' => [ |
134
|
|
|
'builders' => [ |
135
|
|
|
'field' => [ |
136
|
|
|
'RawId' => RawIdField::class, |
137
|
|
|
], |
138
|
|
|
'args' => [ |
139
|
|
|
[ |
140
|
|
|
'alias' => 'Pager', |
141
|
|
|
'class' => PagerArgs::class, |
142
|
|
|
], |
143
|
|
|
], |
144
|
|
|
], |
145
|
|
|
], |
146
|
|
|
], |
147
|
|
|
], |
148
|
|
|
$this->container |
149
|
|
|
); |
150
|
|
|
|
151
|
|
|
$this->extension->load( |
152
|
|
|
[ |
153
|
|
|
[ |
154
|
|
|
'foo' => [ |
155
|
|
|
'type' => 'object', |
156
|
|
|
'config' => [ |
157
|
|
|
'fields' => [ |
158
|
|
|
'rawIDWithDescriptionOverride' => [ |
159
|
|
|
'builder' => 'RawId', |
160
|
|
|
'description' => 'rawIDWithDescriptionOverride description', |
161
|
|
|
], |
162
|
|
|
'rawID' => 'RawId', |
163
|
|
|
'rawIDs' => [ |
164
|
|
|
'type' => '[RawID!]!', |
165
|
|
|
'argsBuilder' => 'Pager', |
166
|
|
|
], |
167
|
|
|
'categories' => [ |
168
|
|
|
'type' => '[String!]!', |
169
|
|
|
'argsBuilder' => ['builder' => 'Pager'], |
170
|
|
|
], |
171
|
|
|
'categories2' => [ |
172
|
|
|
'type' => '[String!]!', |
173
|
|
|
'argsBuilder' => ['builder' => 'Pager', 'config' => ['defaultLimit' => 50]], |
174
|
|
|
], |
175
|
|
|
], |
176
|
|
|
], |
177
|
|
|
], |
178
|
|
|
], |
179
|
|
|
], |
180
|
|
|
$this->container |
181
|
|
|
); |
182
|
|
|
|
183
|
|
|
$this->assertEquals( |
184
|
|
|
[ |
185
|
|
|
'foo' => [ |
186
|
|
|
'type' => 'object', |
187
|
|
|
'config' => [ |
188
|
|
|
'name' => 'foo', |
189
|
|
|
'fields' => [ |
190
|
|
|
'rawIDWithDescriptionOverride' => [ |
191
|
|
|
'description' => 'rawIDWithDescriptionOverride description', |
192
|
|
|
'type' => 'Int!', |
193
|
|
|
'resolve' => '@=value.id', |
194
|
|
|
'args' => [], |
195
|
|
|
], |
196
|
|
|
'rawID' => [ |
197
|
|
|
'description' => 'The raw ID of an object', |
198
|
|
|
'type' => 'Int!', |
199
|
|
|
'resolve' => '@=value.id', |
200
|
|
|
'args' => [], |
201
|
|
|
], |
202
|
|
|
'rawIDs' => [ |
203
|
|
|
'type' => '[RawID!]!', |
204
|
|
|
'args' => [ |
205
|
|
|
'limit' => [ |
206
|
|
|
'type' => 'Int!', |
207
|
|
|
'defaultValue' => 20, |
208
|
|
|
], |
209
|
|
|
'offset' => [ |
210
|
|
|
'type' => 'Int!', |
211
|
|
|
'defaultValue' => 0, |
212
|
|
|
], |
213
|
|
|
], |
214
|
|
|
], |
215
|
|
|
'categories' => [ |
216
|
|
|
'type' => '[String!]!', |
217
|
|
|
'args' => [ |
218
|
|
|
'limit' => [ |
219
|
|
|
'type' => 'Int!', |
220
|
|
|
'defaultValue' => 20, |
221
|
|
|
], |
222
|
|
|
'offset' => [ |
223
|
|
|
'type' => 'Int!', |
224
|
|
|
'defaultValue' => 0, |
225
|
|
|
], |
226
|
|
|
], |
227
|
|
|
], |
228
|
|
|
'categories2' => [ |
229
|
|
|
'type' => '[String!]!', |
230
|
|
|
'args' => [ |
231
|
|
|
'limit' => [ |
232
|
|
|
'type' => 'Int!', |
233
|
|
|
'defaultValue' => 50, |
234
|
|
|
], |
235
|
|
|
'offset' => [ |
236
|
|
|
'type' => 'Int!', |
237
|
|
|
'defaultValue' => 0, |
238
|
|
|
], |
239
|
|
|
], |
240
|
|
|
], |
241
|
|
|
], |
242
|
|
|
'interfaces' => [], |
243
|
|
|
], |
244
|
|
|
], |
245
|
|
|
|
246
|
|
|
], |
247
|
|
|
$this->container->getParameter('overblog_graphql_types.config') |
248
|
|
|
); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
public function internalConfigKeys() |
252
|
|
|
{ |
253
|
|
|
return [ |
254
|
|
|
['_object_config'], |
255
|
|
|
['_enum_config'], |
256
|
|
|
['_interface_config'], |
257
|
|
|
['_union_config'], |
258
|
|
|
['_input_object_config'], |
259
|
|
|
]; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
private function getMappingConfig($type) |
263
|
|
|
{ |
264
|
|
|
$config = [ |
265
|
|
|
'definitions' => [ |
266
|
|
|
'mappings' => [ |
267
|
|
|
'types' => [ |
268
|
|
|
[ |
269
|
|
|
'type' => $type, |
270
|
|
|
'dir' => __DIR__.'/mapping/'.$type, |
271
|
|
|
], |
272
|
|
|
], |
273
|
|
|
], |
274
|
|
|
], |
275
|
|
|
]; |
276
|
|
|
|
277
|
|
|
return $config; |
278
|
|
|
} |
279
|
|
|
} |
280
|
|
|
|