Merge "Chinese Conversion Table Update 2017-6"
[lhc/web/wiklou.git] / tests / phpunit / includes / libs / ProcessCacheLRUTest.php
1 <?php
2
3 /**
4 * Test for ProcessCacheLRU class.
5 *
6 * Note that it uses the ProcessCacheLRUTestable class which extends some
7 * properties and methods visibility. That class is defined at the end of the
8 * file containing this class.
9 *
10 * @group Cache
11 */
12 class ProcessCacheLRUTest extends PHPUnit_Framework_TestCase {
13
14 use MediaWikiCoversValidator;
15
16 /**
17 * Helper to verify emptiness of a cache object.
18 * Compare against an array so we get the cache content difference.
19 */
20 protected function assertCacheEmpty( $cache, $msg = 'Cache should be empty' ) {
21 $this->assertAttributeEquals( [], 'cache', $cache, $msg );
22 }
23
24 /**
25 * Helper to fill a cache object passed by reference
26 */
27 protected function fillCache( &$cache, $numEntries ) {
28 // Fill cache with three values
29 for ( $i = 1; $i <= $numEntries; $i++ ) {
30 $cache->set( "cache-key-$i", "prop-$i", "value-$i" );
31 }
32 }
33
34 /**
35 * Generates an array of what would be expected in cache for a given cache
36 * size and a number of entries filled in sequentially
37 */
38 protected function getExpectedCache( $cacheMaxEntries, $entryToFill ) {
39 $expected = [];
40
41 if ( $entryToFill === 0 ) {
42 // The cache is empty!
43 return [];
44 } elseif ( $entryToFill <= $cacheMaxEntries ) {
45 // Cache is not fully filled
46 $firstKey = 1;
47 } else {
48 // Cache overflowed
49 $firstKey = 1 + $entryToFill - $cacheMaxEntries;
50 }
51
52 $lastKey = $entryToFill;
53
54 for ( $i = $firstKey; $i <= $lastKey; $i++ ) {
55 $expected["cache-key-$i"] = [ "prop-$i" => "value-$i" ];
56 }
57
58 return $expected;
59 }
60
61 /**
62 * Highlight diff between assertEquals and assertNotSame
63 */
64 public function testPhpUnitArrayEquality() {
65 $one = [ 'A' => 1, 'B' => 2 ];
66 $two = [ 'B' => 2, 'A' => 1 ];
67 // ==
68 $this->assertEquals( $one, $two );
69 // ===
70 $this->assertNotSame( $one, $two );
71 }
72
73 /**
74 * @dataProvider provideInvalidConstructorArg
75 * @expectedException Wikimedia\Assert\ParameterAssertionException
76 * @covers ProcessCacheLRU::__construct
77 */
78 public function testConstructorGivenInvalidValue( $maxSize ) {
79 new ProcessCacheLRUTestable( $maxSize );
80 }
81
82 /**
83 * Value which are forbidden by the constructor
84 */
85 public static function provideInvalidConstructorArg() {
86 return [
87 [ null ],
88 [ [] ],
89 [ new stdClass() ],
90 [ 0 ],
91 [ '5' ],
92 [ -1 ],
93 ];
94 }
95
96 /**
97 * @covers ProcessCacheLRU::get
98 * @covers ProcessCacheLRU::set
99 * @covers ProcessCacheLRU::has
100 */
101 public function testAddAndGetAKey() {
102 $oneCache = new ProcessCacheLRUTestable( 1 );
103 $this->assertCacheEmpty( $oneCache );
104
105 // First set just one value
106 $oneCache->set( 'cache-key', 'prop1', 'value1' );
107 $this->assertEquals( 1, $oneCache->getEntriesCount() );
108 $this->assertTrue( $oneCache->has( 'cache-key', 'prop1' ) );
109 $this->assertEquals( 'value1', $oneCache->get( 'cache-key', 'prop1' ) );
110 }
111
112 /**
113 * @covers ProcessCacheLRU::set
114 * @covers ProcessCacheLRU::get
115 */
116 public function testDeleteOldKey() {
117 $oneCache = new ProcessCacheLRUTestable( 1 );
118 $this->assertCacheEmpty( $oneCache );
119
120 $oneCache->set( 'cache-key', 'prop1', 'value1' );
121 $oneCache->set( 'cache-key', 'prop1', 'value2' );
122 $this->assertEquals( 'value2', $oneCache->get( 'cache-key', 'prop1' ) );
123 }
124
125 /**
126 * This test that we properly overflow when filling a cache with
127 * a sequence of always different cache-keys. Meant to verify we correclty
128 * delete the older key.
129 *
130 * @covers ProcessCacheLRU::set
131 * @dataProvider provideCacheFilling
132 * @param int $cacheMaxEntries Maximum entry the created cache will hold
133 * @param int $entryToFill Number of entries to insert in the created cache.
134 */
135 public function testFillingCache( $cacheMaxEntries, $entryToFill, $msg = '' ) {
136 $cache = new ProcessCacheLRUTestable( $cacheMaxEntries );
137 $this->fillCache( $cache, $entryToFill );
138
139 $this->assertSame(
140 $this->getExpectedCache( $cacheMaxEntries, $entryToFill ),
141 $cache->getCache(),
142 "Filling a $cacheMaxEntries entries cache with $entryToFill entries"
143 );
144 }
145
146 /**
147 * Provider for testFillingCache
148 */
149 public static function provideCacheFilling() {
150 // ($cacheMaxEntries, $entryToFill, $msg='')
151 return [
152 [ 1, 0 ],
153 [ 1, 1 ],
154 // overflow
155 [ 1, 2 ],
156 // overflow
157 [ 5, 33 ],
158 ];
159 }
160
161 /**
162 * Create a cache with only one remaining entry then update
163 * the first inserted entry. Should bump it to the top.
164 *
165 * @covers ProcessCacheLRU::set
166 */
167 public function testReplaceExistingKeyShouldBumpEntryToTop() {
168 $maxEntries = 3;
169
170 $cache = new ProcessCacheLRUTestable( $maxEntries );
171 // Fill cache leaving just one remaining slot
172 $this->fillCache( $cache, $maxEntries - 1 );
173
174 // Set an existing cache key
175 $cache->set( "cache-key-1", "prop-1", "new-value-for-1" );
176
177 $this->assertSame(
178 [
179 'cache-key-2' => [ 'prop-2' => 'value-2' ],
180 'cache-key-1' => [ 'prop-1' => 'new-value-for-1' ],
181 ],
182 $cache->getCache()
183 );
184 }
185
186 /**
187 * @covers ProcessCacheLRU::get
188 * @covers ProcessCacheLRU::set
189 * @covers ProcessCacheLRU::has
190 */
191 public function testRecentlyAccessedKeyStickIn() {
192 $cache = new ProcessCacheLRUTestable( 2 );
193 $cache->set( 'first', 'prop1', 'value1' );
194 $cache->set( 'second', 'prop2', 'value2' );
195
196 // Get first
197 $cache->get( 'first', 'prop1' );
198 // Cache a third value, should invalidate the least used one
199 $cache->set( 'third', 'prop3', 'value3' );
200
201 $this->assertFalse( $cache->has( 'second', 'prop2' ) );
202 }
203
204 /**
205 * This first create a full cache then update the value for the 2nd
206 * filled entry.
207 * Given a cache having 1,2,3 as key, updating 2 should bump 2 to
208 * the top of the queue with the new value: 1,3,2* (* = updated).
209 *
210 * @covers ProcessCacheLRU::set
211 * @covers ProcessCacheLRU::get
212 */
213 public function testReplaceExistingKeyInAFullCacheShouldBumpToTop() {
214 $maxEntries = 3;
215
216 $cache = new ProcessCacheLRUTestable( $maxEntries );
217 $this->fillCache( $cache, $maxEntries );
218
219 // Set an existing cache key
220 $cache->set( "cache-key-2", "prop-2", "new-value-for-2" );
221 $this->assertSame(
222 [
223 'cache-key-1' => [ 'prop-1' => 'value-1' ],
224 'cache-key-3' => [ 'prop-3' => 'value-3' ],
225 'cache-key-2' => [ 'prop-2' => 'new-value-for-2' ],
226 ],
227 $cache->getCache()
228 );
229 $this->assertEquals( 'new-value-for-2',
230 $cache->get( 'cache-key-2', 'prop-2' )
231 );
232 }
233
234 /**
235 * @covers ProcessCacheLRU::set
236 */
237 public function testBumpExistingKeyToTop() {
238 $cache = new ProcessCacheLRUTestable( 3 );
239 $this->fillCache( $cache, 3 );
240
241 // Set the very first cache key to a new value
242 $cache->set( "cache-key-1", "prop-1", "new value for 1" );
243 $this->assertEquals(
244 [
245 'cache-key-2' => [ 'prop-2' => 'value-2' ],
246 'cache-key-3' => [ 'prop-3' => 'value-3' ],
247 'cache-key-1' => [ 'prop-1' => 'new value for 1' ],
248 ],
249 $cache->getCache()
250 );
251 }
252 }
253
254 /**
255 * Overrides some ProcessCacheLRU methods and properties accessibility.
256 */
257 class ProcessCacheLRUTestable extends ProcessCacheLRU {
258 public $cache = [];
259
260 public function getCache() {
261 return $this->cache;
262 }
263
264 public function getEntriesCount() {
265 return count( $this->cache );
266 }
267 }