@@ -100,9 +100,52 @@ protected function assertConfigInDb(
100
100
$ this ->assertEquals ($ value , $ configValue );
101
101
}
102
102
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
+ */
103
108
protected function refreshConfigFromDb (): void
104
109
{
110
+ $ bootstrap = $ this ->getBootstrapConfig ();
105
111
$ 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
+ }
106
149
}
107
150
108
151
protected function clearIndices ()
0 commit comments