Skip to content

Commit 450d42d

Browse files
committed
ACP2E-782: Introduce an alternative way to define fixtures using PHP8 Attributes
1 parent d1c3567 commit 450d42d

28 files changed

+698
-270
lines changed

dev/tests/api-functional/phpunit_graphql.xml.dist

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,6 @@
110110
<element key="Override">
111111
<string>Override</string>
112112
</element>
113-
<element key="magentoDataFixtureDataProvider">
114-
<string>magentoDataFixtureDataProvider</string>
115-
</element>
116113
</array>
117114
</arguments>
118115
</listener>

dev/tests/api-functional/phpunit_rest.xml.dist

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,6 @@
116116
<element key="Override">
117117
<string>Override</string>
118118
</element>
119-
<element key="magentoDataFixtureDataProvider">
120-
<string>magentoDataFixtureDataProvider</string>
121-
</element>
122119
</array>
123120
</arguments>
124121
</listener>

dev/tests/api-functional/phpunit_soap.xml.dist

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,6 @@
115115
<element key="Override">
116116
<string>Override</string>
117117
</element>
118-
<element key="magentoDataFixtureDataProvider">
119-
<string>magentoDataFixtureDataProvider</string>
120-
</element>
121118
</array>
122119
</arguments>
123120
</listener>

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

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,26 @@ protected function _getFixtures(TestCase $test, $scope = null)
4848

