3 namespace Drupal\Tests\Core\EventSubscriber;
5 use Drupal\Core\EventSubscriber\DefaultExceptionSubscriber;
6 use Drupal\Tests\UnitTestCase;
7 use Symfony\Component\HttpFoundation\Request;
8 use Symfony\Component\HttpFoundation\Response;
9 use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
10 use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
11 use Symfony\Component\HttpKernel\HttpKernelInterface;
14 * @coversDefaultClass \Drupal\Core\EventSubscriber\DefaultExceptionSubscriber
15 * @group EventSubscriber
17 class DefaultExceptionSubscriberTest extends UnitTestCase {
20 * @covers ::onException
21 * @covers ::onFormatUnknown
23 public function testOnExceptionWithUnknownFormat() {
24 $config_factory = $this->getConfigFactoryStub();
26 $kernel = $this->prophesize(HttpKernelInterface::class);
27 $request = Request::create('/test?_format=bananas');
28 $e = new MethodNotAllowedHttpException(['POST', 'PUT'], 'test message');
29 $event = new GetResponseForExceptionEvent($kernel->reveal(), $request, 'GET', $e);
30 $subscriber = new DefaultExceptionSubscriber($config_factory);
31 $subscriber->onException($event);
32 $response = $event->getResponse();
34 $this->assertInstanceOf(Response::class, $response);
35 $this->assertEquals('test message', $response->getContent());
36 $this->assertEquals(405, $response->getStatusCode());
37 $this->assertEquals('POST, PUT', $response->headers->get('Allow'));
38 // Also check that that text/plain content type was added.
39 $this->assertEquals('text/plain', $response->headers->get('Content-Type'));