Skip to content

Commit 97be07f

Browse files
ENGCOM-7927: community-features#252 Create static test for action controllers. #29339
- Merge Pull Request #29339 from swnsma/magento2-1:community-features-252 - Merged commits: 1. 8f43c63 2. f9ed85c 3. 602dc68 4. 7181dbc 5. 9342d4c 6. 8195b0c 7. f4e35ba 8. 308a5f4 9. 73f93d1 10. d29e8a7 11. 125a5d4 12. ca31a93 13. 87a70e9 14. 4b5eb90 15. 0b92de9 16. 019b611 17. a493bc5 18. 398801c 19. 4164d21
2 parents 18da36b + 4164d21 commit 97be07f

File tree

15 files changed

+409
-62
lines changed

15 files changed

+409
-62
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\TestFramework\Utility;
9+
10+
/**
11+
* Helper class to add list of added new files.
12+
*/
13+
class AddedFiles
14+
{
15+
/**
16+
* Provide list of new files.
17+
*
18+
* @param string $changedFilesBaseDir
19+
*
20+
* @return string[]
21+
*/
22+
public static function getAddedFilesList(string $changedFilesBaseDir): array
23+
{
24+
return FilesSearch::getFilesFromListFile(
25+
$changedFilesBaseDir,
26+
'changed_files*.added.*',
27+
function () {
28+
// if no list files, probably, this is the dev environment
29+
// phpcs:ignore Generic.PHP.NoSilencedErrors,Magento2.Security.InsecureFunction
30+
@exec('git diff --cached --name-only --diff-filter=A', $addedFiles);
31+
return $addedFiles;
32+
}
33+
);
34+
}
35+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\TestFramework\Utility;
9+
10+
/**
11+
* Search for children classes in list of files.
12+
*/
13+
class ChildrenClassesSearch
14+
{
15+
/**
16+
* @var ClassNameExtractor
17+
*/
18+
private $classNameExtractor;
19+
20+
/**
21+
* ChildrenClassesSearch constructor.
22+
*/
23+
public function __construct()
24+
{
25+
$this->classNameExtractor = new ClassNameExtractor();
26+
}
27+
28+
/**
29+
* Get list of classes name which are subclasses of mentioned class.
30+
*
31+
* @param array $fileList
32+
* @param string $parent
33+
* @param bool $asDataSet
34+
*
35+
* @return array
36+
* @throws \ReflectionException
37+
*/
38+
public function getClassesWhichAreChildrenOf(array $fileList, string $parent, bool $asDataSet = true): array
39+
{
40+
$found = [];
41+
42+
foreach ($fileList as $file) {
43+
$name = $asDataSet ? $file[0] : $file;
44+
$class = $this->classNameExtractor->getNameWithNamespace(file_get_contents($name));
45+
46+
if ($class) {
47+
$classReflection = new \ReflectionClass($class);
48+
if ($classReflection->isSubclassOf($parent)) {
49+
$found[] = $class;
50+
}
51+
}
52+
}
53+
54+
return $found;
55+
}
56+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\TestFramework\Utility;
9+
10+
/**
11+
* Helper class to search files by provided directory and file pattern.
12+
*/
13+
class FilesSearch
14+
{
15+
/**
16+
* Read files from generated lists.
17+
*
18+
* @param string $listsBaseDir
19+
* @param string $listFilePattern
20+
* @param callable $noListCallback
21+
* @return string[]
22+
*/
23+
public static function getFilesFromListFile(
24+
string $listsBaseDir,
25+
string $listFilePattern,
26+
callable $noListCallback
27+
): array {
28+
$filesDefinedInList = [];
29+
$listFiles = glob($listsBaseDir . '/_files/' . $listFilePattern);
30+
if (!empty($listFiles)) {
31+
foreach ($listFiles as $listFile) {
32+
$filesDefinedInList[] = file($listFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
33+
}
34+
$filesDefinedInList = array_merge([], ...$filesDefinedInList);
35+
} else {
36+
$filesDefinedInList = call_user_func($noListCallback);
37+
}
38+
array_walk(
39+
$filesDefinedInList,
40+
function (&$file) {
41+
$file = BP . '/' . $file;
42+
}
43+
);
44+
$filesDefinedInList = array_values(array_unique($filesDefinedInList));
45+
46+
return $filesDefinedInList;
47+
}
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\TestFramework\Utility\ChildrenClassesSearch;
7+
8+
class A
9+
{
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\TestFramework\Utility\ChildrenClassesSearch;
7+
8+
class B extends A
9+
{
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\TestFramework\Utility\ChildrenClassesSearch;
7+
8+
class C implements ZInterface
9+
{
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\TestFramework\Utility\ChildrenClassesSearch;
7+
8+
class D
9+
{
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\TestFramework\Utility\ChildrenClassesSearch;
7+
8+
class E extends B
9+
{
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\TestFramework\Utility\ChildrenClassesSearch;
7+
8+
class F extends E implements ZInterface
9+
{
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\TestFramework\Utility\ChildrenClassesSearch;
7+
8+
interface ZInterface
9+
{
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\TestFramework\Utility;
9+
10+
use Magento\TestFramework\Utility\ChildrenClassesSearch\A;
11+
use Magento\TestFramework\Utility\ChildrenClassesSearch\B;
12+
use Magento\TestFramework\Utility\ChildrenClassesSearch\E;
13+
use Magento\TestFramework\Utility\ChildrenClassesSearch\F;
14+
use PHPUnit\Framework\TestCase;
15+
16+
class ChildrenClassesSearchTest extends TestCase
17+
{
18+
/**
19+
* @var ChildrenClassesSearch
20+
*/
21+
private $childrenClassesSearch;
22+
23+
protected function setUp(): void
24+
{
25+
$this->childrenClassesSearch = new ChildrenClassesSearch();
26+
}
27+
28+
public function testChildrenSearch(): void
29+
{
30+
$files = [
31+
__DIR__ . '/ChildrenClassesSearch/A.php',
32+
__DIR__ . '/ChildrenClassesSearch/B.php',
33+
__DIR__ . '/ChildrenClassesSearch/C.php',
34+
__DIR__ . '/ChildrenClassesSearch/D.php',
35+
__DIR__ . '/ChildrenClassesSearch/E.php',
36+
__DIR__ . '/ChildrenClassesSearch/F.php',
37+
__DIR__ . '/ChildrenClassesSearch/ZInterface.php',
38+
];
39+
40+
$found = $this->childrenClassesSearch->getClassesWhichAreChildrenOf(
41+
$files,
42+
A::class,
43+
false
44+
);
45+
46+
$expected = [
47+
B::class,
48+
E::class,
49+
F::class
50+
];
51+
52+
$this->assertSame($expected, $found);
53+
}
54+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\TestFramework\Utility;
9+
10+
use PHPUnit\Framework\TestCase;
11+
12+
class FilesSearchTest extends TestCase
13+
{
14+
/**
15+
* Test files list extraction from file.
16+
*/
17+
public function testGetFiles(): void
18+
{
19+
$pattern = 'changed_files*.txt';
20+
21+
$files = FilesSearch::getFilesFromListFile(__DIR__, $pattern, function () {
22+
return [];
23+
});
24+
25+
$expected = [
26+
BP . '/app/code/Magento/Cms/Block/Block.php',
27+
BP . '/app/code/Magento/Cms/Api/BlockRepositoryInterface.php',
28+
BP . '/app/code/Magento/Cms/Observer/NoCookiesObserver.php'
29+
];
30+
31+
$this->assertSame($files, $expected);
32+
}
33+
34+
/**
35+
* Test callblack function in case when files with lists did not found.
36+
*/
37+
public function testGetEmptyList(): void
38+
{
39+
$pattern = 'zzz.txt';
40+
41+
$files = FilesSearch::getFilesFromListFile(__DIR__, $pattern, function () {
42+
return ['1', '2', '3'];
43+
});
44+
45+
$expected = [
46+
BP . '/1',
47+
BP . '/2',
48+
BP . '/3'
49+
];
50+
51+
$this->assertSame($files, $expected);
52+
}
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
app/code/Magento/Cms/Block/Block.php
2+
app/code/Magento/Cms/Api/BlockRepositoryInterface.php
3+
app/code/Magento/Cms/Observer/NoCookiesObserver.php

0 commit comments

Comments
 (0)