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
|
|
|
/** |
77
|
|
|
* @expectedException \Exception |
78
|
|
|
* @expectedExceptionMessage My Exception message |
79
|
|
|
*/ |
80
|
|
|
public function testMaskErrorWithWrappedExceptionAndThrowExceptionSetToTrue() |
81
|
|
|
{ |
82
|
|
|
$executionResult = new ExecutionResult( |
83
|
|
|
null, |
84
|
|
|
[ |
85
|
|
|
new GraphQLError('Error with wrapped exception', null, null, null, null, new \Exception('My Exception message')), |
|
|
|
|
86
|
|
|
] |
87
|
|
|
); |
88
|
|
|
|
89
|
|
|
$this->errorHandler->handleErrors($executionResult, true); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function testMaskErrorWithWrappedUserErrorAndThrowExceptionSetToTrue() |
93
|
|
|
{ |
94
|
|
|
$executionResult = new ExecutionResult( |
95
|
|
|
null, |
96
|
|
|
[ |
97
|
|
|
new GraphQLError('Error with wrapped user error', null, null, null, null, new UserError('My User Error')), |
|
|
|
|
98
|
|
|
] |
99
|
|
|
); |
100
|
|
|
|
101
|
|
|
$this->errorHandler->handleErrors($executionResult, true); |
102
|
|
|
|
103
|
|
|
$expected = [ |
104
|
|
|
'errors' => [ |
105
|
|
|
[ |
106
|
|
|
'message' => 'Error with wrapped user error', |
107
|
|
|
], |
108
|
|
|
], |
109
|
|
|
]; |
110
|
|
|
|
111
|
|
|
$this->assertEquals($expected, $executionResult->toArray()); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function testMaskErrorWithoutWrappedExceptionAndThrowExceptionSetToTrue() |
115
|
|
|
{ |
116
|
|
|
$executionResult = new ExecutionResult( |
117
|
|
|
null, |
118
|
|
|
[ |
119
|
|
|
new GraphQLError('Error without wrapped exception'), |
120
|
|
|
] |
121
|
|
|
); |
122
|
|
|
|
123
|
|
|
$this->errorHandler->handleErrors($executionResult, true); |
124
|
|
|
|
125
|
|
|
$expected = [ |
126
|
|
|
'errors' => [ |
127
|
|
|
[ |
128
|
|
|
'message' => 'Error without wrapped exception', |
129
|
|
|
], |
130
|
|
|
], |
131
|
|
|
]; |
132
|
|
|
|
133
|
|
|
$this->assertEquals($expected, $executionResult->toArray()); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function testConvertExceptionToUserWarning() |
137
|
|
|
{ |
138
|
|
|
$errorHandler = new ErrorHandler(null, null, [\InvalidArgumentException::class => UserWarning::class]); |
139
|
|
|
|
140
|
|
|
$executionResult = new ExecutionResult( |
141
|
|
|
null, |
142
|
|
|
[ |
143
|
|
|
new GraphQLError('Error with invalid argument exception', null, null, null, null, new \InvalidArgumentException('Invalid argument exception')), |
|
|
|
|
144
|
|
|
] |
145
|
|
|
); |
146
|
|
|
|
147
|
|
|
$errorHandler->handleErrors($executionResult, true); |
148
|
|
|
|
149
|
|
|
$expected = [ |
150
|
|
|
'extensions' => [ |
151
|
|
|
'warnings' => [ |
152
|
|
|
[ |
153
|
|
|
'message' => 'Error with invalid argument exception', |
154
|
|
|
], |
155
|
|
|
], |
156
|
|
|
], |
157
|
|
|
]; |
158
|
|
|
|
159
|
|
|
$this->assertEquals($expected, $executionResult->toArray()); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @param array $exceptionMap |
164
|
|
|
* @param bool $mapExceptionsToParent |
165
|
|
|
* @param array|string $expectedUserError |
166
|
|
|
* |
167
|
|
|
* @dataProvider parentExceptionMappingDataProvider |
168
|
|
|
*/ |
169
|
|
|
public function testConvertExceptionUsingParentExceptionMatchesAlwaysFirstExactExceptionOtherwiseMatchesParent(array $exceptionMap, $mapExceptionsToParent, $expectedUserError) |
170
|
|
|
{ |
171
|
|
|
$errorHandler = new ErrorHandler(null, null, $exceptionMap, $mapExceptionsToParent); |
172
|
|
|
|
173
|
|
|
$executionResult = new ExecutionResult( |
174
|
|
|
null, |
175
|
|
|
[ |
176
|
|
|
new GraphQLError( |
177
|
|
|
'Error with invalid argument exception', |
178
|
|
|
null, |
179
|
|
|
null, |
180
|
|
|
null, |
181
|
|
|
null, |
182
|
|
|
new ChildOfInvalidArgumentException('Invalid argument exception') |
|
|
|
|
183
|
|
|
), |
184
|
|
|
] |
185
|
|
|
); |
186
|
|
|
|
187
|
|
|
if (is_string($expectedUserError)) { |
188
|
|
|
self::expectException($expectedUserError); |
189
|
|
|
} |
190
|
|
|
$errorHandler->handleErrors($executionResult, true); |
191
|
|
|
|
192
|
|
|
if (is_array($expectedUserError)) { |
193
|
|
|
$this->assertEquals($expectedUserError, $executionResult->toArray()); |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @return array |
199
|
|
|
*/ |
200
|
|
|
public function parentExceptionMappingDataProvider() |
201
|
|
|
{ |
202
|
|
|
return [ |
203
|
|
|
'without $mapExceptionsToParent and only the exact class, maps to exact class' => [ |
204
|
|
|
[ |
205
|
|
|
ChildOfInvalidArgumentException::class => UserError::class, |
206
|
|
|
], |
207
|
|
|
false, |
208
|
|
|
[ |
209
|
|
|
'errors' => [ |
210
|
|
|
[ |
211
|
|
|
'message' => 'Error with invalid argument exception', |
212
|
|
|
], |
213
|
|
|
], |
214
|
|
|
], |
215
|
|
|
], |
216
|
|
|
'without $mapExceptionsToParent and only the parent class, does not map to parent' => [ |
217
|
|
|
[ |
218
|
|
|
\InvalidArgumentException::class => UserWarning::class, |
219
|
|
|
], |
220
|
|
|
false, |
221
|
|
|
ChildOfInvalidArgumentException::class, |
222
|
|
|
], |
223
|
|
|
'with $mapExceptionsToParent and only the exact class' => [ |
224
|
|
|
[ |
225
|
|
|
ChildOfInvalidArgumentException::class => UserError::class, |
226
|
|
|
], |
227
|
|
|
true, |
228
|
|
|
[ |
229
|
|
|
'errors' => [ |
230
|
|
|
[ |
231
|
|
|
'message' => 'Error with invalid argument exception', |
232
|
|
|
], |
233
|
|
|
], |
234
|
|
|
], |
235
|
|
|
], |
236
|
|
|
'with $mapExceptionsToParent and only the parent class' => [ |
237
|
|
|
[ |
238
|
|
|
\InvalidArgumentException::class => UserWarning::class, |
239
|
|
|
], |
240
|
|
|
true, |
241
|
|
|
[ |
242
|
|
|
'extensions' => [ |
243
|
|
|
'warnings' => [ |
244
|
|
|
[ |
245
|
|
|
'message' => 'Error with invalid argument exception', |
246
|
|
|
], |
247
|
|
|
], |
248
|
|
|
], |
249
|
|
|
], |
250
|
|
|
], |
251
|
|
|
'with $mapExceptionsToParent and the exact class first matches exact class' => [ |
252
|
|
|
[ |
253
|
|
|
ChildOfInvalidArgumentException::class => UserError::class, |
254
|
|
|
\InvalidArgumentException::class => UserWarning::class, |
255
|
|
|
], |
256
|
|
|
true, |
257
|
|
|
[ |
258
|
|
|
'errors' => [ |
259
|
|
|
[ |
260
|
|
|
'message' => 'Error with invalid argument exception', |
261
|
|
|
], |
262
|
|
|
], |
263
|
|
|
], |
264
|
|
|
], |
265
|
|
|
'with $mapExceptionsToParent and the exact class first but parent maps to error' => [ |
266
|
|
|
[ |
267
|
|
|
ChildOfInvalidArgumentException::class => UserWarning::class, |
268
|
|
|
\InvalidArgumentException::class => UserError::class, |
269
|
|
|
], |
270
|
|
|
true, |
271
|
|
|
[ |
272
|
|
|
'extensions' => [ |
273
|
|
|
'warnings' => [ |
274
|
|
|
[ |
275
|
|
|
'message' => 'Error with invalid argument exception', |
276
|
|
|
], |
277
|
|
|
], |
278
|
|
|
], |
279
|
|
|
], |
280
|
|
|
], |
281
|
|
|
'with $mapExceptionsToParent and the parent class first still matches exact class' => [ |
282
|
|
|
[ |
283
|
|
|
\InvalidArgumentException::class => UserWarning::class, |
284
|
|
|
ChildOfInvalidArgumentException::class => UserError::class, |
285
|
|
|
], |
286
|
|
|
true, |
287
|
|
|
[ |
288
|
|
|
'errors' => [ |
289
|
|
|
[ |
290
|
|
|
'message' => 'Error with invalid argument exception', |
291
|
|
|
], |
292
|
|
|
], |
293
|
|
|
], |
294
|
|
|
], |
295
|
|
|
]; |
296
|
|
|
} |
297
|
|
|
} |
298
|
|
|
|
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: