function VariationCacheTest::setUp

Same name in other branches
  1. 10 core/tests/Drupal/Tests/Core/Cache/VariationCacheTest.php \Drupal\Tests\Core\Cache\VariationCacheTest::setUp()

Overrides UnitTestCase::setUp

File

core/tests/Drupal/Tests/Core/Cache/VariationCacheTest.php, line 126

Class

VariationCacheTest
@coversDefaultClass \Drupal\Core\Cache\VariationCache @group Cache

Namespace

Drupal\Tests\Core\Cache

Code

protected function setUp() : void {
    parent::setUp();
    $this->requestStack = $this->prophesize(RequestStack::class);
    $this->memoryBackend = new MemoryBackend(new Time());
    $this->cacheContextsManager = $this->prophesize(CacheContextsManager::class);
    $housing_type =& $this->housingType;
    $garden_type =& $this->gardenType;
    $house_orientation =& $this->houseOrientation;
    $solar_type =& $this->solarType;
    $this->cacheContextsManager
        ->convertTokensToKeys(Argument::any())
        ->will(function ($args) use (&$housing_type, &$garden_type, &$house_orientation, &$solar_type) {
        $keys = [];
        foreach ($args[0] as $context_id) {
            switch ($context_id) {
                case 'house.type':
                    $keys[] = "ht.{$housing_type}";
                    break;
                case 'garden.type':
                    $keys[] = "gt.{$garden_type}";
                    break;
                case 'house.orientation':
                    $keys[] = "ho.{$house_orientation}";
                    break;
                case 'solar.type':
                    $keys[] = "st.{$solar_type}";
                    break;
                default:
                    $keys[] = $context_id;
            }
        }
        return new ContextCacheKeys($keys);
    });
    $this->variationCache = new VariationCache($this->requestStack
        ->reveal(), $this->memoryBackend, $this->cacheContextsManager
        ->reveal());
    $this->housingTypeCacheability = (new CacheableMetadata())->setCacheTags([
        'foo',
    ])
        ->setCacheContexts([
        'house.type',
    ]);
    $this->gardenTypeCacheability = (new CacheableMetadata())->setCacheTags([
        'bar',
    ])
        ->setCacheContexts([
        'house.type',
        'garden.type',
    ]);
    $this->houseOrientationCacheability = (new CacheableMetadata())->setCacheTags([
        'baz',
    ])
        ->setCacheContexts([
        'house.type',
        'garden.type',
        'house.orientation',
    ]);
}

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