View source
<?php
namespace Drupal\Tests\Core\Cache;
use Drupal\Core\Cache\BackendChain;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\MemoryBackend;
use Drupal\Tests\UnitTestCase;
class BackendChainImplementationUnitTest extends UnitTestCase {
protected $chain;
protected $firstBackend;
protected $secondBackend;
protected $thirdBackend;
protected function setUp() : void {
parent::setUp();
$this->firstBackend = new MemoryBackend();
$this->secondBackend = new MemoryBackend();
$this->thirdBackend = new MemoryBackend();
$this->firstBackend
->set('t123', 1231);
$this->secondBackend
->set('t123', 1232);
$this->thirdBackend
->set('t123', 1233);
$this->secondBackend
->set('t23', 232);
$this->thirdBackend
->set('t23', 233);
$this->thirdBackend
->set('t3', 33);
$this->chain = new BackendChain();
$this->chain
->appendBackend($this->firstBackend)
->appendBackend($this->secondBackend)
->appendBackend($this->thirdBackend);
}
public function testGet() {
$cached = $this->chain
->get('t123');
$this
->assertNotFalse($cached, 'Got key that is on all backends');
$this
->assertSame(1231, $cached->data, 'Got the key from the backend 1');
$cached = $this->chain
->get('t23');
$this
->assertNotFalse($cached, 'Got key that is on backends 2 and 3');
$this
->assertSame(232, $cached->data, 'Got the key from the backend 2');
$cached = $this->chain
->get('t3');
$this
->assertNotFalse($cached, 'Got key that is on the backend 3');
$this
->assertSame(33, $cached->data, 'Got the key from the backend 3');
}
public function testGetMultiple() {
$cids = [
't123',
't23',
't3',
't4',
];
$ret = $this->chain
->getMultiple($cids);
$this
->assertSame($ret['t123']->data, 1231, 'Got key 123 and value is from the first backend');
$this
->assertSame($ret['t23']->data, 232, 'Got key 23 and value is from the second backend');
$this
->assertSame($ret['t3']->data, 33, 'Got key 3 and value is from the third backend');
$this
->assertArrayNotHasKey('t4', $ret);
$this
->assertNotContains('t123', $cids, "Existing key 123 has been removed from &\$cids");
$this
->assertNotContains('t23', $cids, "Existing key 23 has been removed from &\$cids");
$this
->assertNotContains('t3', $cids, "Existing key 3 has been removed from &\$cids");
$this
->assertContains('t4', $cids, "Non existing key 4 is still in &\$cids");
}
public function testSet() {
$this->chain
->set('test', 123);
$cached = $this->firstBackend
->get('test');
$this
->assertNotFalse($cached, 'Test key is in the first backend');
$this
->assertSame(123, $cached->data, 'Test key has the right value');
$cached = $this->secondBackend
->get('test');
$this
->assertNotFalse($cached, 'Test key is in the second backend');
$this
->assertSame(123, $cached->data, 'Test key has the right value');
$cached = $this->thirdBackend
->get('test');
$this
->assertNotFalse($cached, 'Test key is in the third backend');
$this
->assertSame(123, $cached->data, 'Test key has the right value');
}
public function testDelete() {
$this->chain
->set('test', 5);
$cached = $this->firstBackend
->get('test');
$this
->assertNotFalse($cached, 'Test key has been added to the first backend');
$cached = $this->secondBackend
->get('test');
$this
->assertNotFalse($cached, 'Test key has been added to the first backend');
$cached = $this->thirdBackend
->get('test');
$this
->assertNotFalse($cached, 'Test key has been added to the first backend');
$this->chain
->delete('test');
$cached = $this->firstBackend
->get('test');
$this
->assertFalse($cached, 'Test key is removed from the first backend');
$cached = $this->secondBackend
->get('test');
$this
->assertFalse($cached, 'Test key is removed from the second backend');
$cached = $this->thirdBackend
->get('test');
$this
->assertFalse($cached, 'Test key is removed from the third backend');
}
public function testGetHasPropagated() {
$this->chain
->get('t23');
$cached = $this->firstBackend
->get('t23');
$this
->assertNotFalse($cached, 'Test 2 has been propagated to the first backend');
$this->chain
->get('t3');
$cached = $this->firstBackend
->get('t3');
$this
->assertNotFalse($cached, 'Test 3 has been propagated to the first backend');
$cached = $this->secondBackend
->get('t3');
$this
->assertNotFalse($cached, 'Test 3 has been propagated to the second backend');
}
public function testGetMultipleHasPropagated() {
$cids = [
't3',
't23',
];
$this->chain
->getMultiple($cids);
$cached = $this->firstBackend
->get('t3');
$this
->assertNotFalse($cached, 'Test 3 has been propagated to the first backend');
$this
->assertSame(33, $cached->data, 'And value has been kept');
$cached = $this->secondBackend
->get('t3');
$this
->assertNotFalse($cached, 'Test 3 has been propagated to the second backend');
$this
->assertSame(33, $cached->data, 'And value has been kept');
$cached = $this->firstBackend
->get('t23');
$this
->assertNotFalse($cached, 'Test 2 has been propagated to the first backend');
$this
->assertSame(232, $cached->data, 'And value has been kept');
}
public function testDeleteAllPropagation() {
$this->chain
->set('test1', 1, Cache::PERMANENT);
$this->chain
->set('test2', 3, time() + 1000);
$this->chain
->deleteAll();
$this
->assertFalse($this->firstBackend
->get('test1'), 'First key has been deleted in first backend.');
$this
->assertFalse($this->firstBackend
->get('test2'), 'Second key has been deleted in first backend.');
$this
->assertFalse($this->secondBackend
->get('test1'), 'First key has been deleted in second backend.');
$this
->assertFalse($this->secondBackend
->get('test2'), 'Second key has been deleted in second backend.');
$this
->assertFalse($this->thirdBackend
->get('test1'), 'First key has been deleted in third backend.');
$this
->assertFalse($this->thirdBackend
->get('test2'), 'Second key has been deleted in third backend.');
}
public function testDeleteTagsPropagation() {
$this->chain
->set('test_cid_clear1', 'foo', Cache::PERMANENT, [
'test_tag:2',
]);
$this->chain
->set('test_cid_clear2', 'foo', Cache::PERMANENT, [
'test_tag:2',
]);
$this
->assertNotFalse($this->firstBackend
->get('test_cid_clear1') && $this->firstBackend
->get('test_cid_clear2') && $this->secondBackend
->get('test_cid_clear1') && $this->secondBackend
->get('test_cid_clear2') && $this->thirdBackend
->get('test_cid_clear1') && $this->thirdBackend
->get('test_cid_clear2'), 'Two cache items were created in all backends.');
$this->chain
->invalidateTags([
'test_tag:2',
]);
$this
->assertFalse($this->firstBackend
->get('test_cid_clear1') && $this->firstBackend
->get('test_cid_clear2') && $this->secondBackend
->get('test_cid_clear1') && $this->secondBackend
->get('test_cid_clear2') && $this->thirdBackend
->get('test_cid_clear1') && $this->thirdBackend
->get('test_cid_clear2'), 'Two caches removed from all backends after clearing a cache tag.');
$this->chain
->set('test_cid_clear1', 'foo', Cache::PERMANENT, [
'test_tag:1',
]);
$this->chain
->set('test_cid_clear2', 'foo', Cache::PERMANENT, [
'test_tag:1',
]);
$this
->assertNotFalse($this->firstBackend
->get('test_cid_clear1') && $this->firstBackend
->get('test_cid_clear2') && $this->secondBackend
->get('test_cid_clear1') && $this->secondBackend
->get('test_cid_clear2') && $this->thirdBackend
->get('test_cid_clear1') && $this->thirdBackend
->get('test_cid_clear2'), 'Two cache items were created in all backends.');
$this->chain
->invalidateTags([
'test_tag:1',
]);
$this
->assertFalse($this->firstBackend
->get('test_cid_clear1') && $this->firstBackend
->get('test_cid_clear2') && $this->secondBackend
->get('test_cid_clear1') && $this->secondBackend
->get('test_cid_clear2') && $this->thirdBackend
->get('test_cid_clear1') && $this->thirdBackend
->get('test_cid_clear2'), 'Two caches removed from all backends after clearing a cache tag.');
$this->chain
->set('test_cid_clear1', 'foo', Cache::PERMANENT, [
'test_tag:1',
]);
$this->chain
->set('test_cid_clear2', 'foo', Cache::PERMANENT, [
'test_tag:2',
]);
$this->chain
->set('test_cid_clear3', 'foo', Cache::PERMANENT, [
'test_tag_foo:3',
]);
$this
->assertNotFalse($this->firstBackend
->get('test_cid_clear1') && $this->firstBackend
->get('test_cid_clear2') && $this->firstBackend
->get('test_cid_clear3') && $this->secondBackend
->get('test_cid_clear1') && $this->secondBackend
->get('test_cid_clear2') && $this->secondBackend
->get('test_cid_clear3') && $this->thirdBackend
->get('test_cid_clear1') && $this->thirdBackend
->get('test_cid_clear2') && $this->thirdBackend
->get('test_cid_clear3'), 'Three cached items were created in all backends.');
$this->chain
->invalidateTags([
'test_tag_foo:3',
]);
$this
->assertNotFalse($this->firstBackend
->get('test_cid_clear1') && $this->firstBackend
->get('test_cid_clear2') && $this->secondBackend
->get('test_cid_clear1') && $this->secondBackend
->get('test_cid_clear2') && $this->thirdBackend
->get('test_cid_clear1') && $this->thirdBackend
->get('test_cid_clear2'), 'Cached items not matching the tag were not cleared from any of the backends.');
$this
->assertFalse($this->firstBackend
->get('test_cid_clear3') && $this->secondBackend
->get('test_cid_clear3') && $this->thirdBackend
->get('test_cid_clear3'), 'Cached item matching the tag was removed from all backends.');
}
public function testRemoveBin() {
$chain = new BackendChain();
for ($i = 0; $i < 3; $i++) {
$backend = $this
->createMock('Drupal\\Core\\Cache\\CacheBackendInterface');
$backend
->expects($this
->once())
->method('removeBin');
$chain
->appendBackend($backend);
}
$chain
->removeBin();
}
}