-
Notifications
You must be signed in to change notification settings - Fork 132
/
Copy pathActionMergeUtil.php
160 lines (144 loc) · 4.47 KB
/
ActionMergeUtil.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
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\FunctionalTestingFramework\Test\Util;
use Magento\FunctionalTestingFramework\Test\Objects\ActionObject;
/**
* Class ActionMergeUtil
*/
class ActionMergeUtil
{
const WAIT_ATTR = 'timeout';
const WAIT_ACTION_NAME = 'waitForPageLoad';
const WAIT_ACTION_SUFFIX = 'WaitForPageLoad';
const DEFAULT_WAIT_ORDER = 'after';
/**
* Array holding final resulting steps
*
* @var array
*/
private $orderedSteps = [];
/**
* Array holding action to be merged
*
* @var array
*/
private $stepsToMerge = [];
/**
* ActionMergeUtil constructor.
*/
public function __construct()
{
// empty constructor
}
/**
* Method to execute merge of steps and insert wait steps.
*
* @param array $parsedSteps
* @return array
*/
public function mergeStepsAndInsertWaits($parsedSteps)
{
$this->mergeActions($parsedSteps);
$this->insertWaits();
return $this->orderedSteps;
}
/**
* This method runs a step sort, loops steps which need to be merged, and runs the mergeStep function on each one.
*
* @param array $parsedSteps
* @return void
*/
private function mergeActions($parsedSteps)
{
$this->sortActions($parsedSteps);
foreach ($this->stepsToMerge as $stepName => $stepToMerge) {
if (!array_key_exists($stepName, $this->orderedSteps)) {
$this->mergeAction($stepToMerge);
}
}
unset($stepName);
unset($stepToMerge);
}
/**
* Runs through the prepared orderedSteps and calls insertWait if a step requires a wait after it.
*
* @return void
*/
private function insertWaits()
{
foreach ($this->orderedSteps as $step) {
if ($step->getTimeout()) {
$waitStepAttributes = [self::WAIT_ATTR => $step->getTimeout()];
$waitStep = new ActionObject(
$step->getMergeKey() . self::WAIT_ACTION_SUFFIX,
self::WAIT_ACTION_NAME,
$waitStepAttributes,
$step->getMergeKey(),
self::DEFAULT_WAIT_ORDER
);
$this->insertStep($waitStep);
}
}
}
/**
* This method takes the steps from the parser and splits steps which need merge from steps that are ordered.
*
* @param array $parsedSteps
* @return void
* @throws XmlException
*/
private function sortActions($parsedSteps)
{
foreach ($parsedSteps as $parsedStep) {
$parsedStep->resolveReferences();
if ($parsedStep->getLinkedAction()) {
$this->stepsToMerge[$parsedStep->getMergeKey()] = $parsedStep;
} else {
$this->orderedSteps[$parsedStep->getMergeKey()] = $parsedStep;
}
}
}
/**
* Recursively merges in each step and its dependencies
*
* @param ActionObject $stepToMerge
* @throws XmlException
* @return void
*/
private function mergeAction($stepToMerge)
{
$linkedStep = $stepToMerge->getLinkedAction();
if (!array_key_exists($linkedStep, $this->orderedSteps)
and
!array_key_exists($linkedStep, $this->stepsToMerge)) {
throw new XmlException(sprintf(
self::STEP_MISSING_ERROR_MSG,
$this->getName(),
$stepToMerge->getMergeKey(),
$linkedStep
));
} elseif (!array_key_exists($linkedStep, $this->orderedSteps)) {
$this->mergeAction($this->stepsToMerge[$linkedStep]);
}
$this->insertStep($stepToMerge);
}
/**
* Inserts a step into the ordered steps array based on position and step referenced.
*
* @param ActionObject $stepToMerge
* @return void
*/
private function insertStep($stepToMerge)
{
$position = array_search(
$stepToMerge->getLinkedAction(),
array_keys($this->orderedSteps)
) + $stepToMerge->getOrderOffset();
$previous_items = array_slice($this->orderedSteps, 0, $position, true);
$next_items = array_slice($this->orderedSteps, $position, null, true);
$this->orderedSteps = $previous_items + [$stepToMerge->getMergeKey() => $stepToMerge] + $next_items;
}
}