saufen/tests/Feature/Service/ConfigurationServiceTest.php
2025-06-09 19:56:08 +02:00

147 lines
4.5 KiB
PHP

<?php
declare(strict_types=1);
use App\Entity\SystemConfig;
use App\Enum\SystemSettingKey;
use App\Service\ConfigurationService;
test('getAllConfigs returns all configurations', function (): void {
// Arrange
$configService = $this->getContainer()->get(ConfigurationService::class);
// Act
$configs = $configService->getAllConfigs();
// Assert
expect($configs)->toBeArray();
});
test('getConfigValue returns correct value', function (): void {
// Arrange
$configService = $this->getContainer()->get(ConfigurationService::class);
$key = SystemSettingKey::SYSTEM_NAME;
$expectedValue = SystemSettingKey::getDefaultValue($key);
// Act
$value = $configService->getConfigValue($key);
// Assert
expect($value)->toBe($expectedValue);
});
test('setConfigValue updates configuration value', function (): void {
// Arrange
$configService = $this->getContainer()->get(ConfigurationService::class);
$key = SystemSettingKey::SYSTEM_NAME;
$newValue = 'Test System Name';
// Act
$configService->setConfigValue($key, $newValue);
$value = $configService->getConfigValue($key);
// Assert
expect($value)->toBe($newValue);
});
test('getConfigByKey returns correct config', function (): void {
// Arrange
$configService = $this->getContainer()->get(ConfigurationService::class);
$key = SystemSettingKey::SYSTEM_NAME;
// Act
$config = $configService->getConfigByKey($key);
// Assert
expect($config)->toBeInstanceOf(SystemConfig::class)
->and($config->getKey())->toBe($key);
});
test('createConfig throws exception when config already exists', function (): void {
// Arrange
$configService = $this->getContainer()->get(ConfigurationService::class);
$key = SystemSettingKey::SYSTEM_NAME;
$value = 'Test System Name';
// Ensure config exists
$configService->setConfigValue($key, $value);
// Act & Assert
expect(fn() => $configService->createConfig($key, $value))
->toThrow(InvalidArgumentException::class);
});
test('updateConfig updates configuration value', function (): void {
// Arrange
$configService = $this->getContainer()->get(ConfigurationService::class);
$key = SystemSettingKey::SYSTEM_NAME;
$initialValue = 'Initial System Name';
$newValue = 'Updated System Name';
// Create or update config with initial value
$configService->setConfigValue($key, $initialValue);
$config = $configService->getConfigByKey($key);
// Act
$updatedConfig = $configService->updateConfig($config, $newValue);
// Assert
expect($updatedConfig->getValue())->toBe($newValue)
->and($configService->getConfigValue($key))->toBe($newValue);
});
test('updateConfig does not update when value is empty', function (): void {
// Arrange
$configService = $this->getContainer()->get(ConfigurationService::class);
$key = SystemSettingKey::SYSTEM_NAME;
$initialValue = 'Initial System Name';
// Create or update config with initial value
$configService->setConfigValue($key, $initialValue);
$config = $configService->getConfigByKey($key);
// Act
$updatedConfig = $configService->updateConfig($config, '');
// Assert
expect($updatedConfig->getValue())->toBe($initialValue);
expect($configService->getConfigValue($key))->toBe($initialValue);
});
test('resetAllConfigs resets all configurations to default values', function (): void {
// Arrange
$configService = $this->getContainer()->get(ConfigurationService::class);
// Set non-default values for all configs
foreach (SystemSettingKey::cases() as $key) {
$configService->setConfigValue($key, 'non-default-value');
}
// Act
$configService->resetAllConfigs();
// Assert
foreach (SystemSettingKey::cases() as $key) {
$expectedValue = SystemSettingKey::getDefaultValue($key);
$actualValue = $configService->getConfigValue($key);
expect($actualValue)->toBe($expectedValue);
}
});
test('setDefaultValue sets default value for specific key', function (): void {
// Arrange
$configService = $this->getContainer()->get(ConfigurationService::class);
$key = SystemSettingKey::SYSTEM_NAME;
$nonDefaultValue = 'Non-Default System Name';
// Set non-default value
$configService->setConfigValue($key, $nonDefaultValue);
// Act
$configService->setDefaultValue($key);
// Assert
$expectedValue = SystemSettingKey::getDefaultValue($key);
$actualValue = $configService->getConfigValue($key);
expect($actualValue)->toBe($expectedValue);
});