Skip to content

Commit 308a5f4

Browse files
author
oleksandrkravchuk
committed
community-features#252 Create static test for action controllers.
Incapsulate logic in new class. Add test coverage.
1 parent 8195b0c commit 308a5f4

File tree

6 files changed

+156
-32
lines changed

6 files changed

+156
-32
lines changed

dev/tests/integration/etc/config-global.php.dist

Lines changed: 0 additions & 11 deletions
This file was deleted.
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+
/**
11+
* Search for children classes in list of files.
12+
*/
13+
class ChildrenClassesSearch
14+
{
15+
/**
16+
* @var ClassNameExtractor
17+
*/
18+
private $classNameExtractor;
19+
20+
public function __construct()
21+
{
22+
$this->classNameExtractor = new ClassNameExtractor();
23+
}
24+
25+
/**
26+
* Get list of classes name which are subclasses of mentioned class.
27+
*
28+
* @param array $fileList
29+
* @param string $parent
30+
* @param bool $asDataSet
31+
*
32+
* @return array
33+
* @throws \ReflectionException
34+
*/
35+
public function getClassesWhichAreChildrenOf(array $fileList, string $parent, bool $asDataSet = true): array
36+
{
37+
$found = [];
38+
39+
foreach ($fileList as $file) {
40+
$name = $asDataSet ? $file[0] : $file;
41+
$class = $this->classNameExtractor->getNameWithNamespace(file_get_contents($name));
42+
43+
if ($class) {
44+
$classReflection = new \ReflectionClass($class);
45+
if ($classReflection->isSubclassOf($parent)) {
46+
$found[] = $class;
47+
}
48+
}
49+
}
50+
51+
return $found;
52+
}
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\TestFramework\Utility;
7+
8+
use Magento\Framework\App\Action\AbstractAction;
9+
10+
class ChildrenClassesSearchTest extends \PHPUnit\Framework\TestCase
11+
{
12+
/**
13+
* @var ChildrenClassesSearch
14+
*/
15+
private $childrenClassesSearch;
16+
17+
protected function setUp(): void
18+
{
19+
$this->childrenClassesSearch = new ChildrenClassesSearch();
20+
}
21+
22+
public function testChildrenSearch(): void
23+
{
24+
$files = [
25+
__DIR__ . '/PartialNamespace/Foo.php',
26+
__DIR__ . '/PartialNamespace/Bar.php',
27+
__DIR__ . '/PartialNamespace/Baz.php',
28+
];
29+
30+
$found = $this->childrenClassesSearch->getClassesWhichAreChildrenOf(
31+
$files,
32+
AbstractAction::class,
33+
false
34+
);
35+
36+
$this->assertCount(1, $found);
37+
$this->assertEquals(current($found), \Magento\TestFramework\Utility\PartialNamespace\Foo::class);
38+
}
39+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\TestFramework\Utility\PartialNamespace;
7+
8+
use Magento\Framework\App\ActionInterface;
9+
use Magento\Framework\App\ResponseInterface;
10+
11+
class Baz implements ActionInterface
12+
{
13+
/**
14+
* Execute action based on request and return result
15+
*
16+
* @return \Magento\Framework\Controller\ResultInterface|ResponseInterface
17+
* @throws \Magento\Framework\Exception\NotFoundException
18+
*/
19+
public function execute()
20+
{
21+
22+
}
23+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\TestFramework\Utility\PartialNamespace;
7+
8+
use Magento\Framework\App\Action\AbstractAction;
9+
use Magento\Framework\App\RequestInterface;
10+
use Magento\Framework\App\ResponseInterface;
11+
12+
class Foo extends AbstractAction
13+
{
14+
/**
15+
* Dispatch request
16+
*
17+
* @param RequestInterface $request
18+
*
19+
* @return ResponseInterface
20+
*/
21+
public function dispatch(RequestInterface $request)
22+
{
23+
}
24+
25+
/**
26+
* Execute action based on request and return result
27+
*
28+
* @return \Magento\Framework\Controller\ResultInterface|ResponseInterface
29+
* @throws \Magento\Framework\Exception\NotFoundException
30+
*/
31+
public function execute()
32+
{
33+
}
34+
}

dev/tests/static/testsuite/Magento/Test/Legacy/Magento/Framework/App/Action/AbstractActionTest.php

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,22 @@
77

88
namespace Magento\Test\Legacy\Magento\Framework\App\Action;
99

10-
use Exception;
1110
use Magento\Framework\App\Action\AbstractAction;
1211
use Magento\Framework\App\ActionInterface;
1312
use Magento\Framework\App\Utility\Files;
1413
use Magento\Framework\Exception\LocalizedException;
15-
use Magento\TestFramework\Utility\ClassNameExtractor;
14+
use Magento\TestFramework\Utility\ChildrenClassesSearch;
1615
use PHPUnit\Framework\TestCase;
17-
use ReflectionClass;
1816

1917
/**
2018
* Test newly created controllers must do not extend AbstractAction.
2119
*/
2220
class AbstractActionTest extends TestCase
2321
{
2422
/**
25-
* @var ClassNameExtractor
23+
* @var ChildrenClassesSearch
2624
*/
27-
private $classNameExtractor;
25+
private $childrenClassesSearch;
2826

2927
/**
3028
* @var Files
@@ -36,32 +34,20 @@ class AbstractActionTest extends TestCase
3634
*/
3735
protected function setUp(): void
3836
{
39-
$this->classNameExtractor = new ClassNameExtractor();
37+
$this->childrenClassesSearch = new ChildrenClassesSearch();
4038
$this->fileUtilities = Files::init();
4139
}
4240

4341
/**
4442
* Test newly created controllers do not extend deprecated AbstractAction.
43+
*
44+
* @throws \ReflectionException
4545
*/
4646
public function testNewControllersDoNotExtendAbstractAction(): void
4747
{
4848
$files = $this->getTestFiles();
49-
$found = [];
50-
51-
foreach ($files as $file) {
52-
$class = $this->classNameExtractor->getNameWithNamespace(file_get_contents($file[0]));
5349

54-
if ($class) {
55-
try {
56-
$classReflection = new ReflectionClass($class);
57-
if ($classReflection->isSubclassOf(AbstractAction::class)) {
58-
$found[] = $class;
59-
}
60-
} catch (Exception $exception) {
61-
$this->addWarning('Skipped due to exception: ' . $class);
62-
}
63-
}
64-
}
50+
$found = $this->childrenClassesSearch->getClassesWhichAreChildrenOf($files, AbstractAction::class);
6551

6652
$this->assertEmpty(
6753
$found,

0 commit comments

Comments
 (0)