Skip to content

Commit ca85d70

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

26 files changed

+597
-189
lines changed

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

Lines changed: 17 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,10 @@ protected function _getFixtures(TestCase $test, $scope = null)
6161
try {
6262
$fixtures = $parsers->parse($test, $scp);
6363
} catch (\Throwable $exception) {
64-
throw new Exception(
65-
sprintf(
66-
"Unable to parse fixtures\n#0 %s",
67-
$this->getTestReference($this->getTestMetadata($test))
68-
),
64+
ExceptionHandler::handle(
65+
'Unable to parse fixtures',
66+
get_class($test),
67+
$test->getName(false),
6968
$exception
7069
);
7170
}
@@ -118,16 +117,18 @@ protected function _applyFixtures(array $fixtures, TestCase $test)
118117
if (is_callable([get_class($test), $fixture['factory']])) {
119118
$fixture['factory'] = get_class($test) . '::' . $fixture['factory'];
120119
}
121-
$fixture['test'] = $this->getTestMetadata($test);
120+
$fixture['test'] = [
121+
'class' => get_class($test),
122+
'method' => $test->getName(false),
123+
'dataSet' => $test->dataName(),
124+
];
122125
try {
123126
$fixture['result'] = $dataFixtureSetup->apply($fixture);
124127
} catch (\Throwable $exception) {
125-
throw new Exception(
126-
sprintf(
127-
"Unable to apply fixture: %s.\n#0 %s",
128-
$this->getFixtureReference($fixture),
129-
$this->getTestReference($fixture['test'])
130-
),
128+
ExceptionHandler::handle(
129+
'Unable to apply fixture: ' . $this->getFixtureReference($fixture),
130+
get_class($test),
131+
$test->getName(false),
131132
$exception
132133
);
133134
}
@@ -154,12 +155,10 @@ protected function _revertFixtures(?TestCase $test = null)
154155
try {
155156
$dataFixtureSetup->revert($fixture);
156157
} catch (\Throwable $exception) {
157-
throw new Exception(
158-
sprintf(
159-
"Unable to revert fixture: %s.\n#0 %s",
160-
$this->getFixtureReference($fixture),
161-
$this->getTestReference($fixture['test'])
162-
),
158+
ExceptionHandler::handle(
159+
'Unable to revert fixture: ' . $this->getFixtureReference($fixture),
160+
$fixture['test']['class'],
161+
$fixture['test']['method'],
163162
$exception
164163
);
165164
}
@@ -175,21 +174,6 @@ protected function _revertFixtures(?TestCase $test = null)
175174
}
176175
}
177176

178-
/**
179-
* Returns information about the class name
180-
*
181-
* @param TestCase $test
182-
* @return array
183-
*/
184-
private function getTestMetadata(TestCase $test): array
185-
{
186-
return [
187-
'class' => get_class($test),
188-
'method' => $test->getName(false),
189-
'dataSet' => $test->dataName(),
190-
];
191-
}
192-
193177
/**
194178
* Get reference to the fixture definition
195179
*
@@ -205,47 +189,6 @@ private function getFixtureReference(array $fixture): string
205189
);
206190
}
207191

208-
/**
209-
* Get reference to the test definition
210-
*
211-
* @param array $testMetadata
212-
* @return string
213-
*/
214-
private function getTestReference(array $testMetadata): string
215-
{
216-
try {
217-
$reflected = new ReflectionClass($testMetadata['class']);
218-
} catch (ReflectionException $e) {
219-
throw new \PHPUnit\Framework\Exception(
220-
$e->getMessage(),
221-
(int) $e->getCode(),
222-
$e
223-
);
224-
}
225-
226-
$name = $testMetadata['method'];
227-
228-
if ($name && $reflected->hasMethod($name)) {
229-
try {
230-
$reflected = $reflected->getMethod($name);
231-
} catch (ReflectionException $e) {
232-
throw new \PHPUnit\Framework\Exception(
233-
$e->getMessage(),
234-
(int) $e->getCode(),
235-
$e
236-
);
237-
}
238-
}
239-
240-
return sprintf(
241-
"%s(%d): %s->%s()",
242-
$reflected->getFileName(),
243-
$reflected->getStartLine(),
244-
$testMetadata['class'],
245-
$testMetadata['method']
246-
);
247-
}
248-
249192
/**
250193
* Return fixtures parser
251194
*

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

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,37 @@ protected function _getTestAppArea($annotations)
8181
* @throws LocalizedException
8282
*/
8383
public function startTest(TestCase $test)
84+
{
85+
try {
86+
$values = $this->parse($test);
87+
} catch (\Throwable $exception) {
88+
ExceptionHandler::handle(
89+
'Unable to parse fixtures',
90+
get_class($test),
91+
$test->getName(false),
92+
$exception
93+
);
94+
}
95+
96+
$area = $values[0]['area'] ?? Application::DEFAULT_APP_AREA;
97+
98+
if ($this->_application->getArea() !== $area) {
99+
$this->_application->reinitialize();
100+
101+
if ($this->_application->getArea() !== $area) {
102+
$this->_application->loadArea($area);
103+
}
104+
}
105+
}
106+
107+
/**
108+
* Returns AppArea fixtures configuration
109+
*
110+
* @param TestCase $test
111+
* @return array
112+
* @throws LocalizedException
113+
*/
114+
private function parse(TestCase $test): array
84115
{
85116
$objectManager = Bootstrap::getObjectManager();
86117
$parsers = $objectManager
@@ -101,15 +132,6 @@ public function startTest(TestCase $test)
101132
__('Only one "@magentoAppArea" annotation is allowed per test')
102133
);
103134
}
104-
105-
$area = $values[0]['area'] ?? Application::DEFAULT_APP_AREA;
106-
107-
if ($this->_application->getArea() !== $area) {
108-
$this->_application->reinitialize();
109-
110-
if ($this->_application->getArea() !== $area) {
111-
$this->_application->loadArea($area);
112-
}
113-
}
135+
return $values;
114136
}
115137
}

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

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,37 @@ public function endTestSuite()
8787
public function endTest(TestCase $test)
8888
{
8989
$this->hasNonIsolatedTests = true;
90+
try {
91+
$values = $this->parse($test);
92+
} catch (\Throwable $exception) {
93+
ExceptionHandler::handle(
94+
'Unable to parse fixtures',
95+
get_class($test),
96+
$test->getName(false),
97+
$exception
98+
);
99+
}
100+
if ($values) {
101+
$isIsolationEnabled = $values[0]['enabled'];
102+
} else {
103+
/* Controller tests should be isolated by default */
104+
$isIsolationEnabled = $test instanceof AbstractController;
105+
}
106+
107+
if ($isIsolationEnabled) {
108+
$this->_isolateApp();
109+
}
110+
}
111+
112+
/**
113+
* Returns AppIsolation fixtures configuration
114+
*
115+
* @param TestCase $test
116+
* @return array|mixed
117+
* @throws LocalizedException
118+
*/
119+
private function parse(TestCase $test): mixed
120+
{
90121
$objectManager = Bootstrap::getObjectManager();
91122
$parsers = $objectManager
92123
->create(
@@ -106,15 +137,6 @@ public function endTest(TestCase $test)
106137
__('Only one "@magentoAppIsolation" annotation is allowed per test')
107138
);
108139
}
109-
if ($values) {
110-
$isIsolationEnabled = $values[0]['enabled'];
111-
} else {
112-
/* Controller tests should be isolated by default */
113-
$isIsolationEnabled = $test instanceof AbstractController;
114-
}
115-
116-
if ($isIsolationEnabled) {
117-
$this->_isolateApp();
118-
}
140+
return $values;
119141
}
120142
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,10 @@ private function registerComponents(\PHPUnit\Framework\TestCase $test)
9393
]
9494
]
9595
);
96-
$values = $parsers->parse($test, ParserInterface::SCOPE_METHOD)
97-
?: $parsers->parse($test, ParserInterface::SCOPE_CLASS);
96+
$values = array_merge(
97+
$parsers->parse($test, ParserInterface::SCOPE_CLASS),
98+
$parsers->parse($test, ParserInterface::SCOPE_METHOD)
99+
);
98100
$componentAnnotations = array_unique(array_column($values, 'path'));
99101
$reflection = new \ReflectionClass(self::REGISTRAR_CLASS);
100102
$paths = $reflection->getProperty(self::PATHS_FIELD);

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,18 @@ public function rollbackTransaction()
8282
*
8383
* @param TestCase $test
8484
* @return bool|null Returns NULL, if isolation is not defined for the current scope
85-
* @throws LocalizedException
8685
*/
8786
protected function _getIsolation(TestCase $test)
8887
{
89-
return Bootstrap::getObjectManager()->get(DbIsolationState::class)->isEnabled($test);
88+
try {
89+
return Bootstrap::getObjectManager()->get(DbIsolationState::class)->isEnabled($test);
90+
} catch (\Throwable $exception) {
91+
ExceptionHandler::handle(
92+
'Unable to parse fixtures',
93+
get_class($test),
94+
$test->getName(false),
95+
$exception
96+
);
97+
}
9098
}
9199
}

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

Lines changed: 0 additions & 49 deletions
This file was deleted.

0 commit comments

Comments
 (0)