|
17 | 17 | use Symfony\Component\EventDispatcher\EventDispatcher;
|
18 | 18 | use Symfony\Component\HttpFoundation\Request;
|
19 | 19 | use Symfony\Component\HttpFoundation\Response;
|
| 20 | +use Symfony\Component\HttpKernel\Attribute\HttpStatus; |
20 | 21 | use Symfony\Component\HttpKernel\Controller\ArgumentResolver;
|
21 | 22 | use Symfony\Component\HttpKernel\Event\ControllerArgumentsEvent;
|
22 | 23 | use Symfony\Component\HttpKernel\Event\ExceptionEvent;
|
@@ -117,6 +118,36 @@ public function testHandleWithLoggerAndCustomConfiguration()
|
117 | 118 | $this->assertCount(1, $logger->getLogs('warning'));
|
118 | 119 | }
|
119 | 120 |
|
| 121 | + /** |
| 122 | + * @dataProvider exceptionWithAttributeProvider |
| 123 | + */ |
| 124 | + public function testHandleHttpAttribute(\Throwable $exception, int $expectedStatusCode, array $expectedHeaders) |
| 125 | + { |
| 126 | + $request = new Request(); |
| 127 | + $event = new ExceptionEvent(new TestKernel(), $request, HttpKernelInterface::MAIN_REQUEST, $exception); |
| 128 | + $l = new ErrorListener('not used'); |
| 129 | + $l->logKernelException($event); |
| 130 | + $l->onKernelException($event); |
| 131 | + |
| 132 | + $this->assertEquals(new Response('foo', $expectedStatusCode, $expectedHeaders), $event->getResponse()); |
| 133 | + } |
| 134 | + |
| 135 | + public function testHandleCustomConfigurationAndHttpAttribute() |
| 136 | + { |
| 137 | + $request = new Request(); |
| 138 | + $event = new ExceptionEvent(new TestKernel(), $request, HttpKernelInterface::MAIN_REQUEST, new WithGeneralAttribute()); |
| 139 | + $l = new ErrorListener('not used', null, false, [ |
| 140 | + WithGeneralAttribute::class => [ |
| 141 | + 'log_level' => 'warning', |
| 142 | + 'status_code' => 401, |
| 143 | + ], |
| 144 | + ]); |
| 145 | + $l->logKernelException($event); |
| 146 | + $l->onKernelException($event); |
| 147 | + |
| 148 | + $this->assertEquals(new Response('foo', 401), $event->getResponse()); |
| 149 | + } |
| 150 | + |
120 | 151 | public function provider()
|
121 | 152 | {
|
122 | 153 | if (!class_exists(Request::class)) {
|
@@ -216,6 +247,12 @@ public function controllerProvider()
|
216 | 247 | return new Response('OK: '.$exception->getMessage());
|
217 | 248 | }];
|
218 | 249 | }
|
| 250 | + |
| 251 | + public static function exceptionWithAttributeProvider() |
| 252 | + { |
| 253 | + yield [new WithCustomUserProvidedAttribute(), 208, ['name' => 'value']]; |
| 254 | + yield [new WithGeneralAttribute(), 412, ['some' => 'thing']]; |
| 255 | + } |
219 | 256 | }
|
220 | 257 |
|
221 | 258 | class TestLogger extends Logger implements DebugLoggerInterface
|
@@ -246,3 +283,32 @@ public function handle(Request $request, $type = self::MAIN_REQUEST, $catch = tr
|
246 | 283 | throw new \RuntimeException('bar');
|
247 | 284 | }
|
248 | 285 | }
|
| 286 | + |
| 287 | +#[\Attribute(\Attribute::TARGET_CLASS)] |
| 288 | +class UserProvidedHttpStatusCodeAttribute extends HttpStatus |
| 289 | +{ |
| 290 | + public function __construct(array $headers = []) |
| 291 | + { |
| 292 | + parent::__construct( |
| 293 | + Response::HTTP_ALREADY_REPORTED, |
| 294 | + $headers |
| 295 | + ); |
| 296 | + } |
| 297 | +} |
| 298 | + |
| 299 | +#[UserProvidedHttpStatusCodeAttribute(headers: [ |
| 300 | + 'name' => 'value', |
| 301 | +])] |
| 302 | +class WithCustomUserProvidedAttribute extends \Exception |
| 303 | +{ |
| 304 | +} |
| 305 | + |
| 306 | +#[HttpStatus( |
| 307 | + statusCode: Response::HTTP_PRECONDITION_FAILED, |
| 308 | + headers: [ |
| 309 | + 'some' => 'thing', |
| 310 | + ] |
| 311 | +)] |
| 312 | +class WithGeneralAttribute extends \Exception |
| 313 | +{ |
| 314 | +} |
0 commit comments