4 * Note that it uses the ProcessCacheLRUTestable class which extends some
5 * properties and methods visibility. That class is defined at the end of the
6 * file containing this class.
10 class ProcessCacheLRUTest
extends PHPUnit\Framework\TestCase
{
12 use MediaWikiCoversValidator
;
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->assertEquals( 0, $cache->getEntriesCount(), $msg );
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" );
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 ) {
39 if ( $entryToFill === 0 ) {
40 // The cache is empty!
42 } elseif ( $entryToFill <= $cacheMaxEntries ) {
43 // Cache is not fully filled
47 $firstKey = 1 +
$entryToFill - $cacheMaxEntries;
50 $lastKey = $entryToFill;
52 for ( $i = $firstKey; $i <= $lastKey; $i++
) {
53 $expected["cache-key-$i"] = [ "prop-$i" => "value-$i" ];
60 * Highlight diff between assertEquals and assertNotSame
63 public function testPhpUnitArrayEquality() {
64 $one = [ 'A' => 1, 'B' => 2 ];
65 $two = [ 'B' => 2, 'A' => 1 ];
67 $this->assertEquals( $one, $two );
69 $this->assertNotSame( $one, $two );
73 * @dataProvider provideInvalidConstructorArg
74 * @expectedException Wikimedia\Assert\ParameterAssertionException
75 * @covers ProcessCacheLRU::__construct
77 public function testConstructorGivenInvalidValue( $maxSize ) {
78 new ProcessCacheLRUTestable( $maxSize );
82 * Value which are forbidden by the constructor
84 public static function provideInvalidConstructorArg() {
96 * @covers ProcessCacheLRU::get
97 * @covers ProcessCacheLRU::set
98 * @covers ProcessCacheLRU::has
100 public function testAddAndGetAKey() {
101 $oneCache = new ProcessCacheLRUTestable( 1 );
102 $this->assertCacheEmpty( $oneCache );
104 // First set just one value
105 $oneCache->set( 'cache-key', 'prop1', 'value1' );
106 $this->assertEquals( 1, $oneCache->getEntriesCount() );
107 $this->assertTrue( $oneCache->has( 'cache-key', 'prop1' ) );
108 $this->assertEquals( 'value1', $oneCache->get( 'cache-key', 'prop1' ) );
112 * @covers ProcessCacheLRU::set
113 * @covers ProcessCacheLRU::get
115 public function testDeleteOldKey() {
116 $oneCache = new ProcessCacheLRUTestable( 1 );
117 $this->assertCacheEmpty( $oneCache );
119 $oneCache->set( 'cache-key', 'prop1', 'value1' );
120 $oneCache->set( 'cache-key', 'prop1', 'value2' );
121 $this->assertEquals( 'value2', $oneCache->get( 'cache-key', 'prop1' ) );
125 * This test that we properly overflow when filling a cache with
126 * a sequence of always different cache-keys. Meant to verify we correclty
127 * delete the older key.
129 * @covers ProcessCacheLRU::set
130 * @dataProvider provideCacheFilling
131 * @param int $cacheMaxEntries Maximum entry the created cache will hold
132 * @param int $entryToFill Number of entries to insert in the created cache.
134 public function testFillingCache( $cacheMaxEntries, $entryToFill, $msg = '' ) {
135 $cache = new ProcessCacheLRUTestable( $cacheMaxEntries );
136 $this->fillCache( $cache, $entryToFill );
139 $this->getExpectedCache( $cacheMaxEntries, $entryToFill ),
141 "Filling a $cacheMaxEntries entries cache with $entryToFill entries"
146 * Provider for testFillingCache
148 public static function provideCacheFilling() {
149 // ($cacheMaxEntries, $entryToFill, $msg='')
161 * Create a cache with only one remaining entry then update
162 * the first inserted entry. Should bump it to the top.
164 * @covers ProcessCacheLRU::set
166 public function testReplaceExistingKeyShouldBumpEntryToTop() {
169 $cache = new ProcessCacheLRUTestable( $maxEntries );
170 // Fill cache leaving just one remaining slot
171 $this->fillCache( $cache, $maxEntries - 1 );
173 // Set an existing cache key
174 $cache->set( "cache-key-1", "prop-1", "new-value-for-1" );
178 'cache-key-2' => [ 'prop-2' => 'value-2' ],
179 'cache-key-1' => [ 'prop-1' => 'new-value-for-1' ],
186 * @covers ProcessCacheLRU::get
187 * @covers ProcessCacheLRU::set
188 * @covers ProcessCacheLRU::has
190 public function testRecentlyAccessedKeyStickIn() {
191 $cache = new ProcessCacheLRUTestable( 2 );
192 $cache->set( 'first', 'prop1', 'value1' );
193 $cache->set( 'second', 'prop2', 'value2' );
196 $cache->get( 'first', 'prop1' );
197 // Cache a third value, should invalidate the least used one
198 $cache->set( 'third', 'prop3', 'value3' );
200 $this->assertFalse( $cache->has( 'second', 'prop2' ) );
204 * This first create a full cache then update the value for the 2nd
206 * Given a cache having 1,2,3 as key, updating 2 should bump 2 to
207 * the top of the queue with the new value: 1,3,2* (* = updated).
209 * @covers ProcessCacheLRU::set
210 * @covers ProcessCacheLRU::get
212 public function testReplaceExistingKeyInAFullCacheShouldBumpToTop() {
215 $cache = new ProcessCacheLRUTestable( $maxEntries );
216 $this->fillCache( $cache, $maxEntries );
218 // Set an existing cache key
219 $cache->set( "cache-key-2", "prop-2", "new-value-for-2" );
222 'cache-key-1' => [ 'prop-1' => 'value-1' ],
223 'cache-key-3' => [ 'prop-3' => 'value-3' ],
224 'cache-key-2' => [ 'prop-2' => 'new-value-for-2' ],
228 $this->assertEquals( 'new-value-for-2',
229 $cache->get( 'cache-key-2', 'prop-2' )
234 * @covers ProcessCacheLRU::set
236 public function testBumpExistingKeyToTop() {
237 $cache = new ProcessCacheLRUTestable( 3 );
238 $this->fillCache( $cache, 3 );
240 // Set the very first cache key to a new value
241 $cache->set( "cache-key-1", "prop-1", "new value for 1" );
244 'cache-key-2' => [ 'prop-2' => 'value-2' ],
245 'cache-key-3' => [ 'prop-3' => 'value-3' ],
246 'cache-key-1' => [ 'prop-1' => 'new value for 1' ],
254 * Overrides some ProcessCacheLRU methods and properties accessibility.
256 class ProcessCacheLRUTestable
extends ProcessCacheLRU
{
257 public function getCache() {
258 return $this->cache
->toArray();
261 public function getEntriesCount() {
262 return count( $this->cache
->toArray() );