7
7
8
8
namespace Magento \TestFramework \Annotation ;
9
9
10
- use Magento \Config \Model \Config \ Factory as ConfigFactory ;
10
+ use Magento \Config \Model \ResourceModel \ Config as ConfigResource ;
11
11
use Magento \Framework \App \Config \MutableScopeConfigInterface ;
12
12
use Magento \Framework \App \Config \ScopeConfigInterface ;
13
- use Magento \Store \Api \StoreRepositoryInterface ;
14
- use Magento \Store \Api \WebsiteRepositoryInterface ;
15
13
use Magento \Store \Model \ScopeInterface ;
14
+ use Magento \Store \Model \StoreManagerInterface ;
16
15
use Magento \TestFramework \App \ApiMutableScopeConfig ;
17
16
use Magento \TestFramework \Config \Model \ConfigStorage ;
18
17
use Magento \TestFramework \Helper \Bootstrap ;
23
22
class ApiConfigFixture extends ConfigFixture
24
23
{
25
24
/**
26
- * Values are inherited
25
+ * Values need to be deleted form the database
27
26
*
28
27
* @var array
29
28
*/
30
- private $ valuesNotFromDatabase = [];
29
+ private $ valuesToDeleteFromDatabase = [];
31
30
32
31
/**
33
32
* @inheritdoc
@@ -41,7 +40,7 @@ protected function setStoreConfigValue(array $matches, $configPathAndValue): voi
41
40
/** @var ConfigStorage $configStorage */
42
41
$ configStorage = Bootstrap::getObjectManager ()->get (ConfigStorage::class);
43
42
if (!$ configStorage ->checkIsRecordExist ($ configPath , ScopeInterface::SCOPE_STORES , $ storeCode )) {
44
- $ this ->valuesNotFromDatabase [$ storeCode ][$ configPath ?? '' ] = $ requiredValue ?? '' ;
43
+ $ this ->valuesToDeleteFromDatabase [$ storeCode ][$ configPath ?? '' ] = $ requiredValue ?? '' ;
45
44
}
46
45
47
46
parent ::setStoreConfigValue ($ matches , $ configPathAndValue );
@@ -57,7 +56,7 @@ protected function setGlobalConfigValue($configPathAndValue): void
57
56
/** @var ConfigStorage $configStorage */
58
57
$ configStorage = Bootstrap::getObjectManager ()->get (ConfigStorage::class);
59
58
if (!$ configStorage ->checkIsRecordExist ($ configPath )) {
60
- $ this ->valuesNotFromDatabase ['global ' ][$ configPath ] = $ requiredValue ;
59
+ $ this ->valuesToDeleteFromDatabase ['global ' ][$ configPath ] = $ requiredValue ;
61
60
}
62
61
63
62
$ originalValue = $ this ->getScopeConfigValue ($ configPath , ScopeConfigInterface::SCOPE_TYPE_DEFAULT );
@@ -77,7 +76,7 @@ protected function setWebsiteConfigValue(array $matches, $configPathAndValue): v
77
76
/** @var ConfigStorage $configStorage */
78
77
$ configStorage = Bootstrap::getObjectManager ()->get (ConfigStorage::class);
79
78
if (!$ configStorage ->checkIsRecordExist ($ configPath , ScopeInterface::SCOPE_WEBSITES , $ websiteCode )) {
80
- $ this ->valuesNotFromDatabase [$ websiteCode ][$ configPath ?? '' ] = $ requiredValue ?? '' ;
79
+ $ this ->valuesToDeleteFromDatabase [$ websiteCode ][$ configPath ?? '' ] = $ requiredValue ?? '' ;
81
80
}
82
81
83
82
parent ::setWebsiteConfigValue ($ matches , $ configPathAndValue );
@@ -89,15 +88,12 @@ protected function setWebsiteConfigValue(array $matches, $configPathAndValue): v
89
88
*/
90
89
protected function _restoreConfigData ()
91
90
{
91
+ /** @var ConfigResource $configResource */
92
+ $ configResource = Bootstrap::getObjectManager ()->get (ConfigResource::class);
92
93
/* Restore global values */
93
94
foreach ($ this ->globalConfigValues as $ configPath => $ originalValue ) {
94
- if (isset ($ this ->valuesNotFromDatabase ['global ' ][$ configPath ])) {
95
- $ this ->inheritConfig (
96
- $ configPath ,
97
- $ originalValue ,
98
- ScopeConfigInterface::SCOPE_TYPE_DEFAULT ,
99
- ScopeConfigInterface::SCOPE_TYPE_DEFAULT
100
- );
95
+ if (isset ($ this ->valuesToDeleteFromDatabase ['global ' ][$ configPath ])) {
96
+ $ configResource ->deleteConfig ($ configPath );
101
97
} else {
102
98
$ this ->_setConfigValue ($ configPath , $ originalValue );
103
99
}
@@ -107,13 +103,9 @@ protected function _restoreConfigData()
107
103
foreach ($ this ->storeConfigValues as $ storeCode => $ originalData ) {
108
104
foreach ($ originalData as $ configPath => $ originalValue ) {
109
105
$ storeCode = $ storeCode ?: null ;
110
- if (isset ($ this ->valuesNotFromDatabase [$ storeCode ][$ configPath ])) {
111
- $ this ->inheritConfig (
112
- $ configPath ,
113
- (string )$ originalValue ,
114
- ScopeInterface::SCOPE_STORES ,
115
- $ storeCode
116
- );
106
+ if (isset ($ this ->valuesToDeleteFromDatabase [$ storeCode ][$ configPath ])) {
107
+ $ scopeId = $ this ->getIdByScopeType (ScopeInterface::SCOPE_STORES , $ storeCode );
108
+ $ configResource ->deleteConfig ($ configPath , ScopeInterface::SCOPE_STORES , $ scopeId );
117
109
} else {
118
110
$ this ->setScopeConfigValue (
119
111
$ configPath ,
@@ -129,13 +121,9 @@ protected function _restoreConfigData()
129
121
foreach ($ this ->websiteConfigValues as $ websiteCode => $ originalData ) {
130
122
foreach ($ originalData as $ configPath => $ originalValue ) {
131
123
$ websiteCode = $ websiteCode ?: null ;
132
- if (isset ($ this ->valuesNotFromDatabase [$ websiteCode ][$ configPath ])) {
133
- $ this ->inheritConfig (
134
- $ configPath ,
135
- $ originalValue ,
136
- ScopeInterface::SCOPE_WEBSITES ,
137
- $ websiteCode
138
- );
124
+ if (isset ($ this ->valuesToDeleteFromDatabase [$ websiteCode ][$ configPath ])) {
125
+ $ scopeId = $ this ->getIdByScopeType (ScopeInterface::SCOPE_WEBSITES , $ websiteCode );
126
+ $ configResource ->deleteConfig ($ configPath , ScopeInterface::SCOPE_WEBSITES , $ scopeId );
139
127
} else {
140
128
$ this ->setScopeConfigValue (
141
129
$ configPath ,
@@ -171,47 +159,28 @@ protected function getScopeConfigValue(string $configPath, string $scopeType, st
171
159
}
172
160
173
161
/**
174
- * Inherit the config and remove the config from database
162
+ * Get id by code
175
163
*
176
- * @param string $path
177
- * @param string $value
178
164
* @param string $scopeType
179
- * @param string|null $scopeCode
180
- * @return void
165
+ * @param string|null $scopeId
166
+ * @return int
181
167
*/
182
- private function inheritConfig (
183
- string $ path ,
184
- ?string $ value ,
185
- string $ scopeType ,
186
- ?string $ scopeCode
187
- ) {
188
- $ pathParts = explode ('/ ' , $ path );
189
- $ store = 0 ;
190
- $ configData = [
191
- 'section ' => $ pathParts [0 ],
192
- 'website ' => '' ,
193
- 'store ' => $ store ,
194
- 'groups ' => [
195
- $ pathParts [1 ] => [
196
- 'fields ' => [
197
- $ pathParts [2 ] => [
198
- 'value ' => $ value ,
199
- 'inherit ' => 1
200
- ]
201
- ]
202
- ]
203
- ]
204
- ];
205
- $ objectManager = Bootstrap::getObjectManager ();
206
- if ($ scopeType === ScopeInterface::SCOPE_STORES && $ scopeCode !== null ) {
207
- $ store = $ objectManager ->get (StoreRepositoryInterface::class)->get ($ scopeCode )->getId ();
208
- $ configData ['store ' ] = $ store ;
209
- } elseif ($ scopeType === ScopeInterface::SCOPE_WEBSITES && $ scopeCode !== null ) {
210
- $ website = $ objectManager ->get (WebsiteRepositoryInterface::class)->get ($ scopeCode )->getId ();
211
- $ configData ['store ' ] = '' ;
212
- $ configData ['website ' ] = $ website ;
168
+ private function getIdByScopeType (string $ scopeType , ?string $ scopeId ): int
169
+ {
170
+ $ id = 0 ;
171
+ /** @var StoreManagerInterface $storeManager */
172
+ $ storeManager = Bootstrap::getObjectManager ()->get (StoreManagerInterface::class);
173
+ switch ($ scopeType ) {
174
+ case ScopeInterface::SCOPE_WEBSITES :
175
+ $ id = (int )$ storeManager ->getWebsite ($ scopeId )->getId ();
176
+ break ;
177
+ case ScopeInterface::SCOPE_STORES :
178
+ $ id = (int )$ storeManager ->getStore ($ scopeId )->getId ();
179
+ break ;
180
+ default :
181
+ break ;
213
182
}
214
183
215
- $ objectManager -> get (ConfigFactory::class)-> create ([ ' data ' => $ configData ])-> save () ;
184
+ return $ id ;
216
185
}
217
186
}
0 commit comments