saufen/tests/Feature/Repository/SystemConfigRepositoryTest.php
2025-05-31 21:43:13 +02:00

167 lines
5.1 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Feature\Repository;
use Override;
use Tests\TestCase;
use App\Entity\SystemConfig;
use App\Repository\SystemConfigRepository;
class SystemConfigRepositoryTest extends TestCase
{
private SystemConfigRepository $repository;
#[Override]
public function setUp(): void
{
parent::setUp();
$this->setUpDB();
$this->repository = $this->container->get(SystemConfigRepository::class);
}
public function testFindAll(): void
{
// Initially the repository should be empty
$this->assertCount(0, $this->repository->findAll());
// Create some config entries
$config1 = new SystemConfig('key1', 'value1');
$config2 = new SystemConfig('key2', 'value2');
// Save them to the repository
$this->repository->save($config1);
$this->repository->save($config2);
// Now findAll should return both configs
$configs = $this->repository->findAll();
$this->assertCount(2, $configs);
$this->assertContainsOnlyInstancesOf(SystemConfig::class, $configs);
}
public function testFind(): void
{
// Create a config
$config = new SystemConfig('key1', 'value1');
$this->repository->save($config);
// Get the ID
$id = $config->getId();
$this->assertNotNull($id);
// Find by ID
$foundConfig = $this->repository->find($id);
$this->assertInstanceOf(SystemConfig::class, $foundConfig);
$this->assertEquals('key1', $foundConfig->getKey());
$this->assertEquals('value1', $foundConfig->getValue());
// Try to find a non-existent ID
$nonExistentId = 9999;
$this->assertNull($this->repository->find($nonExistentId));
}
public function testFindByKey(): void
{
// Create some configs
$config1 = new SystemConfig('key1', 'value1');
$config2 = new SystemConfig('key2', 'value2');
// Save them to the repository
$this->repository->save($config1);
$this->repository->save($config2);
// Find by key
$foundConfig = $this->repository->findByKey('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->repository->findByKey('nonexistent'));
}
public function testGetValue(): void
{
// Create a config
$config = new SystemConfig('key1', 'value1');
$this->repository->save($config);
// Get value by key
$value = $this->repository->getValue('key1');
$this->assertEquals('value1', $value);
// Get value for a non-existent key (should return default)
$value = $this->repository->getValue('nonexistent', 'default');
$this->assertEquals('default', $value);
}
public function testSetValue(): void
{
// Initially the repository should be empty
$this->assertCount(0, $this->repository->findAll());
// Set a value for a new key
$this->repository->setValue('key1', 'value1');
// Check that a new config was created
$configs = $this->repository->findAll();
$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->repository->setValue('key1', 'updated');
// Check that the value was updated
$config = $this->repository->findByKey('key1');
$this->assertInstanceOf(SystemConfig::class, $config);
$this->assertEquals('key1', $config->getKey());
$this->assertEquals('updated', $config->getValue());
}
public function testSave(): void
{
// Create a config
$config = new SystemConfig('key1', 'value1');
// Save it
$this->repository->save($config);
// Check that it was saved
$id = $config->getId();
$this->assertNotNull($id);
// Find it by ID to confirm it's in the database
$foundConfig = $this->repository->find($id);
$this->assertInstanceOf(SystemConfig::class, $foundConfig);
$this->assertEquals('key1', $foundConfig->getKey());
$this->assertEquals('value1', $foundConfig->getValue());
// Update it
$config->setValue('updated');
$this->repository->save($config);
// Find it again to confirm the update
$foundConfig = $this->repository->find($id);
$this->assertEquals('updated', $foundConfig->getValue());
}
public function testRemove(): void
{
// Create a config
$config = new SystemConfig('key1', 'value1');
$this->repository->save($config);
// Get the ID
$id = $config->getId();
$this->assertNotNull($id);
// Remove it
$this->repository->remove($config);
// Try to find it by ID to confirm it's gone
$this->assertNull($this->repository->find($id));
}
}