3 namespace Drupal\Tests\Component\FileCache;
5 use Drupal\Component\FileCache\FileCache;
6 use PHPUnit\Framework\TestCase;
9 * @coversDefaultClass \Drupal\Component\FileCache\FileCache
12 class FileCacheTest extends TestCase {
15 * FileCache object used for the tests.
17 * @var \Drupal\Component\FileCache\FileCacheInterface
22 * Static FileCache object used for verification of tests.
24 * @var \Drupal\Component\FileCache\FileCacheBackendInterface
26 protected $staticFileCache;
31 protected function setUp() {
34 $this->fileCache = new FileCache('prefix', 'test', '\Drupal\Tests\Component\FileCache\StaticFileCacheBackend', ['bin' => 'llama']);
35 $this->staticFileCache = new StaticFileCacheBackend(['bin' => 'llama']);
40 * @covers ::__construct
42 public function testGet() {
44 $result = $this->fileCache->get(__DIR__ . '/Fixtures/no-llama-42.yml');
45 $this->assertNull($result);
48 $filename = __DIR__ . '/Fixtures/llama-42.txt';
49 $realpath = realpath($filename);
50 $cid = 'prefix:test:' . $realpath;
52 'mtime' => filemtime($realpath),
53 'filepath' => $realpath,
57 $this->staticFileCache->store($cid, $data);
59 $result = $this->fileCache->get($filename);
60 $this->assertEquals(42, $result);
62 // Cleanup static caches.
63 $this->fileCache->delete($filename);
67 * @covers ::getMultiple
69 public function testGetMultiple() {
71 $result = $this->fileCache->getMultiple([__DIR__ . '/Fixtures/no-llama-42.yml']);
72 $this->assertEmpty($result);
75 $filename = __DIR__ . '/Fixtures/llama-42.txt';
76 $realpath = realpath($filename);
77 $cid = 'prefix:test:' . $realpath;
79 'mtime' => filemtime($realpath),
80 'filepath' => $realpath,
84 $this->staticFileCache->store($cid, $data);
86 $result = $this->fileCache->getMultiple([$filename]);
87 $this->assertEquals([$filename => 42], $result);
89 // Test a static cache hit.
90 $file2 = __DIR__ . '/Fixtures/llama-23.txt';
91 $this->fileCache->set($file2, 23);
93 $result = $this->fileCache->getMultiple([$filename, $file2]);
94 $this->assertEquals([$filename => 42, $file2 => 23], $result);
96 // Cleanup static caches.
97 $this->fileCache->delete($filename);
98 $this->fileCache->delete($file2);
104 public function testSet() {
105 $filename = __DIR__ . '/Fixtures/llama-23.txt';
106 $realpath = realpath($filename);
107 $cid = 'prefix:test:' . $realpath;
109 'mtime' => filemtime($realpath),
110 'filepath' => $realpath,
114 $this->fileCache->set($filename, 23);
115 $result = $this->staticFileCache->fetch([$cid]);
116 $this->assertEquals([$cid => $data], $result);
118 // Cleanup static caches.
119 $this->fileCache->delete($filename);
125 public function testDelete() {
126 $filename = __DIR__ . '/Fixtures/llama-23.txt';
127 $realpath = realpath($filename);
128 $cid = 'prefix:test:' . $realpath;
130 $this->fileCache->set($filename, 23);
132 // Ensure data is removed after deletion.
133 $this->fileCache->delete($filename);
135 $result = $this->staticFileCache->fetch([$cid]);
136 $this->assertEquals([], $result);
138 $result = $this->fileCache->get($filename);
139 $this->assertNull($result);