Skip to content

Commit 47db109

Browse files
authored
MQE-612: Break out BASE_URL sanitizer into a Util
- Broke out logic into new function - Checked against Ji's usecase.
1 parent 86ad181 commit 47db109

File tree

4 files changed

+138
-83
lines changed

4 files changed

+138
-83
lines changed

changelog.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
* MFTF Version 1.0 - Changelog
2+
* Initial commit of MFTF v1.0.0
3+
4+
* Core Features:
5+
* **Traceability** for clear logging and reporting capabilities
6+
* **Modularity** to run tests based on modules/extensions installed
7+
* **Customizability** to have an ability to customize existed tests
8+
* **Readability** using clear declarative XML test steps
9+
* **Maintainability** based on simple test creation and overall structure
10+
11+
* Supported Systems:
12+
* OS
13+
* Windows 10
14+
* OSX (Sierra)
15+
* Browser
16+
* Chrome (Latest) with ChromeDriver Latest
17+
* Known Issues:
18+
* Support for Firefox is curently incomplete. This will be resolved to support Firefox 57 (Quantum) and latest Gecko driver in next minor release.
19+
* MAGENTO_BASE_URL in .env file must have final "/" e.g. "http://magento.com/"

src/Magento/FunctionalTestingFramework/Module/MagentoRestDriver.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
use Codeception\Module\REST;
1010
use Magento\FunctionalTestingFramework\Module\MagentoSequence;
11+
use Magento\FunctionalTestingFramework\Util\ConfigSanitizerUtil;
1112
use Flow\JSONPath;
1213

1314
/**
@@ -107,7 +108,7 @@ public function _beforeSuite($settings = [])
107108
if (empty($this->config['url']) || empty($this->config['username']) || empty($this->config['password'])) {
108109
return;
109110
}
110-
$this->config['url'] = $_ENV['MAGENTO_BASE_URL'] . "rest/default/V1/";
111+
$this->config['url'] = ConfigSanitizerUtil::sanitizeUrl($this->config['url']);
111112

112113
$this->haveHttpHeader('Content-Type', 'application/json');
113114
$this->sendPOST(

src/Magento/FunctionalTestingFramework/Module/MagentoWebDriver.php

Lines changed: 3 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Codeception\Util\Uri;
1919
use Codeception\Util\ActionSequence;
2020
use Magento\Setup\Exception;
21+
use Magento\FunctionalTestingFramework\Util\ConfigSanitizerUtil;
2122
use Yandex\Allure\Adapter\Support\AttachmentSupport;
2223

2324
/**
@@ -79,94 +80,14 @@ class MagentoWebDriver extends WebDriver
7980

8081
public function _initialize()
8182
{
82-
$this->sanitizeConfig();
83+
$this->config = ConfigSanitizerUtil::sanitizeWebDriverConfig($this->config);
8384
parent::_initialize();
8485
}
8586

8687
public function _resetConfig()
8788
{
8889
parent::_resetConfig();
89-
$this->sanitizeConfig();
90-
}
91-
92-
/**
93-
* Sanitizes URL and Selenium Variables, then assigns them to the config array.
94-
* @return void
95-
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
96-
* @SuppressWarnings(PHPMD.NPathComplexity)
97-
*/
98-
private function sanitizeConfig()
99-
{
100-
if ($this->config['url'] === "") {
101-
trigger_error("MAGENTO_BASE_URL must be defined in .env", E_USER_ERROR);
102-
}
103-
104-
//Determine if url sanitize is required
105-
if (!preg_match("/(http|https):\/\/[\w.:]+\//", $this->config['url'])) {
106-
$urlParts = parse_url($this->config['url']);
107-
108-
if (!isset($urlParts['scheme'])) {
109-
$urlParts['scheme'] = "http";
110-
}
111-
if (!isset($urlParts['host'])) {
112-
$urlParts['host'] = rtrim($urlParts['path'], "/");
113-
unset($urlParts['path']);
114-
}
115-
116-
if (!isset($urlParts['path'])) {
117-
$urlParts['path'] = "/";
118-
} else {
119-
$urlParts['path'] = rtrim($urlParts['path'], "/") . "/";
120-
}
121-
122-
$_ENV['MAGENTO_BASE_URL'] = str_replace("///", "//", $this->build_url($urlParts));
123-
$this->config['url'] = str_replace("///", "//", $this->build_url($urlParts));
124-
}
125-
126-
//Assign default Values to Selenium configs if they are defined
127-
if ($this->config['protocol'] == '%SELENIUM_PROTOCOL%') {
128-
$this->config['protocol'] = "http";
129-
}
130-
if ($this->config['host'] == '%SELENIUM_HOST%') {
131-
$this->config['host'] = "127.0.0.1";
132-
}
133-
if ($this->config['port'] == '%SELENIUM_PORT%') {
134-
$this->config['port'] = "4444";
135-
}
136-
if ($this->config['path'] == '%SELENIUM_PATH%') {
137-
$this->config['path'] = "/wd/hub";
138-
}
139-
}
140-
141-
/**
142-
* Returns url from $parts given, used with parse_url output for convenience.
143-
* This only exists because of deprecation of http_build_url, which does the exact same thing as the code below.
144-
* @param array $parts
145-
* @return string
146-
*/
147-
private function build_url(array $parts) {
148-
$get = function ($key) use ($parts) {
149-
return isset($parts[$key]) ? $parts[$key] : null;
150-
};
151-
152-
$pass = $get('pass');
153-
$user = $get('user');
154-
$userinfo = $pass !== null ? "$user:$pass" : $user;
155-
$port = $get('port');
156-
$scheme = $get('scheme');
157-
$query = $get('query');
158-
$fragment = $get('fragment');
159-
$authority =
160-
($userinfo !== null ? "$userinfo@" : '') .
161-
$get('host') .
162-
($port ? ":$port" : '');
163-
164-
return
165-
(strlen($scheme) ? "$scheme:" : '') .
166-
(strlen($authority) ? "//$authority" : '') .
167-
$get('path') .
168-
(strlen($query) ? "?$query" : '') .
169-
(strlen($fragment) ? "#$fragment" : '');
90+
$this->config = ConfigSanitizerUtil::sanitizeWebDriverConfig($this->config);
17091
}
17192

