Skip to content

Commit ac1b053

Browse files
committed
[TEST] Add support for warning header checks
1 parent eb4117c commit ac1b053

File tree

1 file changed

+49
-6
lines changed

1 file changed

+49
-6
lines changed

tests/Elasticsearch/Tests/YamlRunnerTest.php

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class YamlRunnerTest extends \PHPUnit_Framework_TestCase
4242

4343
/** @var array A list of supported features */
4444
private static $supportedFeatures = [
45-
'stash_in_path',
45+
'stash_in_path', 'warnings'
4646
];
4747

4848
/** @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
236236
public function operationDo($operation, $lastOperationResult, &$context, $testName, $async = false)
237237
{
238238
$expectedError = null;
239+
$expectedWarnings = null;
239240

240-
// Check if a error must be catched
241+
// Check if a error must be caught
241242
if ('catch' === key($operation)) {
242243
$expectedError = current($operation);
243244
next($operation);
244245
}
245246

247+
// Check if a warning must be caught
248+
if ('warnings' === key($operation)) {
249+
$expectedWarnings = current($operation);
250+
next($operation);
251+
}
252+
246253
$endpointInfo = explode('.', key($operation));
247254
$endpointParams = $this->replaceWithContext(current($operation), $context);
248255
$caller = $this->client;
@@ -285,10 +292,10 @@ public function operationDo($operation, $lastOperationResult, &$context, $testNa
285292

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

291-
return $this->executeRequest($caller, $method, $endpointParams, $expectedError, $testName);
298+
return $this->executeRequest($caller, $method, $endpointParams, $expectedError, $expectedWarnings, $testName);
292299
}
293300

294301
/**
@@ -304,7 +311,7 @@ public function operationDo($operation, $lastOperationResult, &$context, $testNa
304311
*
305312
* @return array|mixed
306313
*/
307-
public function executeRequest($caller, $method, $endpointParams, $expectedError, $testName)
314+
public function executeRequest($caller, $method, $endpointParams, $expectedError, $expectedWarnings, $testName)
308315
{
309316
try {
310317
$response = $caller->$method($endpointParams);
@@ -313,6 +320,8 @@ public function executeRequest($caller, $method, $endpointParams, $expectedError
313320
$response = $response->wait();
314321
}
315322

323+
$this->checkForWarnings($expectedWarnings);
324+
316325
return $response;
317326
} catch (\Exception $exception) {
318327
if (null !== $expectedError) {
@@ -340,7 +349,7 @@ public function executeRequest($caller, $method, $endpointParams, $expectedError
340349
*
341350
* @return bool
342351
*/
343-
public function executeAsyncExistRequest($caller, $method, $endpointParams, $expectedError, $testName)
352+
public function executeAsyncExistRequest($caller, $method, $endpointParams, $expectedError, $expectedWarnings, $testName)
344353
{
345354
try {
346355

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

362+
$this->checkForWarnings($expectedWarnings);
363+
353364
if ($response['status'] === 200) {
354365
return true;
355366
} else {
@@ -368,6 +379,38 @@ public function executeAsyncExistRequest($caller, $method, $endpointParams, $exp
368379
}
369380
}
370381

382+
public function checkForWarnings($expectedWarnings) {
383+
$last = $this->client->transport->getLastConnection()->getLastRequestInfo();
384+
385+
386+
// We have some warnings to check
387+
if ($expectedWarnings !== null) {
388+
if (isset($last['response']['headers']['Warning']) === true) {
389+
foreach ($last['response']['headers']['Warning'] as $warning) {
390+
$position = array_search($warning, $expectedWarnings);
391+
if ($position !== false) {
392+
// found the warning
393+
unset($expectedWarnings[$position]);
394+
} else {
395+
// didn't find, throw error
396+
throw new \Exception("Expected to find warning [$warning] but did not.");
397+
}
398+
}
399+
if (count($expectedWarnings) > 0) {
400+
throw new \Exception("Expected to find more warnings: ". print_r($expectedWarnings, true));
401+
}
402+
}
403+
} else {
404+
// no expected warnings, make sure we have none returned
405+
if (isset($last['response']['headers']['Warning']) === true) {
406+
throw new \Exception("Did not expect to find warnings, found some instead: "
407+
. print_r($last['response']['headers']['Warning'], true));
408+
}
409+
}
410+
411+
412+
}
413+
371414
/**
372415
* Check if a field in the last operation is false
373416
*

0 commit comments

Comments
 (0)