Skip to content

Commit 71ca4e1

Browse files
authored
Fix #1074 allowing multiple function parameters in fromConfig (#1076)
* Fix #1074 allowing multiple function parameters in fromConfig * Fixed CS issues
1 parent 802d258 commit 71ca4e1

File tree

9 files changed

+61
-9
lines changed

9 files changed

+61
-9
lines changed

src/Elasticsearch/ClientBuilder.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,14 @@ public static function fromConfig(array $config, bool $quiet = false): Client
189189
$builder = new static;
190190
foreach ($config as $key => $value) {
191191
$method = "set$key";
192-
if (method_exists($builder, $method)) {
193-
$builder->$method($value);
192+
$reflection = new ReflectionClass($builder);
193+
if ($reflection->hasMethod($method)) {
194+
$func = $reflection->getMethod($method);
195+
if ($func->getNumberOfParameters() > 1) {
196+
$builder->$method(...$value);
197+
} else {
198+
$builder->$method($value);
199+
}
194200
unset($config[$key]);
195201
}
196202
}

src/Elasticsearch/Endpoints/Bulk.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,5 +93,4 @@ public function setBody($body): Bulk
9393
}
9494
return $this;
9595
}
96-
9796
}

src/Elasticsearch/Endpoints/Ml/FindFileStructure.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,5 +86,4 @@ public function setBody($body): FindFileStructure
8686
}
8787
return $this;
8888
}
89-
9089
}

src/Elasticsearch/Endpoints/Ml/PostData.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,5 +90,4 @@ public function setJobId($job_id): PostData
9090

9191
return $this;
9292
}
93-
9493
}

src/Elasticsearch/Endpoints/Monitoring/Bulk.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,5 +82,4 @@ public function setBody($body): Bulk
8282
}
8383
return $this;
8484
}
85-
8685
}

src/Elasticsearch/Endpoints/Msearch.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,5 +90,4 @@ public function setBody($body): Msearch
9090
}
9191
return $this;
9292
}
93-
9493
}

src/Elasticsearch/Endpoints/MsearchTemplate.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,5 +88,4 @@ public function setBody($body): MsearchTemplate
8888
}
8989
return $this;
9090
}
91-
9291
}

tests/Elasticsearch/Tests/ClientBuilderTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,4 +193,56 @@ public function testSetCloudIdWithExplicitPortOnlyEsUuid(string $cloudId, string
193193

194194
$this->assertEquals($url, $connection->getHost());
195195
}
196+
197+
public function getConfig()
198+
{
199+
return [
200+
[[
201+
'hosts' => ['localhost:9200']
202+
]],
203+
[[
204+
'hosts' => ['cloud:9200'],
205+
'apiKey' => ['id-value', 'apikey-value']
206+
]],
207+
[[
208+
'hosts' => ['cloud:9200'],
209+
'basicAuthentication' => ['username-value', 'password-value']
210+
]]
211+
];
212+
}
213+
214+
/**
215+
* @dataProvider getConfig
216+
* @see https://github.com/elastic/elasticsearch-php/issues/1074
217+
*/
218+
public function testFromConfig(array $params)
219+
{
220+
$client = ClientBuilder::fromConfig($params);
221+
$this->assertInstanceOf(Client::class, $client);
222+
}
223+
224+
public function testFromConfigQuiteTrueWithUnknownKey()
225+
{
226+
$client = ClientBuilder::fromConfig(
227+
[
228+
'hosts' => ['localhost:9200'],
229+
'foo' => 'bar'
230+
],
231+
true
232+
);
233+
}
234+
235+
/**
236+
* @expectedException Elasticsearch\Common\Exceptions\RuntimeException
237+
*/
238+
public function testFromConfigQuiteFalseWithUnknownKey()
239+
{
240+
$client = ClientBuilder::fromConfig(
241+
[
242+
'hosts' => ['localhost:9200'],
243+
'foo' => 'bar'
244+
],
245+
false
246+
);
247+
}
196248
}

tests/Elasticsearch/Tests/Utility.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,4 +477,4 @@ private static function waitForClusterStateUpdatesToFinish(Client $client, int $
477477
$stillWaiting = ! empty($result['tasks']);
478478
} while ($stillWaiting && time() < ($start + $timeout));
479479
}
480-
}
480+
}

0 commit comments

Comments
 (0)