Merge "Type hint against LinkTarget in WatchedItemStore"
[lhc/web/wiklou.git] / tests / phpunit / includes / libs / MapCacheLRUTest.php
1 <?php
2 /**
3 * @group Cache
4 */
5 class MapCacheLRUTest extends PHPUnit\Framework\TestCase {
6
7 use MediaWikiCoversValidator;
8
9 /**
10 * @covers MapCacheLRU::newFromArray()
11 * @covers MapCacheLRU::toArray()
12 * @covers MapCacheLRU::getAllKeys()
13 * @covers MapCacheLRU::clear()
14 * @covers MapCacheLRU::getMaxSize()
15 * @covers MapCacheLRU::setMaxSize()
16 */
17 function testArrayConversion() {
18 $raw = [ 'd' => 4, 'c' => 3, 'b' => 2, 'a' => 1 ];
19 $cache = MapCacheLRU::newFromArray( $raw, 3 );
20
21 $this->assertEquals( 3, $cache->getMaxSize() );
22 $this->assertSame( true, $cache->has( 'a' ) );
23 $this->assertSame( true, $cache->has( 'b' ) );
24 $this->assertSame( true, $cache->has( 'c' ) );
25 $this->assertSame( 1, $cache->get( 'a' ) );
26 $this->assertSame( 2, $cache->get( 'b' ) );
27 $this->assertSame( 3, $cache->get( 'c' ) );
28
29 $this->assertSame(
30 [ 'a' => 1, 'b' => 2, 'c' => 3 ],
31 $cache->toArray()
32 );
33 $this->assertSame(
34 [ 'a', 'b', 'c' ],
35 $cache->getAllKeys()
36 );
37
38 $cache->clear( 'a' );
39 $this->assertSame(
40 [ 'b' => 2, 'c' => 3 ],
41 $cache->toArray()
42 );
43
44 $cache->clear();
45 $this->assertSame(
46 [],
47 $cache->toArray()
48 );
49
50 $cache = MapCacheLRU::newFromArray( [ 'd' => 4, 'c' => 3, 'b' => 2, 'a' => 1 ], 4 );
51 $cache->setMaxSize( 3 );
52 $this->assertSame(
53 [ 'c' => 3, 'b' => 2, 'a' => 1 ],
54 $cache->toArray()
55 );
56 }
57
58 /**
59 * @covers MapCacheLRU::serialize()
60 * @covers MapCacheLRU::unserialize()
61 */
62 function testSerialize() {
63 $cache = MapCacheLRU::newFromArray( [ 'd' => 4, 'c' => 3, 'b' => 2, 'a' => 1 ], 10 );
64 $string = serialize( $cache );
65 $ncache = unserialize( $string );
66 $this->assertSame(
67 [ 'd' => 4, 'c' => 3, 'b' => 2, 'a' => 1 ],
68 $ncache->toArray()
69 );
70 }
71
72 /**
73 * @covers MapCacheLRU::has()
74 * @covers MapCacheLRU::get()
75 * @covers MapCacheLRU::set()
76 */
77 function testMissing() {
78 $raw = [ 'a' => 1, 'b' => 2, 'c' => 3 ];
79 $cache = MapCacheLRU::newFromArray( $raw, 3 );
80
81 $this->assertFalse( $cache->has( 'd' ) );
82 $this->assertNull( $cache->get( 'd' ) );
83 $this->assertNull( $cache->get( 'd', 0.0, null ) );
84 $this->assertFalse( $cache->get( 'd', 0.0, false ) );
85 }
86
87 /**
88 * @covers MapCacheLRU::has()
89 * @covers MapCacheLRU::get()
90 * @covers MapCacheLRU::set()
91 */
92 function testLRU() {
93 $raw = [ 'a' => 1, 'b' => 2, 'c' => 3 ];
94 $cache = MapCacheLRU::newFromArray( $raw, 3 );
95
96 $this->assertSame( true, $cache->has( 'c' ) );
97 $this->assertSame(
98 [ 'a' => 1, 'b' => 2, 'c' => 3 ],
99 $cache->toArray()
100 );
101
102 $this->assertSame( 3, $cache->get( 'c' ) );
103 $this->assertSame(
104 [ 'a' => 1, 'b' => 2, 'c' => 3 ],
105 $cache->toArray()
106 );
107
108 $this->assertSame( 1, $cache->get( 'a' ) );
109 $this->assertSame(
110 [ 'b' => 2, 'c' => 3, 'a' => 1 ],
111 $cache->toArray()
112 );
113
114 $cache->set( 'a', 1 );
115 $this->assertSame(
116 [ 'b' => 2, 'c' => 3, 'a' => 1 ],
117 $cache->toArray()
118 );
119
120 $cache->set( 'b', 22 );
121 $this->assertSame(
122 [ 'c' => 3, 'a' => 1, 'b' => 22 ],
123 $cache->toArray()
124 );
125
126 $cache->set( 'd', 4 );
127 $this->assertSame(
128 [ 'a' => 1, 'b' => 22, 'd' => 4 ],
129 $cache->toArray()
130 );
131
132 $cache->set( 'e', 5, 0.33 );
133 $this->assertSame(
134 [ 'e' => 5, 'b' => 22, 'd' => 4 ],
135 $cache->toArray()
136 );
137
138 $cache->set( 'f', 6, 0.66 );
139 $this->assertSame(
140 [ 'b' => 22, 'f' => 6, 'd' => 4 ],
141 $cache->toArray()
142 );
143
144 $cache->set( 'g', 7, 0.90 );
145 $this->assertSame(
146 [ 'f' => 6, 'g' => 7, 'd' => 4 ],
147 $cache->toArray()
148 );
149
150 $cache->set( 'g', 7, 1.0 );
151 $this->assertSame(
152 [ 'f' => 6, 'd' => 4, 'g' => 7 ],
153 $cache->toArray()
154 );
155 }
156
157 /**
158 * @covers MapCacheLRU::has()
159 * @covers MapCacheLRU::get()
160 * @covers MapCacheLRU::set()
161 */
162 public function testExpiry() {
163 $raw = [ 'a' => 1, 'b' => 2, 'c' => 3 ];
164 $cache = MapCacheLRU::newFromArray( $raw, 3 );
165
166 $now = microtime( true );
167 $cache->setMockTime( $now );
168
169 $cache->set( 'd', 'xxx' );
170 $this->assertTrue( $cache->has( 'd', 30 ) );
171 $this->assertEquals( 'xxx', $cache->get( 'd' ) );
172
173 $now += 29;
174 $this->assertTrue( $cache->has( 'd', 30 ) );
175 $this->assertEquals( 'xxx', $cache->get( 'd' ) );
176 $this->assertEquals( 'xxx', $cache->get( 'd', 30 ) );
177
178 $now += 1.5;
179 $this->assertFalse( $cache->has( 'd', 30 ) );
180 $this->assertEquals( 'xxx', $cache->get( 'd' ) );
181 $this->assertNull( $cache->get( 'd', 30 ) );
182 }
183
184 /**
185 * @covers MapCacheLRU::hasField()
186 * @covers MapCacheLRU::getField()
187 * @covers MapCacheLRU::setField()
188 */
189 public function testFields() {
190 $raw = [ 'a' => 1, 'b' => 2, 'c' => 3 ];
191 $cache = MapCacheLRU::newFromArray( $raw, 3 );
192
193 $now = microtime( true );
194 $cache->setMockTime( $now );
195
196 $cache->setField( 'PMs', 'Tony Blair', 'Labour' );
197 $cache->setField( 'PMs', 'Margaret Thatcher', 'Tory' );
198 $this->assertTrue( $cache->hasField( 'PMs', 'Tony Blair', 30 ) );
199 $this->assertEquals( 'Labour', $cache->getField( 'PMs', 'Tony Blair' ) );
200 $this->assertTrue( $cache->hasField( 'PMs', 'Tony Blair', 30 ) );
201
202 $now += 29;
203 $this->assertTrue( $cache->hasField( 'PMs', 'Tony Blair', 30 ) );
204 $this->assertEquals( 'Labour', $cache->getField( 'PMs', 'Tony Blair' ) );
205 $this->assertEquals( 'Labour', $cache->getField( 'PMs', 'Tony Blair', 30 ) );
206
207 $now += 1.5;
208 $this->assertFalse( $cache->hasField( 'PMs', 'Tony Blair', 30 ) );
209 $this->assertEquals( 'Labour', $cache->getField( 'PMs', 'Tony Blair' ) );
210 $this->assertNull( $cache->getField( 'PMs', 'Tony Blair', 30 ) );
211
212 $this->assertEquals(
213 [ 'Tony Blair' => 'Labour', 'Margaret Thatcher' => 'Tory' ],
214 $cache->get( 'PMs' )
215 );
216
217 $cache->set( 'MPs', [
218 'Edwina Currie' => 1983,
219 'Neil Kinnock' => 1970
220 ] );
221 $this->assertEquals(
222 [
223 'Edwina Currie' => 1983,
224 'Neil Kinnock' => 1970
225 ],
226 $cache->get( 'MPs' )
227 );
228
229 $this->assertEquals( 1983, $cache->getField( 'MPs', 'Edwina Currie' ) );
230 $this->assertEquals( 1970, $cache->getField( 'MPs', 'Neil Kinnock' ) );
231 }
232
233 /**
234 * @covers MapCacheLRU::has()
235 * @covers MapCacheLRU::get()
236 * @covers MapCacheLRU::set()
237 * @covers MapCacheLRU::hasField()
238 * @covers MapCacheLRU::getField()
239 * @covers MapCacheLRU::setField()
240 */
241 public function testInvalidKeys() {
242 $cache = MapCacheLRU::newFromArray( [], 3 );
243
244 try {
245 $cache->has( 3.4 );
246 $this->fail( "No exception" );
247 } catch ( UnexpectedValueException $e ) {
248 $this->assertRegExp( '/must be string or integer/', $e->getMessage() );
249 }
250 try {
251 $cache->get( false );
252 $this->fail( "No exception" );
253 } catch ( UnexpectedValueException $e ) {
254 $this->assertRegExp( '/must be string or integer/', $e->getMessage() );
255 }
256 try {
257 $cache->set( 3.4, 'x' );
258 $this->fail( "No exception" );
259 } catch ( UnexpectedValueException $e ) {
260 $this->assertRegExp( '/must be string or integer/', $e->getMessage() );
261 }
262
263 try {
264 $cache->hasField( 'x', 3.4 );
265 $this->fail( "No exception" );
266 } catch ( UnexpectedValueException $e ) {
267 $this->assertRegExp( '/must be string or integer/', $e->getMessage() );
268 }
269 try {
270 $cache->getField( 'x', false );
271 $this->fail( "No exception" );
272 } catch ( UnexpectedValueException $e ) {
273 $this->assertRegExp( '/must be string or integer/', $e->getMessage() );
274 }
275 try {
276 $cache->setField( 'x', 3.4, 'x' );
277 $this->fail( "No exception" );
278 } catch ( UnexpectedValueException $e ) {
279 $this->assertRegExp( '/must be string or integer/', $e->getMessage() );
280 }
281 }
282 }