You are here

public function EntityControllerCacheTest::testDeveloperAppEntityControllerCache in Apigee Edge 8

Tests developer app entity controller cache.

File

tests/src/Kernel/EntityControllerCacheTest.php, line 161

Class

EntityControllerCacheTest
Apigee Edge entity controller cache tests.

Namespace

Drupal\Tests\apigee_edge\Kernel

Code

public function testDeveloperAppEntityControllerCache() {
  $developer_app_cache_factory = $this->container
    ->get('apigee_edge.entity.controller.cache.developer_app_cache_factory');

  // Owner of the developer apps. It should be saved into the developer cache
  // because developer app cache tries to load the owner email from the
  // developer cache to reduce API calls.
  $developer = new Developer([
    'developerId' => $this
      ->getRandomUniqueId(),
    'email' => strtolower($this
      ->randomMachineName()) . '@example.com',
  ]);
  $developer_cache = $this->container
    ->get('apigee_edge.controller.cache.developer');
  $developer_cache
    ->saveEntities([
    $developer,
  ]);
  $developer_apps = [];
  for ($i = 0; $i < 2; $i++) {
    $id = $this
      ->getRandomUniqueId();
    $developer_apps[$id] = new DeveloperApp([
      'appId' => $id,
      'name' => $this
        ->getRandomGenerator()
        ->name(),
      'developerId' => $developer
        ->getDeveloperId(),
    ]);
  }
  list($developer_app_1, $developer_app_2) = array_values($developer_apps);
  $cache_by_email = $developer_app_cache_factory
    ->getAppCache($developer
    ->getEmail());
  $cache_by_id = $developer_app_cache_factory
    ->getAppCache($developer
    ->getDeveloperId());
  $cache_by_email
    ->saveEntities([
    $developer_app_1,
  ]);
  $cache_by_id
    ->saveEntities([
    $developer_app_2,
  ]);
  $this
    ->assertSame($developer_apps, $cache_by_email
    ->getEntities());
  $this
    ->assertSame($developer_apps, $cache_by_id
    ->getEntities());

  /** @var \Apigee\Edge\Api\Management\Entity\DeveloperAppInterface $developer_app */
  foreach ($developer_apps as $developer_app) {

    // Load developer app by name.
    $this
      ->assertSame($developer_app, $cache_by_email
      ->getEntity($developer_app
      ->getName()));
    $this
      ->assertSame($developer_app, $cache_by_id
      ->getEntity($developer_app
      ->getName()));
  }

  // Remove developer app by name from developer app cache by email.
  $cache_by_email
    ->removeEntities([
    $developer_app_1
      ->getName(),
  ]);
  $this
    ->assertNull($cache_by_email
    ->getEntity($developer_app_1
    ->getName()));
  $this
    ->assertNull($cache_by_id
    ->getEntity($developer_app_1
    ->getName()));

  // Remove developer app by name from developer app cache by id.
  $cache_by_id
    ->removeEntities([
    $developer_app_2
      ->getName(),
  ]);
  $this
    ->assertNull($cache_by_email
    ->getEntity($developer_app_2
    ->getName()));
  $this
    ->assertNull($cache_by_id
    ->getEntity($developer_app_2
    ->getName()));
  $this
    ->assertEmpty($cache_by_email
    ->getEntities());
  $this
    ->assertEmpty($cache_by_id
    ->getEntities());
}