function ResourceResponseSubscriberTest::testResponseFormat

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

@covers ::getResponseFormat

Note this does *not* need to test formats being requested that are not accepted by the server, because the routing system would have already prevented those from reaching the controller.

@dataProvider providerTestResponseFormat

File

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

Class

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

Namespace

Drupal\Tests\rest\Unit\EventSubscriber

Code

public function testResponseFormat($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) {
    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));
        $resource_response_subscriber = new ResourceResponseSubscriber($this->prophesize(SerializerInterface::class)
            ->reveal(), $this->prophesize(RendererInterface::class)
            ->reveal(), $route_match);
        $this->assertSame($expected_response_format, $resource_response_subscriber->getResponseFormat($route_match, $request));
    }
}

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