Skip to content

Commit ecc2494

Browse files
authored
MQE-288 Data Persistence using multiple created Entities
1 parent 998cd7c commit ecc2494

File tree

1 file changed

+70
-32
lines changed

1 file changed

+70
-32
lines changed

src/Magento/AcceptanceTestFramework/Util/TestGenerator.php

Lines changed: 70 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -568,17 +568,8 @@ private function generateStepsPhp($stepsObject, $stepsData, $hookObject = false)
568568
case "entity":
569569
$entityData = "[";
570570
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);
582573
}
583574
$entityData .= ']';
584575
if ($hookObject) {
@@ -639,7 +630,14 @@ private function generateStepsPhp($stepsObject, $stepsData, $hookObject = false)
639630
}
640631
break;
641632
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+
);
643641
break;
644642
case "grabFromCurrentUrl":
645643
if ($input) {
@@ -656,9 +654,22 @@ private function generateStepsPhp($stepsObject, $stepsData, $hookObject = false)
656654
break;
657655
case "grabMultiple":
658656
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+
);
660665
} 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+
);
662673
}
663674
break;
664675
case "grabValueFrom":
@@ -1131,30 +1142,57 @@ private function generateStepsPhp($stepsObject, $stepsData, $hookObject = false)
11311142
}
11321143

11331144
/**
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
11371147
* @return string
11381148
*/
1139-
private function resolveTestVariable($variable, $cestScope = false)
1149+
private function resolveTestVariable($inputString)
11401150
{
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;
11481163
}
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;
11551175
}
11561176
}
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);
11581196
}
11591197

11601198
/**

0 commit comments

Comments
 (0)