Skip to content

Commit 99e16f4

Browse files
committed
Added the try-catch-ignore and updated the headers management in YAML tests
1 parent e76fe9c commit 99e16f4

File tree

6 files changed

+58
-31
lines changed

6 files changed

+58
-31
lines changed

util/ActionTest.php

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
class ActionTest
2626
{
2727
const TEMPLATE_ENDPOINT = __DIR__ . '/template/test/endpoint';
28+
const TEMPLATE_ENDPOINT_TRY_CATCH = __DIR__ . '/template/test/endpoint-try-catch';
2829
const TEMPLATE_MATCH_EQUAL = __DIR__ . '/template/test/match-equal';
2930
const TEMPLATE_MATCH_REGEX = __DIR__ . '/template/test/match-regex';
3031
const TEMPLATE_IS_FALSE = __DIR__ . '/template/test/is-false';
@@ -69,6 +70,7 @@ class ActionTest
6970
private $skippedTest = false;
7071
private $output = '';
7172
private $phpUnitVersion;
73+
private $clientParams = [];
7274

7375
public function __construct(array $steps)
7476
{
@@ -89,20 +91,19 @@ private function do(array $actions): string
8991
':endpoint' => '',
9092
':params' => '',
9193
':catch' => '',
92-
':response-check' => ''
94+
':response-check' => '',
95+
':code' => '',
96+
':headers' => '',
97+
':reset-headers' => ''
9398
];
9499
foreach ($actions as $key => $value) {
95100
if (method_exists($this, $key)) {
96101
$this->$key($value, $vars);
97102
} else {
98103
// headers
99104
if (!empty($this->headers)) {
100-
if ($value instanceof stdClass && empty(get_object_vars($value))) {
101-
$value = [];
102-
}
103-
$value['client'] = [
104-
'headers' => $this->formatHeaders($this->headers)
105-
];
105+
$vars[':headers'] = $this->formatHeaders($this->headers);
106+
$vars[':reset-headers'] = $this->resetHeaders($this->headers);
106107
$this->headers = [];
107108
}
108109
// Check if {} (new stdClass) is the parameter of an endpoint
@@ -119,9 +120,34 @@ private function do(array $actions): string
119120
$vars[':params'] = $params;
120121
}
121122
}
123+
// ignore client parameter
124+
if (isset($this->clientParams['ignore'])) {
125+
$vars[':code'] = $this->clientParams['ignore'];
126+
return YamlTests::render(self::TEMPLATE_ENDPOINT_TRY_CATCH, $vars);
127+
}
122128
return YamlTests::render(self::TEMPLATE_ENDPOINT, $vars);
123129
}
124130

131+
/**
132+
* Adjust the client parameters (e.g. ignore)
133+
*/
134+
private function adjustClientParams($params)
135+
{
136+
if (!is_array($params)) {
137+
return $params;
138+
}
139+
$this->clientParams = [];
140+
foreach ($params as $key => $value) {
141+
switch($key) {
142+
case 'ignore':
143+
$this->clientParams['ignore'] = $value;
144+
unset($params[$key]);
145+
break;
146+
}
147+
}
148+
return $params;
149+
}
150+
125151
/**
126152
* ---------- FEATURE FUNCTIONS (BEGIN) ----------
127153
*/
@@ -479,36 +505,20 @@ private function convertStdClass(string $value): string
479505
return preg_replace("/stdClass::__set_state\(array\(\s+\)\)/", '(object) []', $value);
480506
}
481507

482-
/**
483-
* Adjust the client parameters (e.g. ignore)
484-
*/
485-
private function adjustClientParams($params)
508+
private function formatHeaders(array $headers): string
486509
{
487-
if (!is_array($params)) {
488-
return $params;
489-
}
490-
foreach ($params as $key => $value) {
491-
if (in_array($key, ['ignore'])) {
492-
if (isset($params['client'])) {
493-
$params['client'][$key] = $value;
494-
} else {
495-
$params['client'] = [
496-
'ignore' => $value
497-
];
498-
}
499-
unset($params[$key]);
500-
}
510+
$result = '';
511+
foreach ($headers as $key => $value) {
512+
$result .= sprintf("\$this->client->getTransport()->setHeader('%s','%s');\n", $key, $value);
501513
}
502-
return $params;
514+
return $result;
503515
}
504516

505-
private function formatHeaders(array $headers): array
517+
private function resetHeaders(array $headers): string
506518
{
507-
$result = $headers;
519+
$result = '';
508520
foreach ($headers as $key => $value) {
509-
if (!is_array($value)) {
510-
$result[$key] = [$value];
511-
}
521+
$result .= sprintf("\$this->client->getTransport()->setHeader('%s', null);\n", $key);
512522
}
513523
return $result;
514524
}

util/template/test/endpoint

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
:headers
12
:catch$response = $this->client->:endpoint(
23
:params
34
);
5+
:reset-headers
46
:response-check

util/template/test/endpoint-try-catch

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
:headers
2+
try {
3+
:catch$response = $this->client->:endpoint(
4+
:params
5+
);
6+
} catch (ClientResponseException $e) {
7+
if ($e->getCode() !== :code) {
8+
throw $e;
9+
}
10+
}
11+
:reset-headers
12+
:response-check

util/template/test/unit-test-oss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ declare(strict_types = 1);
1717
namespace :namespace;
1818

1919
use PHPUnit\Framework\TestCase;
20+
use Elastic\Elasticsearch\Exception\ClientResponseException;
2021
use Elastic\Elasticsearch\Tests\Utility;
2122
use stdClass;
2223

util/template/test/unit-test-skipped

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ declare(strict_types = 1);
1717
namespace :namespace;
1818

1919
use PHPUnit\Framework\TestCase;
20+
use Elastic\Elasticsearch\Exception\ClientResponseException;
2021
use Elastic\Elasticsearch\Tests\Utility;
2122
use stdClass;
2223

util/template/test/unit-test-xpack

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ declare(strict_types = 1);
1717
namespace :namespace;
1818

1919
use PHPUnit\Framework\TestCase;
20+
use Elastic\Elasticsearch\Exception\ClientResponseException;
2021
use Elastic\Elasticsearch\Tests\Utility;
2122
use stdClass;
2223

0 commit comments

Comments
 (0)