|
12 | 12 | namespace Symfony\Component\HttpKernel\Tests\EventListener;
|
13 | 13 |
|
14 | 14 | use PHPUnit\Framework\TestCase;
|
| 15 | +use Psr\Log\LoggerInterface; |
15 | 16 | use Psr\Log\LogLevel;
|
16 | 17 | use Symfony\Component\Console\Application;
|
17 | 18 | use Symfony\Component\Console\Command\Command;
|
@@ -150,4 +151,89 @@ public function testReplaceExistingExceptionHandler()
|
150 | 151 |
|
151 | 152 | $this->assertSame($userHandler, $eHandler->setExceptionHandler('var_dump'));
|
152 | 153 | }
|
| 154 | + |
| 155 | + public function provideLevelsAssignedToLoggers(): array |
| 156 | + { |
| 157 | + return [ |
| 158 | + [false, false, '0', null, null], |
| 159 | + [false, false, E_ALL, null, null], |
| 160 | + [false, false, [], null, null], |
| 161 | + [false, false, [E_WARNING => LogLevel::WARNING, E_USER_DEPRECATED => LogLevel::NOTICE], null, null], |
| 162 | + |
| 163 | + [true, false, E_ALL, E_ALL, null], |
| 164 | + [true, false, E_DEPRECATED, E_DEPRECATED, null], |
| 165 | + [true, false, [], null, null], |
| 166 | + [true, false, [E_WARNING => LogLevel::WARNING, E_DEPRECATED => LogLevel::NOTICE], [E_WARNING => LogLevel::WARNING, E_DEPRECATED => LogLevel::NOTICE], null], |
| 167 | + |
| 168 | + [false, true, '0', null, null], |
| 169 | + [false, true, E_ALL, null, E_DEPRECATED | E_USER_DEPRECATED], |
| 170 | + [false, true, E_ERROR, null, null], |
| 171 | + [false, true, [], null, null], |
| 172 | + [false, true, [E_ERROR => LogLevel::ERROR, E_DEPRECATED => LogLevel::DEBUG], null, [E_DEPRECATED => LogLevel::DEBUG]], |
| 173 | + |
| 174 | + [true, true, '0', null, null], |
| 175 | + [true, true, E_ALL, E_ALL & ~(E_DEPRECATED | E_USER_DEPRECATED), E_DEPRECATED | E_USER_DEPRECATED], |
| 176 | + [true, true, E_ERROR, E_ERROR, null], |
| 177 | + [true, true, E_USER_DEPRECATED, null, E_USER_DEPRECATED], |
| 178 | + [true, true, [E_ERROR => LogLevel::ERROR, E_DEPRECATED => LogLevel::DEBUG], [E_ERROR => LogLevel::ERROR], [E_DEPRECATED => LogLevel::DEBUG]], |
| 179 | + [true, true, [E_ERROR => LogLevel::ALERT], [E_ERROR => LogLevel::ALERT], null], |
| 180 | + [true, true, [E_USER_DEPRECATED => LogLevel::NOTICE], null, [E_USER_DEPRECATED => LogLevel::NOTICE]], |
| 181 | + ]; |
| 182 | + } |
| 183 | + |
| 184 | + /** |
| 185 | + * @dataProvider provideLevelsAssignedToLoggers |
| 186 | + * |
| 187 | + * @param array|string $levels |
| 188 | + * @param array|string|null $expectedLoggerLevels |
| 189 | + * @param array|string|null $expectedDeprecationLoggerLevels |
| 190 | + */ |
| 191 | + public function testLevelsAssignedToLoggers(bool $hasLogger, bool $hasDeprecationLogger, $levels, $expectedLoggerLevels, $expectedDeprecationLoggerLevels) |
| 192 | + { |
| 193 | + if (!class_exists(ErrorHandler::class)) { |
| 194 | + $this->markTestSkipped('ErrorHandler component is required to run this test.'); |
| 195 | + } |
| 196 | + |
| 197 | + $handler = $this->createMock(ErrorHandler::class); |
| 198 | + |
| 199 | + $expectedCalls = []; |
| 200 | + $logger = null; |
| 201 | + |
| 202 | + $deprecationLogger = null; |
| 203 | + if ($hasDeprecationLogger) { |
| 204 | + $deprecationLogger = $this->createMock(LoggerInterface::class); |
| 205 | + if (null !== $expectedDeprecationLoggerLevels) { |
| 206 | + $expectedCalls[] = [$deprecationLogger, $expectedDeprecationLoggerLevels]; |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + if ($hasLogger) { |
| 211 | + $logger = $this->createMock(LoggerInterface::class); |
| 212 | + if (null !== $expectedLoggerLevels) { |
| 213 | + $expectedCalls[] = [$logger, $expectedLoggerLevels]; |
| 214 | + } |
| 215 | + } |
| 216 | + |
| 217 | + $handler |
| 218 | + ->expects($this->exactly(\count($expectedCalls))) |
| 219 | + ->method('setDefaultLogger') |
| 220 | + ->withConsecutive(...$expectedCalls); |
| 221 | + |
| 222 | + $sut = new DebugHandlersListener(null, $logger, $levels, null, true, null, true, $deprecationLogger); |
| 223 | + $prevHander = set_exception_handler([$handler, 'handleError']); |
| 224 | + |
| 225 | + try { |
| 226 | + $handler |
| 227 | + ->method('handleError') |
| 228 | + ->willReturnCallback(function () use ($prevHander) { |
| 229 | + $prevHander(...\func_get_args()); |
| 230 | + }); |
| 231 | + |
| 232 | + $sut->configure(); |
| 233 | + set_exception_handler($prevHander); |
| 234 | + } catch (\Exception $e) { |
| 235 | + set_exception_handler($prevHander); |
| 236 | + throw $e; |
| 237 | + } |
| 238 | + } |
153 | 239 | }
|
0 commit comments