52f18517466a38e78b4a7238d44cfe167798ca79
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Cache / ChainedFastBackendTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\Cache;
4
5 use Drupal\Core\Cache\ChainedFastBackend;
6 use Drupal\Core\Cache\MemoryBackend;
7 use Drupal\Tests\UnitTestCase;
8
9 /**
10  * @coversDefaultClass \Drupal\Core\Cache\ChainedFastBackend
11  * @group Cache
12  */
13 class ChainedFastBackendTest extends UnitTestCase {
14
15   /**
16    * The consistent cache backend.
17    *
18    * @var \Drupal\Core\Cache\CacheBackendInterface
19    */
20   protected $consistentCache;
21
22   /**
23    * The fast cache backend.
24    *
25    * @var \Drupal\Core\Cache\CacheBackendInterface
26    */
27   protected $fastCache;
28
29   /**
30    * The cache bin.
31    *
32    * @var string
33    */
34   protected $bin;
35
36   /**
37    * Tests a get() on the fast backend, with no hit on the consistent backend.
38    */
39   public function testGetDoesntHitConsistentBackend() {
40     $consistent_cache = $this->getMock('Drupal\Core\Cache\CacheBackendInterface');
41     $timestamp_cid = ChainedFastBackend::LAST_WRITE_TIMESTAMP_PREFIX . 'cache_foo';
42     // Use the request time because that is what we will be comparing against.
43     $timestamp_item = (object) ['cid' => $timestamp_cid, 'data' => (int) $_SERVER['REQUEST_TIME'] - 60];
44     $consistent_cache->expects($this->once())
45       ->method('get')->with($timestamp_cid)
46       ->will($this->returnValue($timestamp_item));
47     $consistent_cache->expects($this->never())
48       ->method('getMultiple');
49
50     $fast_cache = new MemoryBackend();
51     $fast_cache->set('foo', 'baz');
52
53     $chained_fast_backend = new ChainedFastBackend(
54       $consistent_cache,
55       $fast_cache,
56       'foo'
57     );
58     $this->assertEquals('baz', $chained_fast_backend->get('foo')->data);
59   }
60
61   /**
62    * Tests a fast cache miss gets data from the consistent cache backend.
63    */
64   public function testFallThroughToConsistentCache() {
65     $timestamp_item = (object) [
66       'cid' => ChainedFastBackend::LAST_WRITE_TIMESTAMP_PREFIX . 'cache_foo',
67       'data' => time() + 60, // Time travel is easy.
68     ];
69     $cache_item = (object) [
70       'cid' => 'foo',
71       'data' => 'baz',
72       'created' => time(),
73       'expire' => time() + 3600,
74       'tags' => ['tag'],
75     ];
76
77     $consistent_cache = $this->getMock('Drupal\Core\Cache\CacheBackendInterface');
78     $fast_cache = $this->getMock('Drupal\Core\Cache\CacheBackendInterface');
79
80     // We should get a call for the timestamp on the consistent backend.
81     $consistent_cache->expects($this->once())
82       ->method('get')
83       ->with($timestamp_item->cid)
84       ->will($this->returnValue($timestamp_item));
85
86     // We should get a call for the cache item on the consistent backend.
87     $consistent_cache->expects($this->once())
88       ->method('getMultiple')
89       ->with([$cache_item->cid])
90       ->will($this->returnValue([$cache_item->cid => $cache_item]));
91
92     // We should get a call for the cache item on the fast backend.
93     $fast_cache->expects($this->once())
94       ->method('getMultiple')
95       ->with([$cache_item->cid])
96       ->will($this->returnValue([$cache_item->cid => $cache_item]));
97
98     // We should get a call to set the cache item on the fast backend.
99     $fast_cache->expects($this->once())
100       ->method('set')
101       ->with($cache_item->cid, $cache_item->data, $cache_item->expire);
102
103     $chained_fast_backend = new ChainedFastBackend(
104       $consistent_cache,
105       $fast_cache,
106       'foo'
107     );
108     $this->assertEquals('baz', $chained_fast_backend->get('foo')->data);
109   }
110
111 }