You are here

trait EntityControllerCacheUtilsTrait in Apigee Edge 8

A trait to common functions of entity controller tests.

Hierarchy

2 files declare their use of EntityControllerCacheUtilsTrait
EntityControllerCacheTest.php in tests/src/Kernel/EntityControllerCacheTest.php
EntityControllerCacheTest.php in modules/apigee_edge_teams/tests/src/Kernel/EntityControllerCacheTest.php

File

tests/src/Traits/EntityControllerCacheUtilsTrait.php, line 28

Namespace

Drupal\Tests\apigee_edge\Traits
View source
trait EntityControllerCacheUtilsTrait {

  /**
   * Saves entities into the entity cache and checks the result.
   *
   * @param \Apigee\Edge\Entity\EntityInterface[] $entities
   *   Apigee Edge entities to save into the cache.
   * @param \Drupal\apigee_edge\Entity\Controller\Cache\EntityCacheInterface $entity_cache
   *   The entity cache implementation.
   * @param \Drupal\apigee_edge\Entity\Controller\Cache\EntityIdCacheInterface $entity_id_cache
   *   The entity id cache implementation.
   */
  protected function saveAllEntitiesAndValidate(array $entities, EntityCacheInterface $entity_cache, EntityIdCacheInterface $entity_id_cache) {

    // Save the generated entities into the controller cache.
    $entity_cache
      ->saveEntities($entities);
    $this
      ->assertSame($entities, $entity_cache
      ->getEntities());

    // Set cache states to TRUE.
    $entity_cache
      ->allEntitiesInCache(TRUE);
    $this
      ->assertTrue($entity_cache
      ->isAllEntitiesInCache());
    $this
      ->assertTrue($entity_id_cache
      ->isAllIdsInCache());
  }

  /**
   * Checks whether the cache is properly cleared.
   *
   * @param \Drupal\apigee_edge\Entity\Controller\Cache\EntityCacheInterface $entity_cache
   *   The entity cache implementation.
   * @param \Drupal\apigee_edge\Entity\Controller\Cache\EntityIdCacheInterface $entity_id_cache
   *   The entity id cache implementation.
   */
  protected function assertEmptyCaches(EntityCacheInterface $entity_cache, EntityIdCacheInterface $entity_id_cache) {
    $this
      ->assertEmpty($entity_cache
      ->getEntities());
    $this
      ->assertEmpty($entity_id_cache
      ->getIds());
    $this
      ->assertFalse($entity_cache
      ->isAllEntitiesInCache());
    $this
      ->assertFalse($entity_id_cache
      ->isAllIdsInCache());
  }

  /**
   * Gets a random unique ID.
   */
  protected function getRandomUniqueId() : string {
    return $this->container
      ->get('uuid')
      ->generate();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityControllerCacheUtilsTrait::assertEmptyCaches protected function Checks whether the cache is properly cleared.
EntityControllerCacheUtilsTrait::getRandomUniqueId protected function Gets a random unique ID.
EntityControllerCacheUtilsTrait::saveAllEntitiesAndValidate protected function Saves entities into the entity cache and checks the result.