1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SimpleCache\Tests\Phpunit\Cache; |
4
|
|
|
|
5
|
|
|
use SimpleCache\Cache\Cache; |
6
|
|
|
use SimpleCache\Cache\CombinatoryCache; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @covers SimpleCache\Cache\CombinatoryCache |
10
|
|
|
* |
11
|
|
|
* @licence GNU GPL v2+ |
12
|
|
|
* @author Jeroen De Dauw < [email protected] > |
13
|
|
|
*/ |
14
|
|
|
class CombinatoryCacheTest extends \PHPUnit_Framework_TestCase { |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @dataProvider invalidConstructorArgumentProvider |
18
|
|
|
*/ |
19
|
|
|
public function testCannotConstructWithNonCaches( $invalidCachesList ) { |
20
|
|
|
$this->expectException( 'InvalidArgumentException' ); |
21
|
|
|
|
22
|
|
|
new CombinatoryCache( $invalidCachesList ); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function invalidConstructorArgumentProvider() { |
26
|
|
|
$argLists = array(); |
27
|
|
|
|
28
|
|
|
$containedCache = $this->createMock( Cache::class ); |
29
|
|
|
|
30
|
|
|
$argLists[] = array( array() ); |
31
|
|
|
$argLists[] = array( array( null ) ); |
32
|
|
|
$argLists[] = array( array( $containedCache, 42 ) ); |
33
|
|
|
$argLists[] = array( array( $containedCache, new \stdClass(), $containedCache ) ); |
34
|
|
|
|
35
|
|
|
return $argLists; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function testHasWithOneCache() { |
39
|
|
|
$containedCache = $this->createMock( Cache::class ); |
40
|
|
|
|
41
|
|
|
$containedCache |
42
|
|
|
->expects( $this->exactly( 2 ) ) |
43
|
|
|
->method( 'has' ) |
44
|
|
|
->will( $this->returnValue( true ) ); |
45
|
|
|
|
46
|
|
|
$cache = new CombinatoryCache( array( $containedCache ) ); |
47
|
|
|
|
48
|
|
|
$this->assertTrue( $cache->has( 'foo' ) ); |
49
|
|
|
$this->assertTrue( $cache->has( 'bar' ) ); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function testSetWithOneCache() { |
53
|
|
|
$containedCache = $this->createMock( Cache::class ); |
54
|
|
|
|
55
|
|
|
$containedCache |
56
|
|
|
->expects( $this->exactly( 2 ) ) |
57
|
|
|
->method( 'set' ) |
58
|
|
|
->with( |
59
|
|
|
$this->equalTo( 'hax' ), |
60
|
|
|
$this->equalTo( 1337 ) |
61
|
|
|
); |
62
|
|
|
|
63
|
|
|
$cache = new CombinatoryCache( array( $containedCache ) ); |
64
|
|
|
|
65
|
|
|
$cache->set( 'hax', 1337 ); |
66
|
|
|
$cache->set( 'hax', 1337 ); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function testGetWithOneCache() { |
70
|
|
|
$containedCache = $this->createMock( Cache::class ); |
71
|
|
|
|
72
|
|
|
$containedCache |
73
|
|
|
->expects( $this->exactly( 2 ) ) |
74
|
|
|
->method( 'get' ) |
75
|
|
|
->with( |
76
|
|
|
$this->equalTo( 'hax' ) |
77
|
|
|
) |
78
|
|
|
->will( $this->returnValue( 1337 ) ); |
79
|
|
|
|
80
|
|
|
$cache = new CombinatoryCache( array( $containedCache ) ); |
81
|
|
|
|
82
|
|
|
$this->assertEquals( 1337, $cache->get( 'hax' ) ); |
83
|
|
|
$this->assertEquals( 1337, $cache->get( 'hax' ) ); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function testSetHitsAllCaches() { |
87
|
|
|
$containedCache0 = $this->createMock( Cache::class ); |
88
|
|
|
|
89
|
|
|
$containedCache0 |
90
|
|
|
->expects( $this->exactly( 2 ) ) |
91
|
|
|
->method( 'set' ); |
92
|
|
|
|
93
|
|
|
$containedCache1 = $this->createMock( Cache::class ); |
94
|
|
|
|
95
|
|
|
$containedCache1 |
96
|
|
|
->expects( $this->exactly( 2 ) ) |
97
|
|
|
->method( 'set' ); |
98
|
|
|
|
99
|
|
|
$cache = new CombinatoryCache( array( $containedCache0, $containedCache1 ) ); |
100
|
|
|
|
101
|
|
|
$cache->set( 'hax', 1337 ); |
102
|
|
|
$cache->set( 'hax', 1337 ); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function testHasHitsCachesInCorrectOrder() { |
106
|
|
|
$containedCache0 = $this->createMock( Cache::class ); |
107
|
|
|
|
108
|
|
|
$containedCache0 |
109
|
|
|
->expects( $this->once() ) |
110
|
|
|
->method( 'has' ) |
111
|
|
|
->will( $this->returnValue( false ) ); |
112
|
|
|
|
113
|
|
|
$containedCache1 = $this->createMock( Cache::class ); |
114
|
|
|
|
115
|
|
|
$containedCache1 |
116
|
|
|
->expects( $this->once() ) |
117
|
|
|
->method( 'has' ) |
118
|
|
|
->will( $this->returnValue( true ) ); |
119
|
|
|
|
120
|
|
|
$containedCache2 = $this->createMock( Cache::class ); |
121
|
|
|
|
122
|
|
|
$containedCache2 |
123
|
|
|
->expects( $this->never() ) |
124
|
|
|
->method( 'has' ); |
125
|
|
|
|
126
|
|
|
$cache = new CombinatoryCache( array( $containedCache0, $containedCache1, $containedCache2 ) ); |
127
|
|
|
|
128
|
|
|
$cache->has( 'foo' ); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function testGetHitsCachesInCorrectOrder() { |
132
|
|
|
$containedCache0 = $this->createMock( Cache::class ); |
133
|
|
|
|
134
|
|
|
$containedCache0 |
135
|
|
|
->expects( $this->any() ) |
136
|
|
|
->method( 'get' ) |
137
|
|
|
->will( $this->returnValue( null ) ); |
138
|
|
|
|
139
|
|
|
$containedCache0 |
140
|
|
|
->expects( $this->any() ) |
141
|
|
|
->method( 'has' ) |
142
|
|
|
->will( $this->returnValue( false ) ); |
143
|
|
|
|
144
|
|
|
$containedCache1 = $this->createMock( Cache::class ); |
145
|
|
|
|
146
|
|
|
$containedCache1 |
147
|
|
|
->expects( $this->once() ) |
148
|
|
|
->method( 'get' ) |
149
|
|
|
->will( $this->returnValue( 42 ) ); |
150
|
|
|
|
151
|
|
|
$containedCache2 = $this->createMock( Cache::class ); |
152
|
|
|
|
153
|
|
|
$containedCache2 |
154
|
|
|
->expects( $this->never() ) |
155
|
|
|
->method( 'get' ); |
156
|
|
|
|
157
|
|
|
$cache = new CombinatoryCache( array( $containedCache0, $containedCache1, $containedCache2 ) ); |
158
|
|
|
|
159
|
|
|
$this->assertEquals( 42, $cache->get( 'foo' ) ); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function testGetFromLowerCacheWritesToUpperOne() { |
163
|
|
|
$containedCache0 = $this->createMock( Cache::class ); |
164
|
|
|
|
165
|
|
|
$containedCache0 |
166
|
|
|
->expects( $this->any() ) |
167
|
|
|
->method( 'get' ) |
168
|
|
|
->will( $this->returnValue( null ) ); |
169
|
|
|
|
170
|
|
|
$containedCache0 |
171
|
|
|
->expects( $this->any() ) |
172
|
|
|
->method( 'has' ) |
173
|
|
|
->will( $this->returnValue( false ) ); |
174
|
|
|
|
175
|
|
|
$containedCache0 |
176
|
|
|
->expects( $this->once() ) |
177
|
|
|
->method( 'set' ) |
178
|
|
|
->with( |
179
|
|
|
$this->equalTo( 'foo' ), |
180
|
|
|
$this->equalTo( 42 ) |
181
|
|
|
); |
182
|
|
|
|
183
|
|
|
$containedCache1 = $this->createMock( Cache::class ); |
184
|
|
|
|
185
|
|
|
$containedCache1 |
186
|
|
|
->expects( $this->any() ) |
187
|
|
|
->method( 'get' ) |
188
|
|
|
->will( $this->returnValue( null ) ); |
189
|
|
|
|
190
|
|
|
$containedCache1 |
191
|
|
|
->expects( $this->any() ) |
192
|
|
|
->method( 'has' ) |
193
|
|
|
->will( $this->returnValue( false ) ); |
194
|
|
|
|
195
|
|
|
$containedCache1 |
196
|
|
|
->expects( $this->never() ) |
197
|
|
|
->method( 'set' ); |
198
|
|
|
|
199
|
|
|
$containedCache2 = $this->createMock( Cache::class ); |
200
|
|
|
|
201
|
|
|
$containedCache2 |
202
|
|
|
->expects( $this->once() ) |
203
|
|
|
->method( 'get' ) |
204
|
|
|
->will( $this->returnValue( 42 ) ); |
205
|
|
|
|
206
|
|
|
$cache = new CombinatoryCache( array( $containedCache0, $containedCache1, $containedCache2 ) ); |
207
|
|
|
|
208
|
|
|
$this->assertEquals( 42, $cache->get( 'foo' ) ); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
public function testMissingAllCachesReturnsNull() { |
212
|
|
|
$key = 'foo'; |
213
|
|
|
|
214
|
|
|
$containedCache0 = $this->createMock( Cache::class ); |
215
|
|
|
|
216
|
|
|
$containedCache0 |
217
|
|
|
->expects( $this->exactly( 2 ) ) |
218
|
|
|
->method( 'get' ) |
219
|
|
|
->will( $this->returnValue( null ) ); |
220
|
|
|
|
221
|
|
|
$containedCache1 = $this->createMock( Cache::class ); |
222
|
|
|
|
223
|
|
|
$containedCache1 |
224
|
|
|
->expects( $this->exactly( 2 ) ) |
225
|
|
|
->method( 'get' ) |
226
|
|
|
->will( $this->returnValue( null ) ); |
227
|
|
|
|
228
|
|
|
$containedCache2 = $this->createMock( Cache::class ); |
229
|
|
|
|
230
|
|
|
$containedCache2 |
231
|
|
|
->expects( $this->exactly( 2 ) ) |
232
|
|
|
->method( 'get' ) |
233
|
|
|
->will( $this->returnValue( null ) ); |
234
|
|
|
|
235
|
|
|
$cache = new CombinatoryCache( array( $containedCache0, $containedCache1, $containedCache2 ) ); |
236
|
|
|
|
237
|
|
|
$this->assertNull( $cache->get( $key ) ); |
238
|
|
|
$this->assertNull( $cache->get( $key ) ); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
} |
242
|
|
|
|