Skip to content

Commit 87a70e9

Browse files
author
oleksandrkravchuk
committed
community-features#252 Create static test for action controllers.
Apply code review changes.
1 parent ca31a93 commit 87a70e9

File tree

16 files changed

+241
-184
lines changed

16 files changed

+241
-184
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 $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: 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 Z
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 Z
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 Z
9+
{
10+
}

dev/tests/static/framework/tests/unit/testsuite/Magento/TestFramework/Utility/ChildrenClassesSearchTest.php

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,17 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\TestFramework\Utility;
79

8-
use Magento\Framework\App\Action\AbstractAction;
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;
915

10-
class ChildrenClassesSearchTest extends \PHPUnit\Framework\TestCase
16+
class ChildrenClassesSearchTest extends TestCase
1117
{
1218
/**
1319
* @var ChildrenClassesSearch
@@ -22,18 +28,27 @@ protected function setUp(): void
2228
public function testChildrenSearch(): void
2329
{
2430
$files = [
25-
__DIR__ . '/PartialNamespace/Foo.php',
26-
__DIR__ . '/PartialNamespace/Bar.php',
27-
__DIR__ . '/PartialNamespace/Baz.php',
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/Z.php',
2838
];
2939

3040
$found = $this->childrenClassesSearch->getClassesWhichAreChildrenOf(
3141
$files,
32-
AbstractAction::class,
42+
A::class,
3343
false
3444
);
3545

36-
$this->assertCount(1, $found);
37-
$this->assertEquals(current($found), \Magento\TestFramework\Utility\PartialNamespace\Foo::class);
46+
$expected = [
47+
B::class,
48+
E::class,
49+
F::class
50+
];
51+
52+
$this->assertSame($expected, $found);
3853
}
3954
}

0 commit comments

Comments
 (0)