Debian Codegraph @

tests/phpunit/unit/includes/Settings/Source/EtcdSourceTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Unit\Settings\Source;
4
5 use GuzzleHttp\Client;
6 use GuzzleHttp\Exception\ClientException;
7 use GuzzleHttp\Exception\ConnectException;
8 use GuzzleHttp\Exception\GuzzleException;
9 use GuzzleHttp\Exception\ServerException;
10 use GuzzleHttp\Handler\MockHandler;
11 use GuzzleHttp\HandlerStack;
12 use GuzzleHttp\Psr7\Request;
13 use GuzzleHttp\Psr7\Response;
14 use MediaWiki\Settings\SettingsBuilderException;
15 use MediaWiki\Settings\Source\EtcdSource;
16 use PHPUnit\Framework\TestCase;
17
18 /**
19 * @covers \MediaWiki\Settings\Source\EtcdSource
20 */
21 class EtcdSourceTest extends TestCase {
22 public function testGetExpiryTtl() {
23 $source = new EtcdSource();
24 $this->assertSame( 10, $source->getExpiryTtl() );
25 }
26
27 public function testGetExpiryWeight() {
28 $source = new EtcdSource();
29 $this->assertSame( 1.0, $source->getExpiryWeight() );
30 }
31
32 public function testGetHashKey() {
33 $source = new EtcdSource( [ 'host' => 'an.example' ] );
34
35 $this->assertSame(
36 'https://_etcd-client-ssl._tcp.an.example/v2/keys/mediawiki/?recursive=true',
37 $source->getHashKey()
38 );
39 }
40
41 public function testGetHashKeyNoDiscovery() {
42 $source = new EtcdSource( [
43 'host' => 'an.example',
44 'discover' => false,
45 ] );
46
47 $this->assertSame(
48 'https://an.example:2379/v2/keys/mediawiki/?recursive=true',
49 $source->getHashKey()
50 );
51 }
52
53 public function testToString() {
54 $source = new EtcdSource( [
55 'host' => 'an.example',
56 'discover' => false,
57 ] );
58
59 $this->assertSame(
60 'https://an.example:2379/v2/keys/mediawiki/?recursive=true',
61 (string)$source
62 );
63 }
64
65 public function testLoad() {
66 $mapper = $this->mockCallable();
67 $client = $this->mockClientWithResponses( [ [ 200, 'valid.json' ] ] );
68 $resolver = $this->mockCallable();
69 $settings = [
70 'Fish/One' => 'fish',
71 'Fish/Two' => 'fish',
72 'MoreFish/Red' => 'fish',
73 'MoreFish/Blue' => 'fish',
74 ];
75
76 $source = new EtcdSource( [], $mapper, $client, $resolver );
77
78 $mapper->expects( $this->once() )
79 ->method( '__invoke' )
80 ->with( $settings )
81 ->willReturn( $settings );
82
83 $resolver->expects( $this->once() )
84 ->method( '__invoke' )
85 ->willReturn( [
86 [ 'an.example', 4001 ]
87 ] );
88
89 $this->assertSame(
90 [
91 'Fish/One' => 'fish',
92 'Fish/Two' => 'fish',
93 'MoreFish/Red' => 'fish',
94 'MoreFish/Blue' => 'fish',
95 ],
96 $source->load()
97 );
98 }
99
100 /**
101 * @dataProvider serverFailures
102 */
103 public function testLoadAllServersFailed( GuzzleException $exception ) {
104 $client = $this->mockClientWithResponses( [
105 $exception,
106 $exception,
107 ] );
108 $resolver = $this->mockCallable();
109
110 $source = new EtcdSource( [], null, $client, $resolver );
111
112 $resolver->expects( $this->once() )
113 ->method( '__invoke' )
114 ->willReturn( [
115 [ 'bad.example', 123 ],
116 [ 'bad.example', 321 ],
117 ] );
118
119 $this->expectException( SettingsBuilderException::class );
120
121 $source->load();
122 }
123
124 /**
125 * @dataProvider serverFailures
126 */
127 public function testLoadSomeServersFailed( GuzzleException $exception ) {
128 $client = $this->mockClientWithResponses( [
129 $exception,
130 [ 200, 'valid.json' ],
131 ] );
132 $resolver = $this->mockCallable();
133
134 $source = new EtcdSource( [], null, $client, $resolver );
135
136 $resolver->expects( $this->once() )
137 ->method( '__invoke' )
138 ->willReturn( [
139 [ 'bad.example', 123 ],
140 [ 'ok.example', 321 ],
141 ] );
142
143 $this->assertSame(
144 [
145 'Fish/One' => 'fish',
146 'Fish/Two' => 'fish',
147 'MoreFish/Red' => 'fish',
148 'MoreFish/Blue' => 'fish',
149 ],
150 $source->load()
151 );
152 }
153
154 public function testLoadClientFailed() {
155 $client = $this->mockClientWithResponses( [
156 new ClientException(
157 'bad request',
158 new Request( 'GET', '/' ),
159 new Response( 400, [] )
160 )
161 ] );
162 $resolver = $this->mockCallable();
163
164 $source = new EtcdSource( [], null, $client, $resolver );
165
166 $resolver->expects( $this->once() )
167 ->method( '__invoke' )
168 ->willReturn( [
169 [ 'ok.example', 123 ],
170 [ 'ok.example', 321 ],
171 ] );
172
173 $this->expectException( SettingsBuilderException::class );
174
175 $source->load();
176 }
177
178 public function testLoadNotAnEtcdDirectory() {
179 $client = $this->mockClientWithResponses( [
180 [ 200, 'notadirectory.json' ],
181 ] );
182 $resolver = $this->mockCallable();
183
184 $source = new EtcdSource( [], null, $client, $resolver );
185
186 $resolver->expects( $this->once() )
187 ->method( '__invoke' )
188 ->willReturn( [
189 [ 'an.example', 123 ],
190 ] );
191
192 $this->expectException( SettingsBuilderException::class );
193
194 $source->load();
195 }
196
197 /**
198 * All possible server-side exceptions.
199 *
200 * @return array
201 */
202 public function serverFailures(): array {
203 return [
204 [
205 new ConnectException(
206 'connection failure',
207 new Request( 'GET', '/foo' )
208 )
209 ],
210 [
211 new ServerException(
212 'server side failure',
213 new Request( 'GET', '/foo' ),
214 new Response( 500, [], 'server error' )
215 ),
216 ],
217 ];
218 }
219
220 /**
221 * @param array $responses
222 *
223 * @return Client
224 */
225 private function mockClientWithResponses( array $responses ): Client {
226 return new Client( [
227 'handler' => HandlerStack::create(
228 new MockHandler( array_map( static function ( $response ) {
229 return is_array( $response )
230 ? new Response(
231 $response[0],
232 [ 'content-type' => 'application/json' ],
233 file_get_contents( __DIR__ . '/fixtures/etcd/' . $response[1] )
234 )
235 : $response;
236 }, $responses ) )
237 ),
238 ] );
239 }
240
241 /**
242 * @return callable
243 */
244 private function mockCallable() {
245 return $this
246 ->getMockBuilder( __CLASS__ )
247 ->addMethods( [ '__invoke' ] )
248 ->getMock();
249 }
250 }