-
Notifications
You must be signed in to change notification settings - Fork 132
/
Copy pathActionGroupObject.php
374 lines (326 loc) · 12.5 KB
/
ActionGroupObject.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\FunctionalTestingFramework\Test\Objects;
use Magento\FunctionalTestingFramework\Exceptions\TestReferenceException;
use Magento\FunctionalTestingFramework\Test\Util\ActionGroupObjectExtractor;
use Magento\FunctionalTestingFramework\Test\Util\ActionMergeUtil;
/**
* Class ActionGroupObject
*/
class ActionGroupObject
{
const ACTION_GROUP_ORIGIN_NAME = "actionGroupName";
const ACTION_GROUP_ORIGIN_TEST_REF = "testInvocationRef";
/**
* Array of variable-enabled attributes.
* @var array
*/
private $varAttributes;
/**
* The name of the action group
*
* @var string
*/
private $name;
/**
* An array of action objects
*
* @var array
*/
private $parsedActions = [];
/**
* An array used to store argument names to values
*
* @var array
*/
private $arguments;
/**
* ActionGroupObject constructor.
*
* @param string $name
* @param ArgumentObject[] $arguments
* @param array $actions
*/
public function __construct($name, $arguments, $actions)
{
$this->varAttributes = array_merge(
ActionObject::SELECTOR_ENABLED_ATTRIBUTES,
ActionObject::DATA_ENABLED_ATTRIBUTES
);
$this->varAttributes[] = ActionObject::ACTION_ATTRIBUTE_URL;
$this->name = $name;
$this->arguments = $arguments;
$this->parsedActions = $actions;
}
/**
* Gets the ordered steps including merged waits
*
* @param array $arguments
* @param string $actionReferenceKey
* @return array
* @throws TestReferenceException
*/
public function getSteps($arguments, $actionReferenceKey)
{
$mergeUtil = new ActionMergeUtil($this->name, "ActionGroup");
$args = $this->resolveArguments($arguments);
return $mergeUtil->resolveActionSteps($this->getResolvedActionsWithArgs($args, $actionReferenceKey), true);
}
/**
* Iterates through given $arguments and overrides ActionGroup's argument values, if any are found.
* @param array $arguments
* @return ArgumentObject[]
* @throws TestReferenceException
*/
private function resolveArguments($arguments)
{
$resolvedArgumentList = [];
$emptyArguments = [];
foreach ($this->arguments as $argumentObj) {
if ($arguments !== null && array_key_exists($argumentObj->getName(), $arguments)) {
$resolvedArgumentList[] = new ArgumentObject(
$argumentObj->getName(),
$arguments[$argumentObj->getName()],
$argumentObj->getDataType()
);
} elseif ($argumentObj->getValue() === null) {
$emptyArguments[] = $argumentObj->getName();
} else {
$resolvedArgumentList[] = $argumentObj;
}
}
if (!empty($emptyArguments)) {
$error = 'Arguments missed (' . implode(", ", $emptyArguments) . ') for actionGroup "' . $this->name . '"';
throw new TestReferenceException($error);
}
return $resolvedArgumentList;
}
/**
* Function which takes a set of arguments to be appended to an action objects fields returns resulting
* action objects with proper argument.field references.
*
* @param array $arguments
* @param string $actionReferenceKey
* @return array
*/
private function getResolvedActionsWithArgs($arguments, $actionReferenceKey)
{
$resolvedActions = [];
foreach ($this->parsedActions as $action) {
$varAttributes = array_intersect($this->varAttributes, array_keys($action->getCustomActionAttributes()));
$newActionAttributes = [];
if (!empty($varAttributes)) {
$newActionAttributes = $this->resolveAttributesWithArguments(
$arguments,
$action->getCustomActionAttributes()
);
}
// we append the action reference key to any linked action and the action's merge key as the user might
// use this action group multiple times in the same test.
$resolvedActions[$action->getStepKey() . ucfirst($actionReferenceKey)] = new ActionObject(
$action->getStepKey() . ucfirst($actionReferenceKey),
$action->getType(),
array_replace_recursive($action->getCustomActionAttributes(), $newActionAttributes),
$action->getLinkedAction() == null ? null : $action->getLinkedAction() . ucfirst($actionReferenceKey),
$action->getOrderOffset(),
[self::ACTION_GROUP_ORIGIN_NAME => $this->name,
self::ACTION_GROUP_ORIGIN_TEST_REF => $actionReferenceKey]
);
}
return $resolvedActions;
}
/**
* Resolves all references to arguments in attributes, and subAttributes.
* @param array $arguments
* @param array $attributes
* @return array
*/
private function resolveAttributesWithArguments($arguments, $attributes)
{
// $regexPattern match on: $matches[0] {{section.element(arg.field)}}
// $matches[1] = section.element
// $matches[2] = arg.field
$regexPattern = '/{{([\w.\[\]]+)\(*([\w.$\',\s\[\]]+)*\)*}}/';
$newActionAttributes = [];
foreach ($attributes as $attributeKey => $attributeValue) {
if (is_array($attributeValue)) {
// attributes with child elements are parsed as an array, need make recursive call to resolve children
$newActionAttributes[$attributeKey] = $this->resolveAttributesWithArguments(
$arguments,
$attributeValue
);
continue;
}
preg_match_all($regexPattern, $attributeValue, $matches);
if (empty($matches[0])) {
continue;
}
//get rid of full match {{arg.field(arg.field)}}
array_shift($matches);
$newActionAttributes[$attributeKey] = $this->replaceAttributeArguments(
$arguments,
$attributeValue,
$matches
);
}
return $newActionAttributes;
}
/**
* Function that takes an array of replacement arguments, and matches them with args in an actionGroup's attribute.
* Determines if the replacement arguments are persisted data, and replaces them accordingly.
*
* @param array $arguments
* @param string $attributeValue
* @param array $matches
* @return string
*/
private function replaceAttributeArguments($arguments, $attributeValue, $matches)
{
list($mainValueList, $possibleArgumentsList) = $matches;
foreach ($mainValueList as $index => $mainValue) {
$possibleArguments = $possibleArgumentsList[$index];
$attributeValue = $this->replaceAttributeArgumentInVariable($mainValue, $arguments, $attributeValue);
// Split on commas, trim all values, and finally filter out all FALSE values
$argumentList = array_filter(array_map('trim', explode(',', $possibleArguments)));
foreach ($argumentList as $argumentValue) {
$attributeValue = $this->replaceAttributeArgumentInVariable(
$argumentValue,
$arguments,
$attributeValue,
true
);
}
}
return $attributeValue;
}
/**
* Replace attribute arguments in variable.
*
* @param string $variable
* @param array $arguments
* @param string $attributeValue
* @param bool $isInnerArgument
* @return string
*/
private function replaceAttributeArgumentInVariable(
$variable,
$arguments,
$attributeValue,
$isInnerArgument = false
) {
// Truncate arg.field into arg
$variableName = strstr($variable, '.', true);
// Check if arguments has a mapping for the given variableName
if ($variableName === false) {
$variableName = trim($variable, "'");
}
$matchedArgument = $this->findArgumentByName($variableName, $arguments);
if ($matchedArgument === null) {
return $attributeValue;
}
if ($matchedArgument->getDataType() === ArgumentObject::ARGUMENT_DATA_STRING) {
return $this->replaceSimpleArgument(
$matchedArgument->getResolvedValue($isInnerArgument),
$variableName,
$attributeValue,
$isInnerArgument
);
}
$isPersisted = preg_match('/\$[\w.\[\]() ]+\$/', $matchedArgument->getResolvedValue($isInnerArgument));
if ($isPersisted) {
return $this->replacePersistedArgument(
$matchedArgument->getResolvedValue($isInnerArgument),
$attributeValue,
$variable,
$variableName,
$isInnerArgument
);
}
//replace argument ONLY when there is no letters attached before after (ex. category.name vs categoryTreeButton)
return preg_replace(
"/(?<![\w]){$variableName}(?![(\w])/",
$matchedArgument->getResolvedValue($isInnerArgument),
$attributeValue
);
}
/**
* Replaces any arguments that were declared as simpleData="true".
* Takes in isInnerArgument to determine what kind of replacement to expect: {{data}} vs section.element(data)
* @param string $argumentValue
* @param string $variableName
* @param string $attributeValue
* @param boolean $isInnerArgument
* @return string
*/
private function replaceSimpleArgument($argumentValue, $variableName, $attributeValue, $isInnerArgument)
{
if ($isInnerArgument) {
return preg_replace("/(?<![\w]){$variableName}(?![(\w])/", $argumentValue, $attributeValue);
} else {
return str_replace("{{{$variableName}}}", $argumentValue, $attributeValue);
}
}
/**
* Replaces args with replacements given, behavior is specific to persisted arguments.
* @param string $replacement
* @param string $attributeValue
* @param string $fullVariable
* @param string $variable
* @param boolean $isParameter
* @return string
*/
private function replacePersistedArgument($replacement, $attributeValue, $fullVariable, $variable, $isParameter)
{
//hookPersisted will be true if replacement passed in is $$arg.field$$, otherwise assume it's $arg.field$
$hookPersistedArgumentRegex = '/\$\$[\w.\[\]\',]+\$\$/';
$hookPersisted = (preg_match($hookPersistedArgumentRegex, $replacement));
$newAttributeValue = $attributeValue;
$scope = '$';
if ($hookPersisted) {
$scope = '$$';
}
// parameter replacements require changing of (arg.field) to ($arg.field$)
if ($isParameter) {
$fullReplacement = str_replace($variable, trim($replacement, '$'), trim($fullVariable, "'"));
$newAttributeValue = str_replace($fullVariable, $scope . $fullReplacement . $scope, $newAttributeValue);
} else {
$newAttributeValue = str_replace('{{', $scope, str_replace('}}', $scope, $newAttributeValue));
$newAttributeValue = str_replace($variable, trim($replacement, '$'), $newAttributeValue);
}
return $newAttributeValue;
}
/**
* Finds and returns all original stepkeys of actions in actionGroup.
* @return string[]
*/
public function extractStepKeys()
{
$originalKeys = [];
foreach ($this->parsedActions as $action) {
$originalKeys[] = $action->getStepKey();
}
return $originalKeys;
}
/**
* Searches through ActionGroupObject's arguments and returns first argument wi
* @param string $name
* @param array $argumentList
* @return ArgumentObject|null
*/
private function findArgumentByName($name, $argumentList)
{
$matchedArgument = array_filter(
$argumentList,
function ($e) use ($name) {
return $e->getName() == $name;
}
);
if (isset(array_values($matchedArgument)[0])) {
return array_values($matchedArgument)[0];
}
return null;
}
}