Skip to content

Commit 84c4ebb

Browse files
committed
Added contains feature in YAML test + added Utility::getClient()
1 parent 064f8e6 commit 84c4ebb

File tree

3 files changed

+75
-29
lines changed

3 files changed

+75
-29
lines changed

tests/Elasticsearch/Tests/Utility.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818

1919
namespace Elasticsearch\Tests;
2020

21+
use Elasticsearch\Client;
22+
use Elasticsearch\ClientBuilder;
23+
2124
class Utility
2225
{
2326
public static function getHost(): ?string
@@ -34,4 +37,14 @@ public static function getHost(): ?string
3437
}
3538
return null;
3639
}
40+
41+
public static function getClient(): Client
42+
{
43+
$clientBuilder = ClientBuilder::create()
44+
->setHosts([self::getHost()]);
45+
if (getenv('TEST_SUITE') === 'xpack') {
46+
$clientBuilder->setSSLVerification(false);
47+
}
48+
return $clientBuilder->build();
49+
}
3750
}

tests/Elasticsearch/Tests/YamlRunnerTest.php

Lines changed: 60 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class YamlRunnerTest extends \PHPUnit\Framework\TestCase
6262
* @var string[] A list of supported features
6363
*/
6464
private static $supportedFeatures = [
65-
'stash_in_path', 'warnings', 'headers'
65+
'stash_in_path', 'warnings', 'headers', 'contains'
6666
];
6767

6868
/**
@@ -154,12 +154,7 @@ public static function setUpBeforeClass()
154154

155155
public function setUp()
156156
{
157-
$this->client = Elasticsearch\ClientBuilder::create()
158-
->setHosts([self::getHost()]);
159-
if (getenv('TEST_SUITE') === 'xpack') {
160-
$this->client->setSSLVerification(__DIR__ . '/../../../.ci/certs/ca.crt');
161-
}
162-
$this->client = $this->client->build();
157+
$this->client = Utility::getClient();
163158
}
164159

165160
public function tearDown()
@@ -306,6 +301,10 @@ public function processOperation($operation, $lastOperationResult, array &$conte
306301
return $this->operationSkip($operation->{$operationName}, $lastOperationResult, $testName);
307302
}
308303

304+
if ('contains' === $operationName) {
305+
return $this->operationContains($operation->{$operationName}, $lastOperationResult, $context, $testName);
306+
}
307+
309308
self::markTestIncomplete(sprintf('Operation %s not supported for test "%s"', $operationName, $testName));
310309
}
311310

@@ -789,6 +788,24 @@ public function operationSkip($operation, $lastOperationResult, string $testName
789788
return $lastOperationResult;
790789
}
791790

791+
/**
792+
* Check if a field in the last operation contains a value
793+
*
794+
* @param object $operation
795+
* @param array|string|null $lastOperationResult
796+
* @param array $context
797+
* @param string $testName
798+
*/
799+
public function operationContains($operation, $lastOperationResult, &$context, string $testName)
800+
{
801+
$value = $this->resolveValue($lastOperationResult, key($operation), $context);
802+
$expected = current($operation);
803+
804+
$this->assertContains($expected, $value, 'Failed to contains in test ' . $testName);
805+
806+
return $lastOperationResult;
807+
}
808+
792809
/**
793810
* Assert an expected error
794811
*
@@ -1065,10 +1082,10 @@ function ($item) {
10651082
$skip = true;
10661083
}
10671084

1068-
if (!$skip && key($documentParsed) === 'setup') {
1085+
if (!$skip && is_array($documentParsed) && key($documentParsed) === 'setup') {
10691086
$setup = $documentParsed;
10701087
$setupSkip = $skip;
1071-
} elseif (!$teardown && key($documentParsed) === 'teardown') {
1088+
} elseif (!$teardown && is_array($documentParsed) && key($documentParsed) === 'teardown') {
10721089
$teardown = $documentParsed;
10731090
} else {
10741091
$documentsParsed[] = [$documentParsed, $skip || $setupSkip, $setup, $teardown, $fileName];
@@ -1083,27 +1100,44 @@ function ($item) {
10831100
*/
10841101
private function clean()
10851102
{
1086-
$host = static::getHost();
1087-
$ch = curl_init($host."/*");
1088-
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
1089-
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
1090-
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
1091-
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
1092-
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
1103+
// Delete all indices
1104+
$this->client->indices()->delete([
1105+
'index' => '*'
1106+
]);
10931107

1094-
$response = curl_exec($ch);
1095-
curl_close($ch);
1108+
// Delete all template
1109+
$this->client->indices()->deleteTemplate([
1110+
'name' => '*'
1111+
]);
10961112

1097-
$ch = curl_init($host."/_template/*");
1098-
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
1099-
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
1100-
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
1101-
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
1102-
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
1113+
if (getenv('TEST_SUITE') === 'xpack') {
1114+
# Get all roles
1115+
$roles = $this->client->security()->getRole();
1116+
# Delete custom roles (metadata._reserved = 0)
1117+
foreach ($roles as $role => $values) {
1118+
if ($values['metadata']['_reserved'] === 0) {
1119+
$this->client->security()->deleteRole([
1120+
'name' => $role
1121+
]);
1122+
}
1123+
}
11031124

1104-
$response = curl_exec($ch);
1105-
curl_close($ch);
1125+
# Get all users
1126+
$users = $this->client->security()->getUser();
1127+
# Delete custom users (metadata._reserved = 0)
1128+
foreach ($users as $user => $values) {
1129+
if ($values['metadata']['_reserved'] === 0) {
1130+
$this->client->security()->deleteUser([
1131+
'username' => $user
1132+
]);
1133+
}
1134+
}
11061135

1136+
# Get all privileges
1137+
$privileges = $this->client->security()->getPrivileges();
1138+
1139+
}
1140+
11071141
$this->rmDirRecursively('/tmp/test_repo_create_1_loc');
11081142
$this->rmDirRecursively('/tmp/test_repo_restore_1_loc');
11091143
$this->rmDirRecursively('/tmp/test_cat_repo_1_loc');

util/RestSpecRunner.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,8 @@
3737
// Include the composer autoloader
3838
$autoloader = require_once(dirname(__DIR__) . '/vendor/autoload.php');
3939

40-
$client = ClientBuilder::fromConfig([
41-
'hosts' => [Utility::getHost()]
42-
]);
40+
$client = Utility::getClient();
41+
4342
$serverInfo = $client->info();
4443
var_dump($serverInfo);
4544

0 commit comments

Comments
 (0)