Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Module data fixtures for @magentoDataFixtureBeforeTransaction annotations #21465

Merged
merged 3 commits into from
Mar 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -158,22 +158,17 @@ private function isModuleAnnotation(string $fixture)
*/
private function getModulePath(string $fixture)
{
$fixturePathParts = explode('::', $fixture, 2);
$moduleName = $fixturePathParts[0];
$fixtureFile = $fixturePathParts[1];
[$moduleName, $fixtureFile] = explode('::', $fixture, 2);

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

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

return $modulePath . '/' . $fixtureFile;
return $modulePath . '/' . ltrim($fixtureFile, '/');
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
* See COPYING.txt for license details.
*/

/**
* Implementation of the @magentoDataFixture DocBlock annotation
*/
namespace Magento\TestFramework\Annotation;

use Magento\Framework\Component\ComponentRegistrar;
use PHPUnit\Framework\Exception;

/**
* Implementation of the @magentoDataFixtureBeforeTransaction DocBlock annotation
*/
class DataFixtureBeforeTransaction
{
/**
Expand Down Expand Up @@ -93,6 +94,8 @@ protected function _getFixtures(\PHPUnit\Framework\TestCase $test, $scope = null
$fixtureMethod = [get_class($test), $fixture];
if (is_callable($fixtureMethod)) {
$result[] = $fixtureMethod;
} elseif ($this->isModuleAnnotation($fixture)) {
$result[] = $this->getModulePath($fixture);
} else {
$result[] = $this->_fixtureBaseDir . '/' . $fixture;
}
Expand All @@ -102,6 +105,42 @@ protected function _getFixtures(\PHPUnit\Framework\TestCase $test, $scope = null
}

/**
* Check is the Annotation like Magento_InventoryApi::Test/_files/products.php
*
* @param string $fixture
* @return bool
*/
private function isModuleAnnotation(string $fixture)
{
return (strpos($fixture, '::') !== false);
}

/**
* Resolve the Fixture
*
* @param string $fixture
* @return string
* @throws \Magento\Framework\Exception\LocalizedException
* @SuppressWarnings(PHPMD.StaticAccess)
*/
private function getModulePath(string $fixture)
{
[$moduleName, $fixtureFile] = explode('::', $fixture, 2);

$modulePath = (new ComponentRegistrar())->getPath(ComponentRegistrar::MODULE, $moduleName);

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

return $modulePath . '/' . ltrim($fixtureFile, '/');
}

/**
* Get annotations for test.
*
* @param \PHPUnit\Framework\TestCase $test
* @return array
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*/
namespace Magento\Test\Annotation;

use Magento\Framework\Component\ComponentRegistrar;

/**
* Test class for \Magento\TestFramework\Annotation\DataFixture.
*
Expand Down Expand Up @@ -178,4 +180,16 @@ public function testRollbackTransactionRevertFixtureFile()
);
$this->_object->rollbackTransaction();
}

/**
* @magentoDataFixture Foo_DataFixtureDummy::Test/Integration/foo.php
* @SuppressWarnings(PHPMD.StaticAccess)
*/
public function testModuleDataFixture()
{
ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Foo_DataFixtureDummy', __DIR__);
$this->_object->expects($this->once())->method('_applyOneFixture')
->with(__DIR__ . '/Test/Integration/foo.php');
$this->_object->startTransaction($this);
}
}