Introduce improved way to interact with settings in tests

This commit is contained in:
Marcus Moore 2023-04-06 17:27:18 -07:00
parent 383d48fd9c
commit f767cc082f
No known key found for this signature in database
6 changed files with 38 additions and 20 deletions

View file

@ -17,5 +17,6 @@ class SettingObserver
public function saved(Setting $setting)
{
Cache::forget(Setting::SETUP_CHECK_KEY);
Setting::$_cache = null;
}
}

View file

@ -34,13 +34,4 @@ class SettingFactory extends Factory
'email_domain' => 'test.com',
];
}
public function withMultipleFullCompanySupport()
{
return $this->state(function () {
return [
'full_multiple_companies_support' => 1,
];
});
}
}

View file

@ -3,7 +3,6 @@
namespace Tests\Feature\Api\Assets;
use App\Models\Asset;
use App\Models\Setting;
use App\Models\User;
use Illuminate\Testing\Fluent\AssertableJson;
use Laravel\Passport\Passport;
@ -13,8 +12,6 @@ class AssetIndexTest extends TestCase
{
public function testAssetIndexReturnsExpectedAssets()
{
Setting::factory()->create();
Asset::factory()->count(3)->create();
Passport::actingAs(User::factory()->superuser()->create());

View file

@ -3,7 +3,6 @@
namespace Tests\Feature\Api\Users;
use App\Models\Company;
use App\Models\Setting;
use App\Models\User;
use Illuminate\Testing\Fluent\AssertableJson;
use Laravel\Passport\Passport;
@ -13,8 +12,6 @@ class UsersForSelectListTest extends TestCase
{
public function testUsersAreReturned()
{
Setting::factory()->create();
$users = User::factory()->superuser()->count(3)->create();
Passport::actingAs($users->first());
@ -32,7 +29,7 @@ class UsersForSelectListTest extends TestCase
public function testUsersScopedToCompanyWhenMultipleFullCompanySupportEnabled()
{
Setting::factory()->withMultipleFullCompanySupport()->create();
$this->settings->enableMultipleFullCompanySupport();
$jedi = Company::factory()->has(User::factory()->count(3)->sequence(
['first_name' => 'Luke', 'last_name' => 'Skywalker', 'username' => 'lskywalker'],
@ -60,7 +57,7 @@ class UsersForSelectListTest extends TestCase
public function testUsersScopedToCompanyDuringSearchWhenMultipleFullCompanySupportEnabled()
{
Setting::factory()->withMultipleFullCompanySupport()->create();
$this->settings->enableMultipleFullCompanySupport();
$jedi = Company::factory()->has(User::factory()->count(3)->sequence(
['first_name' => 'Luke', 'last_name' => 'Skywalker', 'username' => 'lskywalker'],

View file

@ -0,0 +1,30 @@
<?php
namespace Tests\Support;
use App\Models\Setting;
class Settings
{
private Setting $setting;
public function __construct()
{
$this->setting = Setting::factory()->create();
}
public function enableMultipleFullCompanySupport()
{
$this->update(['full_multiple_companies_support' => 1]);
}
public function disableMultipleFullCompanySupport()
{
$this->update(['full_multiple_companies_support' => 0]);
}
private function update(array $attributes)
{
Setting::unguarded(fn() => $this->setting->update($attributes));
}
}

View file

@ -3,15 +3,17 @@
namespace Tests;
use App\Http\Middleware\SecurityHeaders;
use App\Models\Setting;
use Illuminate\Foundation\Testing\LazilyRefreshDatabase;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use Tests\Support\Settings;
abstract class TestCase extends BaseTestCase
{
use CreatesApplication;
use LazilyRefreshDatabase;
protected Settings $settings;
private array $globallyDisabledMiddleware = [
SecurityHeaders::class,
];
@ -20,8 +22,8 @@ abstract class TestCase extends BaseTestCase
{
parent::setUp();
$this->beforeApplicationDestroyed(fn() => Setting::$_cache = null);
$this->withoutMiddleware($this->globallyDisabledMiddleware);
$this->settings = new Settings();
}
}