Merge "Update docs/hooks.txt for ShowSearchHitTitle"
[mediawiki.git] / tests / phpunit / includes / libs / ProcessCacheLRUTest.php
blob9c189d12930a933e2fd635ffaf77ac0aab8ca8ec
1 <?php
3 /**
4 * Test for ProcessCacheLRU class.
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.
10 * @group Cache
12 class ProcessCacheLRUTest extends PHPUnit_Framework_TestCase {
14 /**
15 * Helper to verify emptiness of a cache object.
16 * Compare against an array so we get the cache content difference.
18 protected function assertCacheEmpty( $cache, $msg = 'Cache should be empty' ) {
19 $this->assertAttributeEquals( [], 'cache', $cache, $msg );
22 /**
23 * Helper to fill a cache object passed by reference
25 protected function fillCache( &$cache, $numEntries ) {
26 // Fill cache with three values
27 for ( $i = 1; $i <= $numEntries; $i++ ) {
28 $cache->set( "cache-key-$i", "prop-$i", "value-$i" );
32 /**
33 * Generates an array of what would be expected in cache for a given cache
34 * size and a number of entries filled in sequentially
36 protected function getExpectedCache( $cacheMaxEntries, $entryToFill ) {
37 $expected = [];
39 if ( $entryToFill === 0 ) {
40 // The cache is empty!
41 return [];
42 } elseif ( $entryToFill <= $cacheMaxEntries ) {
43 // Cache is not fully filled
44 $firstKey = 1;
45 } else {
46 // Cache overflowed
47 $firstKey = 1 + $entryToFill - $cacheMaxEntries;
50 $lastKey = $entryToFill;
52 for ( $i = $firstKey; $i <= $lastKey; $i++ ) {
53 $expected["cache-key-$i"] = [ "prop-$i" => "value-$i" ];
56 return $expected;
59 /**
60 * Highlight diff between assertEquals and assertNotSame
62 public function testPhpUnitArrayEquality() {
63 $one = [ 'A' => 1, 'B' => 2 ];
64 $two = [ 'B' => 2, 'A' => 1 ];
65 // ==
66 $this->assertEquals( $one, $two );
67 // ===
68 $this->assertNotSame( $one, $two );
71 /**
72 * @dataProvider provideInvalidConstructorArg
73 * @expectedException Wikimedia\Assert\ParameterAssertionException
74 * @covers ProcessCacheLRU::__construct
76 public function testConstructorGivenInvalidValue( $maxSize ) {
77 new ProcessCacheLRUTestable( $maxSize );
80 /**
81 * Value which are forbidden by the constructor
83 public static function provideInvalidConstructorArg() {
84 return [
85 [ null ],
86 [ [] ],
87 [ new stdClass() ],
88 [ 0 ],
89 [ '5' ],
90 [ -1 ],
94 /**
95 * @covers ProcessCacheLRU::get
96 * @covers ProcessCacheLRU::set
97 * @covers ProcessCacheLRU::has
99 public function testAddAndGetAKey() {
100 $oneCache = new ProcessCacheLRUTestable( 1 );
101 $this->assertCacheEmpty( $oneCache );
103 // First set just one value
104 $oneCache->set( 'cache-key', 'prop1', 'value1' );
105 $this->assertEquals( 1, $oneCache->getEntriesCount() );
106 $this->assertTrue( $oneCache->has( 'cache-key', 'prop1' ) );
107 $this->assertEquals( 'value1', $oneCache->get( 'cache-key', 'prop1' ) );
111 * @covers ProcessCacheLRU::set
112 * @covers ProcessCacheLRU::get
114 public function testDeleteOldKey() {
115 $oneCache = new ProcessCacheLRUTestable( 1 );
116 $this->assertCacheEmpty( $oneCache );
118 $oneCache->set( 'cache-key', 'prop1', 'value1' );
119 $oneCache->set( 'cache-key', 'prop1', 'value2' );
120 $this->assertEquals( 'value2', $oneCache->get( 'cache-key', 'prop1' ) );
124 * This test that we properly overflow when filling a cache with
125 * a sequence of always different cache-keys. Meant to verify we correclty
126 * delete the older key.
128 * @covers ProcessCacheLRU::set
129 * @dataProvider provideCacheFilling
130 * @param int $cacheMaxEntries Maximum entry the created cache will hold
131 * @param int $entryToFill Number of entries to insert in the created cache.
133 public function testFillingCache( $cacheMaxEntries, $entryToFill, $msg = '' ) {
134 $cache = new ProcessCacheLRUTestable( $cacheMaxEntries );
135 $this->fillCache( $cache, $entryToFill );
137 $this->assertSame(
138 $this->getExpectedCache( $cacheMaxEntries, $entryToFill ),
139 $cache->getCache(),
140 "Filling a $cacheMaxEntries entries cache with $entryToFill entries"
145 * Provider for testFillingCache
147 public static function provideCacheFilling() {
148 // ($cacheMaxEntries, $entryToFill, $msg='')
149 return [
150 [ 1, 0 ],
151 [ 1, 1 ],
152 // overflow
153 [ 1, 2 ],
154 // overflow
155 [ 5, 33 ],
160 * Create a cache with only one remaining entry then update
161 * the first inserted entry. Should bump it to the top.
163 * @covers ProcessCacheLRU::set
165 public function testReplaceExistingKeyShouldBumpEntryToTop() {
166 $maxEntries = 3;
168 $cache = new ProcessCacheLRUTestable( $maxEntries );
169 // Fill cache leaving just one remaining slot
170 $this->fillCache( $cache, $maxEntries - 1 );
172 // Set an existing cache key
173 $cache->set( "cache-key-1", "prop-1", "new-value-for-1" );
175 $this->assertSame(
177 'cache-key-2' => [ 'prop-2' => 'value-2' ],
178 'cache-key-1' => [ 'prop-1' => 'new-value-for-1' ],
180 $cache->getCache()
185 * @covers ProcessCacheLRU::get
186 * @covers ProcessCacheLRU::set
187 * @covers ProcessCacheLRU::has
189 public function testRecentlyAccessedKeyStickIn() {
190 $cache = new ProcessCacheLRUTestable( 2 );
191 $cache->set( 'first', 'prop1', 'value1' );
192 $cache->set( 'second', 'prop2', 'value2' );
194 // Get first
195 $cache->get( 'first', 'prop1' );
196 // Cache a third value, should invalidate the least used one
197 $cache->set( 'third', 'prop3', 'value3' );
199 $this->assertFalse( $cache->has( 'second', 'prop2' ) );
203 * This first create a full cache then update the value for the 2nd
204 * filled entry.
205 * Given a cache having 1,2,3 as key, updating 2 should bump 2 to
206 * the top of the queue with the new value: 1,3,2* (* = updated).
208 * @covers ProcessCacheLRU::set
209 * @covers ProcessCacheLRU::get
211 public function testReplaceExistingKeyInAFullCacheShouldBumpToTop() {
212 $maxEntries = 3;
214 $cache = new ProcessCacheLRUTestable( $maxEntries );
215 $this->fillCache( $cache, $maxEntries );
217 // Set an existing cache key
218 $cache->set( "cache-key-2", "prop-2", "new-value-for-2" );
219 $this->assertSame(
221 'cache-key-1' => [ 'prop-1' => 'value-1' ],
222 'cache-key-3' => [ 'prop-3' => 'value-3' ],
223 'cache-key-2' => [ 'prop-2' => 'new-value-for-2' ],
225 $cache->getCache()
227 $this->assertEquals( 'new-value-for-2',
228 $cache->get( 'cache-key-2', 'prop-2' )
233 * @covers ProcessCacheLRU::set
235 public function testBumpExistingKeyToTop() {
236 $cache = new ProcessCacheLRUTestable( 3 );
237 $this->fillCache( $cache, 3 );
239 // Set the very first cache key to a new value
240 $cache->set( "cache-key-1", "prop-1", "new value for 1" );
241 $this->assertEquals(
243 'cache-key-2' => [ 'prop-2' => 'value-2' ],
244 'cache-key-3' => [ 'prop-3' => 'value-3' ],
245 'cache-key-1' => [ 'prop-1' => 'new value for 1' ],
247 $cache->getCache()
253 * Overrides some ProcessCacheLRU methods and properties accessibility.
255 class ProcessCacheLRUTestable extends ProcessCacheLRU {
256 public $cache = [];
258 public function getCache() {
259 return $this->cache;
262 public function getEntriesCount() {
263 return count( $this->cache );