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

Passed
Pull Request — master (#277)
by Jérémiah
14:57
created

ErrorHandlerTest::parentExceptionMappingDataProvider()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 108
Code Lines 50

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 108
rs 8.2857
cc 1
eloc 50
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
use Symfony\Component\EventDispatcher\EventDispatcher;
14
15
class ErrorHandlerTest extends TestCase
16
{
17
    /** @var ErrorHandler */
18
    private $errorHandler;
19
20
    /** @var EventDispatcher|\PHPUnit_Framework_MockObject_MockObject */
21
    private $dispatcher;
22
23
    public function setUp()
24
    {
25
        $this->dispatcher = $this->getMockBuilder(EventDispatcher::class)->setMethods(['dispatch'])->getMock();
26
        $this->dispatcher->expects($this->any())->method('dispatch')->willReturnArgument(1);
27
        $this->errorHandler = new ErrorHandler($this->dispatcher);
28
    }
29
30
    public function testMaskErrorWithThrowExceptionSetToFalse()
31
    {
32
        $executionResult = new ExecutionResult(
33
            null,
34
            [
35
                new GraphQLError('Error without wrapped exception'),
36
                new GraphQLError('Error with wrapped exception', null, null, null, null, new \Exception('My Exception message')),
37
                new GraphQLError('Error with wrapped user error', null, null, null, null, new UserError('My User Error')),
38
                new GraphQLError('Error with wrapped base user error', null, null, null, null, new GraphQLUserError('My bases User Error')),
39
                new GraphQLError('', null, null, null, null, new UserErrors(['My User Error 1', 'My User Error 2', new UserError('My User Error 3')])),
40
                new GraphQLError('Error with wrapped user warning', null, null, null, null, new UserWarning('My User Warning')),
41
            ]
42
        );
43
44
        $this->errorHandler->handleErrors($executionResult);
45
46
        $expected = [
47
            'errors' => [
48
                [
49
                    'message' => 'Error without wrapped exception',
50
                    'category' => 'graphql',
51
                ],
52
                [
53
                    'message' => ErrorHandler::DEFAULT_ERROR_MESSAGE,
54
                    'category' => 'internal',
55
                ],
56
                [
57
                    'message' => 'Error with wrapped user error',
58
                    'category' => 'user',
59
                ],
60
                [
61
                    'message' => 'Error with wrapped base user error',
62
                    'category' => 'user',
63
                ],
64
                [
65
                    'message' => 'My User Error 1',
66
                    'category' => 'user',
67
                ],
68
                [
69
                    'message' => 'My User Error 2',
70
                    'category' => 'user',
71
                ],
72
                [
73
                    'message' => 'My User Error 3',
74
                    'category' => 'user',
75
                ],
76
            ],
77
            'extensions' => [
78
                'warnings' => [
79
                    [
80
                        'message' => 'Error with wrapped user warning',
81
                        'category' => 'user',
82
                    ],
83
                ],
84
            ],
85
        ];
86
87
        $this->assertEquals($expected, $executionResult->toArray());
88
    }
89
90
    public function testMaskErrorWithWrappedExceptionAndThrowExceptionSetToTrue()
91
    {
92
        $this->expectException(\Exception::class);
93
        $this->expectExceptionMessage('My Exception message');
94
95
        $executionResult = new ExecutionResult(
96
            null,
97
            [
98
                new GraphQLError('Error with wrapped exception', null, null, null, null, new \Exception('My Exception message')),
99
            ]
100
        );
101
102
        $this->errorHandler->handleErrors($executionResult, true);
103
    }
104
105
    public function testInvalidUserErrorsItem()
106
    {
107
        $this->expectException(\InvalidArgumentException::class);
108
        $this->expectExceptionMessage(sprintf('Error must be string or instance of %s', GraphQLUserError::class));
109
110
        new UserErrors([
111
            'Some Error',
112
            false,
113
        ]);
114
    }
115
116
    public function testMaskErrorWithWrappedUserErrorAndThrowExceptionSetToTrue()
117
    {
118
        $executionResult = new ExecutionResult(
119
            null,
120
            [
121
                new GraphQLError('Error with wrapped user error', null, null, null, null, new UserError('My User Error')),
122
            ]
123
        );
124
125
        $this->errorHandler->handleErrors($executionResult, true);
126
127
        $expected = [
128
            'errors' => [
129
                [
130
                    'message' => 'Error with wrapped user error',
131
                    'category' => 'user',
132
                ],
133
            ],
134
        ];
135
136
        $this->assertEquals($expected, $executionResult->toArray());
137
    }
138
139
    public function testDebugEnabled()
140
    {
141
        try {
142
            throw new \Exception();
143
        } catch (\Exception $exception) {
144
            $executionResult = new ExecutionResult(
145
                null,
146
                [
147
                    new GraphQLError('Error wrapped exception', null, null, null, null, $exception),
148
                ]
149
            );
150
151
            $this->errorHandler->handleErrors($executionResult, false, true);
152
153
            $errors = $executionResult->toArray()['errors'];
154
            $this->assertCount(1, $errors);
155
            $this->assertArrayHasKey('debugMessage', $errors[0]);
156
            $this->assertEquals('Error wrapped exception', $errors[0]['debugMessage']);
157
            $this->assertEquals(ErrorHandler::DEFAULT_ERROR_MESSAGE, $errors[0]['message']);
158
            $this->assertArrayHasKey('trace', $errors[0]);
159
        }
160
    }
161
162
    public function testMaskErrorWithoutWrappedExceptionAndThrowExceptionSetToTrue()
163
    {
164
        $executionResult = new ExecutionResult(
165
            null,
166
            [
167
                new GraphQLError('Error without wrapped exception'),
168
            ]
169
        );
170
171
        $this->errorHandler->handleErrors($executionResult, true);
172
173
        $expected = [
174
            'errors' => [
175
                [
176
                    'message' => 'Error without wrapped exception',
177
                    'category' => 'graphql',
178
                ],
179
            ],
180
        ];
181
182
        $this->assertEquals($expected, $executionResult->toArray());
183
    }
184
185
    public function testConvertExceptionToUserWarning()
186
    {
187
        $errorHandler = new ErrorHandler($this->dispatcher, null, [\InvalidArgumentException::class => UserWarning::class]);
188
189
        $executionResult = new ExecutionResult(
190
            null,
191
            [
192
                new GraphQLError('Error with invalid argument exception', null, null, null, null, new \InvalidArgumentException('Invalid argument exception')),
193
            ]
194
        );
195
196
        $errorHandler->handleErrors($executionResult, true);
197
198
        $expected = [
199
            'extensions' => [
200
                'warnings' => [
201
                    [
202
                        'message' => 'Error with invalid argument exception',
203
                        'category' => 'user',
204
                    ],
205
                ],
206
            ],
207
        ];
208
209
        $this->assertEquals($expected, $executionResult->toArray());
210
    }
211
212
    /**
213
     * @param array        $exceptionMap
214
     * @param bool         $mapExceptionsToParent
215
     * @param array|string $expectedUserError
216
     *
217
     * @dataProvider parentExceptionMappingDataProvider
218
     */
219
    public function testConvertExceptionUsingParentExceptionMatchesAlwaysFirstExactExceptionOtherwiseMatchesParent(array $exceptionMap, $mapExceptionsToParent, $expectedUserError)
220
    {
221
        $errorHandler = new ErrorHandler($this->dispatcher, null, $exceptionMap, $mapExceptionsToParent);
222
223
        $executionResult = new ExecutionResult(
224
            null,
225
            [
226
                new GraphQLError(
227
                    'Error with invalid argument exception',
228
                    null,
229
                    null,
230
                    null,
231
                    null,
232
                    new ChildOfInvalidArgumentException('Invalid argument exception')
233
                ),
234
            ]
235
        );
236
237
        if (is_string($expectedUserError)) {
238
            self::expectException($expectedUserError);
239
        }
240
        $errorHandler->handleErrors($executionResult, true);
241
242
        if (is_array($expectedUserError)) {
243
            $this->assertEquals($expectedUserError, $executionResult->toArray());
244
        }
245
    }
246
247
    /**
248
     * @return array
249
     */
250
    public function parentExceptionMappingDataProvider()
251
    {
252
        return [
253
            'without $mapExceptionsToParent and only the exact class, maps to exact class' => [
254
                [
255
                    ChildOfInvalidArgumentException::class => UserError::class,
256
                ],
257
                false,
258
                [
259
                    'errors' => [
260
                        [
261
                            'message' => 'Error with invalid argument exception',
262
                            'category' => 'user',
263
                        ],
264
                    ],
265
                ],
266
            ],
267
            'without $mapExceptionsToParent and only the parent class, does not map to parent' => [
268
                [
269
                    \InvalidArgumentException::class => UserWarning::class,
270
                ],
271
                false,
272
                ChildOfInvalidArgumentException::class,
273
            ],
274
            'with $mapExceptionsToParent and no classes' => [
275
                [],
276
                true,
277
                ChildOfInvalidArgumentException::class,
278
            ],
279
            'with $mapExceptionsToParent and only the exact class' => [
280
                [
281
                    ChildOfInvalidArgumentException::class => UserError::class,
282
                ],
283
                true,
284
                [
285
                    'errors' => [
286
                        [
287
                            'message' => 'Error with invalid argument exception',
288
                            'category' => 'user',
289
                        ],
290
                    ],
291
                ],
292
            ],
293
            'with $mapExceptionsToParent and only the parent class' => [
294
                [
295
                    \InvalidArgumentException::class => UserWarning::class,
296
                ],
297
                true,
298
                [
299
                    'extensions' => [
300
                        'warnings' => [
301
                            [
302
                                'message' => 'Error with invalid argument exception',
303
                                'category' => 'user',
304
                            ],
305
                        ],
306
                    ],
307
                ],
308
            ],
309
            'with $mapExceptionsToParent and the exact class first matches exact class' => [
310
                [
311
                    ChildOfInvalidArgumentException::class => UserError::class,
312
                    \InvalidArgumentException::class => UserWarning::class,
313
                ],
314
                true,
315
                [
316
                    'errors' => [
317
                        [
318
                            'message' => 'Error with invalid argument exception',
319
                            'category' => 'user',
320
                        ],
321
                    ],
322
                ],
323
            ],
324
            'with $mapExceptionsToParent and the exact class first but parent maps to error' => [
325
                [
326
                    ChildOfInvalidArgumentException::class => UserWarning::class,
327
                    \InvalidArgumentException::class => UserError::class,
328
                ],
329
                true,
330
                [
331
                    'extensions' => [
332
                        'warnings' => [
333
                            [
334
                                'message' => 'Error with invalid argument exception',
335
                                'category' => 'user',
336
                            ],
337
                        ],
338
                    ],
339
                ],
340
            ],
341
            'with $mapExceptionsToParent and the parent class first still matches exact class' => [
342
                [
343
                    \InvalidArgumentException::class => UserWarning::class,
344
                    ChildOfInvalidArgumentException::class => UserError::class,
345
                ],
346
                true,
347
                [
348
                    'errors' => [
349
                        [
350
                            'message' => 'Error with invalid argument exception',
351
                            'category' => 'user',
352
                        ],
353
                    ],
354
                ],
355
            ],
356
        ];
357
    }
358
}
359