function ResourceResponseSubscriberTest::testSerialization

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

@covers ::onResponse @dataProvider providerTestSerialization

File

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

Class

ResourceResponseSubscriberTest
@coversDefaultClass <a href="/api/drupal/core%21modules%21rest%21src%21EventSubscriber%21ResourceResponseSubscriber.php/class/ResourceResponseSubscriber/11.x" title="Response subscriber that serializes and removes ResourceResponses&#039; data." class="local">\Drupal\rest\EventSubscriber\ResourceResponseSubscriber</a> @group rest

Namespace

Drupal\Tests\rest\Unit\EventSubscriber

Code

public function testSerialization($data, $expected_response = FALSE) : void {
    $request = new Request();
    $route_match = new RouteMatch('test', new Route('/rest/test', [
        '_rest_resource_config' => 'rest_plugin',
    ], [
        '_format' => 'json',
    ]));
    $handler_response = new ResourceResponse($data);
    $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);
    // Content is a serialized version of the data we provided.
    $this->assertEquals($expected_response !== FALSE ? $expected_response : Json::encode($data), $event->getResponse()
        ->getContent());
}

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