View source
<?php
namespace Drupal\Tests\Core\Cache;
use Drupal\Core\Cache\Cache;
use Drupal\Tests\UnitTestCase;
class CacheCollectorTest extends UnitTestCase {
protected $cacheBackend;
protected $cacheTagsInvalidator;
protected $lock;
protected $cid;
protected $collector;
protected function setUp() {
$this->cacheBackend = $this
->createMock('Drupal\\Core\\Cache\\CacheBackendInterface');
$this->cacheTagsInvalidator = $this
->createMock('Drupal\\Core\\Cache\\CacheTagsInvalidatorInterface');
$this->lock = $this
->createMock('Drupal\\Core\\Lock\\LockBackendInterface');
$this->cid = $this
->randomMachineName();
$this->collector = new CacheCollectorHelper($this->cid, $this->cacheBackend, $this->lock);
$this
->getContainerWithCacheTagsInvalidator($this->cacheTagsInvalidator);
}
public function testResolveCacheMiss() {
$key = $this
->randomMachineName();
$value = $this
->randomMachineName();
$this->collector
->setCacheMissData($key, $value);
$this
->assertEquals($value, $this->collector
->get($key));
}
public function testSetAndGet() {
$key = $this
->randomMachineName();
$value = $this
->randomMachineName();
$this
->assertNull($this->collector
->get($key));
$this->collector
->set($key, $value);
$this
->assertTrue($this->collector
->has($key));
$this
->assertEquals($value, $this->collector
->get($key));
}
public function testSetAndGetNull() {
$key = $this
->randomMachineName();
$value = NULL;
$this->cacheBackend
->expects($this
->once())
->method('invalidate')
->with($this->cid);
$this->collector
->set($key, $value);
$this
->assertTrue($this->collector
->has($key));
$this
->assertEquals($value, $this->collector
->get($key));
$non_existing_key = $this
->randomMachineName(7);
$this->collector
->get($non_existing_key);
$this
->assertFalse($this->collector
->has($non_existing_key));
}
public function testGetFromCache() {
$key = $this
->randomMachineName();
$value = $this
->randomMachineName();
$cache = (object) [
'data' => [
$key => $value,
],
'created' => (int) $_SERVER['REQUEST_TIME'],
];
$this->cacheBackend
->expects($this
->once())
->method('get')
->with($this->cid)
->will($this
->returnValue($cache));
$this
->assertTrue($this->collector
->has($key));
$this
->assertEquals($value, $this->collector
->get($key));
$this
->assertEquals(0, $this->collector
->getCacheMisses());
}
public function testDelete() {
$key = $this
->randomMachineName();
$value = $this
->randomMachineName();
$this
->assertNull($this->collector
->get($key));
$this->collector
->set($key, $value);
$this
->assertTrue($this->collector
->has($key));
$this
->assertEquals($value, $this->collector
->get($key));
$this->cacheBackend
->expects($this
->once())
->method('invalidate')
->with($this->cid);
$this->collector
->delete($key);
$this
->assertFalse($this->collector
->has($key));
$this
->assertEquals(NULL, $this->collector
->get($key));
}
public function testUpdateCacheNoChanges() {
$this->lock
->expects($this
->never())
->method('acquire');
$this->cacheBackend
->expects($this
->never())
->method('set');
$this->collector
->destruct();
}
public function testUpdateCache() {
$key = $this
->randomMachineName();
$value = $this
->randomMachineName();
$this->collector
->setCacheMissData($key, $value);
$this->collector
->get($key);
$this->lock
->expects($this
->once())
->method('acquire')
->with($this->cid . ':Drupal\\Core\\Cache\\CacheCollector')
->will($this
->returnValue(TRUE));
$this->cacheBackend
->expects($this
->once())
->method('get')
->with($this->cid, FALSE);
$this->cacheBackend
->expects($this
->once())
->method('set')
->with($this->cid, [
$key => $value,
], Cache::PERMANENT, []);
$this->lock
->expects($this
->once())
->method('release')
->with($this->cid . ':Drupal\\Core\\Cache\\CacheCollector');
$this->collector
->destruct();
}
public function testUpdateCacheLockFail() {
$key = $this
->randomMachineName();
$value = $this
->randomMachineName();
$this->collector
->setCacheMissData($key, $value);
$this->collector
->get($key);
$this->lock
->expects($this
->once())
->method('acquire')
->with($this->cid . ':Drupal\\Core\\Cache\\CacheCollector')
->will($this
->returnValue(FALSE));
$this->cacheBackend
->expects($this
->never())
->method('set');
$this->collector
->destruct();
}
public function testUpdateCacheInvalidatedConflict() {
$key = $this
->randomMachineName();
$value = $this
->randomMachineName();
$cache = (object) [
'data' => [
$key => $value,
],
'created' => (int) $_SERVER['REQUEST_TIME'],
];
$this->cacheBackend
->expects($this
->at(0))
->method('get')
->with($this->cid)
->will($this
->returnValue($cache));
$this->cacheBackend
->expects($this
->at(1))
->method('invalidate')
->with($this->cid);
$this->collector
->set($key, 'new value');
$this->lock
->expects($this
->once())
->method('acquire')
->with($this->cid . ':Drupal\\Core\\Cache\\CacheCollector')
->will($this
->returnValue(TRUE));
$cache = (object) [
'data' => [
$key => $value,
],
'created' => (int) $_SERVER['REQUEST_TIME'] + 1,
];
$this->cacheBackend
->expects($this
->at(0))
->method('get')
->with($this->cid)
->will($this
->returnValue($cache));
$this->cacheBackend
->expects($this
->once())
->method('delete')
->with($this->cid);
$this->lock
->expects($this
->once())
->method('release')
->with($this->cid . ':Drupal\\Core\\Cache\\CacheCollector');
$this->collector
->destruct();
}
public function testUpdateCacheMerge() {
$key = $this
->randomMachineName();
$value = $this
->randomMachineName();
$this->collector
->setCacheMissData($key, $value);
$this->collector
->get($key);
$this->lock
->expects($this
->once())
->method('acquire')
->with($this->cid . ':Drupal\\Core\\Cache\\CacheCollector')
->will($this
->returnValue(TRUE));
$cache = (object) [
'data' => [
'other key' => 'other value',
],
'created' => (int) $_SERVER['REQUEST_TIME'] + 1,
];
$this->cacheBackend
->expects($this
->at(0))
->method('get')
->with($this->cid)
->will($this
->returnValue($cache));
$this->cacheBackend
->expects($this
->once())
->method('set')
->with($this->cid, [
'other key' => 'other value',
$key => $value,
], Cache::PERMANENT, []);
$this->lock
->expects($this
->once())
->method('release')
->with($this->cid . ':Drupal\\Core\\Cache\\CacheCollector');
$this->collector
->destruct();
}
public function testUpdateCacheDelete() {
$key = $this
->randomMachineName();
$value = $this
->randomMachineName();
$cache = (object) [
'data' => [
$key => $value,
],
'created' => (int) $_SERVER['REQUEST_TIME'],
];
$this->cacheBackend
->expects($this
->at(0))
->method('get')
->with($this->cid)
->will($this
->returnValue($cache));
$this->collector
->delete($key);
$this->lock
->expects($this
->once())
->method('acquire')
->with($this->cid . ':Drupal\\Core\\Cache\\CacheCollector')
->will($this
->returnValue(TRUE));
$this->cacheBackend
->expects($this
->at(0))
->method('get')
->with($this->cid, TRUE)
->will($this
->returnValue($cache));
$this->cacheBackend
->expects($this
->once())
->method('set')
->with($this->cid, [], Cache::PERMANENT, []);
$this->lock
->expects($this
->once())
->method('release')
->with($this->cid . ':Drupal\\Core\\Cache\\CacheCollector');
$this->collector
->destruct();
}
public function testUpdateCacheReset() {
$key = $this
->randomMachineName();
$value = $this
->randomMachineName();
$this->collector
->setCacheMissData($key, $value);
$this
->assertEquals($value, $this->collector
->get($key));
$this
->assertEquals($value, $this->collector
->get($key));
$this
->assertEquals(1, $this->collector
->getCacheMisses());
$this->collector
->reset();
$this
->assertEquals($value, $this->collector
->get($key));
$this
->assertEquals(2, $this->collector
->getCacheMisses());
}
public function testUpdateCacheClear() {
$key = $this
->randomMachineName();
$value = $this
->randomMachineName();
$this->collector
->setCacheMissData($key, $value);
$this
->assertEquals($value, $this->collector
->get($key));
$this
->assertEquals($value, $this->collector
->get($key));
$this
->assertEquals(1, $this->collector
->getCacheMisses());
$this->cacheBackend
->expects($this
->once())
->method('delete')
->with($this->cid);
$this->cacheTagsInvalidator
->expects($this
->never())
->method('invalidateTags');
$this->collector
->clear();
$this
->assertEquals($value, $this->collector
->get($key));
$this
->assertEquals(2, $this->collector
->getCacheMisses());
}
public function testUpdateCacheClearTags() {
$key = $this
->randomMachineName();
$value = $this
->randomMachineName();
$tags = [
$this
->randomMachineName(),
];
$this->collector = new CacheCollectorHelper($this->cid, $this->cacheBackend, $this->lock, $tags);
$this->collector
->setCacheMissData($key, $value);
$this
->assertEquals($value, $this->collector
->get($key));
$this
->assertEquals($value, $this->collector
->get($key));
$this
->assertEquals(1, $this->collector
->getCacheMisses());
$this->cacheBackend
->expects($this
->never())
->method('delete');
$this->cacheTagsInvalidator
->expects($this
->once())
->method('invalidateTags')
->with($tags);
$this->collector
->clear();
$this
->assertEquals($value, $this->collector
->get($key));
$this
->assertEquals(2, $this->collector
->getCacheMisses());
}
}