View source
<?php
namespace Drupal\Tests\apigee_edge\Kernel;
use Apigee\Edge\Api\Management\Entity\CompanyApp;
use Apigee\Edge\Api\Management\Entity\Developer;
use Apigee\Edge\Api\Management\Entity\DeveloperApp;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\apigee_edge\Traits\EntityControllerCacheUtilsTrait;
class EntityControllerCacheTest extends KernelTestBase {
use EntityControllerCacheUtilsTrait;
protected static $mock_api_client_ready = TRUE;
protected static $modules = [
'system',
'apigee_edge',
'key',
'user',
];
protected function setUp() {
parent::setUp();
$this
->installEntitySchema('user');
}
public function testDeveloperEntityControllerCache() {
$developer_cache = $this->container
->get('apigee_edge.controller.cache.developer');
$developer_id_cache = $this->container
->get('apigee_edge.controller.cache.developer_ids');
$developers = [];
for ($i = 0; $i < 3; $i++) {
$id = $this
->getRandomUniqueId();
$developers[$id] = new Developer([
'developerId' => $id,
'email' => strtolower($this
->randomMachineName()) . '@example.com',
]);
}
$this
->saveAllEntitiesAndValidate($developers, $developer_cache, $developer_id_cache);
foreach ($developers as $developer) {
$this
->assertSame($developer, $developer_cache
->getEntity($developer
->getEmail()));
$this
->assertSame($developer, $developer_cache
->getEntity($developer
->getDeveloperId()));
$this
->assertContains($developer
->getEmail(), $developer_id_cache
->getIds());
$developer_cache
->removeEntities([
$developer
->getDeveloperId(),
$this
->getRandomGenerator()
->string(),
]);
$this
->assertNull($developer_cache
->getEntity($developer
->getEmail()));
$this
->assertNull($developer_cache
->getEntity($developer
->getDeveloperId()));
$this
->assertNotContains($developer
->getEmail(), $developer_id_cache
->getIds());
}
$this
->assertEmptyCaches($developer_cache, $developer_id_cache);
}
public function testAppEntityControllerCache() {
$app_cache = $this->container
->get('apigee_edge.controller.cache.apps');
$app_id_cache = $this->container
->get('apigee_edge.controller.cache.app_ids');
$developer_apps = [];
$parent_id = $this
->getRandomUniqueId();
for ($i = 0; $i < 2; $i++) {
$id = $this
->getRandomUniqueId();
$developer_apps[$id] = new DeveloperApp([
'appId' => $id,
'name' => $this
->getRandomGenerator()
->name(),
'developerId' => $parent_id,
]);
}
$company_apps = [];
$parent_id = $this
->getRandomUniqueId();
for ($i = 0; $i < 2; $i++) {
$id = $this
->getRandomUniqueId();
$company_apps[$id] = new CompanyApp([
'appId' => $id,
'name' => $this
->getRandomGenerator()
->name(),
'companyName' => $parent_id,
]);
}
$apps = $developer_apps + $company_apps;
$this
->saveAllEntitiesAndValidate($apps, $app_cache, $app_id_cache);
foreach ($apps as $app) {
$this
->assertSame($app, $app_cache
->getEntity($app
->getAppId()));
$this
->assertContains($app, $app_cache
->getAppsByOwner($app_cache
->getAppOwner($app)));
$this
->assertContains($app
->getAppId(), $app_id_cache
->getIds());
}
$app_cache
->removeAppsByOwner(reset($developer_apps)
->getDeveloperId());
$app_cache
->removeAppsByOwner(reset($company_apps)
->getCompanyName());
foreach ($apps as $app) {
$this
->assertNull($app_cache
->getEntity($app
->getAppId()));
$this
->assertNull($app_cache
->getAppsByOwner($app_cache
->getAppOwner($app)));
$this
->assertNotContains($app
->getAppId(), $app_id_cache
->getIds());
}
$this
->assertEmptyCaches($app_cache, $app_id_cache);
}
public function testDeveloperAppEntityControllerCache() {
$developer_app_cache_factory = $this->container
->get('apigee_edge.entity.controller.cache.developer_app_cache_factory');
$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());
foreach ($developer_apps as $developer_app) {
$this
->assertSame($developer_app, $cache_by_email
->getEntity($developer_app
->getName()));
$this
->assertSame($developer_app, $cache_by_id
->getEntity($developer_app
->getName()));
}
$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()));
$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());
}
}