Skip to content

Commit 9ef97b7

Browse files
committed
ACP2E-782: Introduce an alternative way to define fixtures using PHP8 Attributes
1 parent ca85d70 commit 9ef97b7

File tree

33 files changed

+222
-145
lines changed

33 files changed

+222
-145
lines changed

app/code/Magento/Indexer/Test/Fixture/IndexerMode.php renamed to app/code/Magento/Indexer/Test/Fixture/ScheduleMode.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,8 @@
1212
use Magento\Framework\Indexer\IndexerRegistry;
1313
use Magento\TestFramework\Fixture\RevertibleDataFixtureInterface;
1414

15-
class IndexerMode implements RevertibleDataFixtureInterface
15+
class ScheduleMode implements RevertibleDataFixtureInterface
1616
{
17-
private const DEFAULT_DATA = [
18-
'indexer' => null,
19-
'schedule' => true
20-
];
21-
2217
/**
2318
* @var IndexerRegistry
2419
*/
@@ -42,11 +37,17 @@ public function __construct(
4237
}
4338

4439
/**
45-
* @inheritdoc
40+
* {@inheritdoc}
41+
* @param array $data Parameters
42+
* <pre>
43+
* $data = [
44+
* 'indexer' => (string) Indexer code. Required.
45+
* ]
46+
* </pre>
4647
*/
4748
public function apply(array $data = []): ?DataObject
4849
{
49-
$this->indexerRegistry->get($data['indexer'])->setScheduled($data['schedule']);
50+
$this->indexerRegistry->get($data['indexer'])->setScheduled(true);
5051

5152
return $this->dataObjectFactory->create(['data' => $data]);
5253
}

dev/tests/api-functional/framework/Magento/TestFramework/Annotation/ApiDataFixture.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,21 @@ public function endTestTransactionRequest(TestCase $test, Transaction $param): v
7676
$objectManager = Bootstrap::getObjectManager();
7777
$objectManager->get(AttributeMetadataCache::class)->clean();
7878
}
79+
80+
/**
81+
* @inheritdoc
82+
*/
83+
protected function getParsers(): array
84+
{
85+
$parsers = [];
86+
// Add magentoDataFixture annotations
87+
$parsers[] = Bootstrap::getObjectManager()->create(
88+
\Magento\TestFramework\Annotation\Parser\DataFixture::class,
89+
['annotation' => DataFixture::ANNOTATION]
90+
);
91+
return array_merge(
92+
parent::getParsers(),
93+
$parsers
94+
);
95+
}
7996
}

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

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -55,22 +55,19 @@ protected function _getFixtures(TestCase $test, $scope = null)
5555
'parsers' => $this->getParsers()
5656
]
5757
);
58-
$scopes = $scope ? [$scope] : [ParserInterface::SCOPE_METHOD, ParserInterface::SCOPE_CLASS];
59-
$fixtures = [];
60-
foreach ($scopes as $scp) {
61-
try {
62-
$fixtures = $parsers->parse($test, $scp);
63-
} catch (\Throwable $exception) {
64-
ExceptionHandler::handle(
65-
'Unable to parse fixtures',
66-
get_class($test),
67-
$test->getName(false),
68-
$exception
69-
);
70-
}
71-
if (!empty($fixtures)) {
72-
break;
58+
59+
try {
60+
$fixtures = $parsers->parse($test, $scope ?: ParserInterface::SCOPE_METHOD);
61+
if (!$fixtures && !$scope) {
62+
$fixtures = $parsers->parse($test, ParserInterface::SCOPE_CLASS);
7363
}
64+
} catch (\Throwable $exception) {
65+
ExceptionHandler::handle(
66+
'Unable to parse fixtures',
67+
get_class($test),
68+
$test->getName(false),
69+
$exception
70+
);
7471
}
7572

7673
/* Need to be applied even test does not have added fixtures because fixture can be added via config */
@@ -127,8 +124,8 @@ protected function _applyFixtures(array $fixtures, TestCase $test)
127124
} catch (\Throwable $exception) {
128125
ExceptionHandler::handle(
129126
'Unable to apply fixture: ' . $this->getFixtureReference($fixture),
130-
get_class($test),
131-
$test->getName(false),
127+
$fixture['test']['class'],
128+
$fixture['test']['method'],
132129
$exception
133130
);
134131
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ protected function _getTestAppArea($annotations)
8282
*/
8383
public function startTest(TestCase $test)
8484
{
85+
$values = [];
8586
try {
8687
$values = $this->parse($test);
8788
} catch (\Throwable $exception) {

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818

1919
class AppIsolation
2020
{
21-
private const ANNOTATION = 'magentoAppIsolation';
22-
2321
/**
2422
* Flag to prevent an excessive test case isolation if the last test has been just isolated
2523
*
@@ -87,6 +85,7 @@ public function endTestSuite()
8785
public function endTest(TestCase $test)
8886
{
8987
$this->hasNonIsolatedTests = true;
88+
$values = [];
9089
try {
9190
$values = $this->parse($test);
9291
} catch (\Throwable $exception) {
@@ -113,10 +112,10 @@ public function endTest(TestCase $test)
113112
* Returns AppIsolation fixtures configuration
114113
*
115114
* @param TestCase $test
116-
* @return array|mixed
115+
* @return array
117116
* @throws LocalizedException
118117
*/
119-
private function parse(TestCase $test): mixed
118+
private function parse(TestCase $test): array
120119
{
121120
$objectManager = Bootstrap::getObjectManager();
122121
$parsers = $objectManager

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

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
class Cache
1717
{
18-
public const ANNOTATION = 'magentoCache';
1918
/**
2019
* Original values for cache type states
2120
*
@@ -31,19 +30,17 @@ class Cache
3130
*/
3231
public function startTest(TestCase $test)
3332
{
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-
]
33+
$statusList = [];
34+
try {
35+
$statusList = $this->parse($test);
36+
} catch (\Throwable $exception) {
37+
ExceptionHandler::handle(
38+
'Unable to parse fixtures',
39+
get_class($test),
40+
$test->getName(false),
41+
$exception
4442
);
45-
$statusList = $parsers->parse($test, ParserInterface::SCOPE_METHOD)
46-
?: $parsers->parse($test, ParserInterface::SCOPE_CLASS);
43+
}
4744

4845
if ($statusList) {
4946
$values = [];
@@ -94,7 +91,11 @@ private function setValues($values, TestCase $test)
9491
$states = Bootstrap::getInstance()->getObjectManager()->get(\Magento\Framework\App\Cache\StateInterface::class);
9592
foreach ($values as $type => $isEnabled) {
9693
if (!isset($this->origValues[$type])) {
97-
self::fail("Unknown cache type specified: '{$type}' in @magentoCache", $test);
94+
ExceptionHandler::handle(
95+
"Unknown cache type specified: '{$type}' in @magentoCache",
96+
get_class($test),
97+
$test->getName(false)
98+
);
9899
}
99100
$states->setEnabled($type, $isEnabled);
100101
}
@@ -111,15 +112,26 @@ private static function getTypeList()
111112
}
112113

113114
/**
114-
* Fails the test with specified error message
115+
* Returns Cache fixtures configuration
115116
*
116-
* @param string $message
117117
* @param TestCase $test
118-
* @throws \Exception
118+
* @return array
119+
* @throws \Magento\Framework\Exception\LocalizedException
119120
*/
120-
private static function fail($message, TestCase $test)
121+
private function parse(TestCase $test): array
121122
{
122-
$test->fail("{$message} in the test '{$test->toString()}'");
123-
throw new \Exception('The above line was supposed to throw an exception.');
123+
$objectManager = Bootstrap::getObjectManager();
124+
$parsers = $objectManager
125+
->create(
126+
\Magento\TestFramework\Annotation\Parser\Composite::class,
127+
[
128+
'parsers' => [
129+
$objectManager->get(\Magento\TestFramework\Annotation\Parser\Cache::class),
130+
$objectManager->get(\Magento\TestFramework\Fixture\Parser\Cache::class)
131+
]
132+
]
133+
);
134+
return $parsers->parse($test, ParserInterface::SCOPE_METHOD)
135+
?: $parsers->parse($test, ParserInterface::SCOPE_CLASS);
124136
}
125137
}

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

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -82,21 +82,21 @@ public function endTest(\PHPUnit\Framework\TestCase $test)
8282
*/
8383
private function registerComponents(\PHPUnit\Framework\TestCase $test)
8484
{
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-
]
85+
$values = [];
86+
try {
87+
$values = $this->parse($test);
88+
} catch (\Throwable $exception) {
89+
ExceptionHandler::handle(
90+
'Unable to parse fixtures',
91+
get_class($test),
92+
$test->getName(false),
93+
$exception
9594
);
96-
$values = array_merge(
97-
$parsers->parse($test, ParserInterface::SCOPE_CLASS),
98-
$parsers->parse($test, ParserInterface::SCOPE_METHOD)
99-
);
95+
}
96+
if (!$values) {
97+
return;
98+
}
99+
100100
$componentAnnotations = array_unique(array_column($values, 'path'));
101101
$reflection = new \ReflectionClass(self::REGISTRAR_CLASS);
102102
$paths = $reflection->getProperty(self::PATHS_FIELD);
@@ -139,4 +139,30 @@ private function restoreComponents()
139139
$this->origComponents = null;
140140
}
141141
}
142+
143+
/**
144+
* Returns ComponentsDir fixtures configuration
145+
*
146+
* @param \PHPUnit\Framework\TestCase $test
147+
* @return array
148+
* @throws \Magento\Framework\Exception\LocalizedException
149+
*/
150+
private function parse(\PHPUnit\Framework\TestCase $test): array
151+
{
152+
$objectManager = Bootstrap::getObjectManager();
153+
$parsers = $objectManager
154+
->create(
155+
\Magento\TestFramework\Annotation\Parser\Composite::class,
156+
[
157+
'parsers' => [
158+
$objectManager->get(\Magento\TestFramework\Annotation\Parser\ComponentsDir::class),
159+
$objectManager->get(\Magento\TestFramework\Fixture\Parser\ComponentsDir::class)
160+
]
161+
]
162+
);
163+
return array_merge(
164+
$parsers->parse($test, ParserInterface::SCOPE_CLASS),
165+
$parsers->parse($test, ParserInterface::SCOPE_METHOD)
166+
);
167+
}
142168
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ public function initStoreAfter()
310310
*/
311311
private function getConfigFixturesFromAttribute(TestCase $test): array
312312
{
313-
$parser = Bootstrap::getObjectManager()->create(ConfigFixtureParser::class);
313+
$parser = Bootstrap::getObjectManager()->get(ConfigFixtureParser::class);
314314
$configs = [];
315315

316316
foreach ($parser->parse($test, ParserInterface::SCOPE_METHOD) as $data) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ protected function getAnnotation(): string
9494
protected function getParsers(): array
9595
{
9696
$parsers = [];
97-
$parsers[] = Bootstrap::getObjectManager()->create(
97+
$parsers[] = Bootstrap::getObjectManager()->get(
9898
\Magento\TestFramework\Fixture\Parser\DataFixture::class
9999
);
100100
return array_merge(

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,9 @@ public function rollbackTransaction()
8585
*/
8686
protected function _getIsolation(TestCase $test)
8787
{
88+
$state = null;
8889
try {
89-
return Bootstrap::getObjectManager()->get(DbIsolationState::class)->isEnabled($test);
90+
$state = Bootstrap::getObjectManager()->get(DbIsolationState::class)->isEnabled($test);
9091
} catch (\Throwable $exception) {
9192
ExceptionHandler::handle(
9293
'Unable to parse fixtures',
@@ -95,5 +96,6 @@ protected function _getIsolation(TestCase $test)
9596
$exception
9697
);
9798
}
99+
return $state;
98100
}
99101
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
class ExceptionHandler
1515
{
1616
/**
17+
* Format exception message and throws PHPUnit\Framework\Exception
18+
*
1719
* @param string $message
1820
* @param string $testClass
1921
* @param string|null $testMethod

0 commit comments

Comments
 (0)