Skip to content

Commit 2a40fdf

Browse files
MFTF-33782: Code refactoring
1 parent 41bd267 commit 2a40fdf

File tree

4 files changed

+70
-48
lines changed

4 files changed

+70
-48
lines changed

dev/tests/unit/Magento/FunctionalTestFramework/Util/Path/FilePathFormatterTest.php

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,28 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace tests\unit\Magento\FunctionalTestFramework\Util\Path;
79

810
use Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException;
9-
use tests\unit\Util\MagentoTestCase;
1011
use Magento\FunctionalTestingFramework\Util\Path\FilePathFormatter;
12+
use tests\unit\Util\MagentoTestCase;
1113

1214
class FilePathFormatterTest extends MagentoTestCase
1315
{
1416
/**
15-
* Test file format
17+
* Test file format.
1618
*
17-
* @dataProvider formatDataProvider
1819
* @param string $path
19-
* @param boolean $withTrailingSeparator
20-
* @param mixed string|boolean $expectedPath
20+
* @param bool $withTrailingSeparator
21+
* @param string|null $expectedPath
22+
*
2123
* @return void
2224
* @throws TestFrameworkException
25+
* @dataProvider formatDataProvider
2326
*/
24-
public function testFormat($path, $withTrailingSeparator, $expectedPath)
27+
public function testFormat(string $path, bool $withTrailingSeparator, ?string $expectedPath): void
2528
{
2629
if (null !== $expectedPath) {
2730
$this->assertEquals($expectedPath, FilePathFormatter::format($path, $withTrailingSeparator));
@@ -33,52 +36,54 @@ public function testFormat($path, $withTrailingSeparator, $expectedPath)
3336
}
3437

3538
/**
36-
* Test file format with exception
39+
* Test file format with exception.
3740
*
38-
* @dataProvider formatExceptionDataProvider
3941
* @param string $path
40-
* @param boolean $withTrailingSeparator
42+
* @param bool $withTrailingSeparator
43+
*
4144
* @return void
4245
* @throws TestFrameworkException
46+
* @dataProvider formatExceptionDataProvider
4347
*/
44-
public function testFormatWithException($path, $withTrailingSeparator)
48+
public function testFormatWithException(string $path, bool $withTrailingSeparator): void
4549
{
4650
$this->expectException(TestFrameworkException::class);
4751
$this->expectExceptionMessage("Invalid or non-existing file: $path\n");
4852
FilePathFormatter::format($path, $withTrailingSeparator);
4953
}
5054

5155
/**
52-
* Data input
56+
* Data input.
5357
*
5458
* @return array
5559
*/
56-
public function formatDataProvider()
60+
public function formatDataProvider(): array
5761
{
5862
$path1 = rtrim(TESTS_BP, '/');
5963
$path2 = $path1 . DIRECTORY_SEPARATOR;
64+
6065
return [
61-
[$path1, null, $path1],
66+
[$path1, false, $path1],
6267
[$path1, false, $path1],
6368
[$path1, true, $path2],
64-
[$path2, null, $path1],
69+
[$path2, false, $path1],
6570
[$path2, false, $path1],
6671
[$path2, true, $path2],
67-
[__DIR__. DIRECTORY_SEPARATOR . basename(__FILE__), null, __FILE__],
68-
['', null, null] // Empty string is valid
72+
[__DIR__. DIRECTORY_SEPARATOR . basename(__FILE__), false, __FILE__],
73+
['', false, null] // Empty string is valid
6974
];
7075
}
7176

7277
/**
73-
* Invalid data input
78+
* Invalid data input.
7479
*
7580
* @return array
7681
*/
77-
public function formatExceptionDataProvider()
82+
public function formatExceptionDataProvider(): array
7883
{
7984
return [
80-
['abc', null],
81-
['X://some\dir/@', null],
85+
['abc', false],
86+
['X://some\dir/@', false]
8287
];
8388
}
8489
}

src/Magento/FunctionalTestingFramework/Util/Path/FilePathFormatter.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
67

78
namespace Magento\FunctionalTestingFramework\Util\Path;
89

@@ -11,14 +12,15 @@
1112
class FilePathFormatter implements FormatterInterface
1213
{
1314
/**
14-
* Return formatted full file path from input string, or false on error
15+
* Return formatted full file path from input string, or false on error.
1516
*
1617
* @param string $path
1718
* @param boolean $withTrailingSeparator
19+
*
1820
* @return string
1921
* @throws TestFrameworkException
2022
*/
21-
public static function format($path, $withTrailingSeparator = true)
23+
public static function format(string $path, bool $withTrailingSeparator = true): string
2224
{
2325
$validPath = realpath($path);
2426

src/Magento/FunctionalTestingFramework/Util/Path/FormatterInterface.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
67

78
namespace Magento\FunctionalTestingFramework\Util\Path;
89

@@ -11,12 +12,13 @@
1112
interface FormatterInterface
1213
{
1314
/**
14-
* Return formatted path (file path, url, etc) from input string, or false on error
15+
* Return formatted path (file path, url, etc) from input string, or false on error.
1516
*
1617
* @param string $input
1718
* @param boolean $withTrailingSeparator
19+
*
1820
* @return string
1921
* @throws TestFrameworkException
2022
*/
21-
public static function format($input, $withTrailingSeparator = true);
23+
public static function format(string $input, bool $withTrailingSeparator = true): string;
2224
}

src/Magento/FunctionalTestingFramework/Util/Path/UrlFormatter.php

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
67

78
namespace Magento\FunctionalTestingFramework\Util\Path;
89

@@ -11,14 +12,15 @@
1112
class UrlFormatter implements FormatterInterface
1213
{
1314
/**
14-
* Return formatted url path from input string
15+
* Return formatted url path from input string.
1516
*
1617
* @param string $url
1718
* @param boolean $withTrailingSeparator
19+
*
1820
* @return string
1921
* @throws TestFrameworkException
2022
*/
21-
public static function format($url, $withTrailingSeparator = true)
23+
public static function format(string $url, bool $withTrailingSeparator = true): string
2224
{
2325
$sanitizedUrl = rtrim($url, '/');
2426

@@ -47,12 +49,13 @@ public static function format($url, $withTrailingSeparator = true)
4749
}
4850

4951
/**
50-
* Try to build missing url scheme and host
52+
* Try to build missing url scheme and host.
5153
*
5254
* @param string $url
55+
*
5356
* @return string
5457
*/
55-
private static function buildUrl($url)
58+
private static function buildUrl(string $url): string
5659
{
5760
$urlParts = parse_url($url);
5861

@@ -76,32 +79,42 @@ private static function buildUrl($url)
7679
/**
7780
* Returns url from $parts given, used with parse_url output for convenience.
7881
* This only exists because of deprecation of http_build_url, which does the exact same thing as the code below.
82+
*
7983
* @param array $parts
84+
*
8085
* @return string
8186
*/
82-
private static function merge(array $parts)
87+
private static function merge(array $parts): string
8388
{
8489
$get = function ($key) use ($parts) {
85-
return isset($parts[$key]) ? $parts[$key] : null;
90+
return $parts[$key] ?? '';
8691
};
8792

88-
$pass = $get('pass');
89-
$user = $get('user');
90-
$userinfo = $pass !== null ? "$user:$pass" : $user;
91-
$port = $get('port');
92-
$scheme = $get('scheme');
93-
$query = $get('query');
94-
$fragment = $get('fragment');
95-
$authority =
96-
($userinfo !== null ? "$userinfo@" : '') .
97-
$get('host') .
98-
($port ? ":$port" : '');
99-
100-
return
101-
(strlen($scheme) ? "$scheme:" : '') .
102-
(strlen($authority) ? "//$authority" : '') .
103-
$get('path') .
104-
(strlen($query) ? "?$query" : '') .
105-
(strlen($fragment) ? "#$fragment" : '');
93+
$pass = $get('pass');
94+
$user = $get('user');
95+
$userinfo = $pass !== '' ? "$user:$pass" : $user;
96+
$port = $get('port');
97+
$scheme = $get('scheme');
98+
$query = $get('query');
99+
$fragment = $get('fragment');
100+
$authority = ($userinfo !== '' ? "$userinfo@" : '') . $get('host') . ($port ? ":$port" : '');
101+
102+
return str_replace(
103+
[
104+
'%scheme',
105+
'%authority',
106+
'%path',
107+
'%query',
108+
'%fragment'
109+
],
110+
[
111+
strlen($scheme) ? "$scheme:" : '',
112+
strlen($authority) ? "//$authority" : '',
113+
$get('path'),
114+
strlen($query) ? "?$query" : '',
115+
strlen($fragment) ? "#$fragment" : ''
116+
],
117+
'%scheme%authority%path%query%fragment'
118+
);
106119
}
107120
}

0 commit comments

Comments
 (0)