Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[TEST] Add support for warning header checks
  • Loading branch information
polyfractal committed Nov 2, 2016
1 parent eb4117c commit ac1b053
Showing 1 changed file with 49 additions and 6 deletions.
55 changes: 49 additions & 6 deletions tests/Elasticsearch/Tests/YamlRunnerTest.php
Expand Up @@ -42,7 +42,7 @@ class YamlRunnerTest extends \PHPUnit_Framework_TestCase

/** @var array A list of supported features */
private static $supportedFeatures = [
'stash_in_path',
'stash_in_path', 'warnings'
];

/** @var array A mapping for endpoint when there is a reserved keywords for the method / namespace name */
Expand Down Expand Up @@ -236,13 +236,20 @@ public function processOperation($operation, $lastOperationResult, &$context, $t
public function operationDo($operation, $lastOperationResult, &$context, $testName, $async = false)
{
$expectedError = null;
$expectedWarnings = null;

// Check if a error must be catched
// Check if a error must be caught
if ('catch' === key($operation)) {
$expectedError = current($operation);
next($operation);
}

// Check if a warning must be caught
if ('warnings' === key($operation)) {
$expectedWarnings = current($operation);
next($operation);
}

$endpointInfo = explode('.', key($operation));
$endpointParams = $this->replaceWithContext(current($operation), $context);
$caller = $this->client;
Expand Down Expand Up @@ -285,10 +292,10 @@ public function operationDo($operation, $lastOperationResult, &$context, $testNa

// 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->executeAsyncExistRequest($caller, $method, $endpointParams, $expectedError, $expectedWarnings, $testName);
}

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

/**
Expand All @@ -304,7 +311,7 @@ public function operationDo($operation, $lastOperationResult, &$context, $testNa
*
* @return array|mixed
*/
public function executeRequest($caller, $method, $endpointParams, $expectedError, $testName)
public function executeRequest($caller, $method, $endpointParams, $expectedError, $expectedWarnings, $testName)
{
try {
$response = $caller->$method($endpointParams);
Expand All @@ -313,6 +320,8 @@ public function executeRequest($caller, $method, $endpointParams, $expectedError
$response = $response->wait();
}

$this->checkForWarnings($expectedWarnings);

return $response;
} catch (\Exception $exception) {
if (null !== $expectedError) {
Expand Down Expand Up @@ -340,7 +349,7 @@ public function executeRequest($caller, $method, $endpointParams, $expectedError
*
* @return bool
*/
public function executeAsyncExistRequest($caller, $method, $endpointParams, $expectedError, $testName)
public function executeAsyncExistRequest($caller, $method, $endpointParams, $expectedError, $expectedWarnings, $testName)
{
try {

Expand All @@ -350,6 +359,8 @@ public function executeAsyncExistRequest($caller, $method, $endpointParams, $exp
$response = $response->wait();
}

$this->checkForWarnings($expectedWarnings);

if ($response['status'] === 200) {
return true;
} else {
Expand All @@ -368,6 +379,38 @@ public function executeAsyncExistRequest($caller, $method, $endpointParams, $exp
}
}

public function checkForWarnings($expectedWarnings) {
$last = $this->client->transport->getLastConnection()->getLastRequestInfo();


// We have some warnings to check
if ($expectedWarnings !== null) {
if (isset($last['response']['headers']['Warning']) === true) {
foreach ($last['response']['headers']['Warning'] as $warning) {
$position = array_search($warning, $expectedWarnings);
if ($position !== false) {
// found the warning
unset($expectedWarnings[$position]);
} else {
// didn't find, throw error
throw new \Exception("Expected to find warning [$warning] but did not.");
}
}
if (count($expectedWarnings) > 0) {
throw new \Exception("Expected to find more warnings: ". print_r($expectedWarnings, true));
}
}
} else {
// no expected warnings, make sure we have none returned
if (isset($last['response']['headers']['Warning']) === true) {
throw new \Exception("Did not expect to find warnings, found some instead: "
. print_r($last['response']['headers']['Warning'], true));
}
}


}

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

0 comments on commit ac1b053

Please sign in to comment.