21
21
use Elasticsearch \Client ;
22
22
use Elasticsearch \ClientBuilder ;
23
23
use Elasticsearch \Common \Exceptions \ElasticsearchException ;
24
- use Elasticsearch \Common \Exceptions \InvalidArgumentException ;
24
+ use Elasticsearch \Common \Exceptions \RuntimeException ;
25
25
use Elasticsearch \Tests \ClientBuilder \DummyLogger ;
26
- use GuzzleHttp \Ring \Client \MockHandler ;
27
26
use PHPUnit \Framework \TestCase ;
28
27
29
28
class ClientBuilderTest extends TestCase
30
29
{
31
- /**
32
- * @expectedException TypeError
33
- */
34
30
public function testClientBuilderThrowsExceptionForIncorrectLoggerClass ()
35
31
{
32
+ $ this ->expectException (\TypeError::class);
36
33
ClientBuilder::create ()->setLogger (new DummyLogger );
37
34
}
38
35
39
- /**
40
- * @expectedException TypeError
41
- */
42
36
public function testClientBuilderThrowsExceptionForIncorrectTracerClass ()
43
37
{
38
+ $ this ->expectException (\TypeError::class);
44
39
ClientBuilder::create ()->setTracer (new DummyLogger );
45
40
}
46
41
@@ -51,7 +46,7 @@ public function testGzipEnabledWhenElasticCloudId()
51
46
->build ();
52
47
53
48
$ this ->assertInstanceOf (Client::class, $ client );
54
-
49
+
55
50
try {
56
51
$ result = $ client ->info ();
57
52
} catch (ElasticsearchException $ e ) {
@@ -75,7 +70,7 @@ public function testElasticCloudIdNotOverrideCurlEncoding()
75
70
->build ();
76
71
77
72
$ this ->assertInstanceOf (Client::class, $ client );
78
-
73
+
79
74
try {
80
75
$ result = $ client ->info ();
81
76
} catch (ElasticsearchException $ e ) {
@@ -170,4 +165,135 @@ public function testNotIncludePortInHostHeader()
170
165
$ this ->assertEquals ($ host , $ request ['request ' ]['headers ' ]['Host ' ][0 ]);
171
166
}
172
167
}
173
- }
168
+
169
+ public function getCloudIdExamples ()
170
+ {
171
+ return [
172
+ ['cluster:d2VzdGV1cm9wZS5henVyZS5lbGFzdGljLWNsb3VkLmNvbTo5MjQzJGM2NjM3ZjMxMmM1MjQzY2RhN2RlZDZlOTllM2QyYzE5JA== ' , 'c6637f312c5243cda7ded6e99e3d2c19.westeurope.azure.elastic-cloud.com:9243 ' ],
173
+ ['cluster:d2VzdGV1cm9wZS5henVyZS5lbGFzdGljLWNsb3VkLmNvbSRlN2RlOWYxMzQ1ZTQ0OTAyODNkOTAzYmU1YjZmOTE5ZSQ= ' , 'e7de9f1345e4490283d903be5b6f919e.westeurope.azure.elastic-cloud.com ' ],
174
+ ['cluster:d2VzdGV1cm9wZS5henVyZS5lbGFzdGljLWNsb3VkLmNvbSQ4YWY3ZWUzNTQyMGY0NThlOTAzMDI2YjQwNjQwODFmMiQyMDA2MTU1NmM1NDA0OTg2YmZmOTU3ZDg0YTZlYjUxZg== ' , '8af7ee35420f458e903026b4064081f2.westeurope.azure.elastic-cloud.com ' ]
175
+ ];
176
+ }
177
+
178
+ /**
179
+ * @dataProvider getCloudIdExamples
180
+ */
181
+ public function testSetCloudIdWithExplicitPortOnlyEsUuid (string $ cloudId , string $ url )
182
+ {
183
+ $ client = ClientBuilder::create ()
184
+ ->setElasticCloudId ($ cloudId )
185
+ ->build ();
186
+
187
+ $ connection = $ client ->transport ->getConnection ();
188
+
189
+ $ this ->assertEquals ($ url , $ connection ->getHost ());
190
+ }
191
+
192
+ public function getConfig ()
193
+ {
194
+ return [
195
+ [[
196
+ 'hosts ' => ['localhost:9200 ' ]
197
+ ]],
198
+ [[
199
+ 'hosts ' => ['cloud:9200 ' ],
200
+ 'apiKey ' => ['id-value ' , 'apikey-value ' ]
201
+ ]],
202
+ [[
203
+ 'hosts ' => ['cloud:9200 ' ],
204
+ 'basicAuthentication ' => ['username-value ' , 'password-value ' ]
205
+ ]]
206
+ ];
207
+ }
208
+
209
+ /**
210
+ * @dataProvider getConfig
211
+ * @see https://github.com/elastic/elasticsearch-php/issues/1074
212
+ */
213
+ public function testFromConfig (array $ params )
214
+ {
215
+ $ client = ClientBuilder::fromConfig ($ params );
216
+ $ this ->assertInstanceOf (Client::class, $ client );
217
+ }
218
+
219
+ public function testFromConfigQuiteTrueWithUnknownKey ()
220
+ {
221
+ $ client = ClientBuilder::fromConfig (
222
+ [
223
+ 'hosts ' => ['localhost:9200 ' ],
224
+ 'foo ' => 'bar '
225
+ ],
226
+ true
227
+ );
228
+ }
229
+
230
+ public function testFromConfigQuiteFalseWithUnknownKey ()
231
+ {
232
+ $ this ->expectException (RuntimeException::class);
233
+ $ client = ClientBuilder::fromConfig (
234
+ [
235
+ 'hosts ' => ['localhost:9200 ' ],
236
+ 'foo ' => 'bar '
237
+ ],
238
+ false
239
+ );
240
+ }
241
+
242
+ public function testElasticClientMetaHeaderIsSentByDefault ()
243
+ {
244
+ $ client = ClientBuilder::create ()
245
+ ->build ();
246
+ $ this ->assertInstanceOf (Client::class, $ client );
247
+
248
+ try {
249
+ $ result = $ client ->info ();
250
+ } catch (ElasticsearchException $ e ) {
251
+ $ request = $ client ->transport ->getLastConnection ()->getLastRequestInfo ();
252
+ $ this ->assertTrue (isset ($ request ['request ' ]['headers ' ]['x-elastic-client-meta ' ]));
253
+ $ this ->assertEquals (
254
+ 1 ,
255
+ preg_match (
256
+ '/^[a-z]{1,}=[a-z0-9\.\-]{1,}(?:,[a-z]{1,}=[a-z0-9\.\-]+)*$/ ' ,
257
+ $ request ['request ' ]['headers ' ]['x-elastic-client-meta ' ][0 ]
258
+ )
259
+ );
260
+ }
261
+ }
262
+
263
+ public function testElasticClientMetaHeaderIsSentWhenEnabled ()
264
+ {
265
+ $ client = ClientBuilder::create ()
266
+ ->setElasticMetaHeader (true )
267
+ ->build ();
268
+ $ this ->assertInstanceOf (Client::class, $ client );
269
+
270
+ try {
271
+ $ result = $ client ->info ();
272
+ } catch (ElasticsearchException $ e ) {
273
+ $ request = $ client ->transport ->getLastConnection ()->getLastRequestInfo ();
274
+ $ this ->assertTrue (isset ($ request ['request ' ]['headers ' ]['x-elastic-client-meta ' ]));
275
+ $ this ->assertEquals (
276
+ 1 ,
277
+ preg_match (
278
+ '/^[a-z]{1,}=[a-z0-9\.\-]{1,}(?:,[a-z]{1,}=[a-z0-9\.\-]+)*$/ ' ,
279
+ $ request ['request ' ]['headers ' ]['x-elastic-client-meta ' ][0 ]
280
+ )
281
+ );
282
+ }
283
+ }
284
+
285
+ public function testElasticClientMetaHeaderIsNotSentWhenDisabled ()
286
+ {
287
+ $ client = ClientBuilder::create ()
288
+ ->setElasticMetaHeader (false )
289
+ ->build ();
290
+ $ this ->assertInstanceOf (Client::class, $ client );
291
+
292
+ try {
293
+ $ result = $ client ->info ();
294
+ } catch (ElasticsearchException $ e ) {
295
+ $ request = $ client ->transport ->getLastConnection ()->getLastRequestInfo ();
296
+ $ this ->assertFalse (isset ($ request ['request ' ]['headers ' ]['x-elastic-client-meta ' ]));
297
+ }
298
+ }
299
+ }
0 commit comments