Merge "Remove .mw-help-field-hint and -data CSS classes from mw.legacy/shared"
[lhc/web/wiklou.git] / tests / phpunit / includes / libs / objectcache / HashBagOStuffTest.php
1 <?php
2
3 /**
4 * @group BagOStuff
5 */
6 class HashBagOStuffTest extends PHPUnit_Framework_TestCase {
7
8 /**
9 * @covers HashBagOStuff::delete
10 */
11 public function testDelete() {
12 $cache = new HashBagOStuff();
13 for ( $i = 0; $i < 10; $i++ ) {
14 $cache->set( "key$i", 1 );
15 $this->assertEquals( 1, $cache->get( "key$i" ) );
16 $cache->delete( "key$i" );
17 $this->assertEquals( false, $cache->get( "key$i" ) );
18 }
19 }
20
21 /**
22 * @covers HashBagOStuff::clear
23 */
24 public function testClear() {
25 $cache = new HashBagOStuff();
26 for ( $i = 0; $i < 10; $i++ ) {
27 $cache->set( "key$i", 1 );
28 $this->assertEquals( 1, $cache->get( "key$i" ) );
29 }
30 $cache->clear();
31 for ( $i = 0; $i < 10; $i++ ) {
32 $this->assertEquals( false, $cache->get( "key$i" ) );
33 }
34 }
35
36 /**
37 * @covers HashBagOStuff::doGet
38 * @covers HashBagOStuff::expire
39 */
40 public function testExpire() {
41 $cache = new HashBagOStuff();
42 $cacheInternal = TestingAccessWrapper::newFromObject( $cache );
43 $cache->set( 'foo', 1 );
44 $cache->set( 'bar', 1, 10 );
45 $cache->set( 'baz', 1, -10 );
46
47 $this->assertEquals( 0, $cacheInternal->bag['foo'][$cache::KEY_EXP], 'Indefinite' );
48 // 2 seconds tolerance
49 $this->assertEquals( time() + 10, $cacheInternal->bag['bar'][$cache::KEY_EXP], 'Future', 2 );
50 $this->assertEquals( time() - 10, $cacheInternal->bag['baz'][$cache::KEY_EXP], 'Past', 2 );
51
52 $this->assertEquals( 1, $cache->get( 'bar' ), 'Key not expired' );
53 $this->assertEquals( false, $cache->get( 'baz' ), 'Key expired' );
54 }
55
56 /**
57 * Ensure maxKeys eviction prefers keeping new keys.
58 *
59 * @covers HashBagOStuff::__construct
60 * @covers HashBagOStuff::set
61 */
62 public function testEvictionAdd() {
63 $cache = new HashBagOStuff( [ 'maxKeys' => 10 ] );
64 for ( $i = 0; $i < 10; $i++ ) {
65 $cache->set( "key$i", 1 );
66 $this->assertEquals( 1, $cache->get( "key$i" ) );
67 }
68 for ( $i = 10; $i < 20; $i++ ) {
69 $cache->set( "key$i", 1 );
70 $this->assertEquals( 1, $cache->get( "key$i" ) );
71 $this->assertEquals( false, $cache->get( "key" . $i - 10 ) );
72 }
73 }
74
75 /**
76 * Ensure maxKeys eviction prefers recently set keys
77 * even if the keys pre-exist.
78 *
79 * @covers HashBagOStuff::__construct
80 * @covers HashBagOStuff::set
81 */
82 public function testEvictionSet() {
83 $cache = new HashBagOStuff( [ 'maxKeys' => 3 ] );
84
85 foreach ( [ 'foo', 'bar', 'baz' ] as $key ) {
86 $cache->set( $key, 1 );
87 }
88
89 // Set existing key
90 $cache->set( 'foo', 1 );
91
92 // Add a 4th key (beyond the allowed maximum)
93 $cache->set( 'quux', 1 );
94
95 // Foo's life should have been extended over Bar
96 foreach ( [ 'foo', 'baz', 'quux' ] as $key ) {
97 $this->assertEquals( 1, $cache->get( $key ), "Kept $key" );
98 }
99 $this->assertEquals( false, $cache->get( 'bar' ), 'Evicted bar' );
100 }
101
102 /**
103 * Ensure maxKeys eviction prefers recently retrieved keys (LRU).
104 *
105 * @covers HashBagOStuff::__construct
106 * @covers HashBagOStuff::doGet
107 * @covers HashBagOStuff::hasKey
108 */
109 public function testEvictionGet() {
110 $cache = new HashBagOStuff( [ 'maxKeys' => 3 ] );
111
112 foreach ( [ 'foo', 'bar', 'baz' ] as $key ) {
113 $cache->set( $key, 1 );
114 }
115
116 // Get existing key
117 $cache->get( 'foo', 1 );
118
119 // Add a 4th key (beyond the allowed maximum)
120 $cache->set( 'quux', 1 );
121
122 // Foo's life should have been extended over Bar
123 foreach ( [ 'foo', 'baz', 'quux' ] as $key ) {
124 $this->assertEquals( 1, $cache->get( $key ), "Kept $key" );
125 }
126 $this->assertEquals( false, $cache->get( 'bar' ), 'Evicted bar' );
127 }
128 }