forked from magento/magento2-functional-testing-framework
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathActionGroupObject.php
136 lines (120 loc) · 3.97 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
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\FunctionalTestingFramework\Test\Objects;
use Magento\FunctionalTestingFramework\Test\Util\ActionMergeUtil;
/**
* Class ActionGroupObject
*/
class ActionGroupObject
{
const VAR_ATTRIBUTES = ['userInput', 'selector', 'page'];
/**
* 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->name = $name;
$this->arguments = $arguments;
$this->parsedActions = $actions;
}
/**
* Gets the ordered steps including merged waits
*
* @param array $arguments
* @param string $actionReferenceKey
* @return array
*/
public function getSteps($arguments, $actionReferenceKey)
{
$mergeUtil = new ActionMergeUtil();
$args = $this->arguments;
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 = '/{{([\w]+)/';
foreach ($this->parsedActions as $action) {
$varAttributes = array_intersect(self::VAR_ATTRIBUTES, 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]) & empty($matches[1])) {
continue;
}
$newActionAttributes[$varAttribute] = $this->resolveNewAttribute(
$arguments,
$attributeValue,
$matches
);
}
}
$resolvedActions[$action->getMergeKey() . $actionReferenceKey] = new ActionObject(
$action->getMergeKey() . $actionReferenceKey,
$action->getType(),
array_merge($action->getCustomActionAttributes(), $newActionAttributes),
$action->getLinkedAction(),
$action->getOrderOffset()
);
}
return $resolvedActions;
}
/**
* Function which takes an array of arguments to use for replacement of var name, the string which contains
* the variable for replacement, an array of matching vars.
*
* @param array $arguments
* @param string $attributeValue
* @param array $matches
* @return string
*/
private function resolveNewAttribute($arguments, $attributeValue, $matches)
{
$newAttributeVal = $attributeValue;
foreach ($matches[1] as $var) {
if (array_key_exists($var, $arguments)) {
$newAttributeVal = str_replace($var, $arguments[$var], $newAttributeVal);
}
}
return $newAttributeVal;
}
}