From 18a590fb52d1268d3db1c5348805a629bf6b28e4 Mon Sep 17 00:00:00 2001 From: Vinai Kopp Date: Tue, 26 Feb 2019 22:55:47 +0100 Subject: [PATCH 1/2] 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. --- .../TestFramework/Annotation/DataFixture.php | 11 ++---- .../DataFixtureBeforeTransaction.php | 39 ++++++++++++++++++- .../Test/Annotation/DataFixtureTest.php | 14 +++++++ 3 files changed, 55 insertions(+), 9 deletions(-) diff --git a/dev/tests/integration/framework/Magento/TestFramework/Annotation/DataFixture.php b/dev/tests/integration/framework/Magento/TestFramework/Annotation/DataFixture.php index f0ee8c7b3c2bb..ddebbf37b16d1 100644 --- a/dev/tests/integration/framework/Magento/TestFramework/Annotation/DataFixture.php +++ b/dev/tests/integration/framework/Magento/TestFramework/Annotation/DataFixture.php @@ -158,14 +158,9 @@ 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( @@ -173,7 +168,7 @@ private function getModulePath(string $fixture) ); } - return $modulePath . '/' . $fixtureFile; + return $modulePath . '/' . ltrim($fixtureFile, '/'); } /** diff --git a/dev/tests/integration/framework/Magento/TestFramework/Annotation/DataFixtureBeforeTransaction.php b/dev/tests/integration/framework/Magento/TestFramework/Annotation/DataFixtureBeforeTransaction.php index db7f57362d807..0d4f652823c45 100644 --- a/dev/tests/integration/framework/Magento/TestFramework/Annotation/DataFixtureBeforeTransaction.php +++ b/dev/tests/integration/framework/Magento/TestFramework/Annotation/DataFixtureBeforeTransaction.php @@ -5,10 +5,11 @@ */ /** - * Implementation of the @magentoDataFixture DocBlock annotation + * Implementation of the @magentoDataFixtureBeforeTransaction DocBlock annotation */ namespace Magento\TestFramework\Annotation; +use Magento\Framework\Component\ComponentRegistrar; use PHPUnit\Framework\Exception; class DataFixtureBeforeTransaction @@ -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; } @@ -101,6 +104,40 @@ protected function _getFixtures(\PHPUnit\Framework\TestCase $test, $scope = null return $result; } + /** + * 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, '/'); + } + /** * @param \PHPUnit\Framework\TestCase $test * @return array diff --git a/dev/tests/integration/framework/tests/unit/testsuite/Magento/Test/Annotation/DataFixtureTest.php b/dev/tests/integration/framework/tests/unit/testsuite/Magento/Test/Annotation/DataFixtureTest.php index 22240ad8e1fe9..00af4419e1142 100644 --- a/dev/tests/integration/framework/tests/unit/testsuite/Magento/Test/Annotation/DataFixtureTest.php +++ b/dev/tests/integration/framework/tests/unit/testsuite/Magento/Test/Annotation/DataFixtureTest.php @@ -5,6 +5,8 @@ */ namespace Magento\Test\Annotation; +use Magento\Framework\Component\ComponentRegistrar; + /** * Test class for \Magento\TestFramework\Annotation\DataFixture. * @@ -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); + } } From 749551f939430907a95f0d72ac49ea20ad9a781f Mon Sep 17 00:00:00 2001 From: nmalevanec Date: Wed, 27 Mar 2019 10:06:35 +0200 Subject: [PATCH 2/2] Fix static tests. --- .../Annotation/DataFixtureBeforeTransaction.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/dev/tests/integration/framework/Magento/TestFramework/Annotation/DataFixtureBeforeTransaction.php b/dev/tests/integration/framework/Magento/TestFramework/Annotation/DataFixtureBeforeTransaction.php index 0d4f652823c45..ba23ecd13b6a7 100644 --- a/dev/tests/integration/framework/Magento/TestFramework/Annotation/DataFixtureBeforeTransaction.php +++ b/dev/tests/integration/framework/Magento/TestFramework/Annotation/DataFixtureBeforeTransaction.php @@ -4,14 +4,14 @@ * See COPYING.txt for license details. */ -/** - * Implementation of the @magentoDataFixtureBeforeTransaction DocBlock annotation - */ namespace Magento\TestFramework\Annotation; use Magento\Framework\Component\ComponentRegistrar; use PHPUnit\Framework\Exception; +/** + * Implementation of the @magentoDataFixtureBeforeTransaction DocBlock annotation + */ class DataFixtureBeforeTransaction { /** @@ -139,6 +139,8 @@ private function getModulePath(string $fixture) } /** + * Get annotations for test. + * * @param \PHPUnit\Framework\TestCase $test * @return array */