Skip to content

Commit f4b1d20

Browse files
committed
Fixed tests + updated README
1 parent 02d4590 commit f4b1d20

20 files changed

+288
-41
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
elasticsearch-php
44
=================
55

6-
[![Build Status](https://travis-ci.org/elastic/elasticsearch-php.svg?branch=master)](https://travis-ci.org/elastic/elasticsearch-php) [![Latest Stable Version](https://poser.pugx.org/elasticsearch/elasticsearch/v/stable)](https://packagist.org/packages/elasticsearch/elasticsearch) [![Total Downloads](https://poser.pugx.org/elasticsearch/elasticsearch/downloads)](https://packagist.org/packages/elasticsearch/elasticsearch)
6+
[![Build status](https://github.com/elastic/elasticsearch-php/workflows/PHP%20test/badge.svg)](https://github.com/elastic/elasticsearch-php/actions) [![Latest Stable Version](https://poser.pugx.org/elasticsearch/elasticsearch/v/stable)](https://packagist.org/packages/elasticsearch/elasticsearch) [![Total Downloads](https://poser.pugx.org/elasticsearch/elasticsearch/downloads)](https://packagist.org/packages/elasticsearch/elasticsearch)
77

88
Official low-level client for Elasticsearch. Its goal is to provide common ground for all Elasticsearch-related code in PHP; because of this it tries to be opinion-free and very extendable.
99

src/Elasticsearch/Connections/ConnectionInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,5 +75,5 @@ public function getLastRequestInfo(): array;
7575
* @param null $body
7676
* @return mixed
7777
*/
78-
public function performRequest(string $method, string $uri, array $params = [], $body = null, array $options = [], Transport $transport = null);
78+
public function performRequest(string $method, string $uri, ?array $params = [], $body = null, array $options = [], Transport $transport = null);
7979
}

tests/Elasticsearch/Tests/ClientBuilderTest.php

Lines changed: 137 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,21 @@
2121
use Elasticsearch\Client;
2222
use Elasticsearch\ClientBuilder;
2323
use Elasticsearch\Common\Exceptions\ElasticsearchException;
24-
use Elasticsearch\Common\Exceptions\InvalidArgumentException;
24+
use Elasticsearch\Common\Exceptions\RuntimeException;
2525
use Elasticsearch\Tests\ClientBuilder\DummyLogger;
26-
use GuzzleHttp\Ring\Client\MockHandler;
2726
use PHPUnit\Framework\TestCase;
2827

2928
class ClientBuilderTest extends TestCase
3029
{
31-
/**
32-
* @expectedException TypeError
33-
*/
3430
public function testClientBuilderThrowsExceptionForIncorrectLoggerClass()
3531
{
32+
$this->expectException(\TypeError::class);
3633
ClientBuilder::create()->setLogger(new DummyLogger);
3734
}
3835

39-
/**
40-
* @expectedException TypeError
41-
*/
4236
public function testClientBuilderThrowsExceptionForIncorrectTracerClass()
4337
{
38+
$this->expectException(\TypeError::class);
4439
ClientBuilder::create()->setTracer(new DummyLogger);
4540
}
4641

@@ -51,7 +46,7 @@ public function testGzipEnabledWhenElasticCloudId()
5146
->build();
5247

5348
$this->assertInstanceOf(Client::class, $client);
54-
49+
5550
try {
5651
$result = $client->info();
5752
} catch (ElasticsearchException $e) {
@@ -75,7 +70,7 @@ public function testElasticCloudIdNotOverrideCurlEncoding()
7570
->build();
7671

7772
$this->assertInstanceOf(Client::class, $client);
78-
73+
7974
try {
8075
$result = $client->info();
8176
} catch (ElasticsearchException $e) {
@@ -170,4 +165,135 @@ public function testNotIncludePortInHostHeader()
170165
$this->assertEquals($host, $request['request']['headers']['Host'][0]);
171166
}
172167
}
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+
}

tests/Elasticsearch/Tests/ClientIntegrationTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class ClientIntegrationTest extends \PHPUnit\Framework\TestCase
4444
*/
4545
private $host;
4646

47-
public function setUp()
47+
public function setUp(): void
4848
{
4949
$this->host = Utility::getHost();
5050
if (null == $this->host) {

tests/Elasticsearch/Tests/ClientTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
*/
3434
class ClientTest extends \PHPUnit\Framework\TestCase
3535
{
36-
public function tearDown()
36+
public function tearDown(): void
3737
{
3838
m::close();
3939
}

tests/Elasticsearch/Tests/ConnectionPool/Selectors/RoundRobinSelectorTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
* the GNU Lesser General Public License, Version 2.1, at your option.
1313
* See the LICENSE file in the project root for more information.
1414
*/
15-
16-
1715
declare(strict_types = 1);
1816

1917
namespace Elasticsearch\Tests\ConnectionPool\Selectors;

tests/Elasticsearch/Tests/ConnectionPool/Selectors/StickyRoundRobinSelectorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
*/
3030
class StickyRoundRobinSelectorTest extends \PHPUnit\Framework\TestCase
3131
{
32-
public function tearDown()
32+
public function tearDown(): void
3333
{
3434
m::close();
3535
}

tests/Elasticsearch/Tests/ConnectionPool/SniffingConnectionPoolIntegrationTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
*/
2929
class SniffingConnectionPoolIntegrationTest extends \PHPUnit\Framework\TestCase
3030
{
31-
protected function setUp()
31+
protected function setUp(): void
3232
{
3333
static::markTestSkipped("All of Sniffing unit tests use outdated cluster state format, need to redo");
3434
}

tests/Elasticsearch/Tests/ConnectionPool/SniffingConnectionPoolTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@
3232
*/
3333
class SniffingConnectionPoolTest extends \PHPUnit\Framework\TestCase
3434
{
35-
protected function setUp()
35+
protected function setUp(): void
3636
{
3737
static::markTestSkipped("All of Sniffing unit tests use outdated cluster state format, need to redo");
3838
}
3939

4040

41-
public function tearDown()
41+
public function tearDown(): void
4242
{
4343
m::close();
4444
}

tests/Elasticsearch/Tests/ConnectionPool/StaticConnectionPoolIntegrationTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class StaticConnectionPoolIntegrationTest extends \PHPUnit\Framework\TestCase
3434
*/
3535
private $host;
3636

37-
public function setUp()
37+
public function setUp(): void
3838
{
3939
$this->host = Utility::getHost();
4040
if (null == $this->host) {

tests/Elasticsearch/Tests/ConnectionPool/StaticConnectionPoolTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
*/
3333
class StaticConnectionPoolTest extends \PHPUnit\Framework\TestCase
3434
{
35-
public function tearDown()
35+
public function tearDown(): void
3636
{
3737
m::close();
3838
}

0 commit comments

Comments
 (0)