PermanentDatabaseBackendTest.php in Permanent Cache Bin 8
File
tests/src/Kernel/PermanentDatabaseBackendTest.php
View source
<?php
namespace Drupal\Tests\pcb\Kernel;
use Drupal\Core\Cache\Cache;
use Drupal\KernelTests\Core\Cache\GenericCacheBackendUnitTestBase;
class PermanentDatabaseBackendTest extends GenericCacheBackendUnitTestBase {
public static $modules = [
'system',
'pcb',
];
protected function createCacheBackend($bin) {
$cache = \Drupal::service('cache.backend.permanent_database')
->get($bin);
return $cache;
}
public function testDeleteAll() {
$backend_a = $this
->getCacheBackend();
$backend_b = $this
->getCacheBackend('bootstrap');
$backend_a
->set('test1', 1, Cache::PERMANENT);
$backend_a
->set('test2', 3, time() + 1000);
$backend_b
->set('test3', 4, Cache::PERMANENT);
$backend_a
->deleteAll();
$this
->assertSame(1, $backend_a
->get('test1')->data ?? NULL, 'First key has been deleted.');
$this
->assertSame(3, $backend_a
->get('test2')->data ?? NULL, 'Second key has been deleted.');
$this
->assertSame(4, $backend_b
->get('test3')->data ?? NULL, 'Item in other bin is preserved.');
}
public function testSetGet() {
$backend = $this
->getCacheBackend();
$cid = 'test';
$cache_value = 'This does not matter.';
$this
->assertSame(FALSE, $backend
->get($cid), "Backend does not contain data for the used cache id.");
$backend
->set($cid, $cache_value);
$cached = $backend
->get($cid);
$this
->assertEquals($cache_value, $cached->data, 'Backend returned the proper value before the normal deleting process.');
$backend
->deleteAll();
$cached = $backend
->get($cid);
$this
->assertFalse(!is_object($cached), 'Backend did not provide result after the normal deleting process.');
$this
->assertEquals($cache_value, $cached->data, 'Backend returned the proper value after the normal deleting process.');
$backend
->deleteAllPermanent();
$cached = $backend
->get($cid);
$this
->assertFalse(is_object($cached), 'Backend returned result after the permanent cache was deleted.');
}
}