forked from magento/magento2-functional-testing-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathActionObjectExtractor.php
276 lines (239 loc) · 10.6 KB
/
ActionObjectExtractor.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
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\FunctionalTestingFramework\Test\Util;
use Magento\FunctionalTestingFramework\Config\MftfApplicationConfig;
use Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException;
use Magento\FunctionalTestingFramework\Exceptions\TestReferenceException;
use Magento\FunctionalTestingFramework\Exceptions\XmlException;
use Magento\FunctionalTestingFramework\Test\Objects\ActionObject;
use Magento\FunctionalTestingFramework\Util\Logger\LoggingUtil;
/**
* Class ActionObjectExtractor
*/
class ActionObjectExtractor extends BaseObjectExtractor
{
const TEST_ACTION_BEFORE = 'before';
const TEST_ACTION_AFTER = 'after';
const TEST_STEP_MERGE_KEY = 'stepKey';
const ACTION_GROUP_TAG = 'actionGroup';
const ACTION_GROUP_REF = 'ref';
const ACTION_GROUP_ARGUMENTS = 'arguments';
const ACTION_GROUP_ARG_VALUE = 'value';
const BEFORE_AFTER_ERROR_MSG = "Merge Error - Steps cannot have both before and after attributes.\tStepKey='%s'";
const STEP_KEY_BLACKLIST_ERROR_MSG = "StepKeys cannot contain non alphanumeric characters.\tStepKey='%s'";
const STEP_KEY_EMPTY_ERROR_MSG = "StepKeys cannot be empty.\tAction='%s'";
const DATA_PERSISTENCE_CUSTOM_FIELD = 'field';
const DATA_PERSISTENCE_CUSTOM_FIELD_KEY = 'key';
const ACTION_OBJECT_PERSISTENCE_FIELDS = 'customFields';
const ACTION_OBJECT_USER_INPUT = 'userInput';
const DATA_PERSISTENCE_ACTIONS = ['createData', 'deleteData', 'updateData'];
/**
* ActionObjectExtractor constructor.
*/
public function __construct()
{
//public constructor
}
/**
* This method takes an array of test actions read in from a TestHook or Test. The actions are stripped of
* irrelevant tags and returned as an array of ActionObjects.
*
* @param array $testActions
* @param string $testName
* @return array
* @throws XmlException
* @throws TestReferenceException
*/
public function extractActions($testActions, $testName = null)
{
$actions = [];
$stepKeyRefs = [];
foreach ($testActions as $actionName => $actionData) {
// Removing # from nodeName to match stepKey requirements
$stepKey = strpos($actionData[self::NODE_NAME], ActionObject::COMMENT_ACTION) === false
? $actionData[self::TEST_STEP_MERGE_KEY]
: str_replace("#", "", $actionName);
$actionType = $actionData[self::NODE_NAME];
if (empty($stepKey)) {
throw new XmlException(sprintf(self::STEP_KEY_EMPTY_ERROR_MSG, $actionData['nodeName']));
}
if (preg_match('/[^a-zA-Z0-9_]/', $stepKey)) {
throw new XmlException(sprintf(self::STEP_KEY_BLACKLIST_ERROR_MSG, $actionName));
}
$actionAttributes = $this->stripDescriptorTags(
$actionData,
self::TEST_STEP_MERGE_KEY,
self::NODE_NAME
);
// Flatten AssertSorted "array" element to parameterArray
if (isset($actionData["array"])) {
$actionAttributes['parameterArray'] = $actionData['array']['value'];
}
$actionAttributes = $this->processActionGroupArgs($actionType, $actionAttributes);
$linkedAction = $this->processLinkedActions($actionName, $actionData);
$actions = $this->extractFieldActions($actionData, $actions);
$actionAttributes = $this->extractFieldReferences($actionData, $actionAttributes);
if ($linkedAction['stepKey'] != null) {
$stepKeyRefs[$linkedAction['stepKey']][] = $stepKey;
}
// TODO this is to be implemented later. Currently the schema does not use or need return var.
/*if (array_key_exists(ActionGroupObjectHandler::TEST_ACTION_RETURN_VARIABLE, $actionData)) {
$returnVariable = $actionData[ActionGroupObjectHandler::TEST_ACTION_RETURN_VARIABLE];
}*/
$actions[$stepKey] = new ActionObject(
$stepKey,
$actionType,
$actionAttributes,
$linkedAction['stepKey'],
$linkedAction['order']
);
}
$this->auditMergeSteps($stepKeyRefs, $testName);
return $actions;
}
/**
* Function which processes any actions which have an explicit reference to an additional step for merging purposes.
* Returns an array with keys corresponding to the linked action's stepKey and order.
*
* @param string $actionName
* @param array $actionData
* @return array
* @throws XmlException
*/
private function processLinkedActions($actionName, $actionData)
{
$linkedAction =['stepKey' => null, 'order' => null];
if (array_key_exists(self::TEST_ACTION_BEFORE, $actionData)
and array_key_exists(self::TEST_ACTION_AFTER, $actionData)) {
throw new XmlException(sprintf(self::BEFORE_AFTER_ERROR_MSG, $actionName));
}
if (array_key_exists(self::TEST_ACTION_BEFORE, $actionData)) {
$linkedAction['stepKey'] = $actionData[self::TEST_ACTION_BEFORE];
$linkedAction['order'] = self::TEST_ACTION_BEFORE;
} elseif (array_key_exists(self::TEST_ACTION_AFTER, $actionData)) {
$linkedAction['stepKey'] = $actionData[self::TEST_ACTION_AFTER];
$linkedAction['order'] = self::TEST_ACTION_AFTER;
}
return $linkedAction;
}
/**
* Takes the action group reference and parses out arguments as an array that can be passed to override defaults
* defined in the action group xml.
*
* @param string $actionType
* @param array $actionAttributeData
* @return array
*/
private function processActionGroupArgs($actionType, $actionAttributeData)
{
if ($actionType !== self::ACTION_GROUP_TAG) {
return $actionAttributeData;
}
$actionAttributeArgData = [];
foreach ($actionAttributeData as $attributeDataKey => $attributeDataValues) {
if ($attributeDataKey == self::ACTION_GROUP_REF) {
$actionAttributeArgData[self::ACTION_GROUP_REF] = $attributeDataValues;
continue;
}
$actionAttributeArgData[self::ACTION_GROUP_ARGUMENTS][$attributeDataKey] =
$attributeDataValues[self::ACTION_GROUP_ARG_VALUE] ?? null;
}
return $actionAttributeArgData;
}
/**
* Takes the array representing an action and validates it is a persistence type. If of type persistence,
* the function checks for any user specified fields to extract as separate actions to be resolved independently
* from the the persistence method.
*
* @param array $actionData
* @param array $actions
* @return array
* @throws XmlException
* @throws TestReferenceException
*/
private function extractFieldActions($actionData, $actions)
{
if (!in_array($actionData[self::NODE_NAME], self::DATA_PERSISTENCE_ACTIONS)) {
return $actions;
}
$fieldActions = [];
foreach ($actionData as $type => $data) {
// determine if field type is entity passed in
if (!is_array($data) || $data[self::NODE_NAME] != self::DATA_PERSISTENCE_CUSTOM_FIELD) {
continue;
}
// must append stepKey and userInput to resolve fields passed in properly via extractActions method
$fieldData = $data;
$fieldData[self::TEST_STEP_MERGE_KEY] = $actionData[self::TEST_STEP_MERGE_KEY]
. ucfirst($fieldData[self::DATA_PERSISTENCE_CUSTOM_FIELD_KEY]);
$fieldData[self::ACTION_OBJECT_USER_INPUT] = $fieldData['value'];
$fieldActions[] = $fieldData;
}
// merge resolved actions with those psased in and return
return array_merge($actions, $this->extractActions($fieldActions));
}
/**
* Takes the array representing an action and validates it is a persistence type. If of type persistence, the
* function creates a new set of attributes for the persistence method which represent a named list of fields and
* any other references (such as required entities etc.)
*
* @param array $actionData
* @param array $actionAttributes
* @return array
*/
private function extractFieldReferences($actionData, $actionAttributes)
{
if (!in_array($actionData[self::NODE_NAME], self::DATA_PERSISTENCE_ACTIONS)) {
return $actionAttributes;
}
$attributes = [];
foreach ($actionAttributes as $attributeName => $attributeValue) {
if (!is_array($attributeValue) || $attributeValue[self::NODE_NAME] != self::DATA_PERSISTENCE_CUSTOM_FIELD) {
$attributes[$attributeName] = $attributeValue;
continue;
}
$attributes[self::ACTION_OBJECT_PERSISTENCE_FIELDS][] = $attributeName;
}
if (array_key_exists(self::ACTION_OBJECT_PERSISTENCE_FIELDS, $attributes)) {
$attributes[self::ACTION_OBJECT_PERSISTENCE_FIELDS][self::NODE_NAME] = 'fields';
}
return $attributes;
}
/**
* Function which validates stepKey references within mergeable actions
*
* @param array $stepKeyRefs
* @param string $testName
* @return void
* @throws TestReferenceException
*/
private function auditMergeSteps($stepKeyRefs, $testName)
{
if (empty($stepKeyRefs)) {
return;
}
// check for step keys which are referencing themselves as before/after
$invalidStepRef = array_filter($stepKeyRefs, function ($value, $key) {
return in_array($key, $value);
}, ARRAY_FILTER_USE_BOTH);
if (!empty($invalidStepRef)) {
throw new TestReferenceException(
"Invalid ordering configuration in test",
['test' => $testName, 'stepKey' => array_keys($invalidStepRef)]
);
}
// check for ambiguous references to step keys (multiple refs across test merges).
$atRiskStepRef = array_filter($stepKeyRefs, function ($value) {
return count($value) > 1;
});
foreach ($atRiskStepRef as $stepKey => $stepRefs) {
LoggingUtil::getInstance()->getLogger(ActionObjectExtractor::class)->warn(
'multiple actions referencing step key',
['test' => $testName, 'stepKey' => $stepKey, 'ref' => $stepRefs]
);
}
}
}