Skip to content

Commit

Permalink
Merge branch 'develop' into imported-magento-magento2-functional-test…
Browse files Browse the repository at this point in the history
…ing-framework-868
  • Loading branch information
KevinBKozan authored Aug 23, 2021
2 parents bbf9b20 + 9c4aaaf commit c2d5d6b
Show file tree
Hide file tree
Showing 10 changed files with 132 additions and 82 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"phpmd/phpmd": "^2.8.0",
"phpunit/phpunit": "^9.0",
"sebastian/phpcpd": "~6.0.0",
"squizlabs/php_codesniffer": "~3.5.4"
"squizlabs/php_codesniffer": "~3.6.0"
},
"autoload": {
"files": ["src/Magento/FunctionalTestingFramework/_bootstrap.php"],
Expand Down
12 changes: 6 additions & 6 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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 @@ -64,7 +64,7 @@ public function __construct(
public function isNumericArray($nodeXpath)
{
foreach ($this->numericArrays as $pathPattern) {
if ($this->nodePathMatcher->match($pathPattern, $nodeXpath)) {
if ($this->nodePathMatcher->pathMatch($pathPattern, $nodeXpath)) {
return true;
}
}
Expand All @@ -84,7 +84,7 @@ public function getAssocArrayKeyAttribute($nodeXpath)
}

foreach ($this->assocArrays as $pathPattern => $keyAttribute) {
if ($this->nodePathMatcher->match($pathPattern, $nodeXpath)) {
if ($this->nodePathMatcher->pathMatch($pathPattern, $nodeXpath)) {
return $keyAttribute;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function __construct(NodePathMatcher $nodePathMatcher, array $idAttribute
public function getIdAttribute($nodeXpath)
{
foreach ($this->idAttributes as $pathPattern => $idAttribute) {
if ($this->nodePathMatcher->match($pathPattern, $nodeXpath)) {
if ($this->nodePathMatcher->pathMatch($pathPattern, $nodeXpath)) {
return $idAttribute;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class NodePathMatcher
* @param string $xpathSubject Example: '/some[@attr="value"]/static/ns:path'.
* @return boolean
*/
public function match($pathPattern, $xpathSubject)
public function pathMatch($pathPattern, $xpathSubject)
{
$pathSubject = $this->simplifyXpath($xpathSubject);
$pathPattern = '#^' . $pathPattern . '$#';
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,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

0 comments on commit c2d5d6b

Please sign in to comment.