In Magento 2, the magentoConfigFixture
annotation is used in integration tests to provide a set of configuration values that should be applied during the test. This annotation is useful for testing scenarios where you need to manipulate the configuration settings temporarily. The annotation is typically applied at the class level and provides a clean way to set configuration values specifically for the scope of the test.
Here’s an example of how you can use the magentoConfigFixture
annotation in a Magento 2 integration test:
// app/code/Vendor/Module/Test/Integration/ConfigTest.php
namespace Vendor\Module\Test\Integration;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\AbstractController;
use Magento\Framework\App\Config\ScopeConfigInterface;
/**
* @magentoConfigFixture current_store general/locale/code en_US
* @magentoConfigFixture current_store general/country/default US
*/
class ConfigTest extends AbstractController
{
/**
* @var ScopeConfigInterface
*/
private $scopeConfig;
protected function setUp(): void
{
parent::setUp();
$this->scopeConfig = Bootstrap::getObjectManager()->get(ScopeConfigInterface::class);
}
/**
* Test the configuration values.
*/
public function testConfigValues()
{
$localeCode = $this->scopeConfig->getValue('general/locale/code');
$defaultCountry = $this->scopeConfig->getValue('general/country/default');
$this->assertEquals('en_US', $localeCode);
$this->assertEquals('US', $defaultCountry);
}
}
In this example:
- The
@magentoConfigFixture
annotation is applied at the class level, setting two configuration values (general/locale/code
andgeneral/country/default
) for the current store scope. - The
setUp
method initializes the$scopeConfig
instance for later use in the test. - The
testConfigValues
method checks if the configuration values set in the annotation are applied correctly.
Remember to replace Vendor\Module
with your actual module namespace. Additionally, you can modify the annotation to set other configuration values as needed for your specific test scenario.
Before running the test, ensure that your Magento environment is set up correctly, and you may need to clear the cache for the changes to take effect. You can run the test using the following command:
vendor/bin/phpunit -c dev/tests/integration/phpunit.xml.dist app/code/Vendor/Module/Test/Integration/ConfigTest.php
This example assumes you have set up the necessary test environment, and PHPUnit is available in your Magento project. Adjust the paths and commands based on your project structure and configuration.
Comments