Skip to content

Commit

Permalink
ENGCOM-7927: community-features#252 Create static test for action con…
Browse files Browse the repository at this point in the history
…trollers. #29339
  • Loading branch information
sidolov authored Aug 20, 2020
2 parents fe4f3e0 + 97be07f commit 81b42be
Show file tree
Hide file tree
Showing 15 changed files with 409 additions and 62 deletions.
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

0 comments on commit 81b42be

Please sign in to comment.