-
Notifications
You must be signed in to change notification settings - Fork 132
/
Copy pathConfigSanitizerUtil.php
150 lines (132 loc) · 4.47 KB
/
ConfigSanitizerUtil.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
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\FunctionalTestingFramework\Util;
use Magento\FunctionalTestingFramework\Config\MftfApplicationConfig;
/**
* Class ConfigSanitizerUtil
*/
class ConfigSanitizerUtil
{
/**
* Sanitizes the given Webdriver Config's url and selenium env params, can be selective based on second argument.
* @param array $config
* @param String[] $params
* @return array
*/
public static function sanitizeWebDriverConfig($config, $params = ['url', 'selenium'])
{
self::validateConfigBasedVars($config);
if (in_array('url', $params)) {
$config['url'] = self::sanitizeUrl($config['url']);
}
if (in_array('selenium', $params)) {
$config = self::sanitizeSeleniumEnvs($config);
}
return $config;
}
/**
* Sets Selenium params if they are left as defaults.
* @param array $config
* @return array
*/
private static function sanitizeSeleniumEnvs($config)
{
if ($config['protocol'] == '%SELENIUM_PROTOCOL%') {
$config['protocol'] = "http";
}
if ($config['host'] == '%SELENIUM_HOST%') {
$config['host'] = "127.0.0.1";
}
if ($config['port'] == '%SELENIUM_PORT%') {
$config['port'] = "4444";
}
if ($config['path'] == '%SELENIUM_PATH%') {
$config['path'] = "/wd/hub";
}
return $config;
}
/**
* Method which validates env vars have been properly read into the config. Method implemented as part of
* bug MQE-567
*
* @param array $config
* @return void
*/
private static function validateConfigBasedVars($config)
{
$configStrings = array_filter($config, function ($value) {
return is_string($value);
});
foreach ($configStrings as $configKey => $configValue) {
$var = trim((String)$configValue, '%');
if (array_key_exists($var, $_ENV)) {
trigger_error(
"Issue with setting configuration for test runs. Please make sure '{$var}' is "
. "not duplicated as a system level variable",
E_USER_ERROR
);
}
}
}
/**
* Sanitizes and returns given URL.
* @param string $url
* @return string
*/
public static function sanitizeUrl($url)
{
if (strlen($url) == 0 && !MftfApplicationConfig::getConfig()->forceGenerateEnabled()) {
trigger_error("MAGENTO_BASE_URL must be defined in .env", E_USER_ERROR);
}
if (filter_var($url, FILTER_VALIDATE_URL) === true) {
return rtrim($url, "/") . "/";
}
$urlParts = parse_url($url);
if (!isset($urlParts['scheme'])) {
$urlParts['scheme'] = "http";
}
if (!isset($urlParts['host'])) {
$urlParts['host'] = rtrim($urlParts['path'], "/");
$urlParts['host'] = str_replace("//", "/", $urlParts['host']);
unset($urlParts['path']);
}
if (!isset($urlParts['path'])) {
$urlParts['path'] = "/";
} else {
$urlParts['path'] = rtrim($urlParts['path'], "/") . "/";
}
return str_replace("///", "//", self::buildUrl($urlParts));
}
/**
* Returns url from $parts given, used with parse_url output for convenience.
* This only exists because of deprecation of http_build_url, which does the exact same thing as the code below.
* @param array $parts
* @return string
*/
private static function buildUrl(array $parts)
{
$get = function ($key) use ($parts) {
return isset($parts[$key]) ? $parts[$key] : null;
};
$pass = $get('pass');
$user = $get('user');
$userinfo = $pass !== null ? "$user:$pass" : $user;
$port = $get('port');
$scheme = $get('scheme');
$query = $get('query');
$fragment = $get('fragment');
$authority =
($userinfo !== null ? "$userinfo@" : '') .
$get('host') .
($port ? ":$port" : '');
return
(strlen($scheme) ? "$scheme:" : '') .
(strlen($authority) ? "//$authority" : '') .
$get('path') .
(strlen($query) ? "?$query" : '') .
(strlen($fragment) ? "#$fragment" : '');
}
}