Skip to content

Commit 265f8ec

Browse files
committed
MAGE-1044 Preserve test bootstrap on config reinit
1 parent 93e9698 commit 265f8ec

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

Test/Integration/TestCase.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,52 @@ protected function assertConfigInDb(
100100
$this->assertEquals($value, $configValue);
101101
}
102102

103+
/**
104+
* If testing classes that use WriterInterface under the hood to update the database
105+
* then you need a way to refresh the in-memory cache
106+
* This function achieves that while preserving the original bootstrap config
107+
*/
103108
protected function refreshConfigFromDb(): void
104109
{
110+
$bootstrap = $this->getBootstrapConfig();
105111
$this->objectManager->get(\Magento\Framework\App\Config\ReinitableConfigInterface::class)->reinit();
112+
$this->setConfigFromArray($bootstrap);
113+
}
114+
115+
/**
116+
* @return array<string, string>
117+
*/
118+
protected function getBootstrapConfig(): array
119+
{
120+
$config = $this->objectManager->get(ScopeConfigInterface::class);
121+
122+
$bootstrap = [
123+
ConfigHelper::APPLICATION_ID,
124+
ConfigHelper::SEARCH_ONLY_API_KEY,
125+
ConfigHelper::API_KEY,
126+
ConfigHelper::INDEX_PREFIX
127+
];
128+
129+
return array_combine(
130+
$bootstrap,
131+
array_map(
132+
function($setting) use ($config) {
133+
return $config->getValue($setting, ScopeInterface::SCOPE_STORE);
134+
},
135+
$bootstrap
136+
)
137+
);
138+
}
139+
140+
/**
141+
* @param array<string, string> $settings
142+
* @return void
143+
*/
144+
protected function setConfigFromArray(array $settings): void
145+
{
146+
foreach ($settings as $key => $value) {
147+
$this->setConfig($key, $value);
148+
}
106149
}
107150

108151
protected function clearIndices()

0 commit comments

Comments
 (0)