Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ConfigFixture.php
Go to the documentation of this file.
1 <?php
11 
13 
20 {
26  protected $_currentTest;
27 
33  private $_globalConfigValues = [];
34 
40  private $_storeConfigValues = [];
41 
49  protected function _getConfigValue($configPath, $scopeCode = null)
50  {
52  $result = null;
53  if ($scopeCode !== false) {
55  $scopeConfig = $objectManager->get(\Magento\Framework\App\Config\ScopeConfigInterface::class);
56  $result = $scopeConfig->getValue(
57  $configPath,
58  \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
59  $scopeCode
60  );
61  }
62  return $result;
63  }
64 
72  protected function _setConfigValue($configPath, $value, $storeCode = false)
73  {
75  if ($storeCode === false) {
76  if (strpos($configPath, 'default/') === 0) {
77  $configPath = substr($configPath, 8);
78  $objectManager->get(
79  \Magento\Framework\App\Config\MutableScopeConfigInterface::class
80  )->setValue(
81  $configPath,
82  $value,
84  );
85  }
86  } else {
88  \Magento\Framework\App\Config\MutableScopeConfigInterface::class
89  )->setValue(
90  $configPath,
91  $value,
92  \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
94  );
95  }
96  }
97 
104  protected function _assignConfigData(\PHPUnit\Framework\TestCase $test)
105  {
106  $annotations = $test->getAnnotations();
107  if (!isset($annotations['method']['magentoConfigFixture'])) {
108  return;
109  }
110  foreach ($annotations['method']['magentoConfigFixture'] as $configPathAndValue) {
111  if (preg_match('/^.+?(?=_store\s)/', $configPathAndValue, $matches)) {
112  /* Store-scoped config value */
113  $storeCode = $matches[0] != 'current' ? $matches[0] : null;
114  $parts = preg_split('/\s+/', $configPathAndValue, 3);
115  list($configScope, $configPath, $requiredValue) = $parts + ['', '', ''];
116  $originalValue = $this->_getConfigValue($configPath, $storeCode);
117  $this->_storeConfigValues[$storeCode][$configPath] = $originalValue;
118  $this->_setConfigValue($configPath, $requiredValue, $storeCode);
119  } else {
120  /* Global config value */
121  list($configPath, $requiredValue) = preg_split('/\s+/', $configPathAndValue, 2);
122 
123  $originalValue = $this->_getConfigValue($configPath);
124  $this->_globalConfigValues[$configPath] = $originalValue;
125 
126  $this->_setConfigValue($configPath, $requiredValue);
127  }
128  }
129  }
130 
134  protected function _restoreConfigData()
135  {
136  /* Restore global values */
137  foreach ($this->_globalConfigValues as $configPath => $originalValue) {
138  $this->_setConfigValue($configPath, $originalValue);
139  }
140  $this->_globalConfigValues = [];
141 
142  /* Restore store-scoped values */
143  foreach ($this->_storeConfigValues as $storeCode => $originalData) {
144  foreach ($originalData as $configPath => $originalValue) {
145  if (empty($storeCode)) {
146  $storeCode = null;
147  }
148  $this->_setConfigValue($configPath, $originalValue, $storeCode);
149  }
150  }
151  $this->_storeConfigValues = [];
152  }
153 
159  public function startTest(\PHPUnit\Framework\TestCase $test)
160  {
161  $this->_currentTest = $test;
162  $this->_assignConfigData($test);
163  }
164 
172  public function endTest(\PHPUnit\Framework\TestCase $test)
173  {
174  $this->_currentTest = null;
175  $this->_restoreConfigData();
176  }
177 
181  public function initStoreAfter()
182  {
183  /* process events triggered from within a test only */
184  if ($this->_currentTest) {
185  $this->_assignConfigData($this->_currentTest);
186  }
187  }
188 }
_assignConfigData(\PHPUnit\Framework\TestCase $test)
$objectManager
Definition: bootstrap.php:17
$storeCode
Definition: indexer.php:15
$value
Definition: gender.phtml:16
endTest(\PHPUnit\Framework\TestCase $test)
_setConfigValue($configPath, $value, $storeCode=false)
startTest(\PHPUnit\Framework\TestCase $test)