|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright (c) 2019 TechDivision GmbH |
| 4 | + * All rights reserved |
| 5 | + * |
| 6 | + * This product includes proprietary software developed at TechDivision GmbH, Germany |
| 7 | + * For more information see https://www.techdivision.com/ |
| 8 | + * |
| 9 | + * To obtain a valid license for using this software please contact us at |
| 10 | + * license@techdivision.com |
| 11 | + */ |
| 12 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace Magento\TestFramework\Annotation; |
| 15 | + |
| 16 | +use Magento\Config\Model\Config; |
| 17 | +use Magento\Config\Model\ResourceModel\Config as ConfigResource; |
| 18 | +use Magento\Config\Model\ResourceModel\Config\Data\CollectionFactory; |
| 19 | +use Magento\TestFramework\Helper\Bootstrap; |
| 20 | +use Magento\Store\Model\StoreManagerInterface; |
| 21 | +use PHPUnit\Framework\TestCase; |
| 22 | + |
| 23 | +/** |
| 24 | + * @inheritDoc |
| 25 | + */ |
| 26 | +class ApiConfigFixture extends ConfigFixture |
| 27 | +{ |
| 28 | + /** |
| 29 | + * Original values for global configuration options that need to be restored |
| 30 | + * |
| 31 | + * @var array |
| 32 | + */ |
| 33 | + private $_globalConfigValues = []; |
| 34 | + |
| 35 | + /** |
| 36 | + * Original values for store-scoped configuration options that need to be restored |
| 37 | + * |
| 38 | + * @var array |
| 39 | + */ |
| 40 | + private $_storeConfigValues = []; |
| 41 | + |
| 42 | + /** |
| 43 | + * Values need to be deleted form the database |
| 44 | + * |
| 45 | + * @var array |
| 46 | + */ |
| 47 | + private $_valuesToDeleteFromDatabase = []; |
| 48 | + |
| 49 | + /** |
| 50 | + * Assign required config values and save original ones |
| 51 | + * |
| 52 | + * @param TestCase $test |
| 53 | + * @SuppressWarnings(PHPMD.UnusedLocalVariable) |
| 54 | + */ |
| 55 | + protected function _assignConfigData(TestCase $test) |
| 56 | + { |
| 57 | + $annotations = $test->getAnnotations(); |
| 58 | + if (!isset($annotations['method'][$this->annotation])) { |
| 59 | + return; |
| 60 | + } |
| 61 | + foreach ($annotations['method'][$this->annotation] as $configPathAndValue) { |
| 62 | + if (preg_match('/^.+?(?=_store\s)/', $configPathAndValue, $matches)) { |
| 63 | + /* Store-scoped config value */ |
| 64 | + $storeCode = $matches[0] != 'current' ? $matches[0] : null; |
| 65 | + $parts = preg_split('/\s+/', $configPathAndValue, 3); |
| 66 | + list($configScope, $configPath, $requiredValue) = $parts + ['', '', '']; |
| 67 | + $originalValue = $this->_getConfigValue($configPath, $storeCode); |
| 68 | + $this->_storeConfigValues[$storeCode][$configPath] = $originalValue; |
| 69 | + if ($this->checkIfValueExist($configPath, $storeCode)) { |
| 70 | + $this->_valuesToDeleteFromDatabase[$storeCode][$configPath] = $requiredValue; |
| 71 | + } |
| 72 | + $this->_setConfigValue($configPath, $requiredValue, $storeCode); |
| 73 | + } else { |
| 74 | + /* Global config value */ |
| 75 | + list($configPath, $requiredValue) = preg_split('/\s+/', $configPathAndValue, 2); |
| 76 | + |
| 77 | + $originalValue = $this->_getConfigValue($configPath); |
| 78 | + $this->_globalConfigValues[$configPath] = $originalValue; |
| 79 | + if ($this->checkIfValueExist($configPath)) { |
| 80 | + $this->_valuesToDeleteFromDatabase['global'][$configPath] = $requiredValue; |
| 81 | + } |
| 82 | + |
| 83 | + $this->_setConfigValue($configPath, $requiredValue); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Restore original values for changed config options |
| 90 | + */ |
| 91 | + protected function _restoreConfigData() |
| 92 | + { |
| 93 | + $configResource = Bootstrap::getObjectManager()->get(ConfigResource::class); |
| 94 | + |
| 95 | + /* Restore global values */ |
| 96 | + foreach ($this->_globalConfigValues as $configPath => $originalValue) { |
| 97 | + if (isset($this->_valuesToDeleteFromDatabase['global'][$configPath])) { |
| 98 | + $configResource->deleteConfig($configPath); |
| 99 | + } else { |
| 100 | + $this->_setConfigValue($configPath, $originalValue); |
| 101 | + } |
| 102 | + } |
| 103 | + $this->_globalConfigValues = []; |
| 104 | + |
| 105 | + /* Restore store-scoped values */ |
| 106 | + foreach ($this->_storeConfigValues as $storeCode => $originalData) { |
| 107 | + foreach ($originalData as $configPath => $originalValue) { |
| 108 | + if (empty($storeCode)) { |
| 109 | + $storeCode = null; |
| 110 | + } |
| 111 | + if (isset($this->_valuesToDeleteFromDatabase[$storeCode][$configPath])) { |
| 112 | + $scopeId = $this->getStoreIdByCode($storeCode); |
| 113 | + $configResource->deleteConfig($configPath, 'stores', $scopeId); |
| 114 | + } else { |
| 115 | + $this->_setConfigValue($configPath, $originalValue, $storeCode); |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + $this->_storeConfigValues = []; |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Load configs by path and scope |
| 124 | + * |
| 125 | + * @param string $configPath |
| 126 | + * @param string $storeCode |
| 127 | + * @return Config[] |
| 128 | + */ |
| 129 | + private function loadConfigs(string $configPath, string $storeCode = null): array |
| 130 | + { |
| 131 | + $configCollectionFactory = Bootstrap::getObjectManager()->get(CollectionFactory::class); |
| 132 | + $collection = $configCollectionFactory->create(); |
| 133 | + $scope = $storeCode ? 'stores' : 'default'; |
| 134 | + $scopeId = $storeCode ? $this->getStoreIdByCode($storeCode) : 0; |
| 135 | + |
| 136 | + $collection->addScopeFilter($scope, $scopeId, $configPath); |
| 137 | + return $collection->getItems(); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Check if config exist in the database |
| 142 | + * |
| 143 | + * @param string $configPath |
| 144 | + * @param $originalValue |
| 145 | + * @param string|null $storeCode |
| 146 | + */ |
| 147 | + private function checkIfValueExist(string $configPath, string $storeCode = null): bool |
| 148 | + { |
| 149 | + $configs = $this->loadConfigs($configPath, $storeCode); |
| 150 | + |
| 151 | + return !(bool)$configs; |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Returns the store ID by the store code |
| 156 | + * @param string $storeCode |
| 157 | + * @return int |
| 158 | + */ |
| 159 | + private function getStoreIdByCode(string $storeCode): int |
| 160 | + { |
| 161 | + $storeManager = Bootstrap::getObjectManager()->get(StoreManagerInterface::class); |
| 162 | + $store = $storeManager->getStore($storeCode); |
| 163 | + return (int)$store->getId(); |
| 164 | + } |
| 165 | +} |
0 commit comments