function ResourceResponseSubscriberTest::testOnResponseWithCacheableResponse

Same name in other branches
  1. 9 core/modules/rest/tests/src/Unit/EventSubscriber/ResourceResponseSubscriberTest.php \Drupal\Tests\rest\Unit\EventSubscriber\ResourceResponseSubscriberTest::testOnResponseWithCacheableResponse()
  2. 8.9.x core/modules/rest/tests/src/Unit/EventSubscriber/ResourceResponseSubscriberTest.php \Drupal\Tests\rest\Unit\EventSubscriber\ResourceResponseSubscriberTest::testOnResponseWithCacheableResponse()
  3. 11.x core/modules/rest/tests/src/Unit/EventSubscriber/ResourceResponseSubscriberTest.php \Drupal\Tests\rest\Unit\EventSubscriber\ResourceResponseSubscriberTest::testOnResponseWithCacheableResponse()

@covers ::onResponse @covers ::getResponseFormat @covers ::renderResponseBody @covers ::flattenResponse

@dataProvider providerTestResponseFormat

File

core/modules/rest/tests/src/Unit/EventSubscriber/ResourceResponseSubscriberTest.php, line 118

Class

ResourceResponseSubscriberTest
@coversDefaultClass \Drupal\rest\EventSubscriber\ResourceResponseSubscriber @group rest

Namespace

Drupal\Tests\rest\Unit\EventSubscriber

Code

public function testOnResponseWithCacheableResponse($methods, array $supported_response_formats, array $supported_request_formats, $request_format, array $request_headers, $request_body, $expected_response_format, $expected_response_content_type, $expected_response_content) : void {
    foreach ($request_headers as $key => $value) {
        unset($request_headers[$key]);
        $key = strtoupper(str_replace('-', '_', $key));
        $request_headers[$key] = $value;
    }
    foreach ($methods as $method) {
        $request = Request::create('/rest/test', $method, [], [], [], $request_headers, $request_body);
        // \Drupal\Core\StackMiddleware\NegotiationMiddleware normally takes care
        // of this so we'll hard code it here.
        if ($request_format) {
            $request->setRequestFormat($request_format);
        }
        $route_requirements = $this->generateRouteRequirements($supported_response_formats, $supported_request_formats);
        $route_match = new RouteMatch('test', new Route('/rest/test', [
            '_rest_resource_config' => $this->randomMachineName(),
        ], $route_requirements));
        // The RequestHandler must return a ResourceResponseInterface object.
        $handler_response = new ResourceResponse([
            'REST' => 'Drupal',
        ]);
        $this->assertInstanceOf(ResourceResponseInterface::class, $handler_response);
        $this->assertInstanceOf(CacheableResponseInterface::class, $handler_response);
        // The ResourceResponseSubscriber must then generate a response body and
        // transform it to a plain CacheableResponse object.
        $resource_response_subscriber = $this->getFunctioningResourceResponseSubscriber($route_match);
        $event = new ResponseEvent($this->prophesize(HttpKernelInterface::class)
            ->reveal(), $request, HttpKernelInterface::MAIN_REQUEST, $handler_response);
        $resource_response_subscriber->onResponse($event);
        $final_response = $event->getResponse();
        $this->assertNotInstanceOf(ResourceResponseInterface::class, $final_response);
        $this->assertInstanceOf(CacheableResponseInterface::class, $final_response);
        $this->assertSame($expected_response_content_type, $final_response->headers
            ->get('Content-Type'));
        $this->assertEquals($expected_response_content, $final_response->getContent());
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.