|
9 | 9 | use Overblog\GraphQLBundle\Error\UserWarning; |
10 | 10 | use Overblog\GraphQLBundle\Event\ErrorFormattingEvent; |
11 | 11 | use Overblog\GraphQLBundle\EventListener\ErrorLoggerListener; |
12 | | -use PHPUnit\Framework\Constraint\Constraint; |
13 | 12 | use PHPUnit\Framework\MockObject\Matcher\Invocation; |
14 | 13 | use PHPUnit\Framework\MockObject\MockObject; |
15 | 14 | use PHPUnit\Framework\TestCase; |
16 | 15 | use Psr\Log\LoggerInterface; |
| 16 | +use Psr\Log\LogLevel; |
17 | 17 |
|
18 | | -class ErrorLoggerListenerTest extends TestCase |
| 18 | +final class ErrorLoggerListenerTest extends TestCase |
19 | 19 | { |
20 | | - /** @var ErrorLoggerListener */ |
| 20 | + /** |
| 21 | + * @var ErrorLoggerListener |
| 22 | + */ |
21 | 23 | private $listener; |
22 | 24 |
|
23 | | - /** @var LoggerInterface|MockObject */ |
| 25 | + /** |
| 26 | + * @var LoggerInterface&MockObject |
| 27 | + */ |
24 | 28 | private $logger; |
25 | 29 |
|
26 | 30 | public function setUp(): void |
27 | 31 | { |
28 | | - $this->logger = $this->getMockBuilder(LoggerInterface::class)->getMock(); |
| 32 | + $this->logger = $this->createMock(LoggerInterface::class); |
29 | 33 | $this->listener = new ErrorLoggerListener($this->logger); |
30 | 34 | } |
31 | 35 |
|
32 | 36 | /** |
33 | | - * @param Error $error |
34 | | - * @param Invocation $loggerExpects |
35 | | - * @param Constraint|string $loggerMethod |
36 | | - * @param array|null $with |
37 | | - * |
38 | | - * @dataProvider fixtures |
| 37 | + * @dataProvider onErrorFormattingDataProvider |
39 | 38 | */ |
40 | | - public function testOnErrorFormatting(Error $error, $loggerExpects, $loggerMethod, array $with = null): void |
| 39 | + public function testOnErrorFormatting(Error $error, Invocation $expectedLoggerCalls, array $expectedLoggerMethodArguments): void |
41 | 40 | { |
42 | | - $mock = $this->logger->expects($loggerExpects)->method($loggerMethod); |
43 | | - if ($with) { |
44 | | - $mock->with(...$with); |
45 | | - } |
| 41 | + $this->logger->expects($expectedLoggerCalls) |
| 42 | + ->method('log') |
| 43 | + ->with(...$expectedLoggerMethodArguments); |
46 | 44 |
|
47 | 45 | $this->listener->onErrorFormatting(new ErrorFormattingEvent($error, [])); |
48 | 46 | } |
49 | 47 |
|
50 | | - public function fixtures() |
| 48 | + public function onErrorFormattingDataProvider(): \Generator |
51 | 49 | { |
52 | | - try { |
53 | | - throw new \Exception('Ko!'); |
54 | | - } catch (\Exception $exception) { |
55 | | - } |
56 | | - $with = [ |
57 | | - \sprintf('[GraphQL] %s: %s[%d] (caught throwable) at %s line %s.', \Exception::class, 'Ko!', 0, __FILE__, $exception->getLine()), |
58 | | - ['throwable' => $exception], |
| 50 | + $exception = new \Exception('Ko!'); |
| 51 | + |
| 52 | + yield [ |
| 53 | + new Error('Basic error'), |
| 54 | + $this->never(), |
| 55 | + [$this->anything()], |
59 | 56 | ]; |
60 | 57 |
|
61 | | - return [ |
62 | | - [self::createError('Basic error'), $this->never(), $this->anything()], |
63 | | - [self::createError('Wrapped Base UserError without previous', new \GraphQL\Error\UserError('User error message')), $this->never(), $this->anything()], |
64 | | - [self::createError('Wrapped UserError without previous', new UserError('User error message')), $this->never(), $this->anything()], |
65 | | - [self::createError('Wrapped UserWarning without previous', new UserWarning('User warning message')), $this->never(), $this->anything()], |
66 | | - [self::createError('Wrapped unknown exception', $exception), $this->once(), 'critical', $with], |
67 | | - [self::createError('Wrapped Base UserError with previous', new \GraphQL\Error\UserError('User error message', 0, $exception)), $this->once(), 'error', $with], |
68 | | - [self::createError('Wrapped UserError with previous', new UserError('User error message', 0, $exception)), $this->once(), 'error', $with], |
69 | | - [self::createError('Wrapped UserWarning with previous', new UserWarning('User warning message', 0, $exception)), $this->once(), 'warning', $with], |
| 58 | + yield [ |
| 59 | + new Error('Wrapped Base UserError without previous', null, null, null, null, new UserError('User error message')), |
| 60 | + $this->never(), |
| 61 | + [$this->anything()], |
70 | 62 | ]; |
71 | | - } |
72 | 63 |
|
73 | | - private static function createError($message, \Exception $exception = null) |
74 | | - { |
75 | | - return new Error($message, null, null, null, null, $exception); |
| 64 | + yield [ |
| 65 | + new Error('Wrapped UserError without previous', null, null, null, null, new UserError('User error message')), |
| 66 | + $this->never(), |
| 67 | + [$this->anything()], |
| 68 | + ]; |
| 69 | + |
| 70 | + yield [ |
| 71 | + new Error('Wrapped UserWarning without previous', null, null, null, null, new UserWarning('User warning message')), |
| 72 | + $this->never(), |
| 73 | + [$this->anything()], |
| 74 | + ]; |
| 75 | + |
| 76 | + yield [ |
| 77 | + new Error('Wrapped unknown exception', null, null, null, null, $exception), |
| 78 | + $this->once(), |
| 79 | + [ |
| 80 | + LogLevel::CRITICAL, |
| 81 | + \sprintf('[GraphQL] Exception: Ko![0] (caught throwable) at %s line %s.', __FILE__, $exception->getLine()), |
| 82 | + ['exception' => $exception], |
| 83 | + ], |
| 84 | + ]; |
| 85 | + |
| 86 | + yield [ |
| 87 | + new Error('Wrapped Base UserError with previous', null, null, null, null, new UserError('User error message', 0, $exception)), |
| 88 | + $this->once(), |
| 89 | + [ |
| 90 | + LogLevel::ERROR, |
| 91 | + \sprintf('[GraphQL] Exception: Ko![0] (caught throwable) at %s line %s.', __FILE__, $exception->getLine()), |
| 92 | + ['exception' => $exception], |
| 93 | + ], |
| 94 | + ]; |
| 95 | + |
| 96 | + yield [ |
| 97 | + new Error('Wrapped UserError with previous', null, null, null, null, new UserError('User error message', 0, $exception)), |
| 98 | + $this->once(), |
| 99 | + [ |
| 100 | + LogLevel::ERROR, |
| 101 | + \sprintf('[GraphQL] Exception: Ko![0] (caught throwable) at %s line %s.', __FILE__, $exception->getLine()), |
| 102 | + ['exception' => $exception], |
| 103 | + ], |
| 104 | + ]; |
| 105 | + |
| 106 | + yield [ |
| 107 | + new Error('Wrapped UserWarning with previous', null, null, null, null, new UserWarning('User warning message', 0, $exception)), |
| 108 | + $this->once(), |
| 109 | + [ |
| 110 | + LogLevel::WARNING, |
| 111 | + \sprintf('[GraphQL] Exception: Ko![0] (caught throwable) at %s line %s.', __FILE__, $exception->getLine()), |
| 112 | + ['exception' => $exception], |
| 113 | + ], |
| 114 | + ]; |
76 | 115 | } |
77 | 116 | } |
0 commit comments