Skip to content

Commit 347e5c5

Browse files
committed
Special-case unwrapping for async methods that use Exist* endpoints
1 parent 498c003 commit 347e5c5

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

tests/Elasticsearch/Tests/YamlRunnerTest.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Elasticsearch\Common\Exceptions\Missing404Exception;
1111
use Elasticsearch\Common\Exceptions\RequestTimeout408Exception;
1212
use Elasticsearch\Common\Exceptions\ServerErrorResponseException;
13+
use Elasticsearch\Common\Exceptions\RoutingMissingException;
1314
use GuzzleHttp\Ring\Future\FutureArrayInterface;
1415
use Symfony\Component\Finder\Finder;
1516
use Symfony\Component\Finder\SplFileInfo;
@@ -274,6 +275,29 @@ public function operationDo($operation, $lastOperationResult, &$context, $testNa
274275
self::markTestIncomplete(sprintf('Method "%s" not implement in "%s"', $method, get_class($caller)));
275276
}
276277

278+
// Exist* methods have to be manually 'unwrapped' into true/false for async
279+
if (strpos($method, "exist") !== false && $async === true) {
280+
return $this->executeAsyncExistRequest($caller, $method, $endpointParams, $expectedError, $testName);
281+
}
282+
283+
return $this->executeRequest($caller, $method, $endpointParams, $expectedError, $testName);
284+
}
285+
286+
/**
287+
* Obtain the response from the server
288+
*
289+
* @param $caller
290+
* @param $method
291+
* @param $endpointParams
292+
* @param $expectedError
293+
* @param $testName
294+
*
295+
* @throws \Exception
296+
*
297+
* @return array|mixed
298+
*/
299+
public function executeRequest($caller, $method, $endpointParams, $expectedError, $testName)
300+
{
277301
try {
278302
$response = $caller->$method($endpointParams);
279303

@@ -291,6 +315,48 @@ public function operationDo($operation, $lastOperationResult, &$context, $testNa
291315
}
292316
}
293317

318+
/**
319+
* Obtain the response when it is an Exists* method. These are converted into
320+
* true/false responses
321+
*
322+
* @param $caller
323+
* @param $method
324+
* @param $endpointParams
325+
* @param $expectedError
326+
* @param $testName
327+
*
328+
* @throws \Exception
329+
*
330+
* @return bool
331+
*/
332+
public function executeAsyncExistRequest($caller, $method, $endpointParams, $expectedError, $testName)
333+
{
334+
try {
335+
336+
$response = $caller->$method($endpointParams);
337+
338+
while ($response instanceof FutureArrayInterface) {
339+
$response = $response->wait();
340+
}
341+
342+
if ($response['status'] === 200) {
343+
return true;
344+
} else {
345+
return false;
346+
}
347+
} catch (Missing404Exception $exception) {
348+
return false;
349+
} catch (RoutingMissingException $exception) {
350+
return false;
351+
} catch (\Exception $exception) {
352+
if (null !== $expectedError) {
353+
return $this->assertException($exception, $expectedError, $testName);
354+
}
355+
356+
throw $exception;
357+
}
358+
}
359+
294360
/**
295361
* Check if a field in the last operation is false
296362
*

0 commit comments

Comments
 (0)