147 lines
4.5 KiB
PHP
147 lines
4.5 KiB
PHP
<?php
|
|
|
|
use App\Entity\SystemConfig;
|
|
use App\Enum\SystemSettingKey;
|
|
use App\Repository\SystemConfigRepository;
|
|
use App\Service\ConfigurationService;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
|
|
test('getAllConfigs returns all configurations', function () {
|
|
// Arrange
|
|
$configService = $this->getContainer()->get(ConfigurationService::class);
|
|
|
|
// Act
|
|
$configs = $configService->getAllConfigs();
|
|
|
|
// Assert
|
|
expect($configs)->toBeArray();
|
|
});
|
|
|
|
test('getConfigValue returns correct value', function () {
|
|
// 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 () {
|
|
// 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 () {
|
|
// 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 () {
|
|
// 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 () {
|
|
// 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 () {
|
|
// 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 () {
|
|
// 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 () {
|
|
// 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);
|
|
});
|