Skip to content

Commit

Permalink
ENGCOM-4384: Module data fixtures for @magentoDataFixtureBeforeTransa…
Browse files Browse the repository at this point in the history
…ction annotations #21465
  • Loading branch information
sivaschenko authored Mar 28, 2019
2 parents df34818 + 3efa4f3 commit 5429f22
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 11 deletions.
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);
}
}

0 comments on commit 5429f22

Please sign in to comment.