testitest
This commit is contained in:
parent
43ca79f650
commit
66c4c1fe4f
30 changed files with 4443 additions and 184 deletions
147
tests/Feature/Service/ConfigurationServiceTest.php
Normal file
147
tests/Feature/Service/ConfigurationServiceTest.php
Normal file
|
@ -0,0 +1,147 @@
|
|||
<?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);
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue