Skip to content

Commit 5429f22

Browse files
authored
ENGCOM-4384: Module data fixtures for @magentoDataFixtureBeforeTransaction annotations #21465
2 parents df34818 + 3efa4f3 commit 5429f22

File tree

3 files changed

+59
-11
lines changed

3 files changed

+59
-11
lines changed

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -158,22 +158,17 @@ private function isModuleAnnotation(string $fixture)
158158
*/
159159
private function getModulePath(string $fixture)
160160
{
161-
$fixturePathParts = explode('::', $fixture, 2);
162-
$moduleName = $fixturePathParts[0];
163-
$fixtureFile = $fixturePathParts[1];
161+
[$moduleName, $fixtureFile] = explode('::', $fixture, 2);
164162

165-
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
166-
/** @var ComponentRegistrar $componentRegistrar */
167-
$componentRegistrar = $objectManager->get(ComponentRegistrar::class);
168-
$modulePath = $componentRegistrar->getPath(ComponentRegistrar::MODULE, $moduleName);
163+
$modulePath = (new ComponentRegistrar())->getPath(ComponentRegistrar::MODULE, $moduleName);
169164

170165
if ($modulePath === null) {
171166
throw new \Magento\Framework\Exception\LocalizedException(
172167
new \Magento\Framework\Phrase('Can\'t find registered Module with name %1 .', [$moduleName])
173168
);
174169
}
175170

176-
return $modulePath . '/' . $fixtureFile;
171+
return $modulePath . '/' . ltrim($fixtureFile, '/');
177172
}
178173

179174
/**

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

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44
* See COPYING.txt for license details.
55
*/
66

7-
/**
8-
* Implementation of the @magentoDataFixture DocBlock annotation
9-
*/
107
namespace Magento\TestFramework\Annotation;
118

9+
use Magento\Framework\Component\ComponentRegistrar;
1210
use PHPUnit\Framework\Exception;
1311

12+
/**
13+
* Implementation of the @magentoDataFixtureBeforeTransaction DocBlock annotation
14+
*/
1415
class DataFixtureBeforeTransaction
1516
{
1617
/**
@@ -93,6 +94,8 @@ protected function _getFixtures(\PHPUnit\Framework\TestCase $test, $scope = null
9394
$fixtureMethod = [get_class($test), $fixture];
9495
if (is_callable($fixtureMethod)) {
9596
$result[] = $fixtureMethod;
97+
} elseif ($this->isModuleAnnotation($fixture)) {
98+
$result[] = $this->getModulePath($fixture);
9699
} else {
97100
$result[] = $this->_fixtureBaseDir . '/' . $fixture;
98101
}
@@ -102,6 +105,42 @@ protected function _getFixtures(\PHPUnit\Framework\TestCase $test, $scope = null
102105
}
103106

104107
/**
108+
* Check is the Annotation like Magento_InventoryApi::Test/_files/products.php
109+
*
110+
* @param string $fixture
111+
* @return bool
112+
*/
113+
private function isModuleAnnotation(string $fixture)
114+
{
115+
return (strpos($fixture, '::') !== false);
116+
}
117+
118+
/**
119+
* Resolve the Fixture
120+
*
121+
* @param string $fixture
122+
* @return string
123+
* @throws \Magento\Framework\Exception\LocalizedException
124+
* @SuppressWarnings(PHPMD.StaticAccess)
125+
*/
126+
private function getModulePath(string $fixture)
127+
{
128+
[$moduleName, $fixtureFile] = explode('::', $fixture, 2);
129+
130+
$modulePath = (new ComponentRegistrar())->getPath(ComponentRegistrar::MODULE, $moduleName);
131+
132+
if ($modulePath === null) {
133+
throw new \Magento\Framework\Exception\LocalizedException(
134+
new \Magento\Framework\Phrase('Can\'t find registered Module with name %1 .', [$moduleName])
135+
);
136+
}
137+
138+
return $modulePath . '/' . ltrim($fixtureFile, '/');
139+
}
140+
141+
/**
142+
* Get annotations for test.
143+
*
105144
* @param \PHPUnit\Framework\TestCase $test
106145
* @return array
107146
*/

dev/tests/integration/framework/tests/unit/testsuite/Magento/Test/Annotation/DataFixtureTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*/
66
namespace Magento\Test\Annotation;
77

8+
use Magento\Framework\Component\ComponentRegistrar;
9+
810
/**
911
* Test class for \Magento\TestFramework\Annotation\DataFixture.
1012
*
@@ -178,4 +180,16 @@ public function testRollbackTransactionRevertFixtureFile()
178180
);
179181
$this->_object->rollbackTransaction();
180182
}
183+
184+
/**
185+
* @magentoDataFixture Foo_DataFixtureDummy::Test/Integration/foo.php
186+
* @SuppressWarnings(PHPMD.StaticAccess)
187+
*/
188+
public function testModuleDataFixture()
189+
{
190+
ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Foo_DataFixtureDummy', __DIR__);
191+
$this->_object->expects($this->once())->method('_applyOneFixture')
192+
->with(__DIR__ . '/Test/Integration/foo.php');
193+
$this->_object->startTransaction($this);
194+
}
181195
}

0 commit comments

Comments
 (0)