Skip to content

Commit d5f82e7

Browse files
committed
ACP2E-782: Introduce an alternative way to define fixtures using PHP8 Attributes
1 parent 82b7d5d commit d5f82e7

File tree

2 files changed

+29
-18
lines changed

2 files changed

+29
-18
lines changed

dev/tests/integration/framework/Magento/TestFramework/Annotation/AbstractDataFixture.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ protected function _getFixtures(TestCase $test, $scope = null)
5656
]
5757
);
5858

59+
$fixtures = [];
5960
try {
6061
$fixtures = $parsers->parse($test, $scope ?: ParserInterface::SCOPE_METHOD);
6162
if (!$fixtures && !$scope) {

dev/tests/integration/framework/Magento/TestFramework/Annotation/ComponentRegistrarFixture.php

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,25 @@
66

77
namespace Magento\TestFramework\Annotation;
88

9-
use Magento\TestFramework\Annotation\TestCaseAnnotation;
9+
use FilesystemIterator;
10+
use InvalidArgumentException;
11+
use Magento\Framework\Component\ComponentRegistrar;
12+
use Magento\Framework\Exception\LocalizedException;
13+
use Magento\TestFramework\Annotation\Parser\Composite;
1014
use Magento\TestFramework\Fixture\ParserInterface;
1115
use Magento\TestFramework\Helper\Bootstrap;
16+
use PHPUnit\Framework\TestCase;
1217
use RecursiveDirectoryIterator;
1318
use RecursiveIteratorIterator;
19+
use ReflectionClass;
1420
use RegexIterator;
21+
use SplFileInfo;
22+
use Throwable;
1523

1624
/**
1725
* Implementation of the @magentoComponentsDir DocBlock annotation
26+
*
27+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1828
*/
1929
class ComponentRegistrarFixture
2030
{
@@ -23,7 +33,7 @@ class ComponentRegistrarFixture
2333
/**#@+
2434
* Properties of components registrar
2535
*/
26-
public const REGISTRAR_CLASS = \Magento\Framework\Component\ComponentRegistrar::class;
36+
public const REGISTRAR_CLASS = ComponentRegistrar::class;
2737
public const PATHS_FIELD = 'paths';
2838
/**#@-*/
2939

@@ -54,38 +64,38 @@ public function __construct($fixtureBaseDir)
5464
/**
5565
* Handler for 'startTest' event
5666
*
57-
* @param \PHPUnit\Framework\TestCase $test
67+
* @param TestCase $test
5868
* @return void
5969
*/
60-
public function startTest(\PHPUnit\Framework\TestCase $test)
70+
public function startTest(TestCase $test)
6171
{
6272
$this->registerComponents($test);
6373
}
6474

6575
/**
6676
* Handler for 'endTest' event
6777
*
68-
* @param \PHPUnit\Framework\TestCase $test
78+
* @param TestCase $test
6979
* @return void
7080
*
7181
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
7282
*/
73-
public function endTest(\PHPUnit\Framework\TestCase $test)
83+
public function endTest(TestCase $test)
7484
{
7585
$this->restoreComponents();
7686
}
7787

7888
/**
7989
* Register fixture components
8090
*
81-
* @param \PHPUnit\Framework\TestCase $test
91+
* @param TestCase $test
8292
*/
83-
private function registerComponents(\PHPUnit\Framework\TestCase $test)
93+
private function registerComponents(TestCase $test)
8494
{
8595
$values = [];
8696
try {
8797
$values = $this->parse($test);
88-
} catch (\Throwable $exception) {
98+
} catch (Throwable $exception) {
8999
ExceptionHandler::handle(
90100
'Unable to parse fixtures',
91101
get_class($test),
@@ -98,26 +108,26 @@ private function registerComponents(\PHPUnit\Framework\TestCase $test)
98108
}
99109

100110
$componentAnnotations = array_unique(array_column($values, 'path'));
101-
$reflection = new \ReflectionClass(self::REGISTRAR_CLASS);
111+
$reflection = new ReflectionClass(self::REGISTRAR_CLASS);
102112
$paths = $reflection->getProperty(self::PATHS_FIELD);
103113
$paths->setAccessible(true);
104114
$this->origComponents = $paths->getValue();
105115
$paths->setAccessible(false);
106116
foreach ($componentAnnotations as $fixturePath) {
107117
$fixturesDir = $this->fixtureBaseDir . '/' . $fixturePath;
108118
if (!file_exists($fixturesDir)) {
109-
throw new \InvalidArgumentException(
119+
throw new InvalidArgumentException(
110120
self::ANNOTATION_NAME . " fixture '$fixturePath' does not exist"
111121
);
112122
}
113123
$iterator = new RegexIterator(
114124
new RecursiveIteratorIterator(
115-
new RecursiveDirectoryIterator($fixturesDir, \FilesystemIterator::SKIP_DOTS)
125+
new RecursiveDirectoryIterator($fixturesDir, FilesystemIterator::SKIP_DOTS)
116126
),
117127
'/^.+\/registration\.php$/'
118128
);
119129
/**
120-
* @var \SplFileInfo $registrationFile
130+
* @var SplFileInfo $registrationFile
121131
*/
122132
foreach ($iterator as $registrationFile) {
123133
require $registrationFile->getRealPath();
@@ -131,7 +141,7 @@ private function registerComponents(\PHPUnit\Framework\TestCase $test)
131141
private function restoreComponents()
132142
{
133143
if (null !== $this->origComponents) {
134-
$reflection = new \ReflectionClass(self::REGISTRAR_CLASS);
144+
$reflection = new ReflectionClass(self::REGISTRAR_CLASS);
135145
$paths = $reflection->getProperty(self::PATHS_FIELD);
136146
$paths->setAccessible(true);
137147
$paths->setValue($this->origComponents);
@@ -143,16 +153,16 @@ private function restoreComponents()
143153
/**
144154
* Returns ComponentsDir fixtures configuration
145155
*
146-
* @param \PHPUnit\Framework\TestCase $test
156+
* @param TestCase $test
147157
* @return array
148-
* @throws \Magento\Framework\Exception\LocalizedException
158+
* @throws LocalizedException
149159
*/
150-
private function parse(\PHPUnit\Framework\TestCase $test): array
160+
private function parse(TestCase $test): array
151161
{
152162
$objectManager = Bootstrap::getObjectManager();
153163
$parsers = $objectManager
154164
->create(
155-
\Magento\TestFramework\Annotation\Parser\Composite::class,
165+
Composite::class,
156166
[
157167
'parsers' => [
158168
$objectManager->get(\Magento\TestFramework\Annotation\Parser\ComponentsDir::class),

0 commit comments

Comments
 (0)