17293
/**
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\FunctionalTestingFramework\Util;
8+
9+
/**
10+
* Class ConfigSanitizerUtil
11+
*/
12+
class ConfigSanitizerUtil
13+
{
14+
/**
15+
* Sanitizes the given Webdriver Config's url and selenium env params.
16+
* @param array $config
17+
* @return array
18+
*/
19+
public static function sanitizeWebDriverConfig($config)
20+
{
21+
$config['url'] = self::sanitizeUrl($config['url']);
22+
$config = self::sanitizeSeleniumEnvs($config);
23+
return $config;
24+
}
25+
26+
/**
27+
* Sets Selenium params if they are left as defaults.
28+
* @param array $config
29+
* @return array
30+
*/
31+
private static function sanitizeSeleniumEnvs($config)
32+
{
33+
if ($config['protocol'] == '%SELENIUM_PROTOCOL%') {
34+
$config['protocol'] = "http";
35+
}
36+
if ($config['host'] == '%SELENIUM_HOST%') {
37+
$config['host'] = "127.0.0.1";
38+
}
39+
if ($config['port'] == '%SELENIUM_PORT%') {
40+
$config['port'] = "4444";
41+
}
42+
if ($config['path'] == '%SELENIUM_PATH%') {
43+
$config['path'] = "/wd/hub";
44+
}
45+
return $config;
46+
}
47+
48+
/**
49+
* Sanitizes and returns given URL.
50+
* @param string $url
51+
* @return string
52+
*/
53+
public static function sanitizeUrl($url)
54+
{
55+
if ($url === "") {
56+
trigger_error("MAGENTO_BASE_URL must be defined in .env", E_USER_ERROR);
57+
}
58+
59+
if (filter_var($url, FILTER_VALIDATE_URL) === true) {
60+
return rtrim($url, "/") . "/";
61+
}
62+
63+
$urlParts = parse_url($url);
64+
65+
if (!isset($urlParts['scheme'])) {
66+
$urlParts['scheme'] = "http";
67+
}
68+
if (!isset($urlParts['host'])) {
69+
$urlParts['host'] = rtrim($urlParts['path'], "/");
70+
$urlParts['host'] = str_replace("//", "/", $urlParts['host']);
71+
unset($urlParts['path']);
72+
}
73+
74+
if (!isset($urlParts['path'])) {
75+
$urlParts['path'] = "/";
76+
} else {
77+
$urlParts['path'] = rtrim($urlParts['path'], "/") . "/";
78+
}
79+
80+
return str_replace("///", "//", self::buildUrl($urlParts));
81+
}
82+
83+
/**
84+
* Returns url from $parts given, used with parse_url output for convenience.
85+
* This only exists because of deprecation of http_build_url, which does the exact same thing as the code below.
86+
* @param array $parts
87+
* @return string
88+
*/
89+
private static function buildUrl(array $parts)
90+
{
91+
$get = function ($key) use ($parts) {
92+
return isset($parts[$key]) ? $parts[$key] : null;
93+
};
94+
95+
$pass = $get('pass');
96+
$user = $get('user');
97+
$userinfo = $pass !== null ? "$user:$pass" : $user;
98+
$port = $get('port');
99+
$scheme = $get('scheme');
100+
$query = $get('query');
101+
$fragment = $get('fragment');
102+
$authority =
103+
($userinfo !== null ? "$userinfo@" : '') .
104+
$get('host') .
105+
($port ? ":$port" : '');
106+
107+
return
108+
(strlen($scheme) ? "$scheme:" : '') .
109+
(strlen($authority) ? "//$authority" : '') .
110+
$get('path') .
111+
(strlen($query) ? "?$query" : '') .
112+
(strlen($fragment) ? "#$fragment" : '');
113+
}
114+
}

0 commit comments

Comments
 (0)