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

community-features#252 Create static test for action controllers. #29339

Merged
merged 19 commits into from
Aug 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
8f43c63
community-features#252 Create static test for action controllers.
Jul 30, 2020
f9ed85c
Merge branch '2.4-develop' into community-features-252
swnsma Jul 30, 2020
602dc68
Update dev/tests/static/testsuite/Magento/Test/Legacy/Magento/App/Act…
swnsma Jul 31, 2020
7181dbc
Merge branch '2.4-develop' into community-features-252
swnsma Jul 31, 2020
9342d4c
community-features#252 Create static test for action controllers.
Jul 31, 2020
8195b0c
community-features#252 Create static test for action controllers.
Jul 31, 2020
f4e35ba
Merge branch '2.4-develop' into community-features-252
swnsma Aug 2, 2020
308a5f4
community-features#252 Create static test for action controllers.
Aug 3, 2020
73f93d1
Merge branch 'community-features-252' of github.com:swnsma/magento2-1…
Aug 4, 2020
d29e8a7
Merge branch '2.4-develop' into community-features-252
swnsma Aug 4, 2020
125a5d4
community-features#252 Create static test for action controllers.
Aug 4, 2020
ca31a93
Merge branch 'community-features-252' of github.com:swnsma/magento2-1…
Aug 4, 2020
87a70e9
community-features#252 Create static test for action controllers.
Aug 4, 2020
4b5eb90
Merge branch '2.4-develop' into community-features-252
swnsma Aug 4, 2020
0b92de9
community-features#252 Create static test for action controllers.
Aug 4, 2020
019b611
Merge branch 'community-features-252' of github.com:swnsma/magento2-1…
Aug 5, 2020
a493bc5
community-features#252 Create static test for action controllers..
Aug 5, 2020
398801c
Merge branch '2.4-develop' into community-features-252
swnsma Aug 5, 2020
4164d21
Merge branch '2.4-develop' into community-features-252
engcom-Kilo Aug 14, 2020
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
@@ -0,0 +1,35 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\TestFramework\Utility;

