@@ -568,17 +568,8 @@ private function generateStepsPhp($stepsObject, $stepsData, $hookObject = false)
568
568
case "entity " :
569
569
$ entityData = "[ " ;
570
570
foreach ($ stepsData [$ customActionAttributes ['name ' ]] as $ dataKey => $ dataValue ) {
571
- $ variableReplace = '' ;
572
- if ($ hookObject ) {
573
- $ variableReplace = $ this ->resolveTestVariable ($ dataValue , true );
574
- } else {
575
- $ variableReplace = $ this ->resolveTestVariable ($ dataValue );
576
- }
577
- if (!empty ($ variableReplace )) {
578
- $ entityData .= sprintf ("'%s' => %s, " , $ dataKey , $ variableReplace );
579
- } else {
580
- $ entityData .= sprintf ("'%s' => '%s', " , $ dataKey , $ dataValue );
581
- }
571
+ $ variableReplace = $ this ->resolveTestVariable ($ dataValue );
572
+ $ entityData .= sprintf ("'%s' => %s, " , $ dataKey , $ variableReplace );
582
573
}
583
574
$ entityData .= '] ' ;
584
575
if ($ hookObject ) {
@@ -639,7 +630,14 @@ private function generateStepsPhp($stepsObject, $stepsData, $hookObject = false)
639
630
}
640
631
break ;
641
632
case "grabAttributeFrom " :
642
- $ testSteps .= sprintf ("\t\t$%s = $%s->%s(%s, %s); \n" , $ returnVariable , $ actor , $ actionName , $ selector , $ input );
633
+ $ testSteps .= sprintf (
634
+ "\t\t$%s = $%s->%s(%s, %s); \n" ,
635
+ $ returnVariable ,
636
+ $ actor ,
637
+ $ actionName ,
638
+ $ selector ,
639
+ $ input
640
+ );
643
641
break ;
644
642
case "grabFromCurrentUrl " :
645
643
if ($ input ) {
@@ -656,9 +654,22 @@ private function generateStepsPhp($stepsObject, $stepsData, $hookObject = false)
656
654
break ;
657
655
case "grabMultiple " :
658
656
if ($ input ) {
659
- $ testSteps .= sprintf ("\t\t$%s = $%s->%s(%s, %s); \n" , $ returnVariable , $ actor , $ actionName , $ selector , $ input );
657
+ $ testSteps .= sprintf (
658
+ "\t\t$%s = $%s->%s(%s, %s); \n" ,
659
+ $ returnVariable ,
660
+ $ actor ,
661
+ $ actionName ,
662
+ $ selector ,
663
+ $ input
664
+ );
660
665
} else {
661
- $ testSteps .= sprintf ("\t\t$%s = $%s->%s(%s); \n" , $ returnVariable , $ actor , $ actionName , $ selector );
666
+ $ testSteps .= sprintf (
667
+ "\t\t$%s = $%s->%s(%s); \n" ,
668
+ $ returnVariable ,
669
+ $ actor ,
670
+ $ actionName ,
671
+ $ selector
672
+ );
662
673
}
663
674
break ;
664
675
case "grabValueFrom " :
@@ -1131,30 +1142,57 @@ private function generateStepsPhp($stepsObject, $stepsData, $hookObject = false)
1131
1142
}
1132
1143
1133
1144
/**
1134
- * Resolves regex for given variable. Can be given a cestScope, otherwise assumes it's a test variable.
1135
- * @param string $variable
1136
- * @param bool $cestScope
1145
+ * Resolves regex for given inputString.
1146
+ * @param string $inputString
1137
1147
* @return string
1138
1148
*/
1139
- private function resolveTestVariable ($ variable , $ cestScope = false )
1149
+ private function resolveTestVariable ($ inputString )
1140
1150
{
1141
- $ replacement = '' ;
1142
- if (!$ cestScope ) {
1143
- preg_match ("/ \\$[\w.]+ \\$/ " , $ variable , $ match );
1144
- if (!empty ($ match )) {
1145
- $ match [0 ] = str_replace ('$ ' , '' , $ match [0 ]);
1146
- list ($ entity , $ value ) = explode ('. ' , $ match [0 ]);
1147
- $ replacement = sprintf ("$%s->getCreatedDataByName('%s') " , $ entity , $ value );
1151
+ $ outputString = $ inputString ;
1152
+ $ replaced = false ;
1153
+
1154
+ // Chesk for Cest-scope variables first, stricter regex match.
1155
+ preg_match_all ("/ \\$ \\$[\w.]+ \\$ \\$/ " , $ outputString , $ matches );
1156
+ if (!empty ($ matches )) {
1157
+ foreach ($ matches [0 ] as $ match ) {
1158
+ $ replacement = null ;
1159
+ $ variable = $ this ->stripAndSplitReference ($ match , '$$ ' );
1160
+ $ replacement = sprintf ("\$this->%s->getCreatedDataByName('%s') " , $ variable [0 ], $ variable [1 ]);
1161
+ $ outputString = str_replace ($ match , $ replacement , $ outputString );
1162
+ $ replaced = true ;
1148
1163
}
1149
- } else {
1150
- preg_match ("/ \\$ \\$[\w.]+ \\$ \\$/ " , $ variable , $ match );
1151
- if (!empty ($ match )) {
1152
- $ match [0 ] = str_replace ('$$ ' , '' , $ match [0 ]);
1153
- list ($ entity , $ value ) = explode ('. ' , $ match [0 ]);
1154
- $ replacement = sprintf ("\$this->%s->getCreatedDataByName('%s') " , $ entity , $ value );
1164
+ }
1165
+
1166
+ // Check Test-scope variables
1167
+ preg_match_all ("/ \\$[\w.]+ \\$/ " , $ outputString , $ matches );
1168
+ if (!empty ($ matches )) {
1169
+ foreach ($ matches [0 ] as $ match ) {
1170
+ $ replacement = null ;
1171
+ $ variable = $ this ->stripAndSplitReference ($ match , '$ ' );
1172
+ $ replacement = sprintf ("$%s->getCreatedDataByName('%s') " , $ variable [0 ], $ variable [1 ]);
1173
+ $ outputString = str_replace ($ match , $ replacement , $ outputString );
1174
+ $ replaced = true ;
1155
1175
}
1156
1176
}
1157
- return $ replacement ;
1177
+
1178
+ // If no replacement was made, assume it is a string literal and append single quotes.
1179
+ if (!$ replaced ) {
1180
+ return "' " . $ outputString ."' " ;
1181
+ }
1182
+
1183
+ return $ outputString ;
1184
+ }
1185
+
1186
+ /**
1187
+ * Performs str_replace on variable reference, dependent on delimiter and returns exploded array.
1188
+ * @param string $reference
1189
+ * @param string $delimiter
1190
+ * @return array
1191
+ */
1192
+ private function stripAndSplitReference ($ reference , $ delimiter )
1193
+ {
1194
+ $ strippedReference = str_replace ($ delimiter , '' , $ reference );
1195
+ return explode ('. ' , $ strippedReference );
1158
1196
}
1159
1197
1160
1198
/**
0 commit comments