201 lines
7.3 KiB
PHP
201 lines
7.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Feature\Service;
|
|
|
|
use Override;
|
|
use Tests\TestCase;
|
|
use App\Entity\SystemConfig;
|
|
use App\Service\ConfigurationService;
|
|
use App\Repository\SystemConfigRepository;
|
|
use App\Enum\SystemSettingKey;
|
|
use InvalidArgumentException;
|
|
|
|
class ConfigurationServiceTest extends TestCase
|
|
{
|
|
private ConfigurationService $service;
|
|
private SystemConfigRepository $repository;
|
|
|
|
#[Override]
|
|
public function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
$this->setUpDB();
|
|
$this->repository = $this->container->get(SystemConfigRepository::class);
|
|
$this->service = $this->container->get(ConfigurationService::class);
|
|
}
|
|
|
|
public function testGetAllConfigs(): void
|
|
{
|
|
// Initially the repository should be empty
|
|
$this->assertCount(0, $this->service->getAllConfigs());
|
|
|
|
// Create some config entries
|
|
$config1 = new SystemConfig('key1', 'value1');
|
|
$config2 = new SystemConfig('key2', 'value2');
|
|
$this->repository->save($config1);
|
|
$this->repository->save($config2);
|
|
|
|
// Now getAllConfigs should return both configs
|
|
$configs = $this->service->getAllConfigs();
|
|
$this->assertCount(2, $configs);
|
|
$this->assertContainsOnlyInstancesOf(SystemConfig::class, $configs);
|
|
}
|
|
|
|
public function testGetConfigValue(): void
|
|
{
|
|
// Create a config
|
|
$config = new SystemConfig('key1', 'value1');
|
|
$this->repository->save($config);
|
|
|
|
// Get value by key
|
|
$value = $this->service->getConfigValue('key1');
|
|
$this->assertEquals('value1', $value);
|
|
|
|
// Get value for a non-existent key (should return default)
|
|
$value = $this->service->getConfigValue('nonexistent', 'default');
|
|
$this->assertEquals('default', $value);
|
|
}
|
|
|
|
public function testSetConfigValue(): void
|
|
{
|
|
// Initially the repository should be empty
|
|
$this->assertCount(0, $this->service->getAllConfigs());
|
|
|
|
// Set a value for a new key
|
|
$this->service->setConfigValue('key1', 'value1');
|
|
|
|
// Check that a new config was created
|
|
$configs = $this->service->getAllConfigs();
|
|
$this->assertCount(1, $configs);
|
|
$config = $configs[0];
|
|
$this->assertEquals('key1', $config->getKey());
|
|
$this->assertEquals('value1', $config->getValue());
|
|
|
|
// Set a value for an existing key
|
|
$this->service->setConfigValue('key1', 'updated');
|
|
|
|
// Check that the value was updated
|
|
$config = $this->service->getConfigByKey('key1');
|
|
$this->assertInstanceOf(SystemConfig::class, $config);
|
|
$this->assertEquals('key1', $config->getKey());
|
|
$this->assertEquals('updated', $config->getValue());
|
|
}
|
|
|
|
public function testGetConfigByKey(): void
|
|
{
|
|
// Create some configs
|
|
$config1 = new SystemConfig('key1', 'value1');
|
|
$config2 = new SystemConfig('key2', 'value2');
|
|
$this->repository->save($config1);
|
|
$this->repository->save($config2);
|
|
|
|
// Find by key
|
|
$foundConfig = $this->service->getConfigByKey('key1');
|
|
$this->assertInstanceOf(SystemConfig::class, $foundConfig);
|
|
$this->assertEquals('key1', $foundConfig->getKey());
|
|
$this->assertEquals('value1', $foundConfig->getValue());
|
|
|
|
// Try to find a non-existent key
|
|
$this->assertNull($this->service->getConfigByKey('nonexistent'));
|
|
}
|
|
|
|
public function testCreateConfig(): void
|
|
{
|
|
// Create a new config
|
|
$config = $this->service->createConfig('key1', 'value1');
|
|
|
|
// Check that it was created correctly
|
|
$this->assertInstanceOf(SystemConfig::class, $config);
|
|
$this->assertEquals('key1', $config->getKey());
|
|
$this->assertEquals('value1', $config->getValue());
|
|
|
|
// Check that it's in the database
|
|
$foundConfig = $this->service->getConfigByKey('key1');
|
|
$this->assertInstanceOf(SystemConfig::class, $foundConfig);
|
|
$this->assertEquals('key1', $foundConfig->getKey());
|
|
$this->assertEquals('value1', $foundConfig->getValue());
|
|
|
|
// Try to create a config with the same key (should throw an exception)
|
|
$this->expectException(InvalidArgumentException::class);
|
|
$this->service->createConfig('key1', 'value2');
|
|
}
|
|
|
|
public function testUpdateConfig(): void
|
|
{
|
|
// Create a config
|
|
$config = $this->service->createConfig('key1', 'value1');
|
|
|
|
// Update the value
|
|
$updatedConfig = $this->service->updateConfig($config, null, 'updated');
|
|
$this->assertEquals('key1', $updatedConfig->getKey());
|
|
$this->assertEquals('updated', $updatedConfig->getValue());
|
|
|
|
// Check that it was updated in the database
|
|
$foundConfig = $this->service->getConfigByKey('key1');
|
|
$this->assertEquals('updated', $foundConfig->getValue());
|
|
|
|
// Update the key
|
|
$updatedConfig = $this->service->updateConfig($config, 'newKey', null);
|
|
$this->assertEquals('newKey', $updatedConfig->getKey());
|
|
$this->assertEquals('updated', $updatedConfig->getValue());
|
|
|
|
// Check that it was updated in the database
|
|
$foundConfig = $this->service->getConfigByKey('newKey');
|
|
$this->assertInstanceOf(SystemConfig::class, $foundConfig);
|
|
$this->assertEquals('newKey', $foundConfig->getKey());
|
|
$this->assertEquals('updated', $foundConfig->getValue());
|
|
|
|
// Create another config
|
|
$config2 = $this->service->createConfig('key2', 'value2');
|
|
|
|
// Try to update the key to an existing key (should throw an exception)
|
|
$this->expectException(InvalidArgumentException::class);
|
|
$this->service->updateConfig($config2, 'newKey', null);
|
|
}
|
|
|
|
public function testDeleteConfig(): void
|
|
{
|
|
// Create a config
|
|
$config = $this->service->createConfig('key1', 'value1');
|
|
|
|
// Check that it exists
|
|
$this->assertInstanceOf(SystemConfig::class, $this->service->getConfigByKey('key1'));
|
|
|
|
// Delete it
|
|
$this->service->deleteConfig($config);
|
|
|
|
// Check that it's gone
|
|
$this->assertNull($this->service->getConfigByKey('key1'));
|
|
}
|
|
|
|
public function testInitializeDefaultConfigs(): void
|
|
{
|
|
// Initially the repository should be empty
|
|
$this->assertCount(0, $this->service->getAllConfigs());
|
|
|
|
// Initialize default configs
|
|
$this->service->initializeDefaultConfigs();
|
|
|
|
// Check that the default configs were created
|
|
$configs = $this->service->getAllConfigs();
|
|
$this->assertGreaterThanOrEqual(3, count($configs)); // At least 3 default configs
|
|
|
|
// Check specific default configs
|
|
$lookbackOrders = $this->service->getConfigValue(SystemSettingKey::STOCK_ADJUSTMENT_LOOKBACK_ORDERS->value);
|
|
$this->assertEquals('5', $lookbackOrders);
|
|
|
|
$magnitude = $this->service->getConfigValue(SystemSettingKey::STOCK_ADJUSTMENT_MAGNITUDE->value);
|
|
$this->assertEquals('0.2', $magnitude);
|
|
|
|
$threshold = $this->service->getConfigValue(SystemSettingKey::STOCK_ADJUSTMENT_THRESHOLD->value);
|
|
$this->assertEquals('0.1', $threshold);
|
|
|
|
// Initialize again (should not create duplicates)
|
|
$this->service->initializeDefaultConfigs();
|
|
|
|
// Count should be the same
|
|
$this->assertCount(count($configs), $this->service->getAllConfigs());
|
|
}
|
|
}
|