diff --git a/src/Elasticsearch/ClientBuilder.php b/src/Elasticsearch/ClientBuilder.php index 08f0b09e5..7f88ce78e 100644 --- a/src/Elasticsearch/ClientBuilder.php +++ b/src/Elasticsearch/ClientBuilder.php @@ -189,8 +189,14 @@ public static function fromConfig(array $config, bool $quiet = false): Client $builder = new static; foreach ($config as $key => $value) { $method = "set$key"; - if (method_exists($builder, $method)) { - $builder->$method($value); + $reflection = new ReflectionClass($builder); + if ($reflection->hasMethod($method)) { + $func = $reflection->getMethod($method); + if ($func->getNumberOfParameters() > 1) { + $builder->$method(...$value); + } else { + $builder->$method($value); + } unset($config[$key]); } } diff --git a/src/Elasticsearch/Endpoints/Bulk.php b/src/Elasticsearch/Endpoints/Bulk.php index d581f1275..dbaafd8eb 100644 --- a/src/Elasticsearch/Endpoints/Bulk.php +++ b/src/Elasticsearch/Endpoints/Bulk.php @@ -93,5 +93,4 @@ public function setBody($body): Bulk } return $this; } - } diff --git a/src/Elasticsearch/Endpoints/Ml/FindFileStructure.php b/src/Elasticsearch/Endpoints/Ml/FindFileStructure.php index 60114e1cf..49beea9d9 100644 --- a/src/Elasticsearch/Endpoints/Ml/FindFileStructure.php +++ b/src/Elasticsearch/Endpoints/Ml/FindFileStructure.php @@ -86,5 +86,4 @@ public function setBody($body): FindFileStructure } return $this; } - } diff --git a/src/Elasticsearch/Endpoints/Ml/PostData.php b/src/Elasticsearch/Endpoints/Ml/PostData.php index bfb3f66cc..9816b650a 100644 --- a/src/Elasticsearch/Endpoints/Ml/PostData.php +++ b/src/Elasticsearch/Endpoints/Ml/PostData.php @@ -90,5 +90,4 @@ public function setJobId($job_id): PostData return $this; } - } diff --git a/src/Elasticsearch/Endpoints/Monitoring/Bulk.php b/src/Elasticsearch/Endpoints/Monitoring/Bulk.php index 21b312697..a8cd4a563 100644 --- a/src/Elasticsearch/Endpoints/Monitoring/Bulk.php +++ b/src/Elasticsearch/Endpoints/Monitoring/Bulk.php @@ -82,5 +82,4 @@ public function setBody($body): Bulk } return $this; } - } diff --git a/src/Elasticsearch/Endpoints/Msearch.php b/src/Elasticsearch/Endpoints/Msearch.php index 7c491fc35..ea720b9d7 100644 --- a/src/Elasticsearch/Endpoints/Msearch.php +++ b/src/Elasticsearch/Endpoints/Msearch.php @@ -90,5 +90,4 @@ public function setBody($body): Msearch } return $this; } - } diff --git a/src/Elasticsearch/Endpoints/MsearchTemplate.php b/src/Elasticsearch/Endpoints/MsearchTemplate.php index c9524587b..170b1a31f 100644 --- a/src/Elasticsearch/Endpoints/MsearchTemplate.php +++ b/src/Elasticsearch/Endpoints/MsearchTemplate.php @@ -88,5 +88,4 @@ public function setBody($body): MsearchTemplate } return $this; } - } diff --git a/tests/Elasticsearch/Tests/ClientBuilderTest.php b/tests/Elasticsearch/Tests/ClientBuilderTest.php index 56c2eace6..d437b66de 100644 --- a/tests/Elasticsearch/Tests/ClientBuilderTest.php +++ b/tests/Elasticsearch/Tests/ClientBuilderTest.php @@ -193,4 +193,56 @@ public function testSetCloudIdWithExplicitPortOnlyEsUuid(string $cloudId, string $this->assertEquals($url, $connection->getHost()); } + + public function getConfig() + { + return [ + [[ + 'hosts' => ['localhost:9200'] + ]], + [[ + 'hosts' => ['cloud:9200'], + 'apiKey' => ['id-value', 'apikey-value'] + ]], + [[ + 'hosts' => ['cloud:9200'], + 'basicAuthentication' => ['username-value', 'password-value'] + ]] + ]; + } + + /** + * @dataProvider getConfig + * @see https://github.com/elastic/elasticsearch-php/issues/1074 + */ + public function testFromConfig(array $params) + { + $client = ClientBuilder::fromConfig($params); + $this->assertInstanceOf(Client::class, $client); + } + + public function testFromConfigQuiteTrueWithUnknownKey() + { + $client = ClientBuilder::fromConfig( + [ + 'hosts' => ['localhost:9200'], + 'foo' => 'bar' + ], + true + ); + } + + /** + * @expectedException Elasticsearch\Common\Exceptions\RuntimeException + */ + public function testFromConfigQuiteFalseWithUnknownKey() + { + $client = ClientBuilder::fromConfig( + [ + 'hosts' => ['localhost:9200'], + 'foo' => 'bar' + ], + false + ); + } } diff --git a/tests/Elasticsearch/Tests/Utility.php b/tests/Elasticsearch/Tests/Utility.php index 8343588d6..1ff9399d3 100644 --- a/tests/Elasticsearch/Tests/Utility.php +++ b/tests/Elasticsearch/Tests/Utility.php @@ -477,4 +477,4 @@ private static function waitForClusterStateUpdatesToFinish(Client $client, int $ $stillWaiting = ! empty($result['tasks']); } while ($stillWaiting && time() < ($start + $timeout)); } -} \ No newline at end of file +}