Skip to content

Commit

Permalink
Special-case unwrapping for async methods that use Exist* endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
polyfractal committed Aug 11, 2016
1 parent 498c003 commit 347e5c5
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions tests/Elasticsearch/Tests/YamlRunnerTest.php
Expand Up @@ -10,6 +10,7 @@
use Elasticsearch\Common\Exceptions\Missing404Exception;
use Elasticsearch\Common\Exceptions\RequestTimeout408Exception;
use Elasticsearch\Common\Exceptions\ServerErrorResponseException;
use Elasticsearch\Common\Exceptions\RoutingMissingException;
use GuzzleHttp\Ring\Future\FutureArrayInterface;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;
Expand Down Expand Up @@ -274,6 +275,29 @@ public function operationDo($operation, $lastOperationResult, &$context, $testNa
self::markTestIncomplete(sprintf('Method "%s" not implement in "%s"', $method, get_class($caller)));
}

// Exist* methods have to be manually 'unwrapped' into true/false for async
if (strpos($method, "exist") !== false && $async === true) {
return $this->executeAsyncExistRequest($caller, $method, $endpointParams, $expectedError, $testName);
}

return $this->executeRequest($caller, $method, $endpointParams, $expectedError, $testName);
}

/**
* Obtain the response from the server
*
* @param $caller
* @param $method
* @param $endpointParams
* @param $expectedError
* @param $testName
*
* @throws \Exception
*
* @return array|mixed
*/
public function executeRequest($caller, $method, $endpointParams, $expectedError, $testName)
{
try {
$response = $caller->$method($endpointParams);

Expand All @@ -291,6 +315,48 @@ public function operationDo($operation, $lastOperationResult, &$context, $testNa
}
}

/**
* Obtain the response when it is an Exists* method. These are converted into
* true/false responses
*
* @param $caller
* @param $method
* @param $endpointParams
* @param $expectedError
* @param $testName
*
* @throws \Exception
*
* @return bool
*/
public function executeAsyncExistRequest($caller, $method, $endpointParams, $expectedError, $testName)
{
try {

$response = $caller->$method($endpointParams);

while ($response instanceof FutureArrayInterface) {
$response = $response->wait();
}

if ($response['status'] === 200) {
return true;
} else {
return false;
}
} catch (Missing404Exception $exception) {
return false;
} catch (RoutingMissingException $exception) {
return false;
} catch (\Exception $exception) {
if (null !== $expectedError) {
return $this->assertException($exception, $expectedError, $testName);
}

throw $exception;
}
}

/**
* Check if a field in the last operation is false
*
Expand Down

0 comments on commit 347e5c5

Please sign in to comment.