-
Notifications
You must be signed in to change notification settings - Fork 132
/
Copy pathTestDependencyUtil.php
217 lines (203 loc) · 7.98 KB
/
TestDependencyUtil.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
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\FunctionalTestingFramework\Util\Script;
use Magento\FunctionalTestingFramework\Test\Handlers\TestObjectHandler;
use Magento\FunctionalTestingFramework\Filter\FilterList;
use Magento\FunctionalTestingFramework\Config\MftfApplicationConfig;
/**
* TestDependencyUtil class that contains helper functions for static and upgrade scripts
*
* @package Magento\FunctionalTestingFramework\Util\Script
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class TestDependencyUtil
{
/**
* Array of FullModuleName => [dependencies]
* @var array
*/
private $allDependencies;
/**
* Transactional Array to keep track of what dependencies have already been extracted.
* @var array
*/
private $alreadyExtractedDependencies;
/**
* Builds and returns array of FullModuleNae => composer name
* @param array $moduleNameToPath
* @return array
*/
public function buildModuleNameToComposerName(array $moduleNameToPath): array
{
$moduleNameToComposerName = [];
foreach ($moduleNameToPath as $moduleName => $path) {
$composerData = json_decode(file_get_contents($path . DIRECTORY_SEPARATOR . "composer.json"));
$moduleNameToComposerName[$moduleName] = $composerData->name;
}
return $moduleNameToComposerName;
}
/**
* Builds and returns flattened dependency list based on composer dependencies
* @param array $moduleNameToPath
* @param array $moduleNameToComposerName
* @return array
*/
public function buildComposerDependencyList(array $moduleNameToPath, array $moduleNameToComposerName): array
{
$flattenedDependencies = [];
foreach ($moduleNameToPath as $moduleName => $pathToModule) {
$composerData = json_decode(
file_get_contents($pathToModule . DIRECTORY_SEPARATOR . "composer.json"),
true
);
$this->allDependencies[$moduleName] = $composerData['require'];
}
foreach ($this->allDependencies as $moduleName => $dependencies) {
$this->alreadyExtractedDependencies = [];
$flattenedDependencies[$moduleName] = $this->extractSubDependencies($moduleName, $moduleNameToComposerName);
}
return $flattenedDependencies;
}
/**
* Recursive function to fetch dependencies of given dependency, and its child dependencies
* @param string $subDependencyName
* @param array $moduleNameToComposerName
* @return array
*/
private function extractSubDependencies(string $subDependencyName, array $moduleNameToComposerName): array
{
$flattenedArray = [];
if (in_array($subDependencyName, $this->alreadyExtractedDependencies)) {
return $flattenedArray;
}
if (isset($this->allDependencies[$subDependencyName])) {
$subDependencyArray = $this->allDependencies[$subDependencyName];
$flattenedArray = array_merge($flattenedArray, $this->allDependencies[$subDependencyName]);
// Keep track of dependencies that have already been used, prevents circular dependency problems
$this->alreadyExtractedDependencies[] = $subDependencyName;
foreach ($subDependencyArray as $composerDependencyName => $version) {
$subDependencyFullName = array_search($composerDependencyName, $moduleNameToComposerName);
$flattenedArray = array_merge(
$flattenedArray,
$this->extractSubDependencies($subDependencyFullName, $moduleNameToComposerName)
);
}
}
return $flattenedArray;
}
/**
* Finds unique array composer dependencies of given testObjects
* @param array $allEntities
* @param array $moduleComposerName
* @param array $moduleNameToPath
* @return array
*/
public function getModuleDependenciesFromReferences(
array $allEntities,
array $moduleComposerName,
array $moduleNameToPath
): array {
$filenames = [];
foreach ($allEntities as $item) {
// Should it append ALL filenames, including merges?
$allFiles = explode(",", $item->getFilename());
foreach ($allFiles as $file) {
$moduleName = $this->getModuleName($file, $moduleNameToPath);
if (isset($moduleComposerName[$moduleName])) {
$composerModuleName = $moduleComposerName[$moduleName];
$filenames[$item->getName()][] = $composerModuleName;
}
}
}
return $filenames;
}
/**
* Return module name for a file path
*
* @param string $filePath
* @param array $moduleNameToPath
* @return string|null
*/
public function getModuleName(string $filePath, array $moduleNameToPath): ?string
{
$moduleName = null;
foreach ($moduleNameToPath as $name => $path) {
if (strpos($filePath, $path. "/") !== false) {
$moduleName = $name;
break;
}
}
return $moduleName;
}
/**
* Return array of merge test modules and file path with same test name.
* @param array $testDependencies
* @param array $filterList
* @param array $extendedTestMapping
* @return array
*/
public function mergeDependenciesForExtendingTests(
array $testDependencies,
array $filterList,
array $extendedTestMapping = []
): array {
$testObjects = TestObjectHandler::getInstance()->getAllObjects();
$filters = MftfApplicationConfig::getConfig()->getFilterList()->getFilters();
$filteredTestNames = (count($filterList)>0)?$this->getFilteredTestNames($testObjects, $filters):[];
$temp_array = array_reverse(array_column($testDependencies, "test_name"), true);
if (!empty($extendedTestMapping)) {
foreach ($extendedTestMapping as $value) {
$key = array_search($value["parent_test_name"], $temp_array);
if ($key !== false) {
#if parent test found merge this to child, for doing so just replace test name with child.
$testDependencies[$key]["test_name"] = $value["child_test_name"];
}
}
}
$temp_array = [];
foreach ($testDependencies as $testDependency) {
$temp_array[$testDependency["test_name"]][] = $testDependency;
}
$testDependencies = [];
foreach ($temp_array as $testDependencyArray) {
if ((
empty($filterList)) ||
isset($filteredTestNames[$testDependencyArray[0]["test_name"]])
) {
$testDependencies[] = [
"file_path" => array_column($testDependencyArray, 'file_path'),
"full_name" => $testDependencyArray[0]["full_name"],
"test_name" => $testDependencyArray[0]["test_name"],
"test_modules" => array_values(
array_unique(
call_user_func_array(
'array_merge',
array_column($testDependencyArray, 'test_modules')
)
)
),
];
}
}
return $testDependencies;
}
/**
* Return array of merge test modules and file path with same test name.
* @param array $testObjects
* @param array $filters
* @return array
*/
public function getFilteredTestNames(array $testObjects, array $filters) : array
{
foreach ($filters as $filter) {
$filter->filter($testObjects);
}
$testValues = array_map(function ($testObjects) {
return $testObjects->getName();
}, $testObjects);
return $testValues;
}
}