MediaWiki REL1_32
MapCacheLRUTest.php
Go to the documentation of this file.
1<?php
5class MapCacheLRUTest extends PHPUnit\Framework\TestCase {
6
7 use MediaWikiCoversValidator;
8
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
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
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
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 $this->assertEquals( 'xxx', $cache->get( 'd', 30 ) );
162
163 $now += 1.5;
164 $this->assertFalse( $cache->has( 'd', 30 ) );
165 $this->assertEquals( 'xxx', $cache->get( 'd' ) );
166 $this->assertNull( $cache->get( 'd', 30 ) );
167 }
168
174 public function testFields() {
175 $raw = [ 'a' => 1, 'b' => 2, 'c' => 3 ];
176 $cache = MapCacheLRU::newFromArray( $raw, 3 );
177
178 $now = microtime( true );
179 $cache->setMockTime( $now );
180
181 $cache->setField( 'PMs', 'Tony Blair', 'Labour' );
182 $cache->setField( 'PMs', 'Margaret Thatcher', 'Tory' );
183 $this->assertTrue( $cache->hasField( 'PMs', 'Tony Blair', 30 ) );
184 $this->assertEquals( 'Labour', $cache->getField( 'PMs', 'Tony Blair' ) );
185 $this->assertTrue( $cache->hasField( 'PMs', 'Tony Blair', 30 ) );
186
187 $now += 29;
188 $this->assertTrue( $cache->hasField( 'PMs', 'Tony Blair', 30 ) );
189 $this->assertEquals( 'Labour', $cache->getField( 'PMs', 'Tony Blair' ) );
190 $this->assertEquals( 'Labour', $cache->getField( 'PMs', 'Tony Blair', 30 ) );
191
192 $now += 1.5;
193 $this->assertFalse( $cache->hasField( 'PMs', 'Tony Blair', 30 ) );
194 $this->assertEquals( 'Labour', $cache->getField( 'PMs', 'Tony Blair' ) );
195 $this->assertNull( $cache->getField( 'PMs', 'Tony Blair', 30 ) );
196
197 $this->assertEquals(
198 [ 'Tony Blair' => 'Labour', 'Margaret Thatcher' => 'Tory' ],
199 $cache->get( 'PMs' )
200 );
201
202 $cache->set( 'MPs', [
203 'Edwina Currie' => 1983,
204 'Neil Kinnock' => 1970
205 ] );
206 $this->assertEquals(
207 [
208 'Edwina Currie' => 1983,
209 'Neil Kinnock' => 1970
210 ],
211 $cache->get( 'MPs' )
212 );
213
214 $this->assertEquals( 1983, $cache->getField( 'MPs', 'Edwina Currie' ) );
215 $this->assertEquals( 1970, $cache->getField( 'MPs', 'Neil Kinnock' ) );
216 }
217
226 public function testInvalidKeys() {
227 $cache = MapCacheLRU::newFromArray( [], 3 );
228
229 try {
230 $cache->has( 3.4 );
231 $this->fail( "No exception" );
232 } catch ( UnexpectedValueException $e ) {
233 $this->assertRegExp( '/must be string or integer/', $e->getMessage() );
234 }
235 try {
236 $cache->get( false );
237 $this->fail( "No exception" );
238 } catch ( UnexpectedValueException $e ) {
239 $this->assertRegExp( '/must be string or integer/', $e->getMessage() );
240 }
241 try {
242 $cache->set( 3.4, 'x' );
243 $this->fail( "No exception" );
244 } catch ( UnexpectedValueException $e ) {
245 $this->assertRegExp( '/must be string or integer/', $e->getMessage() );
246 }
247
248 try {
249 $cache->hasField( 'x', 3.4 );
250 $this->fail( "No exception" );
251 } catch ( UnexpectedValueException $e ) {
252 $this->assertRegExp( '/must be string or integer/', $e->getMessage() );
253 }
254 try {
255 $cache->getField( 'x', false );
256 $this->fail( "No exception" );
257 } catch ( UnexpectedValueException $e ) {
258 $this->assertRegExp( '/must be string or integer/', $e->getMessage() );
259 }
260 try {
261 $cache->setField( 'x', 3.4, 'x' );
262 $this->fail( "No exception" );
263 } catch ( UnexpectedValueException $e ) {
264 $this->assertRegExp( '/must be string or integer/', $e->getMessage() );
265 }
266 }
267}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
serialize()
unserialize( $serialized)
testExpiry()
MapCacheLRU::has() MapCacheLRU::get() MapCacheLRU::set()
testFields()
MapCacheLRU::hasField() MapCacheLRU::getField() MapCacheLRU::setField()
testLRU()
MapCacheLRU::has() MapCacheLRU::get() MapCacheLRU::set()
testInvalidKeys()
MapCacheLRU::has() MapCacheLRU::get() MapCacheLRU::set() MapCacheLRU::hasField() MapCacheLRU::getFiel...
testSerialize()
MapCacheLRU::serialize() MapCacheLRU::unserialize()
testArrayConversion()
MapCacheLRU::newFromArray() MapCacheLRU::toArray() MapCacheLRU::getAllKeys() MapCacheLRU::clear() Map...
returning false will NOT prevent logging $e
Definition hooks.txt:2226
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37
$cache
Definition mcc.php:33