Skip to content

Commit 32204c5

Browse files
committed
Fixed PHPUnit 9 regex + index missing for YAML tests
1 parent 29d81a5 commit 32204c5

File tree

16 files changed

+88
-47
lines changed

16 files changed

+88
-47
lines changed

util/ActionTest.php

Lines changed: 47 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -25,33 +25,39 @@
2525
use Elasticsearch\Common\Exceptions\RequestTimeout408Exception;
2626
use Elasticsearch\Common\Exceptions\Unauthorized401Exception;
2727
use Elasticsearch\Util\YamlTests;
28+
use PHPUnit\Runner\Version as PHPUnitVersion;
2829
use stdClass;
2930

3031
class ActionTest
3132
{
32-
const TEMPLATE_ENDPOINT = __DIR__ . '/template/test/endpoint';
33-
const TEMPLATE_MATCH_EQUAL = __DIR__ . '/template/test/match-equal';
34-
const TEMPLATE_MATCH_REGEX = __DIR__ . '/template/test/match-regex';
35-
const TEMPLATE_IS_FALSE = __DIR__ . '/template/test/is-false';
36-
const TEMPLATE_IS_TRUE = __DIR__ . '/template/test/is-true';
37-
const TEMPLATE_IS_NULL = __DIR__ . '/template/test/is-null';
38-
const TEMPLATE_LENGTH = __DIR__ . '/template/test/length';
39-
const TEMPLATE_SKIP_VERSION = __DIR__ . '/template/test/skip-version';
40-
const TEMPLATE_SKIP_FEATURE = __DIR__ . '/template/test/skip-feature';
41-
const TEMPLATE_SKIP_XPACK = __DIR__ . '/template/test/skip-xpack';
42-
const TEMPLATE_SKIP_NODE_SELECTOR = __DIR__ . '/template/test/skip-node-selector';
43-
const TEMPLATE_SKIP_OSS = __DIR__ . '/template/test/skip-oss';
44-
const TEMPLATE_CATCH = __DIR__ . '/template/test/catch';
45-
const TEMPLATE_CATCH_UNAVAILABLE = __DIR__ . '/template/test/catch-unavailable';
46-
const TEMPLATE_CATCH_REGEX = __DIR__ . '/template/test/catch-regex';
47-
const TEMPLATE_SET_VARIABLE = __DIR__ . '/template/test/set-variable';
48-
const TEMPLATE_TRANSFORM_AND_SET = __DIR__ . '/template/test/transform-and-set';
49-
const TEMPLATE_WARNINGS = __DIR__ . '/template/test/warnings';
50-
const TEMPLATE_ALLOWED_WARNINGS = __DIR__ . '/template/test/allowed-warnings';
51-
const TEMPLATE_GT = __DIR__ . '/template/test/gt';
52-
const TEMPLATE_GTE = __DIR__ . '/template/test/gte';
53-
const TEMPLATE_LT = __DIR__ . '/template/test/lt';
54-
const TEMPLATE_LTE = __DIR__ . '/template/test/lte';
33+
const TEMPLATE_ENDPOINT = __DIR__ . '/template/test/endpoint';
34+
const TEMPLATE_MATCH_EQUAL = __DIR__ . '/template/test/match-equal';
35+
const TEMPLATE_MATCH_REGEX = __DIR__ . '/template/test/match-regex';
36+
const TEMPLATE_IS_FALSE = __DIR__ . '/template/test/is-false';
37+
const TEMPLATE_IS_TRUE = __DIR__ . '/template/test/is-true';
38+
const TEMPLATE_IS_NULL = __DIR__ . '/template/test/is-null';
39+
const TEMPLATE_LENGTH = __DIR__ . '/template/test/length';
40+
const TEMPLATE_SKIP_VERSION = __DIR__ . '/template/test/skip-version';
41+
const TEMPLATE_SKIP_FEATURE = __DIR__ . '/template/test/skip-feature';
42+
const TEMPLATE_SKIP_XPACK = __DIR__ . '/template/test/skip-xpack';
43+
const TEMPLATE_SKIP_NODE_SELECTOR = __DIR__ . '/template/test/skip-node-selector';
44+
const TEMPLATE_SKIP_OSS = __DIR__ . '/template/test/skip-oss';
45+
const TEMPLATE_CATCH = __DIR__ . '/template/test/catch';
46+
const TEMPLATE_CATCH_UNAVAILABLE = __DIR__ . '/template/test/catch-unavailable';
47+
const TEMPLATE_CATCH_REGEX = __DIR__ . '/template/test/catch-regex';
48+
const TEMPLATE_SET_VARIABLE = __DIR__ . '/template/test/set-variable';
49+
const TEMPLATE_TRANSFORM_AND_SET = __DIR__ . '/template/test/transform-and-set';
50+
const TEMPLATE_WARNINGS = __DIR__ . '/template/test/warnings';
51+
const TEMPLATE_ALLOWED_WARNINGS = __DIR__ . '/template/test/allowed-warnings';
52+
const TEMPLATE_GT = __DIR__ . '/template/test/gt';
53+
const TEMPLATE_GTE = __DIR__ . '/template/test/gte';
54+
const TEMPLATE_LT = __DIR__ . '/template/test/lt';
55+
const TEMPLATE_LTE = __DIR__ . '/template/test/lte';
56+
57+
// --- PHPUNIT 9 TEMPLATE ---
58+
const TEMPLATE_PHPUNIT9_MATCH_REGEX = __DIR__ . '/template/test/match-regex-9';
59+
const TEMPLATE_PHPUNIT9_CATCH_REGEX = __DIR__ . '/template/test/catch-regex-9';
60+
5561
const TAB14 = ' ';
5662
const SUPPORTED_FEATURES = [
5763
'xpack',
@@ -68,9 +74,12 @@ class ActionTest
6874
private $variables = [];
6975
private $skippedTest = false;
7076
private $output = '';
77+
private $phpUnitVersion;
7178

7279
public function __construct(array $steps)
7380
{
81+
$this->phpUnitVersion = (int) explode('.', PHPUnitVersion::id())[0];
82+
7483
foreach ($steps as $step) {
7584
foreach ($step as $name => $actions) {
7685
if (method_exists($this, $name) && !$this->skippedTest) {
@@ -197,9 +206,10 @@ private function catch(string $action, array &$vars)
197206
break;
198207
default:
199208
$expectedException = ElasticsearchException::class;
200-
$scriptException = YamlTests::render(self::TEMPLATE_CATCH_REGEX,[
201-
':regex' => sprintf("'%s'", addslashes($action))
202-
]);
209+
$scriptException = YamlTests::render(
210+
($this->phpUnitVersion > 8) ? (self::TEMPLATE_PHPUNIT9_CATCH_REGEX) : (self::TEMPLATE_CATCH_REGEX),
211+
[ ':regex' => sprintf("'%s'", addslashes($action)) ]
212+
);
203213
}
204214
$vars[':catch'] = YamlTests::render(self::TEMPLATE_CATCH, [
205215
':exception' => $expectedException
@@ -246,7 +256,10 @@ private function match(array $actions)
246256
$vars[':expected'] = $this->convertJavaRegexToPhp($vars[':expected']);
247257
// Add /sx preg modifier to ignore whitespace
248258
$vars[':expected'] .= "sx";
249-
return YamlTests::render(self::TEMPLATE_MATCH_REGEX, $vars);
259+
return YamlTests::render(
260+
($this->phpUnitVersion > 8) ? (self::TEMPLATE_PHPUNIT9_MATCH_REGEX) : (self::TEMPLATE_MATCH_REGEX),
261+
$vars
262+
);
250263
}
251264
if ($expected instanceof stdClass && empty(get_object_vars($expected))) {
252265
$vars[':expected'] = '[]';
@@ -415,20 +428,24 @@ private function convertDotToArrow(string $dot)
415428

416429
private function convertResponseField(string $field): string
417430
{
431+
$output = '$response';
418432
if ($field === '$body' || $field === '') {
419-
return '';
433+
return $output;
434+
}
435+
// if the field starts with a .$variable remove the first dot
436+
if (substr($field, 0, 2) === '.$') {
437+
$field = substr($field, 1);
420438
}
421439
# Remove \. from $field
422440
$field = str_replace ('\.', chr(200), $field);
423441
$parts = explode('.', $field);
424-
$output = '';
425442
foreach ($parts as $part) {
426443
# Replace \. in $part
427444
$part = str_replace (chr(200), '.', $part);
428445
if (is_int($part)) {
429446
$output .= sprintf("[%d]", $part);
430447
} else {
431-
$output .= sprintf("['%s']", $part);
448+
$output .= sprintf("[\"%s\"]", $part);
432449
}
433450
}
434451
return $output;

util/YamlTests.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class YamlTests
4040
'Cat\Shards\_10_BasicTest::TestCatShardsOutput' => 'Regexp error, it seems not compatible with PHP',
4141
'Indices\Create\_20_Mix_Typeless_TypefulTest::CreateATypedIndexWhileThereIsATypelessTemplate' => 'mismatch on warning header',
4242
'Search\Aggregation\_10_HistogramTest::HistogramProfiler' => "Error reading 'n' field from YAML",
43+
'Search\Highlight\_20_FvhTest::HighlightMultipleNestedDocuments' => 'Undefined index: nested\.title'
4344
];
4445

4546
const SKIPPED_TEST_XPACK = [
@@ -60,7 +61,18 @@ class YamlTests
6061
'Ml\_Delete_Expired_DataTest::TestDeleteExpiredDataWithJobId' => 'Substring mismatch',
6162
'Ml\_Explain_Data_Frame_AnalyticsTest::TestNonemptyDataFrameGivenBody' => 'Expected a different value',
6263
'Rollup\_Put_JobTest::TestPutJobWithTemplates' => 'version not converted from variable',
63-
'Snapshot\_10_BasicTest::CreateASourceOnlySnapshotAndThenRestoreIt' => 'spanshop name already exists',
64+
'RuntimeFields\_100_Geo_PointTest::GetMapping' => 'Substring mismatch',
65+
'RuntimeFields\_10_KeywordTest::GetMapping' => 'Substring mismatch',
66+
'RuntimeFields\_10_KeywordTest::FetchFields' => 'Array mismatch',
67+
'RuntimeFields\_10_KeywordTest::Docvalue_fields' => 'Array mismatch',
68+
'RuntimeFields\_10_KeywordTest::ExplainTermQueryWrappedInScriptScore' => 'Substring mismatch',
69+
'RuntimeFields\_200_Runtime_Fields_StatsTest::UsageStatsWithRuntimeFields' => 'Count mismatch',
70+
'RuntimeFields\_20_LongTest::GetMapping' => 'String mismatch',
71+
'RuntimeFields\_30_DoubleTest::GetMapping' => 'Array mismatch',
72+
'RuntimeFields\_40_DateTest::GetMapping' => 'String mismatch',
73+
'RuntimeFields\_50_IpTest::GetMapping' => 'String mismatch',
74+
'RuntimeFields\_60_BooleanTest::GetMapping' => 'String mismatch',
75+
'Snapshot\_10_BasicTest::CreateASourceOnlySnapshotAndThenRestoreIt' => 'Snapshot name already exists',
6476
'Ssl\_10_BasicTest::TestGetSSLCertificates' => 'Mismatch values',
6577
'Transform\_Transforms_CrudTest::TestDeleteTransformWhenItDoesNotExist' => 'Invalid version format: TRANSFORM HTTP/1.1',
6678
'UnsignedLong\_10_BasicTest::*' => 'Skipped all tests',

util/template/test/catch-regex

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
1-
$this->assertRegExp(:regex, $response['error']['reason']);
1+
$this->assertRegExp(
2+
:regex,
3+
$response['error']['reason']
4+
);
25

util/template/test/catch-regex-9

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
$this->assertMatchesRegularExpression(
2+
:regex,
3+
$response['error']['reason']
4+
);
5+

util/template/test/gt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
$this->assertGreaterThan(
22
:expected,
3-
$response:value
3+
:value
44
);

util/template/test/gte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
$this->assertGreaterThanOrEqual(
22
:expected,
3-
$response:value
3+
:value
44
);

util/template/test/is-false

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
$this->assertFalse(isset($response:value) && $response:value !== 0 && $response:value !== false && $response:value !== '');
1+
$this->assertFalse(isset(:value) && :value !== 0 && :value !== false && :value !== '');

util/template/test/is-null

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
if (isset($response:value)) {
2-
$this->assertNull($response:value);
1+
if (isset(:value)) {
2+
$this->assertNull(:value);
33
} else {
44
$this->assertTrue(true);
55
}

util/template/test/is-true

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
$this->assertTrue(isset($response:value) && $response:value !== 0 && $response:value !== false && $response:value !== '');
1+
$this->assertTrue(isset(:value) && :value !== 0 && :value !== false && :value !== '');

util/template/test/length

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
if (is_array($response:value)) {
2-
$this->assertCount(:expected, $response:value);
3-
} elseif (is_string($response:value)) {
4-
$this->assertEquals(:expected, $response:value);
1+
if (is_array(:value)) {
2+
$this->assertCount(:expected, :value);
3+
} elseif (is_string(:value)) {
4+
$this->assertEquals(:expected, :value);
55
}

0 commit comments

Comments
 (0)