-
Notifications
You must be signed in to change notification settings - Fork 132
/
Copy pathActionGroupObject.php
257 lines (224 loc) · 8.76 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
<?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\ActionMergeUtil;
/**
* Class ActionGroupObject
*/
class ActionGroupObject
{
/**
* 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 the default entities if the user does not specify any
*
* @var array
*/
private $arguments;
/**
* ActionGroupObject constructor.
*
* @param string $name
* @param string $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->arguments;
$emptyArguments = array_keys($args, null, true);
if (!empty($emptyArguments) && $arguments !== null) {
$diff = array_diff($emptyArguments, array_keys($arguments));
if (!empty($diff)) {
$error = 'Argument(s) missed (' . implode(", ", $diff) . ') for actionGroup "' . $this->name . '"';
throw new TestReferenceException($error);
}
} elseif (!empty($emptyArguments)) {
$error = 'Not enough arguments given for actionGroup "' . $this->name . '"';
throw new TestReferenceException($error);
}
if ($arguments) {
$args = array_merge($args, $arguments);
}
return $mergeUtil->resolveActionSteps($this->getResolvedActionsWithArgs($args, $actionReferenceKey), true);
}
/**
* 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 = [];
// $regexPattern match on: $matches[0] {{section.element(arg.field)}}
// $matches[1] = section.element
// $matches[2] = arg.field
$regexPattern = '/{{([\w.\[\]]+)\(*([\w.$\',\s]+)*\)*}}/';
foreach ($this->parsedActions as $action) {
$varAttributes = array_intersect($this->varAttributes, array_keys($action->getCustomActionAttributes()));
$newActionAttributes = [];
if (!empty($varAttributes)) {
// 1 check to see if we have pertinent var
foreach ($varAttributes as $varAttribute) {
$attributeValue = $action->getCustomActionAttributes()[$varAttribute];
preg_match_all($regexPattern, $attributeValue, $matches);
if (empty($matches[0])) {
continue;
}
//get rid of full match {{arg.field(arg.field)}}
array_shift($matches);
$newActionAttributes[$varAttribute] = $this->replaceAttributeArguments(
$arguments,
$attributeValue,
$matches
);
}
}
// 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->getMergeKey() . $actionReferenceKey] = new ActionObject(
$action->getMergeKey() . $actionReferenceKey,
$action->getType(),
array_merge($action->getCustomActionAttributes(), $newActionAttributes),
$action->getLinkedAction() == null ? null : $action->getLinkedAction() . $actionReferenceKey,
$action->getOrderOffset()
);
}
return $resolvedActions;
}
/**
* 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 = $variable;
}
if (!array_key_exists($variableName, $arguments)) {
return $attributeValue;
}
$isPersisted = strstr($arguments[$variableName], '$');
if ($isPersisted) {
return $this->replacePersistedArgument(
$arguments[$variableName],
$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])/", $arguments[$variableName], $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, '$'), $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;
}
}