86c6d86bfd5f43ecd66264b44d2faa7013417f43
[yaffs-website] / Drupal / Tests / Core / EventSubscriber / ExceptionJsonSubscriberTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\EventSubscriber;
4
5 use Drupal\Core\EventSubscriber\ExceptionJsonSubscriber;
6 use Drupal\Tests\UnitTestCase;
7 use Symfony\Component\HttpFoundation\JsonResponse;
8 use Symfony\Component\HttpFoundation\Request;
9 use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
10 use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
11 use Symfony\Component\HttpKernel\HttpKernelInterface;
12
13 /**
14  * @coversDefaultClass \Drupal\Core\EventSubscriber\ExceptionJsonSubscriber
15  * @group EventSubscriber
16  */
17 class ExceptionJsonSubscriberTest extends UnitTestCase {
18
19   /**
20    * @covers ::on4xx
21    */
22   public function testOn4xx() {
23     $kernel = $this->prophesize(HttpKernelInterface::class);
24     $request = Request::create('/test');
25     $e = new MethodNotAllowedHttpException(['POST', 'PUT'], 'test message');
26     $event = new GetResponseForExceptionEvent($kernel->reveal(), $request, 'GET', $e);
27     $subscriber = new ExceptionJsonSubscriber();
28     $subscriber->on4xx($event);
29     $response = $event->getResponse();
30
31     $this->assertInstanceOf(JsonResponse::class, $response);
32     $this->assertEquals('{"message":"test message"}', $response->getContent());
33     $this->assertEquals(405, $response->getStatusCode());
34     $this->assertEquals('POST, PUT', $response->headers->get('Allow'));
35     $this->assertEquals('application/json', $response->headers->get('Content-Type'));
36   }
37
38 }