function RendererBubblingTest::testContextBubblingCustomCacheBin

Same name in other branches
  1. 9 core/tests/Drupal/Tests/Core/Render/RendererBubblingTest.php \Drupal\Tests\Core\Render\RendererBubblingTest::testContextBubblingCustomCacheBin()
  2. 8.9.x core/tests/Drupal/Tests/Core/Render/RendererBubblingTest.php \Drupal\Tests\Core\Render\RendererBubblingTest::testContextBubblingCustomCacheBin()
  3. 10 core/tests/Drupal/Tests/Core/Render/RendererBubblingTest.php \Drupal\Tests\Core\Render\RendererBubblingTest::testContextBubblingCustomCacheBin()

Tests cache context bubbling with a custom cache bin.

File

core/tests/Drupal/Tests/Core/Render/RendererBubblingTest.php, line 82

Class

RendererBubblingTest
@coversDefaultClass \Drupal\Core\Render\Renderer @group Render

Namespace

Drupal\Tests\Core\Render

Code

public function testContextBubblingCustomCacheBin() : void {
    $bin = $this->randomMachineName();
    $this->setUpRequest();
    $this->memoryCache = new VariationCache($this->requestStack, new MemoryBackend(new Time($this->requestStack)), $this->cacheContextsManager);
    $custom_cache = new VariationCache($this->requestStack, new MemoryBackend(new Time($this->requestStack)), $this->cacheContextsManager);
    $this->cacheFactory
        ->expects($this->atLeastOnce())
        ->method('get')
        ->with($bin)
        ->willReturnCallback(function ($requested_bin) use ($bin, $custom_cache) {
        if ($requested_bin === $bin) {
            return $custom_cache;
        }
        else {
            throw new \Exception();
        }
    });
    $this->cacheContextsManager
        ->expects($this->any())
        ->method('convertTokensToKeys')
        ->willReturnArgument(0);
    $build = [
        '#cache' => [
            'keys' => [
                'parent',
            ],
            'contexts' => [
                'foo',
            ],
            'bin' => $bin,
        ],
        '#markup' => 'parent',
        'child' => [
            '#cache' => [
                'contexts' => [
                    'bar',
                ],
                'max-age' => 3600,
            ],
        ],
    ];
    $this->renderer
        ->renderRoot($build);
    $this->assertRenderCacheItem([
        'parent',
    ], [
        '#attached' => [],
        '#cache' => [
            'contexts' => [
                'bar',
                'foo',
            ],
            'tags' => [],
            'max-age' => 3600,
        ],
        '#markup' => 'parent',
    ], $bin);
}

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