@@ -26,7 +26,11 @@ class GroupClassGenerator
26
26
const REQUIRED_ENTITY_KEY = 'requiredEntities ' ;
27
27
const LAST_REQUIRED_ENTITY_TAG = 'last ' ;
28
28
const MUSTACHE_VAR_TAG = 'var ' ;
29
-
29
+ const MAGENTO_CLI_COMMAND_COMMAND = 'command ' ;
30
+ const DATA_PERSISTENCE_ACTIONS = ["createData " , "deleteData " ];
31
+ const REPLACEMENT_ACTIONS = [
32
+ 'comment ' => 'print '
33
+ ];
30
34
const GROUP_DIR_NAME = 'Group ' ;
31
35
32
36
/**
@@ -83,14 +87,30 @@ private function createClassContent($suiteObject)
83
87
84
88
$ mustacheData [self ::BEFORE_MUSTACHE_KEY ] = $ this ->buildHookMustacheArray ($ suiteObject ->getBeforeHook ());
85
89
$ mustacheData [self ::AFTER_MUSTACHE_KEY ] = $ this ->buildHookMustacheArray ($ suiteObject ->getAfterHook ());
86
- $ mustacheData [self ::MUSTACHE_VAR_TAG ] = array_merge (
87
- $ mustacheData [self ::BEFORE_MUSTACHE_KEY ][ ' createData ' ] ?? [] ,
88
- $ mustacheData [self ::AFTER_MUSTACHE_KEY ][ ' createData ' ] ?? []
90
+ $ mustacheData [self ::MUSTACHE_VAR_TAG ] = $ this -> extractClassVar (
91
+ $ mustacheData [self ::BEFORE_MUSTACHE_KEY ],
92
+ $ mustacheData [self ::AFTER_MUSTACHE_KEY ]
89
93
);
90
94
91
95
return $ this ->mustacheEngine ->render (self ::MUSTACHE_TEMPLATE_NAME , $ mustacheData );
92
96
}
93
97
98
+ /**
99
+ * Function which takes the before and after arrays containing the steps for the hook objects and extracts
100
+ * any variables names needed by the class template.
101
+ *
102
+ * @param array $beforeArray
103
+ * @param array $afterArray
104
+ * @return array
105
+ */
106
+ private function extractClassVar ($ beforeArray , $ afterArray )
107
+ {
108
+ $ beforeVar = $ beforeArray [self ::MUSTACHE_VAR_TAG ] ?? [];
109
+ $ afterVar = $ afterArray [self ::MUSTACHE_VAR_TAG ] ?? [];
110
+
111
+ return array_merge ($ beforeVar , $ afterVar );
112
+ }
113
+
94
114
/**
95
115
* Function which takes hook objects and transforms data into array for mustache template engine.
96
116
*
@@ -100,26 +120,112 @@ private function createClassContent($suiteObject)
100
120
private function buildHookMustacheArray ($ hookObj )
101
121
{
102
122
$ mustacheHookArray = [];
123
+ $ actions = [];
124
+ $ hasWebDriverActions = false ;
103
125
foreach ($ hookObj ->getActions () as $ action ) {
104
126
/** @var ActionObject $action */
127
+ $ index = count ($ actions );
128
+ if (!in_array ($ action ->getType (), self ::DATA_PERSISTENCE_ACTIONS )) {
129
+ if (!$ hasWebDriverActions ) {
130
+ $ hasWebDriverActions = true ;
131
+ }
132
+
133
+ $ actions = $ this ->buildWebDriverActionsMustacheArray ($ action , $ actions , $ index );
134
+ continue ;
135
+ }
136
+
137
+ // add these as vars to be created a class level in the template
138
+ if ($ action ->getType () == 'createData ' ) {
139
+ $ mustacheHookArray [self ::MUSTACHE_VAR_TAG ][] = [self ::ENTITY_MERGE_KEY => $ action ->getStepKey ()];
140
+ }
141
+
105
142
$ entityArray = [];
106
143
$ entityArray [self ::ENTITY_MERGE_KEY ] = $ action ->getStepKey ();
107
- $ entityArray [self ::ENTITY_NAME_TAG ] =
108
- $ action ->getCustomActionAttributes ()['entity ' ] ??
109
- $ action ->getCustomActionAttributes ()[TestGenerator::REQUIRED_ENTITY_REFERENCE ];
110
-
111
- // if there is more than 1 custom attribute, we can assume there are required entities
112
- if (count ($ action ->getCustomActionAttributes ()) > 1 ) {
113
- $ entityArray [self ::REQUIRED_ENTITY_KEY ] =
114
- $ this ->buildReqEntitiesMustacheArray ($ action ->getCustomActionAttributes ());
115
- }
144
+ $ entityArray [$ action ->getType ()] = $ action ->getStepKey ();
116
145
117
- $ mustacheHookArray [$ action ->getType ()][] = $ entityArray ;
146
+ $ entityArray = $ this ->buildPersistenceMustacheArray ($ action , $ entityArray );
147
+ $ actions [$ index ] = $ entityArray ;
148
+ }
149
+ $ mustacheHookArray ['actions ' ] = $ actions ;
150
+ if ($ hasWebDriverActions ) {
151
+ array_unshift ($ mustacheHookArray ['actions ' ], ['webDriverInit ' => true ]);
152
+ $ mustacheHookArray ['actions ' ][] = ['webDriverReset ' => true ];
118
153
}
119
154
120
155
return $ mustacheHookArray ;
121
156
}
122
157
158
+ /**
159
+ * Takes an action object and array of generated action steps. Converst the action object into generated php and
160
+ * appends the entry to the given array. The result is returned by the function.
161
+ *
162
+ * @param ActionObject $action
163
+ * @param array $actionEntries
164
+ * @return array
165
+ */
166
+ private function buildWebDriverActionsMustacheArray ($ action , $ actionEntries )
167
+ {
168
+ $ step = TestGenerator::getInstance ()->generateStepsPhp ([$ action ], false , 'webDriver ' );
169
+ $ rawPhp = str_replace (["\t" , "\n" ], "" , $ step );
170
+ $ multipleCommands = explode ("; " , $ rawPhp , -1 );
171
+ foreach ($ multipleCommands as $ command ) {
172
+ $ actionEntries = $ this ->replaceReservedTesterFunctions ($ command . "; " , $ actionEntries , 'webDriver ' );
173
+ }
174
+
175
+ return $ actionEntries ;
176
+ }
177
+
178
+ /**
179
+ * Takes a generated php step, an array containing generated php entries for the template, and the actor name
180
+ * for the generated step.
181
+ *
182
+ * @param string $formattedStep
183
+ * @param array $actionEntries
184
+ * @param string $actor
185
+ * @return array
186
+ */
187
+ private function replaceReservedTesterFunctions ($ formattedStep , $ actionEntries , $ actor )
188
+ {
189
+ foreach (self ::REPLACEMENT_ACTIONS as $ testAction => $ replacement ) {
190
+ $ testActionCall = "\${$ actor }-> {$ testAction }" ;
191
+ if (substr ($ formattedStep , 0 , strlen ($ testActionCall )) == $ testActionCall ) {
192
+ $ resultingStep = str_replace ($ testActionCall , $ replacement , $ formattedStep );
193
+ $ actionEntries [] = ['action ' => $ resultingStep ];
194
+ } else {
195
+ $ actionEntries [] = ['action ' => $ formattedStep ];
196
+ }
197
+ }
198
+
199
+ return $ actionEntries ;
200
+ }
201
+
202
+ /**
203
+ * Takes an action object of persistence type and formats an array entiry for mustache template interpretation.
204
+ *
205
+ * @param ActionObject $action
206
+ * @param array $entityArray
207
+ * @return array
208
+ */
209
+ private function buildPersistenceMustacheArray ($ action , $ entityArray )
210
+ {
211
+ $ entityArray [self ::ENTITY_NAME_TAG ] =
212
+ $ action ->getCustomActionAttributes ()['entity ' ] ??
213
+ $ action ->getCustomActionAttributes ()[TestGenerator::REQUIRED_ENTITY_REFERENCE ];
214
+
215
+ // append entries for any required entities to this entry
216
+ if (array_key_exists ('requiredEntities ' , $ action ->getCustomActionAttributes ())) {
217
+ $ entityArray [self ::REQUIRED_ENTITY_KEY ] =
218
+ $ this ->buildReqEntitiesMustacheArray ($ action ->getCustomActionAttributes ());
219
+ }
220
+
221
+ // append entries for customFields if specified by the user.
222
+ if (array_key_exists ('customFields ' , $ action ->getCustomActionAttributes ())) {
223
+ $ entityArray ['customFields ' ] = $ action ->getStepKey () . 'Fields ' ;
224
+ }
225
+
226
+ return $ entityArray ;
227
+ }
228
+
123
229
/**
124
230
* Function which takes any required entities under a 'createData' tag and transforms data into array to be consumed
125
231
* by mustache template.
0 commit comments