Skip to content

Commit a92df01

Browse files
committed
MQE-391: Variable is not correctly resolved when array symbol ([ or ]) is inside $$ signs
- Added check for syntax of $data.key[index]$, if so generates line like "$data.getCreatedData('key')['index']
1 parent baef8f0 commit a92df01

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

src/Magento/FunctionalTestingFramework/Util/TestGenerator.php

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -806,7 +806,7 @@ private function resolveTestVariable($inputString, $quoteBreak = false)
806806
$replaced = false;
807807

808808
// Check for Cest-scope variables first, stricter regex match.
809-
preg_match_all("/\\$\\$[\w.]+\\$\\$/", $outputString, $matches);
809+
preg_match_all("/\\$\\$[\w.\[\]]+\\$\\$/", $outputString, $matches);
810810
foreach ($matches[0] as $match) {
811811
$replacement = null;
812812
$variable = $this->stripAndSplitReference($match, '$$');
@@ -816,7 +816,19 @@ private function resolveTestVariable($inputString, $quoteBreak = false)
816816
". Hook persisted entity references must follow \$\$entityMergeKey.field\$\$ format."
817817
);
818818
}
819-
$replacement = sprintf("\$this->%s->getCreatedDataByName('%s')", $variable[0], $variable[1]);
819+
preg_match_all("/\[[\w.]+\]/", $variable[1], $arrayMatch);
820+
if (!empty($arrayMatch[0])) {
821+
$variable[1] = str_replace($arrayMatch[0][0], "", $variable[1]);
822+
$arrayMatch[0][0] = trim($arrayMatch[0][0], "[]");
823+
$replacement = sprintf(
824+
"\$this->%s->getCreatedDataByName('%s')['%s']",
825+
$variable[0],
826+
$variable[1],
827+
$arrayMatch[0][0]
828+
);
829+
} else {
830+
$replacement = sprintf("\$this->%s->getCreatedDataByName('%s')", $variable[0], $variable[1]);
831+
}
820832
if ($quoteBreak) {
821833
$replacement = '" . ' . $replacement . ' . "';
822834
}
@@ -825,7 +837,7 @@ private function resolveTestVariable($inputString, $quoteBreak = false)
825837
}
826838

827839
// Check Test-scope variables
828-
preg_match_all("/\\$[\w.]+\\$/", $outputString, $matches);
840+
preg_match_all("/\\$[\w.\[\]]+\\$/", $outputString, $matches);
829841
foreach ($matches[0] as $match) {
830842
$replacement = null;
831843
$variable = $this->stripAndSplitReference($match, '$');
@@ -835,7 +847,19 @@ private function resolveTestVariable($inputString, $quoteBreak = false)
835847
". Test persisted entity references must follow \$entityMergeKey.field\$ format."
836848
);
837849
}
838-
$replacement = sprintf("$%s->getCreatedDataByName('%s')", $variable[0], $variable[1]);
850+
preg_match_all("/\[[\w.]+\]/", $variable[1], $arrayMatch);
851+
if (!empty($arrayMatch[0])) {
852+
$variable[1] = str_replace($arrayMatch[0][0], "", $variable[1]);
853+
$arrayMatch[0][0] = trim($arrayMatch[0][0], "[]");
854+
$replacement = sprintf(
855+
"$%s->getCreatedDataByName('%s')['%s']",
856+
$variable[0],
857+
$variable[1],
858+
$arrayMatch[0][0]
859+
);
860+
} else {
861+
$replacement = sprintf("$%s->getCreatedDataByName('%s')", $variable[0], $variable[1]);
862+
}
839863
if ($quoteBreak) {
840864
$replacement = '" . ' . $replacement . ' . "';
841865
}

0 commit comments

Comments
 (0)