@@ -25,8 +25,7 @@ function createCestFile($cestPhp, $filename)
25
25
$ exportDirectory = TESTS_BP . "/tests/acceptance/Magento/AcceptanceTest/_generated " ;
26
26
$ exportFilePath = sprintf ("%s/%s.php " , $ exportDirectory , $ filename );
27
27
28
- if (!is_dir ($ exportDirectory ))
29
- {
28
+ if (!is_dir ($ exportDirectory )) {
30
29
mkdir ($ exportDirectory , 0777 , true );
31
30
}
32
31
@@ -61,7 +60,7 @@ function createAllCestFiles()
61
60
*/
62
61
function assembleCestPhp ($ cestObject )
63
62
{
64
- $ usePhp = generateUseStatementsPhp ();
63
+ $ usePhp = generateUseStatementsPhp ($ cestObject );
65
64
$ classAnnotationsPhp = generateClassAnnotationsPhp ($ cestObject ->getAnnotations ());
66
65
$ className = $ cestObject ->getName ();
67
66
$ className = str_replace (' ' , '' , $ className );
@@ -104,12 +103,38 @@ function assembleAllCestPhp()
104
103
/**
105
104
* Creates a PHP string for the necessary Allure and AcceptanceTester use statements.
106
105
* Since we don't support other dependencies at this time, this function takes no parameter.
106
+ * @param $cestObject
107
107
* @return string
108
108
*/
109
- function generateUseStatementsPhp ()
109
+ function generateUseStatementsPhp ($ cestObject )
110
110
{
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
+
111
131
$ useStatementsPhp = "use Magento\AcceptanceTestFramework\AcceptanceTester; \n" ;
112
132
133
+ if ($ data ) {
134
+ $ useStatementsPhp .= "use Magento\AcceptanceTestFramework\DataGenerator\Managers\DataManager; \n" ;
135
+ $ useStatementsPhp .= "use Magento\AcceptanceTestFramework\DataGenerator\Api\EntityApiHandler; \n" ;
136
+ }
137
+
113
138
$ allureStatements = ["Yandex\Allure\Adapter\Annotation\Features; " ,
114
139
"Yandex\Allure\Adapter\Annotation\Stories; " ,
115
140
"Yandex\Allure\Adapter\Annotation\Title; " ,
@@ -138,70 +163,60 @@ function generateClassAnnotationsPhp($classAnnotationsObject)
138
163
139
164
foreach ($ classAnnotationsObject as $ annotationType => $ annotationName )
140
165
{
141
- if ($ annotationType == "features " )
142
- {
166
+ if ($ annotationType == "features " ) {
143
167
$ features = "" ;
144
168
145
169
foreach ($ annotationName as $ name )
146
170
{
147
171
$ features .= sprintf ("\"%s \"" , $ name );
148
172
149
- if (next ($ annotationName ))
150
- {
173
+ if (next ($ annotationName )) {
151
174
$ features .= ", " ;
152
175
}
153
176
}
154
177
155
178
$ classAnnotationsPhp .= sprintf (" * @Features({%s}) \n" , $ features );
156
179
}
157
180
158
- if ($ annotationType == "stories " )
159
- {
181
+ if ($ annotationType == "stories " ) {
160
182
$ stories = "" ;
161
183
162
184
foreach ($ annotationName as $ name )
163
185
{
164
186
$ stories .= sprintf ("\"%s \"" , $ name );
165
187
166
- if (next ($ annotationName ))
167
- {
188
+ if (next ($ annotationName )) {
168
189
$ stories .= ", " ;
169
190
}
170
191
}
171
192
172
193
$ classAnnotationsPhp .= sprintf (" * @Stories({%s}) \n" , $ stories );
173
194
}
174
195
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 ]);
178
198
}
179
199
180
- if ($ annotationType == "description " )
181
- {
200
+ if ($ annotationType == "description " ) {
182
201
$ classAnnotationsPhp .= sprintf (" * @Description( \"%s \") \n" , $ annotationName [0 ]);
183
202
}
184
203
185
- if ($ annotationType == "severity " )
186
- {
204
+ if ($ annotationType == "severity " ) {
187
205
$ classAnnotationsPhp .= sprintf (" * @Severity(level = SeverityLevel::%s) \n" , $ annotationName [0 ]);
188
206
}
189
207
190
- if ($ annotationType == "testCaseId " )
191
- {
208
+ if ($ annotationType == "testCaseId " ) {
192
209
$ classAnnotationsPhp .= sprintf (" * TestCaseId( \"%s \") " , $ annotationName [0 ]);
193
210
}
194
211
195
- if ($ annotationType == "group " )
196
- {
212
+ if ($ annotationType == "group " ) {
197
213
foreach ($ annotationName as $ group )
198
214
{
199
215
$ classAnnotationsPhp .= sprintf (" * @group %s \n" , $ group );
200
216
}
201
217
}
202
218
203
- if ($ annotationType == "env " )
204
- {
219
+ if ($ annotationType == "env " ) {
205
220
foreach ($ annotationName as $ env )
206
221
{
207
222
$ classAnnotationsPhp .= sprintf (" * @env %s \n" , $ env );
@@ -219,9 +234,10 @@ function generateClassAnnotationsPhp($classAnnotationsObject)
219
234
* 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.
220
235
* 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.
221
236
* @param $stepsObject
237
+ * @param $hookObject
222
238
* @return string
223
239
*/
224
- function generateStepsPhp ($ stepsObject )
240
+ function generateStepsPhp ($ stepsObject, $ hookObject = false )
225
241
{
226
242
$ testSteps = "" ;
227
243
@@ -320,6 +336,29 @@ function generateStepsPhp($stepsObject)
320
336
$ testSteps .= sprintf ("\t\t$%s->%s(null, %s, %s); \n" , $ actor , $ actionName , $ x , $ y );
321
337
}
322
338
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 ;
323
362
case "dontSee " :
324
363
if ($ selector ) {
325
364
$ testSteps .= sprintf ("\t\t$%s->%s(%s, %s); \n" , $ actor , $ actionName , $ input , $ selector );
@@ -649,23 +688,34 @@ function generateStepsPhp($stepsObject)
649
688
*/
650
689
function generateHooksPhp ($ hookObjects )
651
690
{
652
- $ hooks = "" ;
691
+ $ hooks = "" ;
692
+ $ createData = false ;
653
693
foreach ($ hookObjects as $ hookObject )
654
694
{
655
695
$ type = $ hookObject ->getType ();
656
696
$ dependencies = 'AcceptanceTester $I ' ;
657
- $ steps = generateStepsPhp ($ hookObject ->getActions ());
658
697
659
- if ($ type == " after " )
698
+ foreach ($ hookObject -> getActions () as $ step )
660
699
{
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 " ) {
661
712
$ hooks .= sprintf ("\tpublic function _after(%s) \n" , $ dependencies );
662
713
$ hooks .= "\t{ \n" ;
663
714
$ hooks .= $ steps ;
664
715
$ hooks .= "\t} \n\n" ;
665
716
}
666
717
667
- if ($ type == "before " )
668
- {
718
+ if ($ type == "before " ) {
669
719
$ hooks .= sprintf ("\tpublic function _before(%s) \n" , $ dependencies );
670
720
$ hooks .= "\t{ \n" ;
671
721
$ hooks .= $ steps ;
@@ -689,74 +739,64 @@ function generateTestAnnotationsPhp($testAnnotationsObject)
689
739
690
740
foreach ($ testAnnotationsObject as $ annotationType => $ annotationName )
691
741
{
692
- if ($ annotationType == "features " )
693
- {
742
+ if ($ annotationType == "features " ) {
694
743
$ features = "" ;
695
744
696
745
foreach ($ annotationName as $ name )
697
746
{
698
747
$ features .= sprintf ("\"%s \"" , $ name );
699
748
700
- if (next ($ annotationName ))
701
- {
749
+ if (next ($ annotationName )) {
702
750
$ features .= ", " ;
703
751
}
704
752
}
705
753
706
754
$ testAnnotationsPhp .= sprintf ("\t * @Features({%s}) \n" , $ features );
707
755
}
708
756
709
- if ($ annotationType == "stories " )
710
- {
757
+ if ($ annotationType == "stories " ) {
711
758
$ stories = "" ;
712
759
713
760
foreach ($ annotationName as $ name )
714
761
{
715
762
$ stories .= sprintf ("\"%s \"" , $ name );
716
763
717
- if (next ($ annotationName ))
718
- {
764
+ if (next ($ annotationName )) {
719
765
$ stories .= ", " ;
720
766
}
721
767
}
722
768
723
769
$ testAnnotationsPhp .= sprintf ("\t * @Stories({%s}) \n" , $ stories );
724
770
}
725
771
726
- if ($ annotationType == "title " )
727
- {
772
+ if ($ annotationType == "title " ) {
728
773
$ testAnnotationsPhp .= sprintf ("\t * @Title( \"%s \") \n" , $ annotationName [0 ]);
729
774
}
730
775
731
- if ($ annotationType == "description " )
732
- {
776
+ if ($ annotationType == "description " ) {
733
777
$ testAnnotationsPhp .= sprintf ("\t * @Description( \"%s \") \n" , $ annotationName [0 ]);
734
778
}
735
779
736
- if ($ annotationType == "severity " )
737
- {
780
+ if ($ annotationType == "severity " ) {
738
781
$ testAnnotationsPhp .= sprintf ("\t * @Severity(level = SeverityLevel::%s) \n" , $ annotationName [0 ]);
739
782
}
740
783
741
- if ($ annotationType == "testCaseId " )
742
- {
784
+ if ($ annotationType == "testCaseId " ) {
743
785
$ testAnnotationsPhp .= sprintf ("\t * @TestCaseId( \"%s \") \n" , $ annotationName [0 ]);
744
786
}
745
787
}
746
788
747
789
$ testAnnotationsPhp .= sprintf ("\t * @Parameter(name = \"%s \", value= \"$%s \") \n" , "AcceptanceTester " , "I " );
748
790
749
791
foreach ($ testAnnotationsObject as $ annotationType => $ annotationName ) {
750
- if ($ annotationType == "group " )
751
- {
792
+ if ($ annotationType == "group " ) {
752
793
foreach ($ annotationName as $ name )
753
794
{
754
795
$ testAnnotationsPhp .= sprintf ("\t * @group %s \n" , $ name );
755
796
}
756
797
}
757
798
758
- if ($ annotationType == "env " )
759
- {
799
+ if ($ annotationType == "env " ) {
760
800
foreach ($ annotationName as $ env )
761
801
{
762
802
$ testAnnotationsPhp .= sprintf ("\t * @env %s \n" , $ env );
@@ -795,8 +835,7 @@ function generateTestsPhp($testsObject)
795
835
$ testPhp .= $ steps ;
796
836
$ testPhp .= "\t} \n" ;
797
837
798
- if (sizeof ($ testsObject ) > 1 )
799
- {
838
+ if (sizeof ($ testsObject ) > 1 ) {
800
839
$ testPhp .= "\n" ;
801
840
}
802
841
}
@@ -807,4 +846,4 @@ function generateTestsPhp($testsObject)
807
846
/**
808
847
* Create ALL Cest files.
809
848
*/
810
- createAllCestFiles ();
849
+ createAllCestFiles ();
0 commit comments