function CacheContextsManagerTest::testOptimizeTokens

Same name and namespace in other branches
  1. 11.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. 9 core/tests/Drupal/Tests/Core/Cache/Context/CacheContextsManagerTest.php \Drupal\Tests\Core\Cache\Context\CacheContextsManagerTest::testOptimizeTokens()
  4. 8.9.x core/tests/Drupal/Tests/Core/Cache/Context/CacheContextsManagerTest.php \Drupal\Tests\Core\Cache\Context\CacheContextsManagerTest::testOptimizeTokens()

Tests optimize tokens.

Attributes

#[DataProvider('providerTestOptimizeTokens')]

File

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

Class

CacheContextsManagerTest
Tests Drupal\Core\Cache\Context\CacheContextsManager.

Namespace

Drupal\Tests\Core\Cache\Context

Code

public function testOptimizeTokens(array $context_tokens, array $optimized_context_tokens, int $expected_container_calls) : void {
  $container = $this->createMock(DrupalContainer::class);
  $container->expects($this->exactly($expected_container_calls))
    ->method('get')
    ->willReturnCallback(fn($service_id): FooCacheContext|BazCacheContext|NoOptimizeCacheContext => match ($service_id) {  'cache_context.a' => new FooCacheContext(),
    'cache_context.a.b' => new FooCacheContext(),
    'cache_context.a.b.c' => new BazCacheContext(),
    'cache_context.x' => new BazCacheContext(),
    'cache_context.a.b.no-optimize' => 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.