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\Error; |
13
|
|
|
|
14
|
|
|
use GraphQL\Error; |
15
|
|
|
use GraphQL\Executor\ExecutionResult; |
16
|
|
|
use Overblog\GraphQLBundle\Error\ErrorHandler; |
17
|
|
|
use Overblog\GraphQLBundle\Error\UserError; |
18
|
|
|
use Overblog\GraphQLBundle\Error\UserErrors; |
19
|
|
|
use Overblog\GraphQLBundle\Error\UserWarning; |
20
|
|
|
|
21
|
|
|
class ErrorHandlerTest extends \PHPUnit_Framework_TestCase |
22
|
|
|
{ |
23
|
|
|
/** @var ErrorHandler */ |
24
|
|
|
private $errorHandler; |
25
|
|
|
|
26
|
|
|
public function setUp() |
27
|
|
|
{ |
28
|
|
|
$this->errorHandler = new ErrorHandler(); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function testMaskErrorWithThrowExceptionSetToFalse() |
32
|
|
|
{ |
33
|
|
|
$executionResult = new ExecutionResult( |
34
|
|
|
null, |
35
|
|
|
[ |
36
|
|
|
new Error('Error without wrapped exception'), |
37
|
|
|
new Error('Error with wrapped exception', null, new \Exception('My Exception message')), |
38
|
|
|
new Error('Error with wrapped user error', null, new UserError('My User Error')), |
39
|
|
|
new Error('', null, new UserErrors(['My User Error 1', 'My User Error 2', new UserError('My User Error 3')])), |
40
|
|
|
new Error('Error with wrapped user warning', null, new UserWarning('My User Warning')), |
41
|
|
|
] |
42
|
|
|
); |
43
|
|
|
|
44
|
|
|
$this->errorHandler->handleErrors($executionResult); |
45
|
|
|
|
46
|
|
|
$expected = [ |
47
|
|
|
'data' => null, |
48
|
|
|
'errors' => [ |
49
|
|
|
[ |
50
|
|
|
'message' => 'Error without wrapped exception', |
51
|
|
|
], |
52
|
|
|
[ |
53
|
|
|
'message' => ErrorHandler::DEFAULT_ERROR_MESSAGE, |
54
|
|
|
], |
55
|
|
|
[ |
56
|
|
|
'message' => 'Error with wrapped user error', |
57
|
|
|
], |
58
|
|
|
[ |
59
|
|
|
'message' => 'My User Error 1', |
60
|
|
|
], |
61
|
|
|
[ |
62
|
|
|
'message' => 'My User Error 2', |
63
|
|
|
], |
64
|
|
|
[ |
65
|
|
|
'message' => 'My User Error 3', |
66
|
|
|
], |
67
|
|
|
], |
68
|
|
|
'extensions' => [ |
69
|
|
|
'warnings' => [ |
70
|
|
|
[ |
71
|
|
|
'message' => 'Error with wrapped user warning', |
72
|
|
|
], |
73
|
|
|
], |
74
|
|
|
], |
75
|
|
|
]; |
76
|
|
|
|
77
|
|
|
$this->assertEquals($expected, $executionResult->toArray()); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @expectedException \Exception |
82
|
|
|
* @expectedExceptionMessage My Exception message |
83
|
|
|
*/ |
84
|
|
|
public function testMaskErrorWithWrappedExceptionAndThrowExceptionSetToTrue() |
85
|
|
|
{ |
86
|
|
|
$executionResult = new ExecutionResult( |
87
|
|
|
null, |
88
|
|
|
[ |
89
|
|
|
new Error('Error with wrapped exception', null, new \Exception('My Exception message')), |
90
|
|
|
] |
91
|
|
|
); |
92
|
|
|
|
93
|
|
|
$this->errorHandler->handleErrors($executionResult, true); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
View Code Duplication |
public function testMaskErrorWithWrappedUserErrorAndThrowExceptionSetToTrue() |
|
|
|
|
97
|
|
|
{ |
98
|
|
|
$executionResult = new ExecutionResult( |
99
|
|
|
null, |
100
|
|
|
[ |
101
|
|
|
new Error('Error with wrapped user error', null, new UserError('My User Error')), |
102
|
|
|
] |
103
|
|
|
); |
104
|
|
|
|
105
|
|
|
$this->errorHandler->handleErrors($executionResult, true); |
106
|
|
|
|
107
|
|
|
$expected = [ |
108
|
|
|
'data' => null, |
109
|
|
|
'errors' => [ |
110
|
|
|
[ |
111
|
|
|
'message' => 'Error with wrapped user error', |
112
|
|
|
], |
113
|
|
|
], |
114
|
|
|
]; |
115
|
|
|
|
116
|
|
|
$this->assertEquals($expected, $executionResult->toArray()); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
View Code Duplication |
public function testMaskErrorWithoutWrappedExceptionAndThrowExceptionSetToTrue() |
|
|
|
|
120
|
|
|
{ |
121
|
|
|
$executionResult = new ExecutionResult( |
122
|
|
|
null, |
123
|
|
|
[ |
124
|
|
|
new Error('Error without wrapped exception'), |
125
|
|
|
] |
126
|
|
|
); |
127
|
|
|
|
128
|
|
|
$this->errorHandler->handleErrors($executionResult, true); |
129
|
|
|
|
130
|
|
|
$expected = [ |
131
|
|
|
'data' => null, |
132
|
|
|
'errors' => [ |
133
|
|
|
[ |
134
|
|
|
'message' => 'Error without wrapped exception', |
135
|
|
|
], |
136
|
|
|
], |
137
|
|
|
]; |
138
|
|
|
|
139
|
|
|
$this->assertEquals($expected, $executionResult->toArray()); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function testConvertExceptionToUserWarning() |
143
|
|
|
{ |
144
|
|
|
$errorHandler = new ErrorHandler(null, null, ["InvalidArgumentException" => 'Overblog\\GraphQLBundle\\Error\\UserWarning']); |
145
|
|
|
|
146
|
|
|
$executionResult = new ExecutionResult( |
147
|
|
|
null, |
148
|
|
|
[ |
149
|
|
|
new Error('Error with invalid argument exception', null, new \InvalidArgumentException('Invalid argument exception')), |
150
|
|
|
] |
151
|
|
|
); |
152
|
|
|
|
153
|
|
|
$errorHandler->handleErrors($executionResult, true); |
154
|
|
|
|
155
|
|
|
$expected = [ |
156
|
|
|
'data' => null, |
157
|
|
|
'extensions' => [ |
158
|
|
|
'warnings' => [ |
159
|
|
|
[ |
160
|
|
|
'message' => 'Error with invalid argument exception', |
161
|
|
|
], |
162
|
|
|
], |
163
|
|
|
], |
164
|
|
|
]; |
165
|
|
|
|
166
|
|
|
$this->assertEquals($expected, $executionResult->toArray()); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.