Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / tests / Drupal / Tests / Core / EventSubscriber / ExceptionJsonSubscriberTest.php
index 86c6d86bfd5f43ecd66264b44d2faa7013417f43..8f339a839dcdfa283050249f7fc05c69c2536053 100644 (file)
@@ -2,11 +2,15 @@
 
 namespace Drupal\Tests\Core\EventSubscriber;
 
+use Drupal\Core\Cache\CacheableJsonResponse;
+use Drupal\Core\Cache\CacheableMetadata;
 use Drupal\Core\EventSubscriber\ExceptionJsonSubscriber;
+use Drupal\Core\Http\Exception\CacheableMethodNotAllowedHttpException;
 use Drupal\Tests\UnitTestCase;
 use Symfony\Component\HttpFoundation\JsonResponse;
 use Symfony\Component\HttpFoundation\Request;
 use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
+use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
 use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
 use Symfony\Component\HttpKernel\HttpKernelInterface;
 
@@ -18,21 +22,34 @@ class ExceptionJsonSubscriberTest extends UnitTestCase {
 
   /**
    * @covers ::on4xx
+   * @dataProvider providerTestOn4xx
    */
-  public function testOn4xx() {
+  public function testOn4xx(HttpExceptionInterface $exception, $expected_response_class) {
     $kernel = $this->prophesize(HttpKernelInterface::class);
     $request = Request::create('/test');
-    $e = new MethodNotAllowedHttpException(['POST', 'PUT'], 'test message');
-    $event = new GetResponseForExceptionEvent($kernel->reveal(), $request, 'GET', $e);
+    $event = new GetResponseForExceptionEvent($kernel->reveal(), $request, 'GET', $exception);
     $subscriber = new ExceptionJsonSubscriber();
     $subscriber->on4xx($event);
     $response = $event->getResponse();
 
-    $this->assertInstanceOf(JsonResponse::class, $response);
+    $this->assertInstanceOf($expected_response_class, $response);
     $this->assertEquals('{"message":"test message"}', $response->getContent());
     $this->assertEquals(405, $response->getStatusCode());
     $this->assertEquals('POST, PUT', $response->headers->get('Allow'));
     $this->assertEquals('application/json', $response->headers->get('Content-Type'));
   }
 
+  public function providerTestOn4xx() {
+    return [
+      'uncacheable exception' => [
+        new MethodNotAllowedHttpException(['POST', 'PUT'], 'test message'),
+        JsonResponse::class
+      ],
+      'cacheable exception' => [
+        new CacheableMethodNotAllowedHttpException((new CacheableMetadata())->setCacheContexts(['route']), ['POST', 'PUT'], 'test message'),
+        CacheableJsonResponse::class
+      ],
+    ];
+  }
+
 }