You are here

trait FarmEntityCacheTestTrait in farmOS 2.x

Trait for testing invalidation of entity cache tags.

During a test use populateEntityTestCache to set a cache value dependent on an entity's cache tags. Later in the test use assertEntityTestCache to ensure the cache value's existence after cache tags are invalidated.

Hierarchy

3 files declare their use of FarmEntityCacheTestTrait
GroupTest.php in modules/asset/group/tests/src/Kernel/GroupTest.php
InventoryTest.php in modules/core/inventory/tests/src/Kernel/InventoryTest.php
LocationTest.php in modules/core/location/tests/src/Kernel/LocationTest.php

File

modules/core/test/tests/src/Kernel/FarmEntityCacheTestTrait.php, line 15

Namespace

Drupal\Tests\farm_test\Kernel
View source
trait FarmEntityCacheTestTrait {

  /**
   * Populate a cache value that is dependent on an entity's cache tags.
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   The entity to cache data for.
   * @param string $data
   *   The data to cache. Defaults to a simple string.
   */
  protected function populateEntityTestCache(EntityInterface $entity, string $data = 'data') {
    \Drupal::cache()
      ->set($this
      ->getEntityCacheId($entity), $data, Cache::PERMANENT, $entity
      ->getCacheTags());
  }

  /**
   * Assert that a cache value dependent on an entity's cache tags exists.
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   The entity cache to check.
   * @param bool $cache_hit
   *   Boolean indicating if the cache lookup should result in a cache hit.
   */
  protected function assertEntityTestCache(EntityInterface $entity, bool $cache_hit) {
    $cache_result = \Drupal::cache()
      ->get($this
      ->getEntityCacheId($entity));
    $this
      ->assertEquals($cache_hit, (bool) $cache_result);
  }

  /**
   * Helper method to build the entity test cache ID.
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   The entity to cache data for.
   *
   * @return string
   *   The entity's test cache ID.
   */
  protected function getEntityCacheId(EntityInterface $entity) : string {
    $entity_type = $entity
      ->getEntityTypeId();
    $entity_id = $entity
      ->id();
    return "farm_entity_cache_test:{$entity_type}:{$entity_id}";
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FarmEntityCacheTestTrait::assertEntityTestCache protected function Assert that a cache value dependent on an entity's cache tags exists.
FarmEntityCacheTestTrait::getEntityCacheId protected function Helper method to build the entity test cache ID.
FarmEntityCacheTestTrait::populateEntityTestCache protected function Populate a cache value that is dependent on an entity's cache tags.