-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ENGCOM-7927: community-features#252 Create static test for action con…
…trollers. #29339
- Loading branch information
Showing
15 changed files
with
409 additions
and
62 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
dev/tests/static/framework/Magento/TestFramework/Utility/AddedFiles.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
dev/tests/static/framework/Magento/TestFramework/Utility/ChildrenClassesSearch.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
dev/tests/static/framework/Magento/TestFramework/Utility/FilesSearch.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
.../framework/tests/unit/testsuite/Magento/TestFramework/Utility/ChildrenClassesSearch/A.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
} |
10 changes: 10 additions & 0 deletions
10
.../framework/tests/unit/testsuite/Magento/TestFramework/Utility/ChildrenClassesSearch/B.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
} |
10 changes: 10 additions & 0 deletions
10
.../framework/tests/unit/testsuite/Magento/TestFramework/Utility/ChildrenClassesSearch/C.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
} |
10 changes: 10 additions & 0 deletions
10
.../framework/tests/unit/testsuite/Magento/TestFramework/Utility/ChildrenClassesSearch/D.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
} |
10 changes: 10 additions & 0 deletions
10
.../framework/tests/unit/testsuite/Magento/TestFramework/Utility/ChildrenClassesSearch/E.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
} |
10 changes: 10 additions & 0 deletions
10
.../framework/tests/unit/testsuite/Magento/TestFramework/Utility/ChildrenClassesSearch/F.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
} |
10 changes: 10 additions & 0 deletions
10
...k/tests/unit/testsuite/Magento/TestFramework/Utility/ChildrenClassesSearch/ZInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
} |
54 changes: 54 additions & 0 deletions
54
...ramework/tests/unit/testsuite/Magento/TestFramework/Utility/ChildrenClassesSearchTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
...s/static/framework/tests/unit/testsuite/Magento/TestFramework/Utility/FilesSearchTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
...ests/unit/testsuite/Magento/TestFramework/Utility/_files/changed_files_some_name_test.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.