Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MFTF-33782: Added empty query and fragment testing to the UrlFormatterTest #867

Merged
merged 3 commits into from
Aug 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,82 +3,100 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace tests\unit\Magento\FunctionalTestFramework\Util\Path;

use Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException;
use tests\unit\Util\MagentoTestCase;
use Magento\FunctionalTestingFramework\Util\Path\FilePathFormatter;
use tests\unit\Util\MagentoTestCase;

class FilePathFormatterTest extends MagentoTestCase
{
/**
* Test file format
* Test file format.
*
* @param string $path
* @param bool|null $withTrailingSeparator
* @param string|null $expectedPath
*
* @dataProvider formatDataProvider
* @param string $path
* @param boolean $withTrailingSeparator
* @param mixed string|boolean $expectedPath
* @return void
* @throws TestFrameworkException
* @dataProvider formatDataProvider
*/
public function testFormat($path, $withTrailingSeparator, $expectedPath)
public function testFormat(string $path, ?bool $withTrailingSeparator, ?string $expectedPath): void
{
if (null !== $expectedPath) {
if ($withTrailingSeparator === null) {
$this->assertEquals($expectedPath, FilePathFormatter::format($path));
return;
}
$this->assertEquals($expectedPath, FilePathFormatter::format($path, $withTrailingSeparator));
} else {
// Assert no exception
FilePathFormatter::format($path, $withTrailingSeparator);
if ($withTrailingSeparator === null) {
FilePathFormatter::format($path);
} else {
FilePathFormatter::format($path, $withTrailingSeparator);
}
$this->assertTrue(true);
}
}

/**
* Test file format with exception
* Test file format with exception.
*
* @param string $path
* @param bool|null $withTrailingSeparator
*
* @dataProvider formatExceptionDataProvider
* @param string $path
* @param boolean $withTrailingSeparator
* @return void
* @throws TestFrameworkException
* @dataProvider formatExceptionDataProvider
*/
public function testFormatWithException($path, $withTrailingSeparator)
public function testFormatWithException(string $path, ?bool $withTrailingSeparator): void
{
$this->expectException(TestFrameworkException::class);
$this->expectExceptionMessage("Invalid or non-existing file: $path\n");

if ($withTrailingSeparator === null) {
FilePathFormatter::format($path);
return;
}
FilePathFormatter::format($path, $withTrailingSeparator);
}

/**
* Data input
* Data input.
*
* @return array
*/
public function formatDataProvider()
public function formatDataProvider(): array
{
$path1 = rtrim(TESTS_BP, '/');
$path2 = $path1 . DIRECTORY_SEPARATOR;

return [
[$path1, null, $path1],
[$path1, null, $path2],
[$path1, false, $path1],
[$path1, true, $path2],
[$path2, null, $path1],
[$path2, null, $path2],
[$path2, false, $path1],
[$path2, true, $path2],
[__DIR__. DIRECTORY_SEPARATOR . basename(__FILE__), null, __FILE__],
[__DIR__ . DIRECTORY_SEPARATOR . basename(__FILE__), null, __FILE__ . DIRECTORY_SEPARATOR],
['', null, null] // Empty string is valid
];
}

/**
* Invalid data input
* Invalid data input.
*
* @return array
*/
public function formatExceptionDataProvider()
public function formatExceptionDataProvider(): array
{
return [
['abc', null],
['X://some\dir/@', null],
['X://some\dir/@', null]
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,62 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace tests\unit\Magento\FunctionalTestFramework\Util\Path;

use tests\unit\Util\MagentoTestCase;
use Magento\FunctionalTestingFramework\Util\Path\UrlFormatter;
use Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException;
use Magento\FunctionalTestingFramework\Util\Path\UrlFormatter;
use tests\unit\Util\MagentoTestCase;

class UrlFormatterTest extends MagentoTestCase
{
/**
* Test url format
* Test url format.
*
* @dataProvider formatDataProvider
* @param string $path
* @param boolean $withTrailingSeparator
* @param mixed string|boolean $expectedPath
* @param bool|null $withTrailingSeparator
* @param string $expectedPath
*
* @return void
* @throws TestFrameworkException
* @dataProvider formatDataProvider
*/
public function testFormat($path, $withTrailingSeparator, $expectedPath)
public function testFormat(string $path, ?bool $withTrailingSeparator, string $expectedPath): void
{
if ($withTrailingSeparator === null) {
$this->assertEquals($expectedPath, UrlFormatter::format($path));
return;
}
$this->assertEquals($expectedPath, UrlFormatter::format($path, $withTrailingSeparator));
}

/**
* Test url format with exception
* Test url format with exception.
*
* @dataProvider formatExceptionDataProvider
* @param string $path
* @param boolean $withTrailingSeparator
* @param bool|null $withTrailingSeparator
*
* @return void
* @throws TestFrameworkException
* @dataProvider formatExceptionDataProvider
*/
public function testFormatWithException($path, $withTrailingSeparator)
public function testFormatWithException(string $path, ?bool $withTrailingSeparator): void
{
$this->expectException(TestFrameworkException::class);
$this->expectExceptionMessage("Invalid url: $path\n");

if ($withTrailingSeparator === null) {
UrlFormatter::format($path);
return;
}
UrlFormatter::format($path, $withTrailingSeparator);
}

/**
* Data input
* Data input.
*
* @return array
*/
public function formatDataProvider()
public function formatDataProvider(): array
{
$url1 = 'http://magento.local/index.php';
$url2 = $url1 . '/';
Expand All @@ -58,34 +69,38 @@ public function formatDataProvider()
$url7 = 'http://127.0.0.1:8200';
$url8 = 'wwøw.goåoøgle.coøm';
$url9 = 'http://www.google.com';

return [
[$url1, null, $url1],
[$url1, null, $url2],
[$url1, false, $url1],
[$url1, true, $url2],
[$url2, null, $url1],
[$url2, null, $url2],
[$url2, false, $url1],
[$url2, true, $url2],
[$url3, null, $url3],
[$url3, null, $url4],
[$url3, false, $url3],
[$url3, true, $url4],
[$url4, null, $url3],
[$url4, null, $url4],
[$url4, false, $url3],
[$url4, true, $url4],
[$url5, true, $url6],
[$url7, false, $url7],
[$url8, false, $url9],
['https://magento.local/path?', false, 'https://magento.local/path?'],
['https://magento.local/path#', false, 'https://magento.local/path#'],
['https://magento.local/path?#', false, 'https://magento.local/path?#']
];
}

/**
* Invalid data input
* Invalid data input.
*
* @return array
*/
public function formatExceptionDataProvider()
public function formatExceptionDataProvider(): array
{
return [
['', null],
['', null]
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\FunctionalTestingFramework\Util\Path;

Expand All @@ -11,14 +12,15 @@
class FilePathFormatter implements FormatterInterface
{
/**
* Return formatted full file path from input string, or false on error
* Return formatted full file path from input string, or false on error.
*
* @param string $path
* @param boolean $withTrailingSeparator
*
* @return string
* @throws TestFrameworkException
*/
public static function format($path, $withTrailingSeparator = true)
public static function format(string $path, bool $withTrailingSeparator = true): string
{
$validPath = realpath($path);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\FunctionalTestingFramework\Util\Path;

Expand All @@ -11,12 +12,13 @@
interface FormatterInterface
{
/**
* Return formatted path (file path, url, etc) from input string, or false on error
* Return formatted path (file path, url, etc) from input string, or false on error.
*
* @param string $input
* @param boolean $withTrailingSeparator
*
* @return string
* @throws TestFrameworkException
*/
public static function format($input, $withTrailingSeparator = true);
public static function format(string $input, bool $withTrailingSeparator = true): string;
}
Loading