25
25
class ActionTest
26
26
{
27
27
const TEMPLATE_ENDPOINT = __DIR__ . '/template/test/endpoint ' ;
28
+ const TEMPLATE_ENDPOINT_TRY_CATCH = __DIR__ . '/template/test/endpoint-try-catch ' ;
28
29
const TEMPLATE_MATCH_EQUAL = __DIR__ . '/template/test/match-equal ' ;
29
30
const TEMPLATE_MATCH_REGEX = __DIR__ . '/template/test/match-regex ' ;
30
31
const TEMPLATE_IS_FALSE = __DIR__ . '/template/test/is-false ' ;
@@ -69,6 +70,7 @@ class ActionTest
69
70
private $ skippedTest = false ;
70
71
private $ output = '' ;
71
72
private $ phpUnitVersion ;
73
+ private $ clientParams = [];
72
74
73
75
public function __construct (array $ steps )
74
76
{
@@ -89,20 +91,19 @@ private function do(array $actions): string
89
91
':endpoint ' => '' ,
90
92
':params ' => '' ,
91
93
':catch ' => '' ,
92
- ':response-check ' => ''
94
+ ':response-check ' => '' ,
95
+ ':code ' => '' ,
96
+ ':headers ' => '' ,
97
+ ':reset-headers ' => ''
93
98
];
94
99
foreach ($ actions as $ key => $ value ) {
95
100
if (method_exists ($ this , $ key )) {
96
101
$ this ->$ key ($ value , $ vars );
97
102
} else {
98
103
// headers
99
104
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 );
106
107
$ this ->headers = [];
107
108
}
108
109
// Check if {} (new stdClass) is the parameter of an endpoint
@@ -119,9 +120,34 @@ private function do(array $actions): string
119
120
$ vars [':params ' ] = $ params ;
120
121
}
121
122
}
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
+ }
122
128
return YamlTests::render (self ::TEMPLATE_ENDPOINT , $ vars );
123
129
}
124
130
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
+
125
151
/**
126
152
* ---------- FEATURE FUNCTIONS (BEGIN) ----------
127
153
*/
@@ -479,36 +505,20 @@ private function convertStdClass(string $value): string
479
505
return preg_replace ("/stdClass::__set_state\(array\(\s+\)\)/ " , '(object) [] ' , $ value );
480
506
}
481
507
482
- /**
483
- * Adjust the client parameters (e.g. ignore)
484
- */
485
- private function adjustClientParams ($ params )
508
+ private function formatHeaders (array $ headers ): string
486
509
{
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 );
501
513
}
502
- return $ params ;
514
+ return $ result ;
503
515
}
504
516
505
- private function formatHeaders (array $ headers ): array
517
+ private function resetHeaders (array $ headers ): string
506
518
{
507
- $ result = $ headers ;
519
+ $ result = '' ;
508
520
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 );
512
522
}
513
523
return $ result ;
514
524
}
0 commit comments