diff --git a/core/tests/Drupal/Tests/Core/Cache/InconsistentBackendTest.php b/core/tests/Drupal/Tests/Core/Cache/InconsistentBackendTest.php index f17d80b..822dedd 100644 --- a/core/tests/Drupal/Tests/Core/Cache/InconsistentBackendTest.php +++ b/core/tests/Drupal/Tests/Core/Cache/InconsistentBackendTest.php @@ -1,69 +1,156 @@ '', - 'description' => '', - 'group' => 'Cache' + 'name' => 'Inconsistent Cache Test', + 'description' => 'Tests the inconsistent cache', + 'group' => 'Cache', ); } + /** + * {@inheritdoc} + */ public function setUp() { - $this->consistent_cache = new MemoryBackend("foo"); - $this->inconsistent_cache = new MemoryBackend("foo"); + $this->consistentCache = new MemoryBackend("foo"); + $this->inconsistentCache = new MemoryBackend("foo"); $this->bin = "somebin"; } + /** + * Tests getting data from the inconsistent cache. + */ public function testGetFromInconsistentCache() { - $state = $this->getMock("Drupal\Core\State\StateInterface"); + $state = $this->getMock('Drupal\Core\State\StateInterface'); $state->expects($this->once()) ->method("get") ->will($this->returnValue(1234)); $inconsistent_backend = new InconsistentBackend( - $this->consistent_cache, - $this->inconsistent_cache, + $this->consistentCache, + $this->inconsistentCache, $this->bin, $state ); - $this->consistent_cache->set("foo", "baz"); + $this->consistentCache->set("foo", "baz"); $inconsistent_backend->set("foo", "bar"); $this->assertEquals("bar", $inconsistent_backend->get("foo")->data); } + /** + * Tests getting data from the inconsistent cache doesn't hit the backend. + */ + public function testGetDoesntHitConsistentBackend() { + $write_timestamp = REQUEST_TIME - 99999; + $state = $this->getMock('Drupal\Core\State\StateInterface'); + $state->expects($this->once()) + ->method("get") + ->will($this->returnValue($write_timestamp)); + + // Test outdated cache item. + $inconsistent_cache = new MemoryBackend('bar'); + $inconsistent_cache->set('foo', 'baz'); + + // The test cache item. + $item = (object) array('cid' => 'foo', 'data' => 'baz', 'created' => 22222); + + $consistent_cache = $this->getMock('Drupal\Core\Cache\CacheBackendInterface'); + $consistent_cache->expects($this->never()) + ->method('getMultiple'); + + $inconsistent_backend = new InconsistentBackend( + $consistent_cache, + $inconsistent_cache, + $this->bin, + $state + ); + + $this->assertEquals("baz", $inconsistent_backend->get("foo")->data); + } + + /** + * Tests a cache miss gets data from the inconsistent cache. + */ public function testFallThroughToConsistentCache() { - $state = $this->getMock("Drupal\Core\State\StateInterface"); + + $state = $this->getMock('Drupal\Core\State\StateInterface'); $state->expects($this->once()) ->method("get") ->will($this->returnValue(time() + 99999)); - $inconsistent_cache = $this->getMock("Drupal\Core\Cache\MemoryBackend", array("set"), array("foo")); - $inconsistent_cache->expects($this->exactly(2)) + // Inconsistent cache misses. + $inconsistent_cache = $this->getMock('Drupal\Core\Cache\CacheBackendInterface'); + $inconsistent_cache->expects($this->once()) + ->method('getMultiple')->with(array('foo')) + ->will($this->returnValue(array())); + + + // The test cache item. + $item = (object) array('cid' => 'foo', 'data' => 'baz'); + + $consistent_cache = $this->getMock('Drupal\Core\Cache\CacheBackendInterface'); + $consistent_cache->expects($this->once()) + ->method('getMultiple')->with(array('foo')) + ->will($this->returnValue(array($item))); + + // Inconsistent cache is updated. + $inconsistent_cache->expects($this->once()) ->method("set"); $inconsistent_backend = new InconsistentBackend( - $this->consistent_cache, + $consistent_cache, $inconsistent_cache, $this->bin, $state ); - $inconsistent_cache->set("foo", "bar"); - $this->consistent_cache->set("foo", "baz"); $this->assertEquals("baz", $inconsistent_backend->get("foo")->data); + } }