Skip to content

Commit 18a590f

Browse files
committed
Allow module data fixtures for @magentoDataFixtureBeforeTransaction annotations
Extend the option to specify a module based data fixture files to the @magentoDataFixtureBeforeTransaction annotation. Previously they where only available for the @magentoDataFixture annotation. The syntax is the same: @magentoDataFixtureBeforeAnnotation Foo_DataFixtureDummy::Test/Integration/foo.php There is no backward compatibility break associated with this commit.
1 parent 697978e commit 18a590f

File tree

3 files changed

+55
-9
lines changed

3 files changed

+55
-9
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: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
*/
66

77
/**
8-
* Implementation of the @magentoDataFixture DocBlock annotation
8+
* Implementation of the @magentoDataFixtureBeforeTransaction DocBlock annotation
99
*/
1010
namespace Magento\TestFramework\Annotation;
1111

12+
use Magento\Framework\Component\ComponentRegistrar;
1213
use PHPUnit\Framework\Exception;
1314

1415
class DataFixtureBeforeTransaction
@@ -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
}
@@ -101,6 +104,40 @@ protected function _getFixtures(\PHPUnit\Framework\TestCase $test, $scope = null
101104
return $result;
102105
}
103106

107+
/**
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+
104141
/**
105142
* @param \PHPUnit\Framework\TestCase $test
106143
* @return array

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)