You are here

public function VariationCacheTest::testNestedVariations in VariationCache 8

Tests a cache item that has nested variations.

@covers ::get @covers ::set

File

tests/src/Unit/VariationCacheTest.php, line 217

Class

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

Namespace

Drupal\Tests\variationcache\Unit

Code

public function testNestedVariations() {

  // We are running this scenario in the best possible outcome: The redirects
  // are stored in expanding order, meaning the simplest one is stored first
  // and the nested ones are stored in subsequent ::set() calls. This means no
  // self-healing takes place where overly specific redirects are overwritten
  // with simpler ones.
  $possible_outcomes = [
    'apartment' => 'You have a nice apartment!',
    'house|no-garden' => 'You have a nice house!',
    'house|garden|east' => 'You have a nice house with an east-facing garden!',
    'house|garden|south' => 'You have a nice house with a south-facing garden!',
    'house|garden|west' => 'You have a nice house with a west-facing garden!',
    'house|garden|north' => 'You have a nice house with a north-facing garden!',
  ];
  foreach ($possible_outcomes as $cache_context_values => $data) {
    list($this->housingType, $this->gardenType, $this->houseOrientation) = explode('|', $cache_context_values . '||');
    $cacheability = $this->housingTypeCacheability;
    if (!empty($this->houseOrientation)) {
      $cacheability = $this->houseOrientationCacheability;
    }
    elseif (!empty($this->gardenType)) {
      $cacheability = $this->gardenTypeCacheability;
    }
    $this
      ->assertVariationCacheMiss($this->housingTypeCacheability);
    $this
      ->setVariationCacheItem($data, $cacheability, $this->housingTypeCacheability);
    $this
      ->assertVariationCacheItem($data, $cacheability, $this->housingTypeCacheability);
    $cache_id = "{$this->cacheIdBase}:ht.{$this->housingType}";
    if (!empty($this->gardenType)) {
      $this
        ->assertCacheBackendItem($cache_id, new CacheRedirect($this->gardenTypeCacheability));
      $cache_id .= ":gt.{$this->gardenType}";
    }
    if (!empty($this->houseOrientation)) {
      $this
        ->assertCacheBackendItem($cache_id, new CacheRedirect($this->houseOrientationCacheability));
      $cache_id .= ":ho.{$this->houseOrientation}";
    }
    $this
      ->assertCacheBackendItem($cache_id, $data, $cacheability);
  }
}