Persist system settings in database
This commit is contained in:
@@ -2,33 +2,69 @@
|
||||
|
||||
namespace Tests\Unit;
|
||||
|
||||
use App\Models\Setting;
|
||||
use App\Services\System\SystemSettingService;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
class SystemSettingServiceTest extends TestCase
|
||||
{
|
||||
public function test_it_reads_system_settings_from_config(): void
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_it_reads_config_when_database_empty(): void
|
||||
{
|
||||
// Override konfigurasi untuk memastikan service mengambil nilai terbaru.
|
||||
config([
|
||||
'system.asset.code_prefix' => 'ASTX',
|
||||
'system.asset.code_prefix' => 'CFG',
|
||||
'system.application.name' => 'Custom AMS',
|
||||
]);
|
||||
|
||||
$service = app(SystemSettingService::class);
|
||||
|
||||
$this->assertSame('ASTX', $service->get('asset.code_prefix'));
|
||||
$this->assertSame('CFG', $service->get('asset.code_prefix'));
|
||||
$this->assertSame('Custom AMS', $service->get('application.name'));
|
||||
$this->assertTrue($service->get('asset.qr_enabled'));
|
||||
}
|
||||
|
||||
public function test_it_can_override_runtime_settings(): void
|
||||
public function test_it_prefers_database_overrides_and_casts_types(): void
|
||||
{
|
||||
// Simulasikan override via DB.
|
||||
Setting::create([
|
||||
'key' => 'asset.qr_enabled',
|
||||
'value' => false,
|
||||
'type' => 'boolean',
|
||||
'group' => 'asset',
|
||||
]);
|
||||
|
||||
Setting::create([
|
||||
'key' => 'ui.table_page_size',
|
||||
'value' => 50,
|
||||
'type' => 'integer',
|
||||
'group' => 'ui',
|
||||
]);
|
||||
|
||||
$service = app(SystemSettingService::class);
|
||||
|
||||
// config default = true, namun DB override -> false
|
||||
$this->assertFalse($service->get('asset.qr_enabled'));
|
||||
$this->assertSame(50, $service->get('ui.table_page_size'));
|
||||
}
|
||||
|
||||
public function test_it_sets_and_refreshes_cache_and_config(): void
|
||||
{
|
||||
$service = app(SystemSettingService::class);
|
||||
|
||||
$service->set('ui.date_format', 'Y-m-d');
|
||||
// Panggilan awal untuk memicu cache default.
|
||||
$this->assertFalse($service->get('maintenance.readonly_mode'));
|
||||
|
||||
$this->assertSame('Y-m-d', $service->get('ui.date_format'));
|
||||
$this->assertSame('Y-m-d', config('system.ui.date_format'));
|
||||
// Ubah via service, pastikan DB dan cache/config ter-update.
|
||||
$service->set('maintenance.readonly_mode', true);
|
||||
|
||||
$this->assertDatabaseHas('settings', [
|
||||
'key' => 'maintenance.readonly_mode',
|
||||
'value' => '1',
|
||||
'type' => 'boolean',
|
||||
]);
|
||||
|
||||
$this->assertTrue($service->get('maintenance.readonly_mode'));
|
||||
$this->assertTrue(config('system.maintenance.readonly_mode'));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user