function CacheContextsManagerTest::testOptimizeTokens

Same name and namespace in other branches
  1. 8.9.x core/tests/Drupal/Tests/Core/Cache/Context/CacheContextsManagerTest.php \Drupal\Tests\Core\Cache\Context\CacheContextsManagerTest::testOptimizeTokens()
  2. 10 core/tests/Drupal/Tests/Core/Cache/Context/CacheContextsManagerTest.php \Drupal\Tests\Core\Cache\Context\CacheContextsManagerTest::testOptimizeTokens()
  3. 11.x core/tests/Drupal/Tests/Core/Cache/Context/CacheContextsManagerTest.php \Drupal\Tests\Core\Cache\Context\CacheContextsManagerTest::testOptimizeTokens()

@covers ::optimizeTokens

@dataProvider providerTestOptimizeTokens

File

core/tests/Drupal/Tests/Core/Cache/Context/CacheContextsManagerTest.php, line 29

Class

CacheContextsManagerTest
@coversDefaultClass <a href="/api/drupal/core%21lib%21Drupal%21Core%21Cache%21Context%21CacheContextsManager.php/class/CacheContextsManager/9" title="Converts cache context tokens into cache keys." class="local">\Drupal\Core\Cache\Context\CacheContextsManager</a> @group Cache

Namespace

Drupal\Tests\Core\Cache\Context

Code

public function testOptimizeTokens(array $context_tokens, array $optimized_context_tokens) {
    $container = $this->getMockBuilder('Drupal\\Core\\DependencyInjection\\Container')
        ->disableOriginalConstructor()
        ->getMock();
    $container->expects($this->any())
        ->method('get')
        ->willReturnMap([
        [
            'cache_context.a',
            Container::EXCEPTION_ON_INVALID_REFERENCE,
            new FooCacheContext(),
        ],
        [
            'cache_context.a.b',
            Container::EXCEPTION_ON_INVALID_REFERENCE,
            new FooCacheContext(),
        ],
        [
            'cache_context.a.b.c',
            Container::EXCEPTION_ON_INVALID_REFERENCE,
            new BazCacheContext(),
        ],
        [
            'cache_context.x',
            Container::EXCEPTION_ON_INVALID_REFERENCE,
            new BazCacheContext(),
        ],
        [
            'cache_context.a.b.no-optimize',
            Container::EXCEPTION_ON_INVALID_REFERENCE,
            new NoOptimizeCacheContext(),
        ],
    ]);
    $cache_contexts_manager = new CacheContextsManager($container, $this->getContextsFixture());
    $this->assertSame($optimized_context_tokens, $cache_contexts_manager->optimizeTokens($context_tokens));
}

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