Modify Config to add individual set method with override capabilities. Add 2 tests to test override functionality
Some checks are pending
CI / build_and_test (push) Waiting to run

This commit is contained in:
Funky Waddle 2026-09-11 20:16:11 -05:00
parent 8080e9cbd0
commit a8b2038d63
2 changed files with 47 additions and 5 deletions

View file

@ -12,10 +12,6 @@ declare(strict_types=1);
namespace SiteWeaver\Core;
use Infisical\SDK\InfisicalSDK;
use Infisical\SDK\Services\UniversalAuthService;
use Infisical\SDK\Models\MachineIdentityCredential;
/**
* Configuration service that loads environment variables and optionally fetches secrets from Infisical.
*/
@ -31,6 +27,22 @@ class Config
return $this->values[$key] ?? $default;
}
/**
* Explicitly set or overwrite a configuration value.
*
* @param string $key The configuration key.
* @param mixed $value The new value.
*/
public function set(string $key, mixed $value, bool $override = false): void
{
if ($override || !array_key_exists($key, $this->values)) {
// Allow null values to be stored as well.
if ($key !== null) {
$this->values[$key] = $value;
}
}
}
/**
* Return all configuration values.
*/
@ -47,4 +59,3 @@ class Config
}
}
}

View file

@ -39,4 +39,35 @@ final class ConfigTest extends TestCase
$this->assertSame($env, $config->all());
}
public function testNoOverrideByDefault(): void {
$env = [
'FOO' => 'bar',
'BAZ' => 123,
];
$config = new Config();
$config->load($env);
$config->set('FOO', 'asdf');
$this->assertSame($env, $config->all());
}
public function testForcedOverride(): void {
$env = [
'FOO' => 'bar',
'BAZ' => 123,
];
$config = new Config();
$config->load($env);
$config->set('FOO', 'asdf', true);
$new_env = [
'FOO' => 'asdf',
'BAZ' => 123,
];
$this->assertSame($new_env, $config->all());
}
}