@@ -213,6 +213,7 @@ public function sendRequest(string $verb, string $url, $body = null, array $head
213213
214214 try {
215215 list ($ fullUrl , $ options ) = $ this ->beforeRequest ($ fullUrl , $ options );
216+ $ options = $ this ->normalizePayloadForRequest ($ verb , $ options );
216217 $ this ->requestOptions = $ options ;
217218 $ this ->response = $ client ->{$ verb }($ fullUrl , $ options );
218219 } catch (ClientException $ ex ) {
@@ -222,6 +223,42 @@ public function sendRequest(string $verb, string $url, $body = null, array $head
222223 }
223224 }
224225
226+ private function normalizePayloadForRequest (string $ verb , array $ options ): array {
227+ if (empty ($ options ['form_params ' ])) {
228+ return $ options ;
229+ }
230+
231+ $ writeVerbs = ['post ' , 'put ' , 'patch ' ];
232+ if (!in_array (strtolower ($ verb ), $ writeVerbs , true )) {
233+ return $ options ;
234+ }
235+
236+ $ hasComplexPayload = false ;
237+ foreach ($ options ['form_params ' ] as $ value ) {
238+ if (is_array ($ value ) || $ value instanceof \stdClass) {
239+ $ hasComplexPayload = true ;
240+ break ;
241+ }
242+ }
243+
244+ if (!$ hasComplexPayload ) {
245+ return $ options ;
246+ }
247+
248+ $ encoded = json_encode ($ options ['form_params ' ]);
249+ Assert::assertIsString ($ encoded );
250+ $ decoded = json_decode ($ encoded , true );
251+ Assert::assertIsArray ($ decoded );
252+
253+ $ options ['json ' ] = $ decoded ;
254+ unset($ options ['form_params ' ]);
255+ if (!isset ($ options ['headers ' ]['Content-Type ' ])) {
256+ $ options ['headers ' ]['Content-Type ' ] = 'application/json ' ;
257+ }
258+
259+ return $ options ;
260+ }
261+
225262 #[Given('/^set the custom http header "([^"]*)" with "([^"]*)" as value to next request$/ ' )]
226263 public function setTheCustomHttpHeaderAsValueToNextRequest (string $ header , string $ value ):void {
227264 if (empty ($ value )) {
@@ -239,11 +276,15 @@ protected function beforeRequest(string $fullUrl, array $options): array {
239276
240277 protected function decodeIfIsJsonString (array $ list ): array {
241278 foreach ($ list as $ key => $ value ) {
242- if ($ this -> isJson ($ value )) {
243- $ list [ $ key ] = json_decode ( $ value ) ;
279+ if (! is_string ($ value )) {
280+ continue ;
244281 }
245282 if (str_starts_with ($ value , '(string) ' )) {
246283 $ list [$ key ] = substr ($ value , strlen ('(string) ' ));
284+ continue ;
285+ }
286+ if ($ this ->isJson ($ value )) {
287+ $ list [$ key ] = json_decode ($ value );
247288 }
248289 }
249290 return $ list ;
@@ -447,6 +488,7 @@ public function setAppConfig(string $appId, TableNode $formData): void {
447488 protected function parseFormParams (array $ options ): array {
448489 if (!empty ($ options ['form_params ' ])) {
449490 $ this ->parseTextRcursive ($ options ['form_params ' ]);
491+ $ options ['form_params ' ] = $ this ->decodeIfIsJsonString ($ options ['form_params ' ]);
450492 }
451493 return $ options ;
452494 }
@@ -489,12 +531,17 @@ public static function runCommand(string $command): array {
489531 if ($ owner === false ) {
490532 throw new \Exception ('Could not retrieve owner information for UID ' . $ fileOwnerUid );
491533 }
492- $ fullCommand = 'php ' . $ console . ' ' . $ command ;
493- if (!empty (self ::$ environments )) {
494- $ fullCommand = http_build_query (self ::$ environments , '' , ' ' ) . ' ' . $ fullCommand ;
495- }
534+ $ baseCommand = 'php ' . $ console . ' ' . $ command ;
535+ $ environmentPrefix = !empty (self ::$ environments )
536+ ? http_build_query (self ::$ environments , '' , ' ' )
537+ : '' ;
538+
496539 if (posix_getuid () !== $ owner ['uid ' ]) {
497- $ fullCommand = 'runuser -u ' . $ owner ['name ' ] . ' -- ' . $ fullCommand ;
540+ $ fullCommand = 'runuser -u ' . $ owner ['name ' ] . ' -- '
541+ . ($ environmentPrefix !== '' ? 'env ' . $ environmentPrefix . ' ' : '' )
542+ . $ baseCommand ;
543+ } else {
544+ $ fullCommand = ($ environmentPrefix !== '' ? $ environmentPrefix . ' ' : '' ) . $ baseCommand ;
498545 }
499546 $ fullCommand .= ' 2>&1 ' ;
500547 return self ::runBashCommand ($ fullCommand );
@@ -544,7 +591,7 @@ private static function runBashCommand(string $command): array {
544591
545592 #[Given('the output of the last command should contain the following text: ' )]
546593 public static function theOutputOfTheLastCommandContains (PyStringNode $ text ): void {
547- Assert::assertStringContainsString ((string ) $ text , self ::$ commandOutput , 'The output of the last command does not contain: ' . $ text );
594+ Assert::assertStringContainsString ((string ) $ text , self ::$ commandOutput , 'The output of the last command does not contain: ' . ( string ) $ text );
548595 }
549596
550597 #[Given('the output of the last command should be empty ' )]
0 commit comments