View source
<?php
namespace Drupal\Tests\Component\FileCache;
use Drupal\Component\FileCache\FileCache;
use PHPUnit\Framework\TestCase;
class FileCacheTest extends TestCase {
protected $fileCache;
protected $staticFileCache;
protected function setUp() : void {
parent::setUp();
$this->fileCache = new FileCache('prefix', 'test', '\\Drupal\\Tests\\Component\\FileCache\\StaticFileCacheBackend', [
'bin' => 'llama',
]);
$this->staticFileCache = new StaticFileCacheBackend([
'bin' => 'llama',
]);
}
public function testGet() {
$result = $this->fileCache
->get(__DIR__ . '/Fixtures/no-llama-42.yml');
$this
->assertNull($result);
$filename = __DIR__ . '/Fixtures/llama-42.txt';
$realpath = realpath($filename);
$cid = 'prefix:test:' . $realpath;
$data = [
'mtime' => filemtime($realpath),
'filepath' => $realpath,
'data' => 42,
];
$this->staticFileCache
->store($cid, $data);
$result = $this->fileCache
->get($filename);
$this
->assertEquals(42, $result);
$this->fileCache
->delete($filename);
}
public function testGetMultiple() {
$result = $this->fileCache
->getMultiple([
__DIR__ . '/Fixtures/no-llama-42.yml',
]);
$this
->assertEmpty($result);
$filename = __DIR__ . '/Fixtures/llama-42.txt';
$realpath = realpath($filename);
$cid = 'prefix:test:' . $realpath;
$data = [
'mtime' => filemtime($realpath),
'filepath' => $realpath,
'data' => 42,
];
$this->staticFileCache
->store($cid, $data);
$result = $this->fileCache
->getMultiple([
$filename,
]);
$this
->assertEquals([
$filename => 42,
], $result);
$file2 = __DIR__ . '/Fixtures/llama-23.txt';
$this->fileCache
->set($file2, 23);
$result = $this->fileCache
->getMultiple([
$filename,
$file2,
]);
$this
->assertEquals([
$filename => 42,
$file2 => 23,
], $result);
$this->fileCache
->delete($filename);
$this->fileCache
->delete($file2);
}
public function testSet() {
$filename = __DIR__ . '/Fixtures/llama-23.txt';
$realpath = realpath($filename);
$cid = 'prefix:test:' . $realpath;
$data = [
'mtime' => filemtime($realpath),
'filepath' => $realpath,
'data' => 23,
];
$this->fileCache
->set($filename, 23);
$result = $this->staticFileCache
->fetch([
$cid,
]);
$this
->assertEquals([
$cid => $data,
], $result);
$this->fileCache
->delete($filename);
}
public function testDelete() {
$filename = __DIR__ . '/Fixtures/llama-23.txt';
$realpath = realpath($filename);
$cid = 'prefix:test:' . $realpath;
$this->fileCache
->set($filename, 23);
$this->fileCache
->delete($filename);
$result = $this->staticFileCache
->fetch([
$cid,
]);
$this
->assertEquals([], $result);
$result = $this->fileCache
->get($filename);
$this
->assertNull($result);
}
}