1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Overblog\GraphQLBundle\Tests\Error; |
4
|
|
|
|
5
|
|
|
use GraphQL\Error\Error as GraphQLError; |
6
|
|
|
use GraphQL\Error\UserError as GraphQLUserError; |
7
|
|
|
use GraphQL\Executor\ExecutionResult; |
8
|
|
|
use Overblog\GraphQLBundle\Error\ErrorHandler; |
9
|
|
|
use Overblog\GraphQLBundle\Error\UserError; |
10
|
|
|
use Overblog\GraphQLBundle\Error\UserErrors; |
11
|
|
|
use Overblog\GraphQLBundle\Error\UserWarning; |
12
|
|
|
use PHPUnit\Framework\TestCase; |
13
|
|
|
|
14
|
|
|
class ErrorHandlerTest extends TestCase |
15
|
|
|
{ |
16
|
|
|
/** @var ErrorHandler */ |
17
|
|
|
private $errorHandler; |
18
|
|
|
|
19
|
|
|
public function setUp() |
20
|
|
|
{ |
21
|
|
|
$this->errorHandler = new ErrorHandler(); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function testMaskErrorWithThrowExceptionSetToFalse() |
25
|
|
|
{ |
26
|
|
|
$executionResult = new ExecutionResult( |
27
|
|
|
null, |
28
|
|
|
[ |
29
|
|
|
new GraphQLError('Error without wrapped exception'), |
30
|
|
|
new GraphQLError('Error with wrapped exception', null, null, null, null, new \Exception('My Exception message')), |
|
|
|
|
31
|
|
|
new GraphQLError('Error with wrapped user error', null, null, null, null, new UserError('My User Error')), |
|
|
|
|
32
|
|
|
new GraphQLError('', null, null, null, null, new UserErrors(['My User Error 1', 'My User Error 2', new UserError('My User Error 3')])), |
|
|
|
|
33
|
|
|
new GraphQLError('Error with wrapped user warning', null, null, null, null, new UserWarning('My User Warning')), |
|
|
|
|
34
|
|
|
new GraphQLError('Invalid value!', null, null, null, null, new GraphQLUserError('Invalid value!')), |
|
|
|
|
35
|
|
|
] |
36
|
|
|
); |
37
|
|
|
|
38
|
|
|
$this->errorHandler->handleErrors($executionResult); |
39
|
|
|
|
40
|
|
|
$expected = [ |
41
|
|
|
'errors' => [ |
42
|
|
|
[ |
43
|
|
|
'message' => 'Error without wrapped exception', |
44
|
|
|
], |
45
|
|
|
[ |
46
|
|
|
'message' => ErrorHandler::DEFAULT_ERROR_MESSAGE, |
47
|
|
|
], |
48
|
|
|
[ |
49
|
|
|
'message' => 'Error with wrapped user error', |
50
|
|
|
], |
51
|
|
|
[ |
52
|
|
|
'message' => 'My User Error 1', |
53
|
|
|
], |
54
|
|
|
[ |
55
|
|
|
'message' => 'My User Error 2', |
56
|
|
|
], |
57
|
|
|
[ |
58
|
|
|
'message' => 'My User Error 3', |
59
|
|
|
], |
60
|
|
|
[ |
61
|
|
|
'message' => 'Invalid value!', |
62
|
|
|
], |
63
|
|
|
], |
64
|
|
|
'extensions' => [ |
65
|
|
|
'warnings' => [ |
66
|
|
|
[ |
67
|
|
|
'message' => 'Error with wrapped user warning', |
68
|
|
|
], |
69
|
|
|
], |
70
|
|
|
], |
71
|
|
|
]; |
72
|
|
|
|
73
|
|
|
$this->assertEquals($expected, $executionResult->toArray()); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function testMaskErrorWithWrappedExceptionAndThrowExceptionSetToTrue() |
77
|
|
|
{ |
78
|
|
|
$this->expectException(\Exception::class); |
79
|
|
|
$this->expectExceptionMessage('My Exception message'); |
80
|
|
|
|
81
|
|
|
$executionResult = new ExecutionResult( |
82
|
|
|
null, |
83
|
|
|
[ |
84
|
|
|
new GraphQLError('Error with wrapped exception', null, null, null, null, new \Exception('My Exception message')), |
|
|
|
|
85
|
|
|
] |
86
|
|
|
); |
87
|
|
|
|
88
|
|
|
$this->errorHandler->handleErrors($executionResult, true); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function testInvalidUserErrorsItem() |
92
|
|
|
{ |
93
|
|
|
$this->expectException(\InvalidArgumentException::class); |
94
|
|
|
$this->expectExceptionMessage('Error must be string or instance of Overblog\GraphQLBundle\Error\UserError.'); |
95
|
|
|
|
96
|
|
|
new UserErrors([ |
97
|
|
|
'Some Error', |
98
|
|
|
false, |
99
|
|
|
]); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function testMaskErrorWithWrappedUserErrorAndThrowExceptionSetToTrue() |
103
|
|
|
{ |
104
|
|
|
$executionResult = new ExecutionResult( |
105
|
|
|
null, |
106
|
|
|
[ |
107
|
|
|
new GraphQLError('Error with wrapped user error', null, null, null, null, new UserError('My User Error')), |
|
|
|
|
108
|
|
|
] |
109
|
|
|
); |
110
|
|
|
|
111
|
|
|
$this->errorHandler->handleErrors($executionResult, true); |
112
|
|
|
|
113
|
|
|
$expected = [ |
114
|
|
|
'errors' => [ |
115
|
|
|
[ |
116
|
|
|
'message' => 'Error with wrapped user error', |
117
|
|
|
], |
118
|
|
|
], |
119
|
|
|
]; |
120
|
|
|
|
121
|
|
|
$this->assertEquals($expected, $executionResult->toArray()); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function testMaskErrorWithoutWrappedExceptionAndThrowExceptionSetToTrue() |
125
|
|
|
{ |
126
|
|
|
$executionResult = new ExecutionResult( |
127
|
|
|
null, |
128
|
|
|
[ |
129
|
|
|
new GraphQLError('Error without wrapped exception'), |
130
|
|
|
] |
131
|
|
|
); |
132
|
|
|
|
133
|
|
|
$this->errorHandler->handleErrors($executionResult, true); |
134
|
|
|
|
135
|
|
|
$expected = [ |
136
|
|
|
'errors' => [ |
137
|
|
|
[ |
138
|
|
|
'message' => 'Error without wrapped exception', |
139
|
|
|
], |
140
|
|
|
], |
141
|
|
|
]; |
142
|
|
|
|
143
|
|
|
$this->assertEquals($expected, $executionResult->toArray()); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function testConvertExceptionToUserWarning() |
147
|
|
|
{ |
148
|
|
|
$errorHandler = new ErrorHandler(null, null, [\InvalidArgumentException::class => UserWarning::class]); |
149
|
|
|
|
150
|
|
|
$executionResult = new ExecutionResult( |
151
|
|
|
null, |
152
|
|
|
[ |
153
|
|
|
new GraphQLError('Error with invalid argument exception', null, null, null, null, new \InvalidArgumentException('Invalid argument exception')), |
|
|
|
|
154
|
|
|
] |
155
|
|
|
); |
156
|
|
|
|
157
|
|
|
$errorHandler->handleErrors($executionResult, true); |
158
|
|
|
|
159
|
|
|
$expected = [ |
160
|
|
|
'extensions' => [ |
161
|
|
|
'warnings' => [ |
162
|
|
|
[ |
163
|
|
|
'message' => 'Error with invalid argument exception', |
164
|
|
|
], |
165
|
|
|
], |
166
|
|
|
], |
167
|
|
|
]; |
168
|
|
|
|
169
|
|
|
$this->assertEquals($expected, $executionResult->toArray()); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @param array $exceptionMap |
174
|
|
|
* @param bool $mapExceptionsToParent |
175
|
|
|
* @param array|string $expectedUserError |
176
|
|
|
* |
177
|
|
|
* @dataProvider parentExceptionMappingDataProvider |
178
|
|
|
*/ |
179
|
|
|
public function testConvertExceptionUsingParentExceptionMatchesAlwaysFirstExactExceptionOtherwiseMatchesParent(array $exceptionMap, $mapExceptionsToParent, $expectedUserError) |
180
|
|
|
{ |
181
|
|
|
$errorHandler = new ErrorHandler(null, null, $exceptionMap, $mapExceptionsToParent); |
182
|
|
|
|
183
|
|
|
$executionResult = new ExecutionResult( |
184
|
|
|
null, |
185
|
|
|
[ |
186
|
|
|
new GraphQLError( |
187
|
|
|
'Error with invalid argument exception', |
188
|
|
|
null, |
189
|
|
|
null, |
190
|
|
|
null, |
191
|
|
|
null, |
192
|
|
|
new ChildOfInvalidArgumentException('Invalid argument exception') |
|
|
|
|
193
|
|
|
), |
194
|
|
|
] |
195
|
|
|
); |
196
|
|
|
|
197
|
|
|
if (is_string($expectedUserError)) { |
198
|
|
|
self::expectException($expectedUserError); |
199
|
|
|
} |
200
|
|
|
$errorHandler->handleErrors($executionResult, true); |
201
|
|
|
|
202
|
|
|
if (is_array($expectedUserError)) { |
203
|
|
|
$this->assertEquals($expectedUserError, $executionResult->toArray()); |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @return array |
209
|
|
|
*/ |
210
|
|
|
public function parentExceptionMappingDataProvider() |
211
|
|
|
{ |
212
|
|
|
return [ |
213
|
|
|
'without $mapExceptionsToParent and only the exact class, maps to exact class' => [ |
214
|
|
|
[ |
215
|
|
|
ChildOfInvalidArgumentException::class => UserError::class, |
216
|
|
|
], |
217
|
|
|
false, |
218
|
|
|
[ |
219
|
|
|
'errors' => [ |
220
|
|
|
[ |
221
|
|
|
'message' => 'Error with invalid argument exception', |
222
|
|
|
], |
223
|
|
|
], |
224
|
|
|
], |
225
|
|
|
], |
226
|
|
|
'without $mapExceptionsToParent and only the parent class, does not map to parent' => [ |
227
|
|
|
[ |
228
|
|
|
\InvalidArgumentException::class => UserWarning::class, |
229
|
|
|
], |
230
|
|
|
false, |
231
|
|
|
ChildOfInvalidArgumentException::class, |
232
|
|
|
], |
233
|
|
|
'with $mapExceptionsToParent and no classes' => [ |
234
|
|
|
[], |
235
|
|
|
true, |
236
|
|
|
ChildOfInvalidArgumentException::class, |
237
|
|
|
], |
238
|
|
|
'with $mapExceptionsToParent and only the exact class' => [ |
239
|
|
|
[ |
240
|
|
|
ChildOfInvalidArgumentException::class => UserError::class, |
241
|
|
|
], |
242
|
|
|
true, |
243
|
|
|
[ |
244
|
|
|
'errors' => [ |
245
|
|
|
[ |
246
|
|
|
'message' => 'Error with invalid argument exception', |
247
|
|
|
], |
248
|
|
|
], |
249
|
|
|
], |
250
|
|
|
], |
251
|
|
|
'with $mapExceptionsToParent and only the parent class' => [ |
252
|
|
|
[ |
253
|
|
|
\InvalidArgumentException::class => UserWarning::class, |
254
|
|
|
], |
255
|
|
|
true, |
256
|
|
|
[ |
257
|
|
|
'extensions' => [ |
258
|
|
|
'warnings' => [ |
259
|
|
|
[ |
260
|
|
|
'message' => 'Error with invalid argument exception', |
261
|
|
|
], |
262
|
|
|
], |
263
|
|
|
], |
264
|
|
|
], |
265
|
|
|
], |
266
|
|
|
'with $mapExceptionsToParent and the exact class first matches exact class' => [ |
267
|
|
|
[ |
268
|
|
|
ChildOfInvalidArgumentException::class => UserError::class, |
269
|
|
|
\InvalidArgumentException::class => UserWarning::class, |
270
|
|
|
], |
271
|
|
|
true, |
272
|
|
|
[ |
273
|
|
|
'errors' => [ |
274
|
|
|
[ |
275
|
|
|
'message' => 'Error with invalid argument exception', |
276
|
|
|
], |
277
|
|
|
], |
278
|
|
|
], |
279
|
|
|
], |
280
|
|
|
'with $mapExceptionsToParent and the exact class first but parent maps to error' => [ |
281
|
|
|
[ |
282
|
|
|
ChildOfInvalidArgumentException::class => UserWarning::class, |
283
|
|
|
\InvalidArgumentException::class => UserError::class, |
284
|
|
|
], |
285
|
|
|
true, |
286
|
|
|
[ |
287
|
|
|
'extensions' => [ |
288
|
|
|
'warnings' => [ |
289
|
|
|
[ |
290
|
|
|
'message' => 'Error with invalid argument exception', |
291
|
|
|
], |
292
|
|
|
], |
293
|
|
|
], |
294
|
|
|
], |
295
|
|
|
], |
296
|
|
|
'with $mapExceptionsToParent and the parent class first still matches exact class' => [ |
297
|
|
|
[ |
298
|
|
|
\InvalidArgumentException::class => UserWarning::class, |
299
|
|
|
ChildOfInvalidArgumentException::class => UserError::class, |
300
|
|
|
], |
301
|
|
|
true, |
302
|
|
|
[ |
303
|
|
|
'errors' => [ |
304
|
|
|
[ |
305
|
|
|
'message' => 'Error with invalid argument exception', |
306
|
|
|
], |
307
|
|
|
], |
308
|
|
|
], |
309
|
|
|
], |
310
|
|
|
]; |
311
|
|
|
} |
312
|
|
|
} |
313
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: