Skip to content

Commit 232b63a

Browse files
committed
fix: corrected reflection only configuration
1 parent 6d7120b commit 232b63a

1 file changed

Lines changed: 54 additions & 3 deletions

File tree

tests/phpMyFAQ/Functional/ControllerWebTestCase.php

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ abstract class ControllerWebTestCase extends WebTestCase
1717
/** @var array<string, array{configuration: Configuration, values: array<string, mixed>}> */
1818
private array $originalConfigurations = [];
1919

20+
/** @var array<string, list<string>> */
21+
private array $addedConfigurationKeys = [];
22+
2023
protected function requestPublic(string $method, string $uri, array $parameters = [], array $server = []): Response
2124
{
2225
return $this->requestWithContext('public', $method, $uri, $parameters, $server);
@@ -88,7 +91,34 @@ protected function overrideConfigurationValues(array $values, string $context =
8891
];
8992
}
9093

91-
$configProperty->setValue($configuration, array_merge($currentConfig, $values));
94+
$this->addedConfigurationKeys[$context] ??= [];
95+
foreach ($values as $name => $value) {
96+
$storedValue = $this->normalizeConfigurationValue($value);
97+
$existingValue = $configuration->get((string) $name);
98+
if ($existingValue === null) {
99+
$configuration->add((string) $name, $storedValue);
100+
$this->addedConfigurationKeys[$context][] = (string) $name;
101+
} else {
102+
$configuration->update([(string) $name => $storedValue]);
103+
}
104+
}
105+
106+
$latestConfig = $configProperty->getValue($configuration);
107+
self::assertIsArray($latestConfig);
108+
$configProperty->setValue($configuration, array_merge($latestConfig, $values));
109+
}
110+
111+
private function normalizeConfigurationValue(mixed $value): string
112+
{
113+
if (is_bool($value)) {
114+
return $value ? 'true' : 'false';
115+
}
116+
117+
if (is_array($value)) {
118+
return (string) json_encode($value);
119+
}
120+
121+
return (string) $value;
92122
}
93123

94124
protected function getConfiguration(string $context = 'public'): Configuration
@@ -198,6 +228,8 @@ private function ensureClientForContext(string $context): HttpKernelBrowser
198228
$configuration = $container->get('phpmyfaq.configuration');
199229
self::assertInstanceOf(Configuration::class, $configuration);
200230

231+
$configuration->update(['main.referenceURL' => 'https://localhost/']);
232+
201233
$reflection = new ReflectionClass(Configuration::class);
202234
$configProperty = $reflection->getProperty('config');
203235
$currentConfig = $configProperty->getValue($configuration);
@@ -212,14 +244,33 @@ private function ensureClientForContext(string $context): HttpKernelBrowser
212244

213245
protected function tearDown(): void
214246
{
215-
foreach ($this->originalConfigurations as $configurationSnapshot) {
247+
foreach ($this->originalConfigurations as $context => $configurationSnapshot) {
216248
$configuration = $configurationSnapshot['configuration'];
249+
$originalValues = $configurationSnapshot['values'];
217250
$reflection = new ReflectionClass(Configuration::class);
218251
$configProperty = $reflection->getProperty('config');
219-
$configProperty->setValue($configuration, $configurationSnapshot['values']);
252+
253+
$addedKeys = $this->addedConfigurationKeys[$context] ?? [];
254+
foreach ($addedKeys as $addedKey) {
255+
$configuration->delete($addedKey);
256+
}
257+
258+
$restore = [];
259+
foreach ($originalValues as $name => $value) {
260+
if (in_array($name, $addedKeys, true)) {
261+
continue;
262+
}
263+
$restore[$name] = $value;
264+
}
265+
if ($restore !== []) {
266+
$configuration->update($restore);
267+
}
268+
269+
$configProperty->setValue($configuration, $originalValues);
220270
}
221271

222272
$this->originalConfigurations = [];
273+
$this->addedConfigurationKeys = [];
223274
self::$activeContext = null;
224275
parent::tearDown();
225276
}

0 commit comments

Comments
 (0)