-
Notifications
You must be signed in to change notification settings - Fork 132
/
Copy pathModuleResolver.php
234 lines (205 loc) · 5.97 KB
/
ModuleResolver.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
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\FunctionalTestingFramework\Util;
/**
* Class ModuleResolver, resolve module path based on enabled modules of target Magento instance.
*
* @api
*/
class ModuleResolver
{
/**
* Environment field name for module whitelist.
*/
const MODULE_WHITELIST = 'MODULE_WHITELIST';
/**
* Enabled modules.
*
* @var array|null
*/
protected $enabledModules = null;
/**
* Paths for enabled modules.
*
* @var array|null
*/
protected $enabledModulePaths = null;
/**
* Configuration instance.
*
* @var \Magento\FunctionalTestingFramework\Config\DataInterface
*/
protected $configuration;
/**
* Admin url for integration token.
*
* @var string
*/
protected $adminTokenUrl = "rest/V1/integration/admin/token";
/**
* Url for with module list.
*
* @var string
*/
protected $moduleUrl = "rest/V1/modules";
/**
* List of known directory that does not map to a Magento module.
*
* @var array
*/
protected $knownDirectories = ['SampleData' => 1];
/**
* ModuleResolver instance.
*
* @var ModuleResolver
*/
private static $instance = null;
/**
* SequenceSorter instance.
*
* @var ModuleResolver\SequenceSorterInterface
*/
protected $sequenceSorter;
/**
* Get ModuleResolver instance.
*
* @return ModuleResolver
*/
public static function getInstance()
{
if (!self::$instance) {
self::$instance = new ModuleResolver();
}
return self::$instance;
}
/**
* ModuleResolver constructor.
*/
private function __construct()
{
$objectManager = \Magento\FunctionalTestingFramework\ObjectManagerFactory::getObjectManager();
$this->sequenceSorter = $objectManager->get(
\Magento\FunctionalTestingFramework\Util\ModuleResolver\SequenceSorterInterface::class
);
}
/**
* Return an array of enabled modules of target Magento instance.
*
* @return array
*/
public function getEnabledModules()
{
if (isset($this->enabledModules)) {
return $this->enabledModules;
}
$token = $this->getAdminToken();
if (!$token || !is_string($token)) {
$this->enabledModules = [];
return $this->enabledModules;
}
$url = $_ENV['MAGENTO_BASE_URL'] . $this->moduleUrl;
$headers = [
'Authorization: Bearer ' . $token,
];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
if (!$response) {
$this->enabledModules = [];
} else {
$this->enabledModules = json_decode($response);
}
return $this->enabledModules;
}
/**
* Return an array of module whitelist that not exist in target Magento instance.
*
* @return array
*/
protected function getModuleWhitelist()
{
$moduleWhitelist = getenv(self::MODULE_WHITELIST);
if (empty($moduleWhitelist)) {
return [];
}
return array_map('trim', explode(',', $moduleWhitelist));
}
/**
* Return the modules path based on which modules are enabled in the target Magento instance.
*
* @return array
*/
public function getModulesPath()
{
if (isset($this->enabledModulePaths)) {
return $this->enabledModulePaths;
}
$enabledModules = $this->getEnabledModules();
$modulePath = defined('TESTS_MODULE_PATH') ? TESTS_MODULE_PATH : TESTS_BP;
$allModulePaths = glob($modulePath . '*/*');
if (empty($enabledModules)) {
$this->enabledModulePaths = $allModulePaths;
return $this->enabledModulePaths;
}
$enabledModules = array_merge($enabledModules, $this->getModuleWhitelist());
$enabledDirectories = [];
foreach ($enabledModules as $module) {
$directoryName = explode('_', $module)[1];
$enabledDirectories[$directoryName] = $directoryName;
}
foreach ($allModulePaths as $index => $modulePath) {
$moduleShortName = basename($modulePath);
if (!isset($enabledDirectories[$moduleShortName]) && !isset($this->knownDirectories[$moduleShortName])) {
unset($allModulePaths[$index]);
}
}
$this->enabledModulePaths = $allModulePaths;
return $this->enabledModulePaths;
}
/**
* Get the API token for admin.
*
* @return string|bool
*/
protected function getAdminToken()
{
$login = $_ENV['MAGENTO_ADMIN_USERNAME'];
$password = $_ENV['MAGENTO_ADMIN_PASSWORD'];
if (!$login || !$password || !isset($_ENV['MAGENTO_BASE_URL'])) {
return false;
}
$url = $_ENV['MAGENTO_BASE_URL'] . $this->adminTokenUrl;
$data = [
'username' => $login,
'password' => $password
];
$headers = [
'Content-Type: application/json',
];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
if (!$response) {
return $response;
}
return json_decode($response);
}
/**
* Sort files according module sequence.
*
* @param array $files
* @return array
*/
public function sortFilesByModuleSequence(array $files)
{
return $this->sequenceSorter->sort($files);
}
}