diff --git a/tests/Elasticsearch/Tests/YamlRunnerTest.php b/tests/Elasticsearch/Tests/YamlRunnerTest.php index 29a35318c..fc73daa3a 100644 --- a/tests/Elasticsearch/Tests/YamlRunnerTest.php +++ b/tests/Elasticsearch/Tests/YamlRunnerTest.php @@ -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 */ @@ -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; @@ -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); } /** @@ -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); @@ -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) { @@ -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 { @@ -350,6 +359,8 @@ public function executeAsyncExistRequest($caller, $method, $endpointParams, $exp $response = $response->wait(); } + $this->checkForWarnings($expectedWarnings); + if ($response['status'] === 200) { return true; } else { @@ -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 *