Skip to content

Commit 3e5fa15

Browse files
committed
MQE-1470: add interface to handle file path and url format
1 parent 6c13d9d commit 3e5fa15

File tree

7 files changed

+336
-71
lines changed

7 files changed

+336
-71
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Tests\unit\Magento\FunctionalTestFramework\Util\Path;
7+
8+
use Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException;
9+
use Magento\FunctionalTestingFramework\Util\MagentoTestCase;
10+
use Magento\FunctionalTestingFramework\Util\Path\FilePathFormatter;
11+
12+
class FilePathFormatterTest extends MagentoTestCase
13+
{
14+
/**
15+
* Test file format
16+
*
17+
* @dataProvider formatDataProvider
18+
* @param string $path
19+
* @param boolean $withTrailingSeparator
20+
* @param mixed string|boolean $expectedPath
21+
* @return void
22+
*/
23+
public function testFormat($path, $withTrailingSeparator, $expectedPath)
24+
{
25+
if (null !== $expectedPath) {
26+
$this->assertEquals($expectedPath, FilePathFormatter::format($path, $withTrailingSeparator));
27+
} else {
28+
// Assert no exception
29+
FilePathFormatter::format($path, $withTrailingSeparator);
30+
$this->assertTrue(true);
31+
}
32+
33+
}
34+
35+
/**
36+
* Test file format with exception
37+
*
38+
* @dataProvider formatExceptionDataProvider
39+
* @param string $path
40+
* @param boolean $withTrailingSeparator
41+
* @return void
42+
*/
43+
public function testFormatWithException($path, $withTrailingSeparator)
44+
{
45+
$this->expectException(TestFrameworkException::class);
46+
$this->expectExceptionMessage("Invalid or non-existing file: $path\n");
47+
FilePathFormatter::format($path, $withTrailingSeparator);
48+
}
49+
50+
/**
51+
* Data input
52+
*
53+
* @return array
54+
*/
55+
public function formatDataProvider()
56+
{
57+
$path1 = rtrim(TESTS_BP, '/');
58+
$path2 = $path1 . DIRECTORY_SEPARATOR;
59+
return [
60+
[$path1, null, $path1],
61+
[$path1, false, $path1],
62+
[$path1, true, $path2],
63+
[$path2, null, $path1],
64+
[$path2, false, $path1],
65+
[$path2, true, $path2],
66+
[__DIR__. DIRECTORY_SEPARATOR . basename(__FILE__), null, __FILE__],
67+
['', null, null] // Empty string is valid
68+
];
69+
}
70+
71+
/**
72+
* Invalid data input
73+
*
74+
* @return array
75+
*/
76+
public function formatExceptionDataProvider()
77+
{
78+
return [
79+
['abc', null],
80+
['X://some\dir/@', null],
81+
];
82+
}
83+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Tests\unit\Magento\FunctionalTestFramework\Util\Path;
7+
8+
use Magento\FunctionalTestingFramework\Util\MagentoTestCase;
9+
use Magento\FunctionalTestingFramework\Util\Path\UrlFormatter;
10+
use Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException;
11+
12+
class UrlFormatterTest extends MagentoTestCase
13+
{
14+
/**
15+
* Test url format
16+
*
17+
* @dataProvider formatDataProvider
18+
* @param string $path
19+
* @param boolean $withTrailingSeparator
20+
* @param mixed string|boolean $expectedPath
21+
* @return void
22+
*/
23+
public function testFormat($path, $withTrailingSeparator, $expectedPath)
24+
{
25+
$this->assertEquals($expectedPath, UrlFormatter::format($path, $withTrailingSeparator));
26+
}
27+
28+
/**
29+
* Test url format with exception
30+
*
31+
* @dataProvider formatExceptionDataProvider
32+
* @param string $path
33+
* @param boolean $withTrailingSeparator
34+
* @return void
35+
*/
36+
public function testFormatWithException($path, $withTrailingSeparator)
37+
{
38+
$this->expectException(TestFrameworkException::class);
39+
$this->expectExceptionMessage("Invalid url: $path\n");
40+
UrlFormatter::format($path, $withTrailingSeparator);
41+
}
42+
43+
/**
44+
* Data input
45+
*
46+
* @return array
47+
*/
48+
public function formatDataProvider()
49+
{
50+
$url1 = 'http://magento.local/index.php';
51+
$url2 = $url1 . '/';
52+
$url3 = 'https://www.example.com/index.php/admin';
53+
$url4 = $url3 . '/';
54+
$url5 = 'www.google.com';
55+
$url6 = 'http://www.google.com/';
56+
return [
57+
[$url1, null, $url1],
58+
[$url1, false, $url1],
59+
[$url1, true, $url2],
60+
[$url2, null, $url1],
61+
[$url2, false, $url1],
62+
[$url2, true, $url2],
63+
[$url3, null, $url3],
64+
[$url3, false, $url3],
65+
[$url3, true, $url4],
66+
[$url4, null, $url3],
67+
[$url4, false, $url3],
68+
[$url4, true, $url4],
69+
[$url5, true, $url6],
70+
];
71+
}
72+
73+
/**
74+
* Invalid data input
75+
*
76+
* @return array
77+
*/
78+
public function formatExceptionDataProvider()
79+
{
80+
return [
81+
['', null],
82+
];
83+
}
84+
}

src/Magento/FunctionalTestingFramework/Util/ConfigSanitizerUtil.php

Lines changed: 3 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
namespace Magento\FunctionalTestingFramework\Util;
88

99
use Magento\FunctionalTestingFramework\Config\MftfApplicationConfig;
10+
use Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException;
11+
use Magento\FunctionalTestingFramework\Util\Path\UrlFormatter;
1012

1113
/**
1214
* Class ConfigSanitizerUtil
@@ -24,7 +26,7 @@ public static function sanitizeWebDriverConfig($config, $params = ['url', 'selen
2426
self::validateConfigBasedVars($config);
2527

2628
if (in_array('url', $params)) {
27-
$config['url'] = self::sanitizeUrl($config['url']);
29+
$config['url'] = UrlFormatter::format($config['url']);
2830
}
2931

3032
if (in_array('selenium', $params)) {
@@ -80,71 +82,4 @@ private static function validateConfigBasedVars($config)
8082
}
8183
}
8284
}
83-
84-
/**
85-
* Sanitizes and returns given URL.
86-
* @param string $url
87-
* @return string
88-
*/
89-
public static function sanitizeUrl($url)
90-
{
91-
if (strlen($url) == 0 && !MftfApplicationConfig::getConfig()->forceGenerateEnabled()) {
92-
trigger_error("MAGENTO_BASE_URL must be defined in .env", E_USER_ERROR);
93-
}
94-
95-
if (filter_var($url, FILTER_VALIDATE_URL) === true) {
96-
return rtrim($url, "/") . "/";
97-
}
98-
99-
$urlParts = parse_url($url);
100-
101-
if (!isset($urlParts['scheme'])) {
102-
$urlParts['scheme'] = "http";
103-
}
104-
if (!isset($urlParts['host'])) {
105-
$urlParts['host'] = rtrim($urlParts['path'], "/");
106-
$urlParts['host'] = str_replace("//", "/", $urlParts['host']);
107-
unset($urlParts['path']);
108-
}
109-
110-
if (!isset($urlParts['path'])) {
111-
$urlParts['path'] = "/";
112-
} else {
113-
$urlParts['path'] = rtrim($urlParts['path'], "/") . "/";
114-
}
115-
116-
return str_replace("///", "//", self::buildUrl($urlParts));
117-
}
118-
119-
/**
120-
* Returns url from $parts given, used with parse_url output for convenience.
121-
* This only exists because of deprecation of http_build_url, which does the exact same thing as the code below.
122-
* @param array $parts
123-
* @return string
124-
*/
125-
private static function buildUrl(array $parts)
126-
{
127-
$get = function ($key) use ($parts) {
128-
return isset($parts[$key]) ? $parts[$key] : null;
129-
};
130-
131-
$pass = $get('pass');
132-
$user = $get('user');
133-
$userinfo = $pass !== null ? "$user:$pass" : $user;
134-
$port = $get('port');
135-
$scheme = $get('scheme');
136-
$query = $get('query');
137-
$fragment = $get('fragment');
138-
$authority =
139-
($userinfo !== null ? "$userinfo@" : '') .
140-
$get('host') .
141-
($port ? ":$port" : '');
142-
143-
return
144-
(strlen($scheme) ? "$scheme:" : '') .
145-
(strlen($authority) ? "//$authority" : '') .
146-
$get('path') .
147-
(strlen($query) ? "?$query" : '') .
148-
(strlen($fragment) ? "#$fragment" : '');
149-
}
15085
}

