Skip to content

Commit 2b93872

Browse files
committed
MQE-232 - Updating generator with Data Create/Delete methods.
1 parent 8c0ebee commit 2b93872

File tree

1 file changed

+93
-54
lines changed

1 file changed

+93
-54
lines changed

src/Magento/AcceptanceTestFramework/Util/GenerateTestsFromObjects.php

Lines changed: 93 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ function createCestFile($cestPhp, $filename)
2525
$exportDirectory = TESTS_BP . "/tests/acceptance/Magento/AcceptanceTest/_generated";
2626
$exportFilePath = sprintf("%s/%s.php", $exportDirectory, $filename);
2727

28-
if (!is_dir($exportDirectory))
29-
{
28+
if (!is_dir($exportDirectory)) {
3029
mkdir($exportDirectory, 0777, true);
3130
}
3231

@@ -61,7 +60,7 @@ function createAllCestFiles()
6160
*/
6261
function assembleCestPhp($cestObject)
6362
{
64-
$usePhp = generateUseStatementsPhp();
63+
$usePhp = generateUseStatementsPhp($cestObject);
6564
$classAnnotationsPhp = generateClassAnnotationsPhp($cestObject->getAnnotations());
6665
$className = $cestObject->getName();
6766
$className = str_replace(' ', '', $className);
@@ -104,12 +103,38 @@ function assembleAllCestPhp()
104103
/**
105104
* Creates a PHP string for the necessary Allure and AcceptanceTester use statements.
106105
* Since we don't support other dependencies at this time, this function takes no parameter.
106+
* @param $cestObject
107107
* @return string
108108
*/
109-
function generateUseStatementsPhp()
109+
function generateUseStatementsPhp($cestObject)
110110
{
111+
$hooks = $cestObject->getHooks();
112+
$data = false;
113+
114+
/**
115+
* Loop over each Hook object, loop over each Step in the Hook object looking for a createData or deleteData.
116+
* If they are present set a variable to True so the PHP generator uses $this->_____->createData() instead of $______->createData().
117+
*/
118+
foreach ($hooks as $hook) {
119+
if (!$data) {
120+
$steps = $hook->getActions();
121+
122+
foreach ($steps as $step) {
123+
if ($step->getType() === "createData" || $step->getType() === "deleteData") {
124+
$data = true;
125+
break;
126+
}
127+
}
128+
}
129+
}
130+
111131
$useStatementsPhp = "use Magento\AcceptanceTestFramework\AcceptanceTester;\n";
112132

133+
if ($data) {
134+
$useStatementsPhp .= "use Magento\AcceptanceTestFramework\DataGenerator\Managers\DataManager;\n";
135+
$useStatementsPhp .= "use Magento\AcceptanceTestFramework\DataGenerator\Api\EntityApiHandler;\n";
136+
}
137+
113138
$allureStatements = ["Yandex\Allure\Adapter\Annotation\Features;",
114139
"Yandex\Allure\Adapter\Annotation\Stories;",
115140
"Yandex\Allure\Adapter\Annotation\Title;",
@@ -138,70 +163,60 @@ function generateClassAnnotationsPhp($classAnnotationsObject)
138163

139164
foreach ($classAnnotationsObject as $annotationType => $annotationName)
140165
{
141-
if ($annotationType == "features")
142-
{
166+
if ($annotationType == "features") {
143167
$features = "";
144168

145169
foreach ($annotationName as $name)
146170
{
147171
$features .= sprintf("\"%s\"", $name);
148172

149-
if (next($annotationName))
150-
{
173+
if (next($annotationName)) {
151174
$features .= ", ";
152175
}
153176
}
154177

155178
$classAnnotationsPhp .= sprintf(" * @Features({%s})\n", $features);
156179
}
157180

158-
if ($annotationType == "stories")
159-
{
181+
if ($annotationType == "stories") {
160182
$stories = "";
161183

162184
foreach ($annotationName as $name)
163185
{
164186
$stories .= sprintf("\"%s\"", $name);
165187

166-
if (next($annotationName))
167-
{
188+
if (next($annotationName)) {
168189
$stories .= ", ";
169190
}
170191
}
171192

172193
$classAnnotationsPhp .= sprintf(" * @Stories({%s})\n", $stories);
173194
}
174195

175-
if ($annotationType == "title")
176-
{
177-
$classAnnotationsPhp .= sprintf(" * @Title(\"%s\")\n", $annotationName[0]);
196+
if ($annotationType == "title") {
197+
$classAnnotationsPhp .= sprintf(" * @Title(\"%s\")\n", ucwords($annotationType), $annotationName[0]);
178198
}
179199

180-
if ($annotationType == "description")
181-
{
200+
if ($annotationType == "description") {
182201
$classAnnotationsPhp .= sprintf(" * @Description(\"%s\")\n", $annotationName[0]);
183202
}
184203

185-
if ($annotationType == "severity")
186-
{
204+
if ($annotationType == "severity") {
187205
$classAnnotationsPhp .= sprintf(" * @Severity(level = SeverityLevel::%s)\n", $annotationName[0]);
188206
}
189207

190-
if ($annotationType == "testCaseId")
191-
{
208+
if ($annotationType == "testCaseId") {
192209
$classAnnotationsPhp .= sprintf(" * TestCaseId(\"%s\")", $annotationName[0]);
193210
}
194211

195-
if ($annotationType == "group")
196-
{
212+
if ($annotationType == "group") {
197213
foreach ($annotationName as $group)
198214
{
199215
$classAnnotationsPhp .= sprintf(" * @group %s\n", $group);
200216
}
201217
}
202218

203-
if ($annotationType == "env")
204-
{
219+
if ($annotationType == "env") {
205220
foreach ($annotationName as $env)
206221
{
207222
$classAnnotationsPhp .= sprintf(" * @env %s\n", $env);
@@ -219,9 +234,10 @@ function generateClassAnnotationsPhp($classAnnotationsObject)
219234
* Since nearly half of all Codeception methods don't share the same signature I had to setup a massive Case statement to handle each unique action.
220235
* At the bottom of the case statement there is a generic function that can construct the PHP string for nearly half of all Codeception actions.
221236
* @param $stepsObject
237+
* @param $hookObject
222238
* @return string
223239
*/
224-
function generateStepsPhp($stepsObject)
240+
function generateStepsPhp($stepsObject, $hookObject = false)
225241
{
226242
$testSteps = "";
227243

@@ -320,6 +336,29 @@ function generateStepsPhp($stepsObject)
320336
$testSteps .= sprintf("\t\t$%s->%s(null, %s, %s);\n", $actor, $actionName, $x, $y);
321337
}
322338
break;
339+
case "createData":
340+
$entity = $customActionAttributes['entity'];
341+
$key = $steps->getMergeKey();
342+
343+
if ($hookObject) {
344+
$testSteps .= sprintf("\t\t$%s = DataManager::getInstance()->getEntity(\"%s\");\n", $entity, $entity);
345+
$testSteps .= sprintf("\t\t\$this->%s = new EntityApiHandler($%s);\n", $key, $entity);
346+
$testSteps .= sprintf("\t\t\$this->%s->createEntity();\n", $key);
347+
} else {
348+
$testSteps .= sprintf("\t\t$%s = DataManager::getInstance()->getEntity(\"%s\");\n", $entity, $entity);
349+
$testSteps .= sprintf("\t\t$%s = new EntityApiHandler($%s);\n", $key, $entity);
350+
$testSteps .= sprintf("\t\t$%s->createEntity();\n", $key);
351+
}
352+
break;
353+
case "deleteData":
354+
$key = $customActionAttributes['createDataKey'];
355+
356+
if ($hookObject) {
357+
$testSteps .= sprintf("\t\t\$this->%s->deleteEntity();\n", $key);
358+
} else {
359+
$testSteps .= sprintf("\t\t$%s->deleteEntity();\n", $key);
360+
}
361+
break;
323362
case "dontSee":
324363
if ($selector) {
325364
$testSteps .= sprintf("\t\t$%s->%s(%s, %s);\n", $actor, $actionName, $input, $selector);
@@ -649,23 +688,34 @@ function generateStepsPhp($stepsObject)
649688
*/
650689
function generateHooksPhp($hookObjects)
651690
{
652-
$hooks = "";
691+
$hooks = "";
692+
$createData = false;
653693
foreach ($hookObjects as $hookObject)
654694
{
655695
$type = $hookObject->getType();
656696
$dependencies = 'AcceptanceTester $I';
657-
$steps = generateStepsPhp($hookObject->getActions());
658697

659-
if ($type == "after")
698+
foreach ($hookObject->getActions() as $step)
660699
{
700+
if ($step->getType() == "createData") {
701+
$hooks .= "\t/**\n";
702+
$hooks .= sprintf("\t * @var EntityApiHandler $%s;\n", $step->getMergeKey());
703+
$hooks .= "\t */\n";
704+
$hooks .= sprintf("\tprotected $%s;\n\n", $step->getMergeKey());
705+
$createData = true;
706+
}
707+
}
708+
709+
$steps = generateStepsPhp($hookObject->getActions(), $createData);
710+
711+
if ($type == "after") {
661712
$hooks .= sprintf("\tpublic function _after(%s)\n", $dependencies);
662713
$hooks .= "\t{\n";
663714
$hooks .= $steps;
664715
$hooks .= "\t}\n\n";
665716
}
666717

667-
if ($type == "before")
668-
{
718+
if ($type == "before") {
669719
$hooks .= sprintf("\tpublic function _before(%s)\n", $dependencies);
670720
$hooks .= "\t{\n";
671721
$hooks .= $steps;
@@ -689,74 +739,64 @@ function generateTestAnnotationsPhp($testAnnotationsObject)
689739

690740
foreach ($testAnnotationsObject as $annotationType => $annotationName)
691741
{
692-
if ($annotationType == "features")
693-
{
742+
if ($annotationType == "features") {
694743
$features = "";
695744

696745
foreach ($annotationName as $name)
697746
{
698747
$features .= sprintf("\"%s\"", $name);
699748

700-
if (next($annotationName))
701-
{
749+
if (next($annotationName)) {
702750
$features .= ", ";
703751
}
704752
}
705753

706754
$testAnnotationsPhp .= sprintf("\t * @Features({%s})\n", $features);
707755
}
708756

709-
if ($annotationType == "stories")
710-
{
757+
if ($annotationType == "stories") {
711758
$stories = "";
712759

713760
foreach ($annotationName as $name)
714761
{
715762
$stories .= sprintf("\"%s\"", $name);
716763

717-
if (next($annotationName))
718-
{
764+
if (next($annotationName)) {
719765
$stories .= ", ";
720766
}
721767
}
722768

723769
$testAnnotationsPhp .= sprintf("\t * @Stories({%s})\n", $stories);
724770
}
725771

726-
if ($annotationType == "title")
727-
{
772+
if ($annotationType == "title") {
728773
$testAnnotationsPhp .= sprintf("\t * @Title(\"%s\")\n", $annotationName[0]);
729774
}
730775

731-
if ($annotationType == "description")
732-
{
776+
if ($annotationType == "description") {
733777
$testAnnotationsPhp .= sprintf("\t * @Description(\"%s\")\n", $annotationName[0]);
734778
}
735779

736-
if ($annotationType == "severity")
737-
{
780+
if ($annotationType == "severity") {
738781
$testAnnotationsPhp .= sprintf("\t * @Severity(level = SeverityLevel::%s)\n", $annotationName[0]);
739782
}
740783

741-
if ($annotationType == "testCaseId")
742-
{
784+
if ($annotationType == "testCaseId") {
743785
$testAnnotationsPhp .= sprintf("\t * @TestCaseId(\"%s\")\n", $annotationName[0]);
744786
}
745787
}
746788

747789
$testAnnotationsPhp .= sprintf("\t * @Parameter(name = \"%s\", value=\"$%s\")\n", "AcceptanceTester", "I");
748790

749791
foreach ($testAnnotationsObject as $annotationType => $annotationName) {
750-
if ($annotationType == "group")
751-
{
792+
if ($annotationType == "group") {
752793
foreach ($annotationName as $name)
753794
{
754795
$testAnnotationsPhp .= sprintf("\t * @group %s\n", $name);
755796
}
756797
}
757798

758-
if ($annotationType == "env")
759-
{
799+
if ($annotationType == "env") {
760800
foreach ($annotationName as $env)
761801
{
762802
$testAnnotationsPhp .= sprintf("\t * @env %s\n", $env);
@@ -795,8 +835,7 @@ function generateTestsPhp($testsObject)
795835
$testPhp .= $steps;
796836
$testPhp .= "\t}\n";
797837

798-
if (sizeof($testsObject) > 1)
799-
{
838+
if (sizeof($testsObject) > 1) {
800839
$testPhp .= "\n";
801840
}
802841
}
@@ -807,4 +846,4 @@ function generateTestsPhp($testsObject)
807846
/**
808847
* Create ALL Cest files.
809848
*/
810-
createAllCestFiles();
849+
createAllCestFiles();

0 commit comments

Comments
 (0)