@@ -876,57 +876,102 @@ private function generateStepsPhp($stepsObject, $stepsData, $hookObject = false)
876
876
}
877
877
878
878
/**
879
- * Resolves replacement of $input$ and $$input$$ in given string.
880
- * Can be given a boolean to surround replacement with quote breaking .
879
+ * Resolves replacement of $input$ and $$input$$ in given function, recursing and replacing individual arguments
880
+ * Also determines if each argument requires any quote replacement .
881
881
* @param string $inputString
882
- * @param bool $quoteBreak
882
+ * @param array $args
883
883
* @return string
884
- * @throws \Exception
885
884
*/
886
- private function resolveTestVariable ($ inputString , $ quoteBreak = false )
885
+ private function resolveTestVariable ($ inputString , $ args )
887
886
{
888
887
$ outputString = $ inputString ;
889
- $ replaced = false ;
890
888
891
- // Check for Cest-scope variables first, stricter regex match.
892
- preg_match_all ("/ \\$ \\$[\w.\[\]]+ \\$ \\$/ " , $ outputString , $ matches );
893
- foreach ($ matches [0 ] as $ match ) {
894
- $ replacement = null ;
895
- $ variable = $ this ->stripAndSplitReference ($ match , '$$ ' );
896
- if (count ($ variable ) != 2 ) {
897
- throw new \Exception (
898
- "Invalid Persisted Entity Reference: " . $ match .
899
- ". Hook persisted entity references must follow \$\$entityMergeKey.field \$\$ format. "
900
- );
901
- }
902
- $ replacement = sprintf ("\$this->%s->getCreatedDataByName('%s') " , $ variable [0 ], $ variable [1 ]);
903
- if ($ quoteBreak ) {
904
- $ replacement = '" . ' . $ replacement . ' . " ' ;
905
- }
906
- $ outputString = str_replace ($ match , $ replacement , $ outputString );
907
- $ replaced = true ;
889
+ //Loop through each argument, replace and then replace
890
+ foreach ($ args as $ arg ) {
891
+ $ outputArg = $ arg ;
892
+ // Match on any $$data.key$$ found inside arg, matches[0] will be array of $$data.key$$
893
+ preg_match_all ("/ \\$ \\$[\w.\[\]]+ \\$ \\$/ " , $ outputArg , $ matches );
894
+ $ this ->replaceMatchesIntoArg ($ matches [0 ], $ outputArg , "$$ " );
895
+
896
+ // Match on any $data.key$ found inside arg, matches[0] will be array of $data.key$
897
+ preg_match_all ("/ \\$[\w.\[\]]+ \\$/ " , $ outputArg , $ matches );
898
+ $ this ->replaceMatchesIntoArg ($ matches [0 ], $ outputArg , "$ " );
899
+
900
+ $ outputString = str_replace ($ arg , $ outputArg , $ outputString );
908
901
}
909
902
910
- // Check Test-scope variables
911
- preg_match_all ("/ \\$[\w.\[\]]+ \\$/ " , $ outputString , $ matches );
912
- foreach ($ matches [0 ] as $ match ) {
903
+ return $ outputString ;
904
+ }
905
+
906
+ /**
907
+ * Replaces all matches into given outputArg with. Variable scope determined by delimiter given
908
+ * @param array $matches
909
+ * @param string &$outputArg
910
+ * @param string $delimiter
911
+ * @return void
912
+ * @throws \Exception
913
+ */
914
+ private function replaceMatchesIntoArg ($ matches , &$ outputArg , $ delimiter )
915
+ {
916
+ foreach ($ matches as $ match ) {
913
917
$ replacement = null ;
914
- $ variable = $ this ->stripAndSplitReference ($ match , ' $ ' );
918
+ $ variable = $ this ->stripAndSplitReference ($ match , $ delimiter );
915
919
if (count ($ variable ) != 2 ) {
916
920
throw new \Exception (
917
- "Invalid Persisted Entity Reference: " . $ match .
918
- " . Test persisted entity references must follow \$ entityMergeKey.field \$ format. "
921
+ "Invalid Persisted Entity Reference: { $ match} .
922
+ Test persisted entity references must follow { $ delimiter } entityMergeKey.field { $ delimiter } format. "
919
923
);
920
924
}
921
- $ replacement = sprintf ("$%s->getCreatedDataByName('%s') " , $ variable [0 ], $ variable [1 ]);
922
- if ($ quoteBreak ) {
923
- $ replacement = '" . ' . $ replacement . ' . " ' ;
925
+ if ($ delimiter == "$ " ) {
926
+ $ replacement = sprintf ("$%s->getCreatedDataByName('%s') " , $ variable [0 ], $ variable [1 ]);
927
+ } elseif ($ delimiter == "$$ " ) {
928
+ $ replacement = sprintf ("\$this->%s->getCreatedDataByName('%s') " , $ variable [0 ], $ variable [1 ]);
929
+ }
930
+
931
+ //Determine if quoteBreak check is necessary. Assume replacement is surrounded in quotes, then override
932
+ if (strpos ($ outputArg , "\"" ) !== false ) {
933
+ $ outputArg = $ this ->processQuoteBreaks ($ match , $ outputArg , $ replacement );
934
+ } else {
935
+ $ outputArg = str_replace ($ match , $ replacement , $ outputArg );
924
936
}
925
- $ outputString = str_replace ($ match , $ replacement , $ outputString );
926
- $ replaced = true ;
927
937
}
938
+ }
928
939
929
- return $ outputString ;
940
+ /**
941
+ * Processes an argument for $data.key$ and determines if it needs quote breaks on either ends.
942
+ * Returns an output with quote breaks and replacement already done.
943
+ * @param string $match
944
+ * @param string $argument
945
+ * @param string $replacement
946
+ * @return string
947
+ */
948
+ private function processQuoteBreaks ($ match , $ argument , $ replacement )
949
+ {
950
+ $ outputArg = $ argument ;
951
+ $ beforeIndex = strpos ($ outputArg , $ match ) - 1 ;
952
+ $ afterIndex = $ beforeIndex + strlen ($ match ) + 1 ;
953
+ $ quoteBefore = true ;
954
+ $ quoteAfter = true ;
955
+
956
+ // Prepare replacement with quote breaks if needed
957
+ if ($ argument [$ beforeIndex ] != "\"" ) {
958
+ $ replacement = '" . ' . $ replacement ;
959
+ $ quoteBefore = false ;
960
+ }
961
+ if ($ argument [$ afterIndex ] != "\"" ) {
962
+ $ replacement = $ replacement . ' . " ' ;
963
+ $ quoteAfter = false ;
964
+ }
965
+ //Remove quotes at either end of argument if they aren't necessary.
966
+ if ($ quoteBefore ) {
967
+ $ outputArg = substr ($ outputArg , 0 , $ beforeIndex ) . substr ($ outputArg , $ beforeIndex +1 );
968
+ $ afterIndex --;
969
+ }
970
+ if ($ quoteAfter ) {
971
+ $ outputArg = substr ($ outputArg , 0 , $ afterIndex ) . substr ($ outputArg , $ afterIndex +1 );
972
+ }
973
+ $ outputArg = str_replace ($ match , $ replacement , $ outputArg );
974
+ return $ outputArg ;
930
975
}
931
976
932
977
/**
@@ -1226,9 +1271,7 @@ private function wrapFunctionCall($actor, $action, ...$args)
1226
1271
}
1227
1272
$ output .= "); \n" ;
1228
1273
1229
- // TODO put in condiional to prevent unncessary quote break (i.e. there are no strings to be appended to
1230
- // variable call.
1231
- return $ this ->resolveTestVariable ($ output , true );
1274
+ return $ this ->resolveTestVariable ($ output , $ args );
1232
1275
}
1233
1276
1234
1277
/**
@@ -1256,9 +1299,7 @@ private function wrapFunctionCallWithReturnValue($returnVariable, $actor, $actio
1256
1299
}
1257
1300
$ output .= "); \n" ;
1258
1301
1259
- // TODO put in condiional to prevent unncessary quote break (i.e. there are no strings to be appended to
1260
- // variable call.
1261
- return $ output = $ this ->resolveTestVariable ($ output , true );
1302
+ return $ this ->resolveTestVariable ($ output , $ args );
1262
1303
}
1263
1304
// @codingStandardsIgnoreEnd
1264
1305
}
0 commit comments