You are here

public function BubbleableMetadataTest::testMerge in Drupal 8

Same name and namespace in other branches
  1. 9 core/tests/Drupal/Tests/Core/Render/BubbleableMetadataTest.php \Drupal\Tests\Core\Render\BubbleableMetadataTest::testMerge()

@covers ::merge @dataProvider providerTestMerge

This only tests at a high level, because it reuses existing logic. Detailed tests exist for the existing logic:

See also

\Drupal\Tests\Core\Cache\CacheTest::testMergeTags()

\Drupal\Tests\Core\Cache\CacheTest::testMergeMaxAges()

\Drupal\Tests\Core\Cache\CacheContextsTest

\Drupal\Tests\Core\Render\RendererPlaceholdersTest

testMergeAttachmentsLibraryMerging()

testMergeAttachmentsFeedMerging()

testMergeAttachmentsHtmlHeadMerging()

testMergeAttachmentsHtmlHeadLinkMerging()

testMergeAttachmentsHttpHeaderMerging()

File

core/tests/Drupal/Tests/Core/Render/BubbleableMetadataTest.php, line 34

Class

BubbleableMetadataTest
@coversDefaultClass \Drupal\Core\Render\BubbleableMetadata @group Render

Namespace

Drupal\Tests\Core\Render

Code

public function testMerge(BubbleableMetadata $a, CacheableMetadata $b, BubbleableMetadata $expected) {

  // Verify that if the second operand is a CacheableMetadata object, not a
  // BubbleableMetadata object, that BubbleableMetadata::merge() doesn't
  // attempt to merge assets.
  if (!$b instanceof BubbleableMetadata) {
    $renderer = $this
      ->getMockBuilder('Drupal\\Core\\Render\\Renderer')
      ->disableOriginalConstructor()
      ->setMethods([
      'mergeAttachments',
    ])
      ->getMock();
    $renderer
      ->expects($this
      ->never())
      ->method('mergeAttachments');
  }
  else {
    $renderer = $this
      ->getMockBuilder('Drupal\\Core\\Render\\Renderer')
      ->disableOriginalConstructor()
      ->setMethods(NULL)
      ->getMock();
  }
  $cache_contexts_manager = $this
    ->getMockBuilder('Drupal\\Core\\Cache\\Context\\CacheContextsManager')
    ->disableOriginalConstructor()
    ->getMock();
  $cache_contexts_manager
    ->method('assertValidTokens')
    ->willReturn(TRUE);
  $container = new ContainerBuilder();
  $container
    ->set('cache_contexts_manager', $cache_contexts_manager);
  $container
    ->set('renderer', $renderer);
  \Drupal::setContainer($container);
  $this
    ->assertEquals($expected, $a
    ->merge($b));
}