123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- /**
- * Implementation of the @magentoConfigFixture DocBlock annotation
- */
- namespace Magento\TestFramework\Annotation;
- use Magento\Framework\App\Config\ScopeConfigInterface;
- /**
- * Handler which works with magentoConfigFixture annotations
- *
- * @package Magento\TestFramework\Annotation
- */
- class ConfigFixture
- {
- /**
- * Test instance that is available between 'startTest' and 'stopTest' events
- *
- * @var \PHPUnit\Framework\TestCase
- */
- protected $_currentTest;
- /**
- * Original values for global configuration options that need to be restored
- *
- * @var array
- */
- private $_globalConfigValues = [];
- /**
- * Original values for store-scoped configuration options that need to be restored
- *
- * @var array
- */
- private $_storeConfigValues = [];
- /**
- * Retrieve configuration node value
- *
- * @param string $configPath
- * @param string|bool|null $scopeCode
- * @return string
- */
- protected function _getConfigValue($configPath, $scopeCode = null)
- {
- $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
- $result = null;
- if ($scopeCode !== false) {
- /** @var \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig */
- $scopeConfig = $objectManager->get(\Magento\Framework\App\Config\ScopeConfigInterface::class);
- $result = $scopeConfig->getValue(
- $configPath,
- \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
- $scopeCode
- );
- }
- return $result;
- }
- /**
- * Assign configuration node value
- *
- * @param string $configPath
- * @param string $value
- * @param string|bool|null $storeCode
- */
- protected function _setConfigValue($configPath, $value, $storeCode = false)
- {
- $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
- if ($storeCode === false) {
- if (strpos($configPath, 'default/') === 0) {
- $configPath = substr($configPath, 8);
- $objectManager->get(
- \Magento\Framework\App\Config\MutableScopeConfigInterface::class
- )->setValue(
- $configPath,
- $value,
- ScopeConfigInterface::SCOPE_TYPE_DEFAULT
- );
- }
- } else {
- \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
- \Magento\Framework\App\Config\MutableScopeConfigInterface::class
- )->setValue(
- $configPath,
- $value,
- \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
- $storeCode
- );
- }
- }
- /**
- * Assign required config values and save original ones
- *
- * @param \PHPUnit\Framework\TestCase $test
- * @SuppressWarnings(PHPMD.UnusedLocalVariable)
- */
- protected function _assignConfigData(\PHPUnit\Framework\TestCase $test)
- {
- $annotations = $test->getAnnotations();
- if (!isset($annotations['method']['magentoConfigFixture'])) {
- return;
- }
- foreach ($annotations['method']['magentoConfigFixture'] as $configPathAndValue) {
- if (preg_match('/^.+?(?=_store\s)/', $configPathAndValue, $matches)) {
- /* Store-scoped config value */
- $storeCode = $matches[0] != 'current' ? $matches[0] : null;
- $parts = preg_split('/\s+/', $configPathAndValue, 3);
- list($configScope, $configPath, $requiredValue) = $parts + ['', '', ''];
- $originalValue = $this->_getConfigValue($configPath, $storeCode);
- $this->_storeConfigValues[$storeCode][$configPath] = $originalValue;
- $this->_setConfigValue($configPath, $requiredValue, $storeCode);
- } else {
- /* Global config value */
- list($configPath, $requiredValue) = preg_split('/\s+/', $configPathAndValue, 2);
- $originalValue = $this->_getConfigValue($configPath);
- $this->_globalConfigValues[$configPath] = $originalValue;
- $this->_setConfigValue($configPath, $requiredValue);
- }
- }
- }
- /**
- * Restore original values for changed config options
- */
- protected function _restoreConfigData()
- {
- /* Restore global values */
- foreach ($this->_globalConfigValues as $configPath => $originalValue) {
- $this->_setConfigValue($configPath, $originalValue);
- }
- $this->_globalConfigValues = [];
- /* Restore store-scoped values */
- foreach ($this->_storeConfigValues as $storeCode => $originalData) {
- foreach ($originalData as $configPath => $originalValue) {
- if (empty($storeCode)) {
- $storeCode = null;
- }
- $this->_setConfigValue($configPath, $originalValue, $storeCode);
- }
- }
- $this->_storeConfigValues = [];
- }
- /**
- * Handler for 'startTest' event
- *
- * @param \PHPUnit\Framework\TestCase $test
- */
- public function startTest(\PHPUnit\Framework\TestCase $test)
- {
- $this->_currentTest = $test;
- $this->_assignConfigData($test);
- }
- /**
- * Handler for 'endTest' event
- *
- * @param \PHPUnit\Framework\TestCase $test
- *
- * @SuppressWarnings(PHPMD.UnusedFormalParameter)
- */
- public function endTest(\PHPUnit\Framework\TestCase $test)
- {
- $this->_currentTest = null;
- $this->_restoreConfigData();
- }
- /**
- * Reassign configuration data whenever application is reset
- */
- public function initStoreAfter()
- {
- /* process events triggered from within a test only */
- if ($this->_currentTest) {
- $this->_assignConfigData($this->_currentTest);
- }
- }
- }
|