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!