|
2 | 2 |
|
3 | 3 | namespace App\Tests\EventListener;
|
4 | 4 |
|
5 |
| -use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; |
| 5 | +use App\EventListener\AjaxListener; |
| 6 | +use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; |
| 7 | +use Symfony\Component\HttpFoundation\Request; |
| 8 | +use Symfony\Component\HttpFoundation\Response; |
| 9 | +use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; |
| 10 | +use Symfony\Component\HttpKernel\HttpKernelInterface; |
| 11 | +use Symfony\Component\Security\Core\Exception\AccessDeniedException; |
| 12 | +use Symfony\Component\Security\Core\Exception\AuthenticationException; |
| 13 | +use Symfony\Component\Serializer\Serializer; |
6 | 14 |
|
7 | 15 | /**
|
8 | 16 | * @covers \App\EventListener\AjaxListener
|
9 | 17 | */
|
10 |
| -class AjaxListenerTest extends WebTestCase { |
11 |
| - public function test403sOnAuthenticationFailure() { |
12 |
| - $client = $this->createClient([], [ |
13 |
| - 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest', |
14 |
| - ]); |
| 18 | +class AjaxListenerTest extends KernelTestCase { |
| 19 | + /** |
| 20 | + * @var Serializer |
| 21 | + */ |
| 22 | + private $serializer; |
15 | 23 |
|
16 |
| - $client->request('POST', '/cv/1.json'); |
| 24 | + /** |
| 25 | + * @var AjaxListener |
| 26 | + */ |
| 27 | + private $listener; |
17 | 28 |
|
18 |
| - $this->assertSame(403, $client->getResponse()->getStatusCode()); |
| 29 | + protected function setUp() { |
| 30 | + static::bootKernel(); |
| 31 | + |
| 32 | + $this->serializer = self::$kernel->getContainer()->get('serializer'); |
| 33 | + $this->listener = new AjaxListener($this->serializer); |
19 | 34 | }
|
20 | 35 |
|
21 |
| - public function testRedirectsToLoginWithoutXhr() { |
22 |
| - $client = $this->createClient(); |
23 |
| - $client->request('POST', '/cv/1.json'); |
24 | 36 |
|
25 |
| - $this->assertTrue($client->getResponse()->isRedirect()); |
| 37 | + public function testDoesNotSetResponseOnNotXhrRequests(): void { |
| 38 | + $request = new Request(); |
| 39 | + $event = $this->createEvent($request, new AccessDeniedException()); |
| 40 | + |
| 41 | + $this->listener->onKernelException($event); |
| 42 | + |
| 43 | + $this->assertNull($event->getResponse()); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * @dataProvider fourOhThreeOnExceptionWithSerializedBodyProvider |
| 48 | + */ |
| 49 | + public function test403sOnExceptionWithSerializedBody(Request $request, \Exception $e): void { |
| 50 | + $event = $this->createEvent($request, $e); |
| 51 | + |
| 52 | + $this->listener->onKernelException($event); |
| 53 | + |
| 54 | + $this->assertInstanceOf(Response::class, $event->getResponse()); |
| 55 | + $this->assertEquals(403, $event->getResponse()->getStatusCode()); |
| 56 | + $this->assertEquals( |
| 57 | + $this->serializer->serialize( |
| 58 | + ['error' => $e->getMessage()], |
| 59 | + $request->getRequestFormat() |
| 60 | + ), |
| 61 | + $event->getResponse()->getContent() |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * @dataProvider fourOhThreeOnExceptionWithPlainBodyProvider |
| 67 | + */ |
| 68 | + public function test403sOnExceptionWithPlainBody(Request $request, \Exception $e): void { |
| 69 | + $event = $this->createEvent($request, $e); |
| 70 | + |
| 71 | + $this->listener->onKernelException($event); |
| 72 | + |
| 73 | + $this->assertInstanceOf(Response::class, $event->getResponse()); |
| 74 | + $this->assertEquals(403, $event->getResponse()->getStatusCode()); |
| 75 | + $this->assertEquals($e->getMessage(), $event->getResponse()->getContent()); |
| 76 | + } |
| 77 | + |
| 78 | + public function fourOhThreeOnExceptionWithSerializedBodyProvider() { |
| 79 | + $request = new Request(); |
| 80 | + $request->headers->set('X-Requested-With', 'XMLHttpRequest'); |
| 81 | + $request->setRequestFormat('json'); |
| 82 | + $exception = new AuthenticationException('foo'); |
| 83 | + |
| 84 | + yield [$request, $exception]; |
| 85 | + |
| 86 | + $request->setRequestFormat('xml'); |
| 87 | + |
| 88 | + yield [$request, $exception]; |
| 89 | + |
| 90 | + $exception = new AccessDeniedException('aaa'); |
| 91 | + |
| 92 | + yield [$request, $exception]; |
| 93 | + } |
| 94 | + |
| 95 | + public function fourOhThreeOnExceptionWithPlainBodyProvider() { |
| 96 | + $request = new Request(); |
| 97 | + $request->headers->set('X-Requested-With', 'XMLHttpRequest'); |
| 98 | + $exception = new AuthenticationException('sheep'); |
| 99 | + |
| 100 | + yield [$request, $exception]; |
| 101 | + |
| 102 | + $exception = new AccessDeniedException('cow'); |
| 103 | + |
| 104 | + yield [$request, $exception]; |
| 105 | + } |
| 106 | + |
| 107 | + private function createEvent(Request $request, \Exception $e): GetResponseForExceptionEvent { |
| 108 | + return new GetResponseForExceptionEvent( |
| 109 | + self::$kernel, |
| 110 | + $request, |
| 111 | + HttpKernelInterface::MASTER_REQUEST, |
| 112 | + $e |
| 113 | + ); |
26 | 114 | }
|
27 | 115 | }
|
0 commit comments