4949
$resolver = Resolver::getInstance();
5050
$resolver->setCurrentFixtureType($annotationKey);
51-
$parsers = $this->getParsers();
51+
$parsers = Bootstrap::getObjectManager()
52+
->create(
53+
\Magento\TestFramework\Annotation\Parser\Composite::class,
54+
[
55+
'parsers' => $this->getParsers()
56+
]
57+
);
5258
$scopes = $scope ? [$scope] : [ParserInterface::SCOPE_METHOD, ParserInterface::SCOPE_CLASS];
5359
$fixtures = [];
5460
foreach ($scopes as $scp) {
55-
foreach ($parsers as $parser) {
56-
try {
57-
array_push($fixtures, ...$parser->parse($test, $scp));
58-
} catch (\Throwable $exception) {
59-
throw new Exception(
60-
sprintf(
61-
"Unable to parse fixtures\n#0 %s",
62-
$this->getTestReference($this->getTestMetadata($test))
63-
),
64-
$exception
65-
);
66-
}
61+
try {
62+
$fixtures = $parsers->parse($test, $scp);
63+
} catch (\Throwable $exception) {
64+
throw new Exception(
65+
sprintf(
66+
"Unable to parse fixtures\n#0 %s",
67+
$this->getTestReference($this->getTestMetadata($test))
68+
),
69+
$exception
70+
);
6771
}
6872
if (!empty($fixtures)) {
6973
break;
@@ -265,7 +269,8 @@ protected function getParsers(): array
265269
*/
266270
protected function getDbIsolationState(TestCase $test)
267271
{
268-
return Bootstrap::getObjectManager()->get(DbIsolationState::class)->getState($test) ?: null;
272+
$isEnabled = Bootstrap::getObjectManager()->get(DbIsolationState::class)->isEnabled($test);
273+
return $isEnabled === null ? null : [$isEnabled];
269274
}
270275

271276
/**

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

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -78,20 +78,32 @@ protected function _getTestAppArea($annotations)
7878
* Start test case event observer
7979
*
8080
* @param TestCase $test
81+
* @throws LocalizedException
8182
*/
8283
public function startTest(TestCase $test)
8384
{
84-
$area = $this->getArea($test);
85+
$objectManager = Bootstrap::getObjectManager();
86+
$parsers = $objectManager
87+
->create(
88+
\Magento\TestFramework\Annotation\Parser\Composite::class,
89+
[
90+
'parsers' => [
91+
$objectManager->get(\Magento\TestFramework\Annotation\Parser\AppArea::class),
92+
$objectManager->get(\Magento\TestFramework\Fixture\Parser\AppArea::class)
93+
]
94+
]
95+
);
96+
$values = $parsers->parse($test, ParserInterface::SCOPE_METHOD)
97+
?: $parsers->parse($test, ParserInterface::SCOPE_CLASS);
8598

86-
if (!in_array($area, $this->_allowedAreas, true)) {
99+
if (count($values) > 1) {
87100
throw new LocalizedException(
88-
__(
89-
'Invalid "@magentoAppArea" annotation, can be "%1" only.',
90-
implode('", "', $this->_allowedAreas)
91-
)
101+
__('Only one "@magentoAppArea" annotation is allowed per test')
92102
);
93103
}
94104

105+
$area = $values[0]['area'] ?? Application::DEFAULT_APP_AREA;
106+
95107
if ($this->_application->getArea() !== $area) {
96108
$this->_application->reinitialize();
97109

@@ -100,23 +112,4 @@ public function startTest(TestCase $test)
100112
}
101113
}
102114
}
103-
104-
/**
105-
* Get the configured application area
106-
*
107-
* @param TestCase $test
108-
* @return string
109-
* @throws LocalizedException
110-
*/
111-
private function getArea(TestCase $test): string
112-
{
113-
$annotations = TestCaseAnnotation::getInstance()->getAnnotations($test);
114-
$parser = Bootstrap::getObjectManager()->create(\Magento\TestFramework\Fixture\Parser\AppArea::class);
115-
$converter = static fn ($info) => $info['area'];
116-
$classAppIsolationState = array_map($converter, $parser->parse($test, ParserInterface::SCOPE_CLASS))
117-
?: ($annotations['class'][self::ANNOTATION_NAME] ?? []);
118-
$methodAppIsolationState = array_map($converter, $parser->parse($test, ParserInterface::SCOPE_METHOD))
119-
?: ($annotations['method'][self::ANNOTATION_NAME] ?? []);
120-
return current($methodAppIsolationState ?: $classAppIsolationState) ?: Application::DEFAULT_APP_AREA;
121-
}
122115
}

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

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -87,22 +87,27 @@ public function endTestSuite()
8787
public function endTest(TestCase $test)
8888
{
8989
$this->hasNonIsolatedTests = true;
90+
$objectManager = Bootstrap::getObjectManager();
91+
$parsers = $objectManager
92+
->create(
93+
\Magento\TestFramework\Annotation\Parser\Composite::class,
94+
[
95+
'parsers' => [
96+
$objectManager->get(\Magento\TestFramework\Annotation\Parser\AppIsolation::class),
97+
$objectManager->get(\Magento\TestFramework\Fixture\Parser\AppIsolation::class)
98+
]
99+
]
100+
);
101+
$values = $parsers->parse($test, ParserInterface::SCOPE_METHOD)
102+
?: $parsers->parse($test, ParserInterface::SCOPE_CLASS);
90103

91-
$annotations = TestCaseAnnotation::getInstance()->getAnnotations($test);
92-
$parser = Bootstrap::getObjectManager()->create(\Magento\TestFramework\Fixture\Parser\AppIsolation::class);
93-
$converter = static fn ($stateInfo) => $stateInfo['enabled'] ? 'enabled' : 'disabled';
94-
$classAppIsolationState = array_map($converter, $parser->parse($test, ParserInterface::SCOPE_CLASS))
95-
?: ($annotations['class'][self::ANNOTATION] ?? []);
96-
$methodAppIsolationState = array_map($converter, $parser->parse($test, ParserInterface::SCOPE_METHOD))
97-
?: ($annotations['method'][self::ANNOTATION] ?? []);
98-
$isolation = $methodAppIsolationState ?: $classAppIsolationState;
99-
if ($isolation) {
100-
if ($isolation !== ['enabled'] && $isolation !== ['disabled']) {
101-
throw new LocalizedException(
102-
__('Invalid "@magentoAppIsolation" annotation, can be "enabled" or "disabled" only.')
103-
);
104-
}
105-
$isIsolationEnabled = $isolation === ['enabled'];
104+
if (count($values) > 1) {
105+
throw new LocalizedException(
106+
__('Only one "@magentoAppIsolation" annotation is allowed per test')
107+
);
108+
}
109+
if ($values) {
110+
$isIsolationEnabled = $values[0]['enabled'];
106111
} else {
107112
/* Controller tests should be isolated by default */
108113
$isIsolationEnabled = $test instanceof AbstractController;
@@ -112,18 +117,4 @@ public function endTest(TestCase $test)
112117
$this->_isolateApp();
113118
}
114119
}
115-
116-
/**
117-
* Get method annotations. Overwrites class-defined annotations.
118-
*
119-
* @param TestCase $test
120-
*
121-
* @return array
122-
*/
123-
private function getAnnotations(TestCase $test): array
124-
{
125-
$annotations = TestCaseAnnotation::getInstance()->getAnnotations($test);
126-
127-
return array_replace((array)$annotations['class'], (array)$annotations['method']);
128-
}
129120
}

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

Lines changed: 12 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66

77
namespace Magento\TestFramework\Annotation;
88

9-
use Magento\Framework\Exception\LocalizedException;
10-
use Magento\TestFramework\Fixture\Parser\Cache as CacheFixtureParser;
119
use Magento\TestFramework\Fixture\ParserInterface;
1210
use Magento\TestFramework\Helper\Bootstrap;
1311
use PHPUnit\Framework\TestCase;
@@ -33,16 +31,19 @@ class Cache
3331
*/
3432
public function startTest(TestCase $test)
3533
{
36-
$statusList = array_merge(
37-
$this->getFixturesFromCacheAttribute($test, ParserInterface::SCOPE_METHOD),
38-
$this->getFixturesFromCacheAnnotation($test, ParserInterface::SCOPE_METHOD)
39-
);
40-
if (!$statusList) {
41-
$statusList = array_merge(
42-
$this->getFixturesFromCacheAttribute($test, ParserInterface::SCOPE_CLASS),
43-
$this->getFixturesFromCacheAnnotation($test, ParserInterface::SCOPE_CLASS)
34+
$objectManager = Bootstrap::getObjectManager();
35+
$parsers = $objectManager
36+
->create(
37+
\Magento\TestFramework\Annotation\Parser\Composite::class,
38+
[
39+
'parsers' => [
40+
$objectManager->get(\Magento\TestFramework\Annotation\Parser\Cache::class),
41+
$objectManager->get(\Magento\TestFramework\Fixture\Parser\Cache::class)
42+
]
43+
]
4444
);
45-
}
45+
$statusList = $parsers->parse($test, ParserInterface::SCOPE_METHOD)
46+
?: $parsers->parse($test, ParserInterface::SCOPE_CLASS);
4647

4748
if ($statusList) {
4849
$values = [];
@@ -121,40 +122,4 @@ private static function fail($message, TestCase $test)
121122
$test->fail("{$message} in the test '{$test->toString()}'");
122123
throw new \Exception('The above line was supposed to throw an exception.');
123124
}
124-
125-
/**
126-
* Returns cache fixtures defined using Cache annotation
127-
*
128-
* @param TestCase $test
129-
* @param string $scope
130-
* @return array
131-
* @throws \Exception
132-
*/
133-
private function getFixturesFromCacheAnnotation(TestCase $test, string $scope): array
134-
{
135-
$annotations = TestCaseAnnotation::getInstance()->getAnnotations($test);
136-
$configs = [];
137-
138-
foreach ($annotations[$scope][self::ANNOTATION] ?? [] as $annotation) {
139-
if (!preg_match('/^([a-z_]+)\s(enabled|disabled)$/', $annotation, $matches)) {
140-
self::fail("Invalid @magentoCache declaration: '{$annotation}'", $test);
141-
}
142-
$configs[] = ['type' => $matches[1], 'status' => $matches[2] === 'enabled'];
143-
}
144-
145-
return $configs;
146-
}
147-
148-
/**
149-
* Returns cache fixtures defined using Cache attribute
150-
*
151-
* @param TestCase $test
152-
* @param string $scope
153-
* @return array
154-
* @throws LocalizedException
155-
*/
156-
private function getFixturesFromCacheAttribute(TestCase $test, string $scope): array
157-
{
158-
return Bootstrap::getObjectManager()->create(CacheFixtureParser::class)->parse($test, $scope);
159-
}
160125
}

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

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
namespace Magento\TestFramework\Annotation;
88

99
use Magento\TestFramework\Annotation\TestCaseAnnotation;
10+
use Magento\TestFramework\Fixture\ParserInterface;
11+
use Magento\TestFramework\Helper\Bootstrap;
1012
use RecursiveDirectoryIterator;
1113
use RecursiveIteratorIterator;
1214
use RegexIterator;
@@ -21,8 +23,8 @@ class ComponentRegistrarFixture
2123
/**#@+
2224
* Properties of components registrar
2325
*/
24-
const REGISTRAR_CLASS = \Magento\Framework\Component\ComponentRegistrar::class;
25-
const PATHS_FIELD = 'paths';
26+
public const REGISTRAR_CLASS = \Magento\Framework\Component\ComponentRegistrar::class;
27+
public const PATHS_FIELD = 'paths';
2628
/**#@-*/
2729

2830
/**
@@ -80,18 +82,20 @@ public function endTest(\PHPUnit\Framework\TestCase $test)
8082
*/
8183
private function registerComponents(\PHPUnit\Framework\TestCase $test)
8284
{
83-
$annotations = TestCaseAnnotation::getInstance()->getAnnotations($test);
84-
$componentAnnotations = [];
85-
if (isset($annotations['class'][self::ANNOTATION_NAME])) {
86-
$componentAnnotations = array_merge($componentAnnotations, $annotations['class'][self::ANNOTATION_NAME]);
87-
}
88-
if (isset($annotations['method'][self::ANNOTATION_NAME])) {
89-
$componentAnnotations = array_merge($componentAnnotations, $annotations['method'][self::ANNOTATION_NAME]);
90-
}
91-
if (empty($componentAnnotations)) {
92-
return;
93-
}
94-
$componentAnnotations = array_unique($componentAnnotations);
85+
$objectManager = Bootstrap::getObjectManager();
86+
$parsers = $objectManager
87+
->create(
88+
\Magento\TestFramework\Annotation\Parser\Composite::class,
89+
[
90+
'parsers' => [
91+
$objectManager->get(\Magento\TestFramework\Annotation\Parser\ComponentsDir::class),
92+
$objectManager->get(\Magento\TestFramework\Fixture\Parser\ComponentsDir::class)
93+
]
94+
]
95+
);
96+
$values = $parsers->parse($test, ParserInterface::SCOPE_METHOD)
97+
?: $parsers->parse($test, ParserInterface::SCOPE_CLASS);
98+
$componentAnnotations = array_unique(array_column($values, 'path'));
9599
$reflection = new \ReflectionClass(self::REGISTRAR_CLASS);
96100
$paths = $reflection->getProperty(self::PATHS_FIELD);
97101
$paths->setAccessible(true);

0 commit comments

Comments
 (0)