Debian Codegraph @

tests/phpunit/unit/includes/libs/MapCacheLRUTest.php

raw symbols SCIP
1 <?php
2
3 namespace Wikimedia\Tests;
4
5 use MapCacheLRU;
6 use MediaWikiCoversValidator;
7 use PHPUnit\Framework\TestCase;
8 use UnexpectedValueException;
9
10 /**
11 * @group Cache
12 * @covers \MapCacheLRU
13 */
14 class MapCacheLRUTest extends TestCase {
15
16 use MediaWikiCoversValidator;
17
18 public function testArrayConversion() {
19 $raw = [ 'd' => 4, 'c' => 3, 'b' => 2, 'a' => 1 ];
20 $cache = MapCacheLRU::newFromArray( $raw, 3 );
21
22 $this->assertEquals( 3, $cache->getMaxSize() );
23 $this->assertSame( true, $cache->has( 'a' ) );
24 $this->assertSame( true, $cache->has( 'b' ) );
25 $this->assertSame( true, $cache->has( 'c' ) );
26 $this->assertSame( 1, $cache->get( 'a' ) );
27 $this->assertSame( 2, $cache->get( 'b' ) );
28 $this->assertSame( 3, $cache->get( 'c' ) );
29
30 $this->assertSame(
31 [ 'a' => 1, 'b' => 2, 'c' => 3 ],
32 $cache->toArray()
33 );
34 $this->assertSame(
35 [ 'a', 'b', 'c' ],
36 $cache->getAllKeys()
37 );
38
39 $cache->clear( 'a' );
40 $this->assertSame(
41 [ 'b' => 2, 'c' => 3 ],
42 $cache->toArray()
43 );
44
45 $cache->clear();
46 $this->assertSame(
47 [],
48 $cache->toArray()
49 );
50
51 $cache = MapCacheLRU::newFromArray( [ 'd' => 4, 'c' => 3, 'b' => 2, 'a' => 1 ], 4 );
52 $cache->setMaxSize( 3 );
53 $this->assertSame(
54 [ 'c' => 3, 'b' => 2, 'a' => 1 ],
55 $cache->toArray()
56 );
57 }
58
59 public function testSerialize() {
60 $cache = new MapCacheLRU( 3 );
61 $cache->set( 'a', 1 );
62 $cache->set( 'b', 2 );
63 $cache->set( 'c', 3 );
64
65 $string = serialize( $cache );
66 $cache = unserialize( $string );
67 $this->assertSame(
68 [ 'a' => 1, 'b' => 2, 'c' => 3 ],
69 $cache->toArray(),
70 'entries are preserved'
71 );
72
73 $cache->set( 'd', 4 );
74 $this->assertSame(
75 [ 'b' => 2, 'c' => 3, 'd' => 4 ],
76 $cache->toArray(),
77 'maxKeys is preserved'
78 );
79 }
80
81 public function testGetWithSetCallback() {
82 $cache = new MapCacheLRU( 3 );
83 $i = 0;
84 $fn = static function () use ( &$i ) {
85 $i++;
86 return $i;
87 };
88
89 $this->assertSame( null, $cache->get( 'a' ), 'initial' );
90 $this->assertSame( 1, $cache->getWithSetCallback( 'a', $fn ), 'first gen' );
91 $this->assertSame( 1, $cache->getWithSetCallback( 'a', $fn ), 'getWithSet hit' );
92 $this->assertSame( 1, $cache->get( 'a' ), 'plain get hit' );
93 $cache->clear( 'a' );
94 $this->assertSame( 2, $cache->getWithSetCallback( 'a', $fn ), 'second gen' );
95 }
96
97 public function testMissing() {
98 $raw = [ 'a' => 1, 'b' => 2, 'c' => 3 ];
99 $cache = MapCacheLRU::newFromArray( $raw, 3 );
100
101 $this->assertFalse( $cache->has( 'd' ) );
102 $this->assertNull( $cache->get( 'd' ) );
103 $this->assertNull( $cache->get( 'd', 0.0, null ) );
104 $this->assertFalse( $cache->get( 'd', 0.0, false ) );
105 }
106
107 public function testLRU() {
108 $raw = [ 'a' => 1, 'b' => 2, 'c' => 3 ];
109 $cache = MapCacheLRU::newFromArray( $raw, 3 );
110
111 $this->assertSame( true, $cache->has( 'c' ) );
112 $this->assertSame(
113 [ 'a' => 1, 'b' => 2, 'c' => 3 ],
114 $cache->toArray()
115 );
116
117 $this->assertSame( 3, $cache->get( 'c' ) );
118 $this->assertSame(
119 [ 'a' => 1, 'b' => 2, 'c' => 3 ],
120 $cache->toArray()
121 );
122
123 $this->assertSame( 1, $cache->get( 'a' ) );
124 $this->assertSame(
125 [ 'b' => 2, 'c' => 3, 'a' => 1 ],
126 $cache->toArray()
127 );
128
129 $cache->set( 'a', 1 );
130 $this->assertSame(
131 [ 'b' => 2, 'c' => 3, 'a' => 1 ],
132 $cache->toArray()
133 );
134
135 $cache->set( 'b', 22 );
136 $this->assertSame(
137 [ 'c' => 3, 'a' => 1, 'b' => 22 ],
138 $cache->toArray()
139 );
140
141 $cache->set( 'd', 4 );
142 $this->assertSame(
143 [ 'a' => 1, 'b' => 22, 'd' => 4 ],
144 $cache->toArray()
145 );
146
147 $cache->set( 'e', 5, 0.33 );
148 $this->assertSame(
149 [ 'e' => 5, 'b' => 22, 'd' => 4 ],
150 $cache->toArray()
151 );
152
153 $cache->set( 'f', 6, 0.66 );
154 $this->assertSame(
155 [ 'b' => 22, 'f' => 6, 'd' => 4 ],
156 $cache->toArray()
157 );
158
159 $cache->set( 'g', 7, 0.90 );
160 $this->assertSame(
161 [ 'f' => 6, 'g' => 7, 'd' => 4 ],
162 $cache->toArray()
163 );
164
165 $cache->set( 'g', 7, 1.0 );
166 $this->assertSame(
167 [ 'f' => 6, 'd' => 4, 'g' => 7 ],
168 $cache->toArray()
169 );
170 }
171
172 public function testExpiry() {
173 $raw = [ 'a' => 1, 'b' => 2, 'c' => 3 ];
174 $cache = MapCacheLRU::newFromArray( $raw, 3 );
175
176 $now = microtime( true );
177 $cache->setMockTime( $now );
178
179 $cache->set( 'd', 'xxx' );
180 $this->assertTrue( $cache->has( 'd', 30 ) );
181 $this->assertEquals( 'xxx', $cache->get( 'd' ) );
182
183 $now += 29;
184 $this->assertTrue( $cache->has( 'd', 30 ) );
185 $this->assertEquals( 'xxx', $cache->get( 'd' ) );
186 $this->assertEquals( 'xxx', $cache->get( 'd', 30 ) );
187
188 $now += 1.5;
189 $this->assertFalse( $cache->has( 'd', 30 ) );
190 $this->assertEquals( 'xxx', $cache->get( 'd' ) );
191 $this->assertNull( $cache->get( 'd', 30 ) );
192 }
193
194 public function testFields() {
195 $raw = [ 'a' => 1, 'b' => 2, 'c' => 3 ];
196 $cache = MapCacheLRU::newFromArray( $raw, 3 );
197
198 $now = microtime( true );
199 $cache->setMockTime( $now );
200
201 $cache->setField( 'PMs', 'Tony Blair', 'Labour' );
202 $cache->setField( 'PMs', 'Margaret Thatcher', 'Tory' );
203 $this->assertTrue( $cache->hasField( 'PMs', 'Tony Blair', 30 ) );
204 $this->assertEquals( 'Labour', $cache->getField( 'PMs', 'Tony Blair' ) );
205 $this->assertTrue( $cache->hasField( 'PMs', 'Tony Blair', 30 ) );
206
207 $now += 29;
208 $this->assertTrue( $cache->hasField( 'PMs', 'Tony Blair', 30 ) );
209 $this->assertEquals( 'Labour', $cache->getField( 'PMs', 'Tony Blair' ) );
210 $this->assertEquals( 'Labour', $cache->getField( 'PMs', 'Tony Blair', 30 ) );
211
212 $now += 1.5;
213 $this->assertFalse( $cache->hasField( 'PMs', 'Tony Blair', 30 ) );
214 $this->assertEquals( 'Labour', $cache->getField( 'PMs', 'Tony Blair' ) );
215 $this->assertNull( $cache->getField( 'PMs', 'Tony Blair', 30 ) );
216
217 $this->assertEquals(
218 [ 'Tony Blair' => 'Labour', 'Margaret Thatcher' => 'Tory' ],
219 $cache->get( 'PMs' )
220 );
221
222 $cache->set( 'MPs', [
223 'Edwina Currie' => 1983,
224 'Neil Kinnock' => 1970
225 ] );
226 $this->assertEquals(
227 [
228 'Edwina Currie' => 1983,
229 'Neil Kinnock' => 1970
230 ],
231 $cache->get( 'MPs' )
232 );
233
234 $this->assertEquals( 1983, $cache->getField( 'MPs', 'Edwina Currie' ) );
235 $this->assertEquals( 1970, $cache->getField( 'MPs', 'Neil Kinnock' ) );
236 }
237
238 public static function provideInvalidKeys() {
239 yield [ 3.4 ];
240 yield [ false ];
241 }
242
243 /**
244 * @dataProvider provideInvalidKeys
245 */
246 public function testHasInvalidKey( $key ) {
247 $cache = new MapCacheLRU( 3 );
248 $this->expectException( UnexpectedValueException::class );
249 $this->expectExceptionMessage( 'must be string or integer' );
250 $cache->has( $key );
251 }
252
253 public static function provideMakeKey() {
254 yield [ 'foo', 'foo' ];
255 yield [ 'foo:bar:4:baz:qu%3Aux%2520', 'foo', 'bar', 4, 'baz', 'qu:ux%20' ];
256 yield [ 'qu%3Aux%2520:4', 'qu:ux%20', 4 ];
257 }
258
259 /**
260 * @dataProvider provideMakeKey
261 */
262 public function testMakeKey( $expected, ...$params ) {
263 $cache = new MapCacheLRU( 3 );
264 $this->assertSame( $expected, $cache->makeKey( ...$params ) );
265 }
266
267 /**
268 * @dataProvider provideInvalidKeys
269 */
270 public function testGetInvalidKey( $key ) {
271 $cache = new MapCacheLRU( 3 );
272 $this->expectException( UnexpectedValueException::class );
273 $this->expectExceptionMessage( 'must be string or integer' );
274 $cache->get( $key );
275 }
276
277 /**
278 * @dataProvider provideInvalidKeys
279 */
280 public function testSetInvalidKey( $key ) {
281 $cache = new MapCacheLRU( 3 );
282 $this->expectException( UnexpectedValueException::class );
283 $this->expectExceptionMessage( 'must be string or integer' );
284 $cache->set( $key, 'x' );
285 }
286
287 /**
288 * @dataProvider provideInvalidKeys
289 */
290 public function testHasFieldInvalidKey( $field ) {
291 $cache = MapCacheLRU::newFromArray( [ 'key' => [] ], 3 );
292 $this->expectException( UnexpectedValueException::class );
293 $this->expectExceptionMessage( 'must be string or integer' );
294 $cache->hasField( 'key', $field );
295 }
296
297 /**
298 * @dataProvider provideInvalidKeys
299 */
300 public function testGetFieldInvalidKey( $field ) {
301 $cache = MapCacheLRU::newFromArray( [ 'key' => [] ], 3 );
302 $this->expectException( UnexpectedValueException::class );
303 $this->expectExceptionMessage( 'must be string or integer' );
304 $cache->getField( 'key', $field );
305 }
306
307 /**
308 * @dataProvider provideInvalidKeys
309 */
310 public function testSetFieldInvalidKey( $field ) {
311 $cache = new MapCacheLRU( 3 );
312 $this->expectException( UnexpectedValueException::class );
313 $this->expectExceptionMessage( 'must be string or integer' );
314 $cache->setField( 'key', $field, 'x' );
315 }
316 }