Debian Codegraph @

tests/phpunit/unit/includes/Settings/Cache/CachedSourceTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Unit\Settings\Cache;
4
5 use MediaWiki\Settings\Cache\CacheableSource;
6 use MediaWiki\Settings\Cache\CachedSource;
7 use MediaWiki\Settings\SettingsBuilderException;
8 use PHPUnit\Framework\TestCase;
9 use Wikimedia\ObjectCache\HashBagOStuff;
10
11 /**
12 * @covers \MediaWiki\Settings\Cache\CachedSource
13 */
14 class CachedSourceTest extends TestCase {
15 public function testLoadWithMiss() {
16 $settings = [ 'config' => [ 'Foo' => 'value' ] ];
17 $hashKey = 'abc123';
18 $ttl = 123;
19
20 [ $cache, $key ] = $this->createCacheMock( $hashKey );
21 $source = $this->createMock( CacheableSource::class );
22 $cacheSource = new CachedSource( $cache, $source );
23
24 $source
25 ->expects( $this->once() )
26 ->method( 'getHashKey' )
27 ->willReturn( $hashKey );
28
29 $cache
30 ->expects( $this->once() )
31 ->method( 'get' )
32 ->with( $key )
33 ->willReturn( false );
34
35 $source
36 ->expects( $this->atLeastOnce() )
37 ->method( 'getExpiryTtl' )
38 ->willReturn( $ttl );
39
40 $source
41 ->expects( $this->once() )
42 ->method( 'load' )
43 ->willReturn( $settings );
44
45 $cache
46 ->expects( $this->once() )
47 ->method( 'set' )
48 ->with(
49 $key,
50 $this->logicalAnd(
51 $this->arrayHasKey( 'value' ),
52 $this->arrayHasKey( 'expiry' ),
53 $this->arrayHasKey( 'generation' ),
54 $this->callback( function ( $item ) use ( $settings ) {
55 $this->assertSame( $settings, $item['value'] );
56 $this->assertGreaterThan( 0, $item['expiry'] );
57 $this->assertGreaterThan( 0, $item['generation'] );
58
59 return true;
60 } )
61 ),
62 $ttl
63 );
64
65 $this->assertSame( $settings, $cacheSource->load() );
66 }
67
68 public function testLoadWithHit() {
69 $settings = [ 'config' => [ 'Foo' => 'value' ] ];
70 $hashKey = 'abc123';
71
72 [ $cache, $key ] = $this->createCacheMock( $hashKey );
73 $source = $this->createMock( CacheableSource::class );
74 $cacheSource = new CachedSource( $cache, $source );
75
76 $source
77 ->expects( $this->once() )
78 ->method( 'getHashKey' )
79 ->willReturn( $hashKey );
80
81 $cache
82 ->expects( $this->once() )
83 ->method( 'get' )
84 ->with( $key )
85 ->willReturn(
86 [
87 'value' => $settings,
88 'expiry' => microtime( true ) + 60,
89 'generation' => 1.0
90 ]
91 );
92
93 // Effectively disable early expiry for the test since it's
94 // probabilistic and thus cannot be tested reliably without more
95 // dependency injection, but at least ensure that the code path is
96 // exercised.
97 $source
98 ->expects( $this->once() )
99 ->method( 'getExpiryWeight' )
100 ->willReturn( 0.0 );
101
102 $source
103 ->expects( $this->never() )
104 ->method( 'load' );
105
106 $this->assertSame( $settings, $cacheSource->load() );
107 }
108
109 public function testLoadStaleOnSourceException() {
110 $settings = [ 'config' => [ 'Foo' => 'value' ] ];
111 $hashKey = 'abc123';
112 $expired = microtime( true ) - 1;
113
114 [ $cache, $key ] = $this->createCacheMock( $hashKey );
115 $source = $this->createMock( CacheableSource::class );
116 $cacheSource = new CachedSource( $cache, $source );
117
118 $source
119 ->expects( $this->atLeastOnce() )
120 ->method( 'allowsStaleLoad' )
121 ->willReturn( true );
122
123 $source
124 ->expects( $this->once() )
125 ->method( 'getHashKey' )
126 ->willReturn( $hashKey );
127
128 $cache
129 ->expects( $this->once() )
130 ->method( 'get' )
131 ->with( $key )
132 ->willReturn(
133 [
134 'value' => $settings,
135 'expiry' => $expired,
136 'generation' => 1.0
137 ]
138 );
139
140 $source
141 ->expects( $this->once() )
142 ->method( 'load' )
143 ->willThrowException( new SettingsBuilderException( 'foo' ) );
144
145 $this->assertSame( $settings, $cacheSource->load() );
146 }
147
148 public function testLoadStaleOnLockFailure() {
149 $settings = [ 'config' => [ 'Foo' => 'value' ] ];
150 $hashKey = 'abc123';
151 $expired = microtime( true ) - 1;
152
153 [ $cache, $key ] = $this->createCacheMock( $hashKey, [ 'lock' ] );
154 $source = $this->createMock( CacheableSource::class );
155 $cacheSource = new CachedSource( $cache, $source );
156
157 $source
158 ->expects( $this->atLeastOnce() )
159 ->method( 'allowsStaleLoad' )
160 ->willReturn( true );
161
162 $source
163 ->expects( $this->once() )
164 ->method( 'getHashKey' )
165 ->willReturn( $hashKey );
166
167 $cache
168 ->expects( $this->once() )
169 ->method( 'get' )
170 ->with( $key )
171 ->willReturn(
172 [
173 'value' => $settings,
174 'expiry' => $expired,
175 'generation' => 1.0
176 ]
177 );
178
179 $cache
180 ->expects( $this->once() )
181 ->method( 'lock' )
182 ->willReturn( false );
183
184 $this->assertSame( $settings, $cacheSource->load() );
185 }
186
187 /**
188 * Creates a mock interface to the cache for the given key.
189 *
190 * @param string $hashKey Source hash key.
191 * @param ?array $methods Additional methods to mock.
192 *
193 * @return array The cache mock and global cache key.
194 */
195 private function createCacheMock( string $hashKey, ?array $methods = [] ): array {
196 $cache = $this->createPartialMock(
197 HashBagOStuff::class,
198 array_merge( [ 'get', 'makeGlobalKey', 'set' ], $methods )
199 );
200
201 $key = 'global:' . self::class . ':' . $hashKey;
202
203 $cache
204 ->expects( $this->once() )
205 ->method( 'makeGlobalKey' )
206 ->with( CachedSource::class, $hashKey )
207 ->willReturn( $key );
208
209 return [ $cache, $key ];
210 }
211 }