CacheCollectorHelper.php

Same filename and directory in other branches
  1. 11.x core/tests/Drupal/Tests/Core/Cache/CacheCollectorHelper.php
  2. 10 core/tests/Drupal/Tests/Core/Cache/CacheCollectorHelper.php
  3. 9 core/tests/Drupal/Tests/Core/Cache/CacheCollectorHelper.php
  4. 8.9.x core/tests/Drupal/Tests/Core/Cache/CacheCollectorHelper.php

Namespace

Drupal\Tests\Core\Cache

File

core/tests/Drupal/Tests/Core/Cache/CacheCollectorHelper.php

View source
<?php

declare (strict_types=1);
namespace Drupal\Tests\Core\Cache;

use Drupal\Core\Cache\CacheCollector;

/**
 * Helper class to test the cache collector.
 */
class CacheCollectorHelper extends CacheCollector {
  
  /**
   * Contains data to return on a cache miss.
   *
   * @var array
   */
  protected $cacheMissData = [];
  
  /**
   * Number of calls to \Drupal\Core\Cache\CacheCollector::resolveCacheMiss().
   *
   * @var int
   */
  protected $cacheMisses = 0;
  
  /**
   * {@inheritdoc}
   */
  public function set($key, $value) : void {
    parent::set($key, $value);
    $this->persist($key);
  }
  
  /**
   * {@inheritdoc}
   */
  public function resolveCacheMiss($key) {
    $this->cacheMisses++;
    if (isset($this->cacheMissData[$key])) {
      $this->storage[$key] = $this->cacheMissData[$key];
      $this->persist($key);
      return $this->cacheMissData[$key];
    }
  }
  
  /**
   * Sets data to return from a cache miss resolve.
   *
   * @param string $key
   *   The key being looked for.
   * @param mixed $value
   *   The value to return.
   */
  public function setCacheMissData($key, $value) : void {
    $this->cacheMissData[$key] = $value;
  }
  
  /**
   * Returns the number of cache misses.
   *
   * @return int
   *   Number of calls to the resolve cache miss method.
   */
  public function getCacheMisses() {
    return $this->cacheMisses;
  }
  
  /**
   * Simulates having loaded a cache item with the given data, for unit tests.
   *
   * Records the loaded data that ::updateCache() fingerprints and compares
   * against to detect a concurrent change.
   *
   * @param array $data
   *   The data the cache item would have contained when loaded.
   */
  public function setLoadedData(array $data) : void {
    $this->loadedData = $data;
  }

}

Classes

Title Deprecated Summary
CacheCollectorHelper Helper class to test the cache collector.

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