5

For integration tests purposes I need to set a different config value to a single config directive. However the configInterface always returns the value which was set as the first one.

Code of the test:

class FooTest extends AbstractTestCase
{

    public function testFoo()
    {
        $this->setConfig('cataloginventory/options/show_out_of_stock', 0);

        ...
    }

    public function testBar()
    {
        $this->setConfig('cataloginventory/options/show_out_of_stock', 1);

        ...
    }

    private function setConfig($path, $value)
    {
        /** @var \Magento\Config\Model\ResourceModel\Config $model */
        $model = $this->getObjectManager()->create('Magento\Config\Model\ResourceModel\Config');
        $model->saveConfig($path, $value, 'default', 0);

        /** @var \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList */
        $cacheTypeList = $this->getObjectManager()->create('Magento\Framework\App\Cache\TypeListInterface');
        $cacheTypeList->cleanType('config');
    }

    /** @return \Magento\Framework\ObjectManagerInterface */
    protected function getObjectManager()
    {
        return Bootstrap::getObjectManager();
    }

}

Code of the config value fetching within my extension:

class ConfigHelper
{
    private $configInterface;

    public function __construct(Magento\Framework\App\Config\ScopeConfigInterface $configInterface)
    {
        $this->configInterface = $configInterface;
    }

    public function getShowOutOfStock($storeId = null)
    {
        return (bool) $this->configInterface->getValue('cataloginventory/options/show_out_of_stock', ScopeInterface::SCOPE_STORE, $storeId);
    }
}

The value fetched by ConfigHelper is always the one which is set in testFoo() method. The value set in testBar() method is saved to DB, but value returned by getShowOutOfStock() is always the first one.

I'm wondering how to modify the setConfig() method to invalidate the internal Magento cache and to fetch the real value.

Any help appreciated, thanks!

1 Answer 1

2

The Magento integration test framework provides an annotation for config values, which is sufficient in most cases.

/**
 * @magentoConfigFixture current_store cataloginventory/options/show_out_of_stock 0
 */
public function testFoo()

/**
 * @magentoConfigFixture current_store cataloginventory/options/show_out_of_stock 1
 */
public function testBar()

You can also set the value for a specific store, or the default value, as I described here: https://www.schmengler-se.de/en/2016/09/magento2-integration-tests-magentoconfigfixture/

Set default value 42 for configuration path x/y/z:

/**
 * @magentoConfigFixture default/x/y/z 42
 */

Set store specific value 42 for configuration path x/y/z in store with code store1

/**
 * @magentoConfigFixture store1_store x/y/z 42
 */

Set store specific value 42 for configuration path x/y/z in current store (i.e. default store)

/**
 * @magentoConfigFixture current_store x/y/z 42
 */

These are all possible formats. The first parameter must end with _store or be ommitted. And if it is omitted, the path must start with default/, otherwise it is ignored.

1
  • Thanks Fabian, works like a charm! However when I used @magentoConfigFixture annotation to set show_out_of_stock value, other config values set previously with setConfig() value don't work anymore - return NULL. Anyway, I updated the setConfig method with code from the fixture and it works great now. Thanks again!
    – Jan Petr
    Commented Mar 12, 2017 at 17:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.