DatabaseBackendTest.php in Drupal 10
File
core/tests/Drupal/KernelTests/Core/Cache/DatabaseBackendTest.php
View source
<?php
namespace Drupal\KernelTests\Core\Cache;
use Drupal\Core\Cache\DatabaseBackend;
class DatabaseBackendTest extends GenericCacheBackendUnitTestBase {
protected static $maxRows = 100;
protected static $modules = [
'system',
];
protected function createCacheBackend($bin) {
return new DatabaseBackend($this->container
->get('database'), $this->container
->get('cache_tags.invalidator.checksum'), $bin, static::$maxRows);
}
public function testSetGet() {
parent::testSetGet();
$backend = $this
->getCacheBackend();
$cid_long = str_repeat('愛€', 500);
$cached_value_long = $this
->randomMachineName();
$backend
->set($cid_long, $cached_value_long);
$this
->assertSame($cached_value_long, $backend
->get($cid_long)->data, "Backend contains the correct value for long, non-ASCII cache id.");
$cid_short = '愛1€';
$cached_value_short = $this
->randomMachineName();
$backend
->set($cid_short, $cached_value_short);
$this
->assertSame($cached_value_short, $backend
->get($cid_short)->data, "Backend contains the correct value for short, non-ASCII cache id.");
}
public function testGarbageCollection() {
$backend = $this
->getCacheBackend();
$max_rows = static::$maxRows;
$this
->assertSame(0, (int) $this
->getNumRows());
for ($i = 0; $i < $max_rows; $i++) {
usleep(1000);
$backend
->set("test{$i}", $i);
}
$this
->assertSame($max_rows, $this
->getNumRows());
$backend
->garbageCollection();
$this
->assertSame($max_rows, $this
->getNumRows());
$backend
->set('test' . ($max_rows + 1), $max_rows + 1);
$this
->assertSame($max_rows + 1, $this
->getNumRows());
$backend
->garbageCollection();
$this
->assertSame($max_rows, $this
->getNumRows());
$this
->assertFalse($backend
->get('test0'));
}
protected function getNumRows() {
$table = 'cache_' . $this->testBin;
$connection = $this->container
->get('database');
$query = $connection
->select($table);
$query
->addExpression('COUNT([cid])', 'cid');
return (int) $query
->execute()
->fetchField();
}
public function testCacheTagsInvalidatorChecksumIsBackendOverridable() {
$definition = $this->container
->getDefinition('cache_tags.invalidator.checksum');
$this
->assertTrue($definition
->hasTag('backend_overridable'));
}
public function testCacheBackendDatabaseIsBackendOverridable() {
$definition = $this->container
->getDefinition('cache.backend.database');
$this
->assertTrue($definition
->hasTag('backend_overridable'));
}
}