first commit
This commit is contained in:
45
tests/Settings/SettingTest.php
Normal file
45
tests/Settings/SettingTest.php
Normal file
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Settings;
|
||||
|
||||
use Netig\Netslim\Settings\Domain\Entity\Setting;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class SettingTest extends TestCase
|
||||
{
|
||||
public function testSerializesAndRestoresTypedValues(): void
|
||||
{
|
||||
$setting = new Setting('site.enabled', true);
|
||||
|
||||
self::assertSame('bool', $setting->getType());
|
||||
self::assertSame('1', $setting->toStorageValue());
|
||||
|
||||
$restored = Setting::fromStorage([
|
||||
'setting_key' => 'site.enabled',
|
||||
'setting_value' => '1',
|
||||
'value_type' => 'bool',
|
||||
]);
|
||||
|
||||
self::assertTrue($restored->getValue());
|
||||
}
|
||||
|
||||
public function testRejectsEmptyKey(): void
|
||||
{
|
||||
$this->expectException(\InvalidArgumentException::class);
|
||||
new Setting('', 'value');
|
||||
}
|
||||
|
||||
public function testRejectsUnsupportedStoredType(): void
|
||||
{
|
||||
$this->expectException(\LogicException::class);
|
||||
$this->expectExceptionMessage('Type de paramètre stocké non supporté');
|
||||
|
||||
Setting::fromStorage([
|
||||
'setting_key' => 'site.mode',
|
||||
'setting_value' => 'legacy',
|
||||
'value_type' => 'legacy-type',
|
||||
]);
|
||||
}
|
||||
}
|
||||
54
tests/Settings/SettingsServiceTest.php
Normal file
54
tests/Settings/SettingsServiceTest.php
Normal file
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Settings;
|
||||
|
||||
use Netig\Netslim\Settings\Application\SettingsApplicationService;
|
||||
use Netig\Netslim\Settings\Infrastructure\PdoSettingRepository;
|
||||
use PDO;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class SettingsServiceTest extends TestCase
|
||||
{
|
||||
private PDO $db;
|
||||
|
||||
private SettingsApplicationService $service;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->db = new PDO('sqlite::memory:', options: [
|
||||
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
||||
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
|
||||
]);
|
||||
$this->db->exec('CREATE TABLE settings (setting_key TEXT PRIMARY KEY, setting_value TEXT DEFAULT NULL, value_type TEXT NOT NULL, updated_at TEXT NOT NULL)');
|
||||
$this->service = new SettingsApplicationService(new PdoSettingRepository($this->db));
|
||||
}
|
||||
|
||||
public function testStoresAndReadsTypedValues(): void
|
||||
{
|
||||
$this->service->set('site.name', 'Netslim');
|
||||
$this->service->set('posts.per_page', 12);
|
||||
$this->service->set('site.enabled', true);
|
||||
|
||||
self::assertSame('Netslim', $this->service->getString('site.name'));
|
||||
self::assertSame(12, $this->service->getInt('posts.per_page'));
|
||||
self::assertTrue($this->service->getBool('site.enabled'));
|
||||
self::assertSame([
|
||||
'posts.per_page' => 12,
|
||||
'site.enabled' => true,
|
||||
'site.name' => 'Netslim',
|
||||
], $this->service->all());
|
||||
}
|
||||
|
||||
public function testDeleteRemovesSetting(): void
|
||||
{
|
||||
$this->service->set('site.name', 'Netslim');
|
||||
self::assertTrue($this->service->has('site.name'));
|
||||
|
||||
$this->service->delete('site.name');
|
||||
|
||||
self::assertFalse($this->service->has('site.name'));
|
||||
self::assertSame('fallback', $this->service->getString('site.name', 'fallback'));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user