spatie/laravel-settings

fake settings do not persist after ->refresh() call

markwalet opened this issue · 2 comments

I'm writing a test for a job that automatically updates a setting in some specific case. But when I refresh my settings class after the job, the values default back to the default values defined in my migrations.

It does work without the refresh() call in my test, but then I wouldn't ensure that I actually called the ->save() method on the setting.

Simplified example:

// Migration:
$this->migrator->add('example.processing_enabled', false);

// Successful test, false positive because setting gets reset to false even without the dispatch.
public function it_updates_the_setting_in_a_specific_case(): void
{
    $settings = ExampleSettings::fake(['processing_enabled' => true]);

    dispatch(new ExampleJob(shouldUpdateSettings: true));
    $settings->refresh();

    $this->assertFalse($settings->processing_enabled);
}

// Failing test
public function it_does_not_update_the_setting_in_other_cases(): void
{
    // example.processing_enabled = false
    $settings = ExampleSettings::fake(['processing_enabled' => true]);

    dispatch(new ExampleJob(shouldUpdateSettings: false));
    $settings->refresh();

    $this->assertTrue($settings->processing_enabled);
}

Any thoughts on this? Is there another way to actually enforce the call to ->save() in my tests while using fake settings? In my opinion the refresh method should work as expected even for fake settings but I'm not sure if I'm missing something.

Thanks in advance. This package is truly amazing!

Fake settings were built to quickly set some settings in tests without having to update migrations. We've never built them to be saved actually. Why don't you work on the real database in this case?

Closing due to inactivity.