Completed
Push — master ( 9fa596...e14f7e )
by Marco
02:55
created

ChainCacheTest   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 155
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 6
dl 0
loc 155
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A _getCacheDriver() 0 4 1
A testGetStats() 0 7 1
A testOnlyFetchFirstOne() 0 12 1
A testOnlyFetchFirstCompleteSet() 0 15 1
A testFetchPropagateToFastestCache() 0 16 1
A testFetchMultiplePropagateToFastestCache() 0 18 1
A testNamespaceIsPropagatedToAllProviders() 0 11 1
A testDeleteToAllProviders() 0 11 1
A testDeleteMultipleToAllProviders() 0 17 1
A testFlushToAllProviders() 0 11 1
A testChainCacheAcceptsArrayIteratorsAsDependency() 0 10 1
A isSharedStorage() 0 4 1
1
<?php
2
3
namespace Doctrine\Tests\Common\Cache;
4
5
use Doctrine\Common\Cache\ArrayCache;
6
use Doctrine\Common\Cache\CacheProvider;
7
use Doctrine\Common\Cache\ChainCache;
8
9
class ChainCacheTest extends CacheTest
10
{
11
    protected function _getCacheDriver()
12
    {
13
        return new ChainCache([new ArrayCache()]);
14
    }
15
16
    public function testGetStats()
17
    {
18
        $cache = $this->_getCacheDriver();
19
        $stats = $cache->getStats();
20
21
        $this->assertInternalType('array', $stats);
22
    }
23
24
    public function testOnlyFetchFirstOne()
25
    {
26
        $cache1 = new ArrayCache();
27
        $cache2 = $this->getMockForAbstractClass('Doctrine\Common\Cache\CacheProvider');
28
29
        $cache2->expects($this->never())->method('doFetch');
30
31
        $chainCache = new ChainCache([$cache1, $cache2]);
32
        $chainCache->save('id', 'bar');
33
34
        $this->assertEquals('bar', $chainCache->fetch('id'));
35
    }
36
37
    public function testOnlyFetchFirstCompleteSet()
38
    {
39
        $cache1 = new ArrayCache();
40
        $cache2 = $this
41
            ->getMockBuilder(CacheProvider::class)
42
            ->setMethods(['doFetchMultiple'])
43
            ->getMockForAbstractClass();
44
45
        $cache2->expects($this->never())->method('doFetchMultiple');
46
47
        $chainCache = new ChainCache([$cache1, $cache2]);
48
        $chainCache->saveMultiple(['bar' => 'Bar', 'foo' => 'Foo']);
49
50
        $this->assertEquals(['bar' => 'Bar', 'foo' => 'Foo'], $chainCache->fetchMultiple(['bar', 'foo']));
51
    }
52
53
    public function testFetchPropagateToFastestCache()
54
    {
55
        $cache1 = new ArrayCache();
56
        $cache2 = new ArrayCache();
57
58
        $cache2->save('bar', 'value');
59
60
        $chainCache = new ChainCache([$cache1, $cache2]);
61
62
        $this->assertFalse($cache1->contains('bar'));
63
64
        $result = $chainCache->fetch('bar');
65
66
        $this->assertEquals('value', $result);
67
        $this->assertTrue($cache1->contains('bar'));
68
    }
69
70
    public function testFetchMultiplePropagateToFastestCache()
71
    {
72
        $cache1 = new ArrayCache();
73
        $cache2 = new ArrayCache();
74
75
        $cache1->save('bar', 'Bar');
76
        $cache2->saveMultiple(['bar' => 'Bar', 'foo' => 'Foo']);
77
78
        $chainCache = new ChainCache([$cache1, $cache2]);
79
80
        $this->assertTrue($cache1->contains('bar'));
81
        $this->assertFalse($cache1->contains('foo'));
82
83
        $result = $chainCache->fetchMultiple(['bar', 'foo']);
84
85
        $this->assertEquals(['bar' => 'Bar', 'foo' => 'Foo'], $result);
86
        $this->assertTrue($cache1->contains('foo'));
87
    }
88
89
    public function testNamespaceIsPropagatedToAllProviders()
90
    {
91
        $cache1 = new ArrayCache();
92
        $cache2 = new ArrayCache();
93
94
        $chainCache = new ChainCache([$cache1, $cache2]);
95
        $chainCache->setNamespace('bar');
96
97
        $this->assertEquals('bar', $cache1->getNamespace());
98
        $this->assertEquals('bar', $cache2->getNamespace());
99
    }
100
101
    public function testDeleteToAllProviders()
102
    {
103
        $cache1 = $this->getMockForAbstractClass('Doctrine\Common\Cache\CacheProvider');
104
        $cache2 = $this->getMockForAbstractClass('Doctrine\Common\Cache\CacheProvider');
105
106
        $cache1->expects($this->once())->method('doDelete');
107
        $cache2->expects($this->once())->method('doDelete');
108
109
        $chainCache = new ChainCache([$cache1, $cache2]);
110
        $chainCache->delete('bar');
111
    }
112
113
    public function testDeleteMultipleToAllProviders()
114
    {
115
        $cache1 = $this
116
            ->getMockBuilder(CacheProvider::class)
117
            ->setMethods(['doDeleteMultiple'])
118
            ->getMockForAbstractClass();
119
        $cache2 = $this
120
            ->getMockBuilder(CacheProvider::class)
121
            ->setMethods(['doDeleteMultiple'])
122
            ->getMockForAbstractClass();
123
124
        $cache1->expects($this->once())->method('doDeleteMultiple')->willReturn(true);
125
        $cache2->expects($this->once())->method('doDeleteMultiple')->willReturn(true);
126
127
        $chainCache = new ChainCache(array($cache1, $cache2));
128
        $chainCache->deleteMultiple(array('bar', 'foo'));
129
    }
130
131
    public function testFlushToAllProviders()
132
    {
133
        $cache1 = $this->getMockForAbstractClass('Doctrine\Common\Cache\CacheProvider');
134
        $cache2 = $this->getMockForAbstractClass('Doctrine\Common\Cache\CacheProvider');
135
136
        $cache1->expects($this->once())->method('doFlush');
137
        $cache2->expects($this->once())->method('doFlush');
138
139
        $chainCache = new ChainCache([$cache1, $cache2]);
140
        $chainCache->flushAll();
141
    }
142
143
    /**
144
     * @group 155
145
     *
146
     * @return void
147
     */
148
    public function testChainCacheAcceptsArrayIteratorsAsDependency()
149
    {
150
        $cache1 = $this->getMockForAbstractClass(CacheProvider::class);
151
        $cache2 = $this->getMockForAbstractClass(CacheProvider::class);
152
153
        $cache1->expects($this->once())->method('doFlush');
154
        $cache2->expects($this->once())->method('doFlush');
155
156
        (new ChainCache(new \ArrayIterator([$cache1, $cache2])))->flushAll();
157
    }
158
159
    protected function isSharedStorage()
160
    {
161
        return false;
162
    }
163
}
164