Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Completed
Pull Request — master (#236)
by Herberto
08:10
created

testMaskErrorWithWrappedUserErrorAndThrowExceptionSetToTrue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 0
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')),
0 ignored issues
show
Documentation introduced by
new \Exception('My Exception message') is of type object<Exception>, but the function expects a object<Throwable>|null.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
31
                new GraphQLError('Error with wrapped user error', null, null, null, null, new UserError('My User Error')),
0 ignored issues
show
Documentation introduced by
new \Overblog\GraphQLBun...rError('My User Error') is of type object<Overblog\GraphQLBundle\Error\UserError>, but the function expects a object<Throwable>|null.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
32
                new GraphQLError('', null, null, null, null, new UserErrors(['My User Error 1', 'My User Error 2', new UserError('My User Error 3')])),
0 ignored issues
show
Documentation introduced by
new \Overblog\GraphQLBun...or('My User Error 3'))) is of type object<Overblog\GraphQLBundle\Error\UserErrors>, but the function expects a object<Throwable>|null.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
33
                new GraphQLError('Error with wrapped user warning', null, null, null, null, new UserWarning('My User Warning')),
0 ignored issues
show
Documentation introduced by
new \Overblog\GraphQLBun...ning('My User Warning') is of type object<Overblog\GraphQLBundle\Error\UserWarning>, but the function expects a object<Throwable>|null.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
34
                new GraphQLError('Invalid value!', null, null, null, null, new GraphQLUserError('Invalid value!')),
0 ignored issues
show
Documentation introduced by
new \GraphQL\Error\UserError('Invalid value!') is of type object<GraphQL\Error\UserError>, but the function expects a object<Throwable>|null.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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')),
0 ignored issues
show
Documentation introduced by
new \Exception('My Exception message') is of type object<Exception>, but the function expects a object<Throwable>|null.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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')),
0 ignored issues
show
Documentation introduced by
new \Overblog\GraphQLBun...rError('My User Error') is of type object<Overblog\GraphQLBundle\Error\UserError>, but the function expects a object<Throwable>|null.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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')),
0 ignored issues
show
Documentation introduced by
new \InvalidArgumentExce...id argument exception') is of type object<InvalidArgumentException>, but the function expects a object<Throwable>|null.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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 string $expectedUserError
165
     *
166
     * @dataProvider parentExceptionMappingDataProvider
167
     */
168
    public function testConvertExceptionUsingParentExceptionMatchesAlwaysFirstExactExceptionOtherwiseMatchesParent(array $exceptionMap, $mapToParentExceptions, $expectedUserError)
169
    {
170
        $errorHandler = new ErrorHandler(null, null, $exceptionMap, $mapToParentExceptions);
171
172
        $executionResult = new ExecutionResult(
173
            null,
174
            [
175
                new GraphQLError(
176
                    'Error with invalid argument exception',
177
                    null,
178
                    null,
179
                    null,
180
                    null,
181
                    new ChildOfInvalidArgumentException('Invalid argument exception')
0 ignored issues
show
Documentation introduced by
new \Overblog\GraphQLBun...id argument exception') is of type object<Overblog\GraphQLB...validArgumentException>, but the function expects a object<Throwable>|null.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
182
                ),
183
            ]
184
        );
185
186
        $errorHandler->handleErrors($executionResult, true);
187
188
        $this->assertEquals($expectedUserError, $executionResult->toArray());
189
    }
190
191
    /**
192
     * @return array
193
     */
194
    public function parentExceptionMappingDataProvider()
195
    {
196
        return [
197
            'without $mapToParentExceptions and only the exact class, maps to exact class' => [
198
                [
199
                    ChildOfInvalidArgumentException::class => UserError::class
200
                ],
201
                true,
202
                [
203
                    'errors' => [
204
                        [
205
                            'message' => 'Error with invalid argument exception',
206
                        ],
207
                    ],
208
                ],
209
            ],
210
            'without $mapToParentExceptions and only the parent class, does not map to parent' => [
211
                [
212
                    \InvalidArgumentException::class => UserWarning::class
213
                ],
214
                true,
215
                [
216
                    'extensions' => [
217
                        'warnings' => [
218
                            [
219
                                'message' => 'Error with invalid argument exception',
220
                            ],
221
                        ],
222
                    ],
223
                ],
224
            ],
225
            'with $mapToParentExceptions and only the exact class' => [
226
                [
227
                    ChildOfInvalidArgumentException::class => UserError::class
228
                ],
229
                true,
230
                [
231
                    'errors' => [
232
                        [
233
                            'message' => 'Error with invalid argument exception',
234
                        ],
235
                    ],
236
                ],
237
            ],
238
            'with $mapToParentExceptions and only the parent class' => [
239
                [
240
                    \InvalidArgumentException::class => UserWarning::class
241
                ],
242
                true,
243
                [
244
                    'extensions' => [
245
                        'warnings' => [
246
                            [
247
                                'message' => 'Error with invalid argument exception',
248
                            ],
249
                        ],
250
                    ],
251
                ],
252
            ],
253
            'with $mapToParentExceptions and the exact class first matches exact class' => [
254
                [
255
                    ChildOfInvalidArgumentException::class => UserError::class,
256
                    \InvalidArgumentException::class => UserWarning::class,
257
                ],
258
                true,
259
                [
260
                    'errors' => [
261
                        [
262
                            'message' => 'Error with invalid argument exception',
263
                        ],
264
                    ],
265
                ],
266
            ],
267
            'with $mapToParentExceptions and the exact class first but parent maps to error' => [
268
                [
269
                    ChildOfInvalidArgumentException::class => UserWarning::class,
270
                    \InvalidArgumentException::class => UserError::class,
271
                ],
272
                true,
273
                [
274
                    'extensions' => [
275
                        'warnings' => [
276
                            [
277
                                'message' => 'Error with invalid argument exception',
278
                            ],
279
                        ],
280
                    ],
281
                ],
282
            ],
283
            'with $mapToParentExceptions and the parent class first still matches exact class' => [
284
                [
285
                    \InvalidArgumentException::class => UserWarning::class,
286
                    ChildOfInvalidArgumentException::class => UserError::class,
287
                ],
288
                true,
289
                [
290
                    'errors' => [
291
                        [
292
                            'message' => 'Error with invalid argument exception',
293
                        ],
294
                    ],
295
                ],
296
            ],
297
        ];
298
    }
299
}
300