Merge "Introduce new schema flags and use them in RevisionStore."
[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 testLRU() {
78 $raw = [ 'a' => 1, 'b' => 2, 'c' => 3 ];
79 $cache = MapCacheLRU::newFromArray( $raw, 3 );
80
81 $this->assertSame( true, $cache->has( 'c' ) );
82 $this->assertSame(
83 [ 'a' => 1, 'b' => 2, 'c' => 3 ],
84 $cache->toArray()
85 );
86
87 $this->assertSame( 3, $cache->get( 'c' ) );
88 $this->assertSame(
89 [ 'a' => 1, 'b' => 2, 'c' => 3 ],
90 $cache->toArray()
91 );
92
93 $this->assertSame( 1, $cache->get( 'a' ) );
94 $this->assertSame(
95 [ 'b' => 2, 'c' => 3, 'a' => 1 ],
96 $cache->toArray()
97 );
98
99 $cache->set( 'a', 1 );
100 $this->assertSame(
101 [ 'b' => 2, 'c' => 3, 'a' => 1 ],
102 $cache->toArray()
103 );
104
105 $cache->set( 'b', 22 );
106 $this->assertSame(
107 [ 'c' => 3, 'a' => 1, 'b' => 22 ],
108 $cache->toArray()
109 );
110
111 $cache->set( 'd', 4 );
112 $this->assertSame(
113 [ 'a' => 1, 'b' => 22, 'd' => 4 ],
114 $cache->toArray()
115 );
116
117 $cache->set( 'e', 5, 0.33 );
118 $this->assertSame(
119 [ 'e' => 5, 'b' => 22, 'd' => 4 ],
120 $cache->toArray()
121 );
122
123 $cache->set( 'f', 6, 0.66 );
124 $this->assertSame(
125 [ 'b' => 22, 'f' => 6, 'd' => 4 ],
126 $cache->toArray()
127 );
128
129 $cache->set( 'g', 7, 0.90 );
130 $this->assertSame(
131 [ 'f' => 6, 'g' => 7, 'd' => 4 ],
132 $cache->toArray()
133 );
134
135 $cache->set( 'g', 7, 1.0 );
136 $this->assertSame(
137 [ 'f' => 6, 'd' => 4, 'g' => 7 ],
138 $cache->toArray()
139 );
140 }
141
142 /**
143 * @covers MapCacheLRU::has()
144 * @covers MapCacheLRU::get()
145 * @covers MapCacheLRU::set()
146 */
147 public function testExpiry() {
148 $raw = [ 'a' => 1, 'b' => 2, 'c' => 3 ];
149 $cache = MapCacheLRU::newFromArray( $raw, 3 );
150
151 $now = microtime( true );
152 $cache->setMockTime( $now );
153
154 $cache->set( 'd', 'xxx' );
155 $this->assertTrue( $cache->has( 'd', 30 ) );
156 $this->assertEquals( 'xxx', $cache->get( 'd' ) );
157
158 $now += 29;
159 $this->assertTrue( $cache->has( 'd', 30 ) );
160 $this->assertEquals( 'xxx', $cache->get( 'd' ) );
161
162 $now += 1.5;
163 $this->assertFalse( $cache->has( 'd', 30 ) );
164 $this->assertEquals( 'xxx', $cache->get( 'd' ) );
165 }
166
167 /**
168 * @covers MapCacheLRU::hasField()
169 * @covers MapCacheLRU::getField()
170 * @covers MapCacheLRU::setField()
171 */
172 public function testFields() {
173 $raw = [ 'a' => 1, 'b' => 2, 'c' => 3 ];
174 $cache = MapCacheLRU::newFromArray( $raw, 3 );
175
176 $now = microtime( true );
177 $cache->setMockTime( $now );
178
179 $cache->setField( 'PMs', 'Tony Blair', 'Labour' );
180 $cache->setField( 'PMs', 'Margaret Thatcher', 'Tory' );
181 $this->assertTrue( $cache->hasField( 'PMs', 'Tony Blair', 30 ) );
182 $this->assertEquals( 'Labour', $cache->getField( 'PMs', 'Tony Blair' ) );
183
184 $now += 29;
185 $this->assertTrue( $cache->hasField( 'PMs', 'Tony Blair', 30 ) );
186 $this->assertEquals( 'Labour', $cache->getField( 'PMs', 'Tony Blair' ) );
187
188 $now += 1.5;
189 $this->assertFalse( $cache->hasField( 'PMs', 'Tony Blair', 30 ) );
190 $this->assertEquals( 'Labour', $cache->getField( 'PMs', 'Tony Blair' ) );
191
192 $this->assertEquals(
193 [ 'Tony Blair' => 'Labour', 'Margaret Thatcher' => 'Tory' ],
194 $cache->get( 'PMs' )
195 );
196
197 $cache->set( 'MPs', [
198 'Edwina Currie' => 1983,
199 'Neil Kinnock' => 1970
200 ] );
201 $this->assertEquals(
202 [
203 'Edwina Currie' => 1983,
204 'Neil Kinnock' => 1970
205 ],
206 $cache->get( 'MPs' )
207 );
208
209 $this->assertEquals( 1983, $cache->getField( 'MPs', 'Edwina Currie' ) );
210 $this->assertEquals( 1970, $cache->getField( 'MPs', 'Neil Kinnock' ) );
211 }
212 }