-
Notifications
You must be signed in to change notification settings - Fork 132
/
Copy pathActionGroupObject.php
525 lines (464 loc) · 16.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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
<?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\Handlers\ActionGroupObjectHandler;
use Magento\FunctionalTestingFramework\Test\Util\ActionGroupObjectExtractor;
use Magento\FunctionalTestingFramework\Test\Util\ActionMergeUtil;
use Magento\FunctionalTestingFramework\Test\Util\ObjectExtension;
/**
* Class ActionGroupObject
*/
class ActionGroupObject
{
const ACTION_GROUP_ORIGIN_NAME = "actionGroupName";
const ACTION_GROUP_ORIGIN_TEST_REF = "testInvocationRef";
const STEPKEY_REPLACEMENT_ENABLED_TYPES = [
"executeJS",
"magentoCLI",
"generateDate",
"formatMoney",
"deleteData",
"getData",
"updateData",
"createData",
"grabAttributeFrom",
"grabCookie",
"grabFromCurrentUrl",
"grabMultiple",
"grabPageSource",
"grabTextFrom",
"grabValueFrom"
];
/**
* 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;
/**
* An array used to store annotation information to values
*
* @var array
*/
private $annotations;
/**
* An array used to store a list of filenames the action group is created by
*
* @var array
*/
private $filenames = [];
/**
* String of parent Action Group
*
* @var string
*/
private $parentActionGroup;
/**
* ActionGroupObject constructor.
*
* @param string $name
* @param array $annotations
* @param ArgumentObject[] $arguments
* @param array $actions
* @param string $parentActionGroup
* @param string $filename
*/
public function __construct($name, $annotations, $arguments, $actions, $parentActionGroup, $filename)
{
$this->varAttributes = array_merge(
ActionObject::SELECTOR_ENABLED_ATTRIBUTES,
ActionObject::DATA_ENABLED_ATTRIBUTES
);
$this->varAttributes[] = ActionObject::ACTION_ATTRIBUTE_URL;
$this->name = $name;
$this->annotations = $annotations;
$this->arguments = $arguments;
$this->parsedActions = $actions;
$this->parentActionGroup = $parentActionGroup;
$this->filenames = array_merge(
$this->filenames,
[$filename]
);
}
/**
* 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 = [];
$replacementStepKeys = [];
foreach ($this->parsedActions as $action) {
$replacementStepKeys[$action->getStepKey()] = $action->getStepKey() . ucfirst($actionReferenceKey);
$varAttributes = array_intersect($this->varAttributes, array_keys($action->getCustomActionAttributes()));
// replace createDataKey attributes inside the action group
$resolvedActionAttributes = $this->replaceCreateDataKeys($action, $replacementStepKeys);
$newActionAttributes = [];
if (!empty($varAttributes)) {
$newActionAttributes = $this->resolveAttributesWithArguments(
$arguments,
$resolvedActionAttributes
);
}
// translate 0/1 back to before/after
$orderOffset = ActionObject::MERGE_ACTION_ORDER_BEFORE;
if ($action->getOrderOffset() === 1) {
$orderOffset = ActionObject::MERGE_ACTION_ORDER_AFTER;
}
// 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($resolvedActionAttributes, $newActionAttributes),
$action->getLinkedAction() == null ? null : $action->getLinkedAction() . ucfirst($actionReferenceKey),
$orderOffset,
[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 = '/{{([^(}]+)\(*([^)}]+)*\)*}}/';
$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 boolean $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 {
$fullReplacement = str_replace($variable, trim($replacement, '$'), $fullVariable);
$newAttributeValue = str_replace(
'{{' . $fullVariable . '}}',
$scope . $fullReplacement . $scope,
$newAttributeValue
);
}
return $newAttributeValue;
}
/**
* Finds and returns all original stepkeys of actions in actionGroup.
* @return string[]
*/
public function extractStepKeys()
{
$originalKeys = [];
foreach ($this->parsedActions as $action) {
//limit actions returned to list that are relevant
foreach (self::STEPKEY_REPLACEMENT_ENABLED_TYPES as $actionValue) {
if ($actionValue === $action->getType()) {
$originalKeys[] = $action->getStepKey();
}
}
}
return $originalKeys;
}
/**
* Getter for the Action Group Name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Getter for the Parent Action Group Name
*
* @return string
*/
public function getParentName()
{
return $this->parentActionGroup;
}
/**
* Getter for the Action Group Actions
*
* @return ActionObject[]
*/
public function getActions()
{
return $this->parsedActions;
}
/**
* Getter for the Action Group Arguments
*
* @return array
*/
public function getArguments()
{
return $this->arguments;
}
/**
* Getter for the Action Group Annotations
*
* @return array
*/
public function getAnnotations()
{
return $this->annotations;
}
/**
* Getter for the Action Group File Names
*
* @return array
*/
public function getFileNames()
{
return $this->filenames;
}
/**
* 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;
}
/**
* Replaces references to step keys used earlier in an action group
*
* @param ActionObject $action
* @param array $replacementStepKeys
* @return ActionObject[]
*/
private function replaceCreateDataKeys($action, $replacementStepKeys)
{
$resolvedActionAttributes = [];
foreach ($action->getCustomActionAttributes() as $actionAttribute => $actionAttributeDetails) {
if (is_array($actionAttributeDetails) && array_key_exists('createDataKey', $actionAttributeDetails)) {
$actionAttributeDetails['createDataKey'] =
$replacementStepKeys[$actionAttributeDetails['createDataKey']] ??
$actionAttributeDetails['createDataKey'];
}
$resolvedActionAttributes[$actionAttribute] = $actionAttributeDetails;
}
return $resolvedActionAttributes;
}
}