You are here

public function BigPipeResponseAttachmentsProcessorTest::testHtmlResponse in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/big_pipe/tests/src/Unit/Render/BigPipeResponseAttachmentsProcessorTest.php \Drupal\Tests\big_pipe\Unit\Render\BigPipeResponseAttachmentsProcessorTest::testHtmlResponse()
  2. 9 core/modules/big_pipe/tests/src/Unit/Render/BigPipeResponseAttachmentsProcessorTest.php \Drupal\Tests\big_pipe\Unit\Render\BigPipeResponseAttachmentsProcessorTest::testHtmlResponse()

@covers ::processAttachments

@dataProvider attachmentsProvider

File

core/modules/big_pipe/tests/src/Unit/Render/BigPipeResponseAttachmentsProcessorTest.php, line 52

Class

BigPipeResponseAttachmentsProcessorTest
@coversDefaultClass \Drupal\big_pipe\Render\BigPipeResponseAttachmentsProcessor @group big_pipe

Namespace

Drupal\Tests\big_pipe\Unit\Render

Code

public function testHtmlResponse(array $attachments) {
  $big_pipe_response = new BigPipeResponse(new HtmlResponse('original'));
  $big_pipe_response
    ->setAttachments($attachments);

  // This mock is the main expectation of this test: verify that the decorated
  // service (that is this mock) never receives BigPipe placeholder
  // attachments, because it doesn't know (nor should it) how to handle them.
  $html_response_attachments_processor = $this
    ->prophesize(AttachmentsResponseProcessorInterface::class);
  $html_response_attachments_processor
    ->processAttachments(Argument::that(function ($response) {
    return $response instanceof HtmlResponse && empty(array_intersect([
      'big_pipe_placeholders',
      'big_pipe_nojs_placeholders',
    ], array_keys($response
      ->getAttachments())));
  }))
    ->will(function ($args) {

    /** @var \Symfony\Component\HttpFoundation\Response|\Drupal\Core\Render\AttachmentsInterface $response */
    $response = $args[0];

    // Simulate its actual behavior.
    $attachments = array_diff_key($response
      ->getAttachments(), [
      'html_response_attachment_placeholders' => TRUE,
    ]);
    $response
      ->setContent('processed');
    $response
      ->setAttachments($attachments);
    return $response;
  })
    ->shouldBeCalled();
  $big_pipe_response_attachments_processor = $this
    ->createBigPipeResponseAttachmentsProcessor($html_response_attachments_processor);
  $processed_big_pipe_response = $big_pipe_response_attachments_processor
    ->processAttachments($big_pipe_response);

  // The secondary expectation of this test: the original (passed in) response
  // object remains unchanged, the processed (returned) response object has
  // the expected values.
  $this
    ->assertSame($attachments, $big_pipe_response
    ->getAttachments(), 'Attachments of original response object MUST NOT be changed.');
  $this
    ->assertEquals('original', $big_pipe_response
    ->getContent(), 'Content of original response object MUST NOT be changed.');
  $this
    ->assertEquals(array_diff_key($attachments, [
    'html_response_attachment_placeholders' => TRUE,
  ]), $processed_big_pipe_response
    ->getAttachments(), 'Attachments of returned (processed) response object MUST be changed.');
  $this
    ->assertEquals('processed', $processed_big_pipe_response
    ->getContent(), 'Content of returned (processed) response object MUST be changed.');
}