|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PhpList\RestBundle\Tests\Integration\EventListener; |
| 6 | + |
| 7 | +use PhpList\RestBundle\EventListener\ExceptionListener; |
| 8 | +use PhpList\RestBundle\Exception\SubscriptionCreationException; |
| 9 | +use PHPUnit\Framework\TestCase; |
| 10 | +use Symfony\Component\HttpFoundation\JsonResponse; |
| 11 | +use Symfony\Component\HttpKernel\Event\ExceptionEvent; |
| 12 | +use Symfony\Component\HttpKernel\HttpKernelInterface; |
| 13 | +use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; |
| 14 | +use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
| 15 | +use Symfony\Component\HttpFoundation\Request; |
| 16 | + |
| 17 | +class ExceptionListenerTest extends TestCase |
| 18 | +{ |
| 19 | + private function createExceptionEvent(\Throwable $exception): ExceptionEvent |
| 20 | + { |
| 21 | + $kernel = $this->createMock(HttpKernelInterface::class); |
| 22 | + $request = new Request(); |
| 23 | + return new ExceptionEvent($kernel, $request, HttpKernelInterface::MAIN_REQUEST, $exception); |
| 24 | + } |
| 25 | + |
| 26 | + public function testAccessDeniedExceptionHandled(): void |
| 27 | + { |
| 28 | + $listener = new ExceptionListener(); |
| 29 | + $event = $this->createExceptionEvent(new AccessDeniedHttpException('Forbidden')); |
| 30 | + |
| 31 | + $listener->onKernelException($event); |
| 32 | + $response = $event->getResponse(); |
| 33 | + |
| 34 | + $this->assertInstanceOf(JsonResponse::class, $response); |
| 35 | + $this->assertEquals(403, $response->getStatusCode()); |
| 36 | + $this->assertEquals(['message' => 'Forbidden'], json_decode($response->getContent(), true)); |
| 37 | + } |
| 38 | + |
| 39 | + public function testHttpExceptionHandled(): void |
| 40 | + { |
| 41 | + $listener = new ExceptionListener(); |
| 42 | + $event = $this->createExceptionEvent(new NotFoundHttpException('Not found')); |
| 43 | + |
| 44 | + $listener->onKernelException($event); |
| 45 | + $response = $event->getResponse(); |
| 46 | + |
| 47 | + $this->assertInstanceOf(JsonResponse::class, $response); |
| 48 | + $this->assertEquals(404, $response->getStatusCode()); |
| 49 | + $this->assertEquals(['message' => 'Not found'], json_decode($response->getContent(), true)); |
| 50 | + } |
| 51 | + |
| 52 | + public function testSubscriptionCreationExceptionHandled(): void |
| 53 | + { |
| 54 | + $listener = new ExceptionListener(); |
| 55 | + $exception = new SubscriptionCreationException('Subscription error', 409); |
| 56 | + $event = $this->createExceptionEvent($exception); |
| 57 | + |
| 58 | + $listener->onKernelException($event); |
| 59 | + $response = $event->getResponse(); |
| 60 | + |
| 61 | + $this->assertInstanceOf(JsonResponse::class, $response); |
| 62 | + $this->assertEquals(409, $response->getStatusCode()); |
| 63 | + $this->assertEquals(['message' => 'Subscription error'], json_decode($response->getContent(), true)); |
| 64 | + } |
| 65 | + |
| 66 | + public function testGenericExceptionHandled(): void |
| 67 | + { |
| 68 | + $listener = new ExceptionListener(); |
| 69 | + $event = $this->createExceptionEvent(new \RuntimeException('Something went wrong')); |
| 70 | + |
| 71 | + $listener->onKernelException($event); |
| 72 | + $response = $event->getResponse(); |
| 73 | + |
| 74 | + $this->assertInstanceOf(JsonResponse::class, $response); |
| 75 | + $this->assertEquals(500, $response->getStatusCode()); |
| 76 | + $this->assertEquals(['message' => 'Something went wrong'], json_decode($response->getContent(), true)); |
| 77 | + } |
| 78 | +} |
0 commit comments