src/Magento/FunctionalTestingFramework/Util/ModuleResolver.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Magento\FunctionalTestingFramework\Config\MftfApplicationConfig;
1010
use Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException;
1111
use Magento\FunctionalTestingFramework\Util\Logger\LoggingUtil;
12+
use Magento\FunctionalTestingFramework\Util\Path\UrlFormatter;
1213
use Symfony\Component\HttpFoundation\Response;
1314

1415
/**
@@ -201,7 +202,7 @@ public function getEnabledModules()
201202

202203
$token = $this->getAdminToken();
203204

204-
$url = ConfigSanitizerUtil::sanitizeUrl(getenv('MAGENTO_BASE_URL')) . $this->moduleUrl;
205+
$url = UrlFormatter::format(getenv('MAGENTO_BASE_URL')) . $this->moduleUrl;
205206

206207
$headers = [
207208
'Authorization: Bearer ' . $token,
@@ -677,7 +678,7 @@ private function printMagentoVersionInfo()
677678
if (MftfApplicationConfig::getConfig()->forceGenerateEnabled()) {
678679
return;
679680
}
680-
$url = ConfigSanitizerUtil::sanitizeUrl(getenv('MAGENTO_BASE_URL')) . $this->versionUrl;
681+
$url = UrlFormatter::format(getenv('MAGENTO_BASE_URL')) . $this->versionUrl;
681682
LoggingUtil::getInstance()->getLogger(ModuleResolver::class)->info(
682683
"Fetching version information.",
683684
['url' => $url]
@@ -718,7 +719,7 @@ protected function getAdminToken()
718719
throw new TestFrameworkException($message, $context);
719720
}
720721

721-
$url = ConfigSanitizerUtil::sanitizeUrl($this->getBackendUrl()) . $this->adminTokenUrl;
722+
$url = UrlFormatter::format($this->getBackendUrl()) . $this->adminTokenUrl;
722723
$data = [
723724
'username' => $login,
724725
'password' => $password
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: jilu
5+
* Date: 11/19/19
6+
* Time: 2:16 PM
7+
*/
8+
9+
namespace Magento\FunctionalTestingFramework\Util\Path;
10+
11+
use Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException;
12+
13+
class FilePathFormatter implements FileUrlFormatterInterface
14+
{
15+
/**
16+
* Return formatted full file path from input string, or false on error
17+
*
18+
* @param string $path
19+
* @param boolean $withTrailingSeparator
20+
* @return string
21+
* @throws TestFrameworkException
22+
*/
23+
public static function format($path, $withTrailingSeparator = false)
24+
{
25+
$validPath = realpath($path);
26+
27+
if ($validPath) {
28+
return $withTrailingSeparator ? $validPath . DIRECTORY_SEPARATOR : $validPath;
29+
}
30+
31+
throw new TestFrameworkException("Invalid or non-existing file: $path\n");
32+
}
33+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: jilu
5+
* Date: 11/19/19
6+
* Time: 2:16 PM
7+
*/
8+
9+
namespace Magento\FunctionalTestingFramework\Util\Path;
10+
11+
use Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException;
12+
13+
interface FileUrlFormatterInterface
14+
{
15+
/**
16+
* Return formatted path (file path, url, etc) from input string, or false on error
17+
*
18+
* @param string $input
19+
* @param boolean $withTrailingSeparator
20+
* @return string
21+
* @throws TestFrameworkException
22+
*/
23+
public static function format($input, $withTrailingSeparator = false);
24+
}

0 commit comments

Comments
 (0)