/**
* Helper class to add list of added new files.
*/
class AddedFiles
{
/**
* Provide list of new files.
*
* @param string $changedFilesBaseDir
*
* @return string[]
*/
public static function getAddedFilesList(string $changedFilesBaseDir): array
{
return FilesSearch::getFilesFromListFile(
$changedFilesBaseDir,
'changed_files*.added.*',
function () {
// if no list files, probably, this is the dev environment
// phpcs:ignore Generic.PHP.NoSilencedErrors,Magento2.Security.InsecureFunction
@exec('git diff --cached --name-only --diff-filter=A', $addedFiles);
return $addedFiles;
}
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\TestFramework\Utility;

/**
* Search for children classes in list of files.
*/
class ChildrenClassesSearch
{
/**
* @var ClassNameExtractor
*/
private $classNameExtractor;

/**
* ChildrenClassesSearch constructor.
*/
public function __construct()
{
$this->classNameExtractor = new ClassNameExtractor();
}

/**
* Get list of classes name which are subclasses of mentioned class.
*
* @param array $fileList
* @param string $parent
* @param bool $asDataSet
*
* @return array
* @throws \ReflectionException
*/
public function getClassesWhichAreChildrenOf(array $fileList, string $parent, bool $asDataSet = true): array
{
$found = [];

foreach ($fileList as $file) {
$name = $asDataSet ? $file[0] : $file;
$class = $this->classNameExtractor->getNameWithNamespace(file_get_contents($name));

if ($class) {
$classReflection = new \ReflectionClass($class);
if ($classReflection->isSubclassOf($parent)) {
$found[] = $class;
}
}
}

return $found;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\TestFramework\Utility;

/**
* Helper class to search files by provided directory and file pattern.
*/
class FilesSearch
{
/**
* Read files from generated lists.
*
* @param string $listsBaseDir
* @param string $listFilePattern
* @param callable $noListCallback
* @return string[]
*/
public static function getFilesFromListFile(
string $listsBaseDir,
string $listFilePattern,
callable $noListCallback
): array {
$filesDefinedInList = [];
$listFiles = glob($listsBaseDir . '/_files/' . $listFilePattern);
if (!empty($listFiles)) {
foreach ($listFiles as $listFile) {
$filesDefinedInList[] = file($listFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
}
$filesDefinedInList = array_merge([], ...$filesDefinedInList);
} else {
$filesDefinedInList = call_user_func($noListCallback);
}
array_walk(
$filesDefinedInList,
function (&$file) {
$file = BP . '/' . $file;
}
);
$filesDefinedInList = array_values(array_unique($filesDefinedInList));

return $filesDefinedInList;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\TestFramework\Utility\ChildrenClassesSearch;

class A
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\TestFramework\Utility\ChildrenClassesSearch;

class B extends A
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\TestFramework\Utility\ChildrenClassesSearch;

class C implements ZInterface
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\TestFramework\Utility\ChildrenClassesSearch;

class D
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\TestFramework\Utility\ChildrenClassesSearch;

class E extends B
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\TestFramework\Utility\ChildrenClassesSearch;

class F extends E implements ZInterface
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\TestFramework\Utility\ChildrenClassesSearch;

interface ZInterface
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\TestFramework\Utility;

use Magento\TestFramework\Utility\ChildrenClassesSearch\A;
use Magento\TestFramework\Utility\ChildrenClassesSearch\B;
use Magento\TestFramework\Utility\ChildrenClassesSearch\E;
use Magento\TestFramework\Utility\ChildrenClassesSearch\F;
use PHPUnit\Framework\TestCase;

class ChildrenClassesSearchTest extends TestCase
{
/**
* @var ChildrenClassesSearch
*/
private $childrenClassesSearch;

protected function setUp(): void
{
$this->childrenClassesSearch = new ChildrenClassesSearch();
}

public function testChildrenSearch(): void
{
$files = [
__DIR__ . '/ChildrenClassesSearch/A.php',
__DIR__ . '/ChildrenClassesSearch/B.php',
__DIR__ . '/ChildrenClassesSearch/C.php',
__DIR__ . '/ChildrenClassesSearch/D.php',
__DIR__ . '/ChildrenClassesSearch/E.php',
__DIR__ . '/ChildrenClassesSearch/F.php',
__DIR__ . '/ChildrenClassesSearch/ZInterface.php',
];

$found = $this->childrenClassesSearch->getClassesWhichAreChildrenOf(
$files,
A::class,
false
);

$expected = [
B::class,
E::class,
F::class
];

$this->assertSame($expected, $found);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\TestFramework\Utility;

use PHPUnit\Framework\TestCase;

class FilesSearchTest extends TestCase
{
/**
* Test files list extraction from file.
*/
public function testGetFiles(): void
{
$pattern = 'changed_files*.txt';

$files = FilesSearch::getFilesFromListFile(__DIR__, $pattern, function () {
return [];
});

$expected = [
BP . '/app/code/Magento/Cms/Block/Block.php',
BP . '/app/code/Magento/Cms/Api/BlockRepositoryInterface.php',
BP . '/app/code/Magento/Cms/Observer/NoCookiesObserver.php'
];

$this->assertSame($files, $expected);
}

/**
* Test callblack function in case when files with lists did not found.
*/
public function testGetEmptyList(): void
{
$pattern = 'zzz.txt';

$files = FilesSearch::getFilesFromListFile(__DIR__, $pattern, function () {
return ['1', '2', '3'];
});

$expected = [
BP . '/1',
BP . '/2',
BP . '/3'
];

$this->assertSame($files, $expected);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
app/code/Magento/Cms/Block/Block.php
app/code/Magento/Cms/Api/BlockRepositoryInterface.php
app/code/Magento/Cms/Observer/NoCookiesObserver.php
Loading