25
25
use Elasticsearch \Common \Exceptions \RequestTimeout408Exception ;
26
26
use Elasticsearch \Common \Exceptions \Unauthorized401Exception ;
27
27
use Elasticsearch \Util \YamlTests ;
28
+ use PHPUnit \Runner \Version as PHPUnitVersion ;
28
29
use stdClass ;
29
30
30
31
class ActionTest
31
32
{
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
+
55
61
const TAB14 = ' ' ;
56
62
const SUPPORTED_FEATURES = [
57
63
'xpack ' ,
@@ -68,9 +74,12 @@ class ActionTest
68
74
private $ variables = [];
69
75
private $ skippedTest = false ;
70
76
private $ output = '' ;
77
+ private $ phpUnitVersion ;
71
78
72
79
public function __construct (array $ steps )
73
80
{
81
+ $ this ->phpUnitVersion = (int ) explode ('. ' , PHPUnitVersion::id ())[0 ];
82
+
74
83
foreach ($ steps as $ step ) {
75
84
foreach ($ step as $ name => $ actions ) {
76
85
if (method_exists ($ this , $ name ) && !$ this ->skippedTest ) {
@@ -197,9 +206,10 @@ private function catch(string $action, array &$vars)
197
206
break ;
198
207
default :
199
208
$ 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
+ );
203
213
}
204
214
$ vars [':catch ' ] = YamlTests::render (self ::TEMPLATE_CATCH , [
205
215
':exception ' => $ expectedException
@@ -246,7 +256,10 @@ private function match(array $actions)
246
256
$ vars [':expected ' ] = $ this ->convertJavaRegexToPhp ($ vars [':expected ' ]);
247
257
// Add /sx preg modifier to ignore whitespace
248
258
$ 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
+ );
250
263
}
251
264
if ($ expected instanceof stdClass && empty (get_object_vars ($ expected ))) {
252
265
$ vars [':expected ' ] = '[] ' ;
@@ -415,20 +428,24 @@ private function convertDotToArrow(string $dot)
415
428
416
429
private function convertResponseField (string $ field ): string
417
430
{
431
+ $ output = '$response ' ;
418
432
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 );
420
438
}
421
439
# Remove \. from $field
422
440
$ field = str_replace ('\. ' , chr (200 ), $ field );
423
441
$ parts = explode ('. ' , $ field );
424
- $ output = '' ;
425
442
foreach ($ parts as $ part ) {
426
443
# Replace \. in $part
427
444
$ part = str_replace (chr (200 ), '. ' , $ part );
428
445
if (is_int ($ part )) {
429
446
$ output .= sprintf ("[%d] " , $ part );
430
447
} else {
431
- $ output .= sprintf ("['%s' ] " , $ part );
448
+ $ output .= sprintf ("[ \" %s \" ] " , $ part );
432
449
}
433
450
}
434
451
return $ output ;
0 commit comments