From d7123998b244364e8cc077da87895b5af65002a2 Mon Sep 17 00:00:00 2001 From: Evan Burrell Date: Thu, 31 Oct 2024 10:47:33 +0000 Subject: [PATCH] Morphs --- config/settings.php | 22 +-- ...ub => add_settings_morphs_fields.php.stub} | 8 +- phpstan.neon.dist | 1 - src/Contracts/Driver.php | 12 +- src/Contracts/Setting.php | 12 +- src/Drivers/DatabaseDriver.php | 60 ++++--- src/Drivers/EloquentDriver.php | 24 +-- src/Drivers/Factory.php | 2 +- src/Events/SettingWasDeleted.php | 3 +- src/Events/SettingsFlushed.php | 3 +- src/Facades/Settings.php | 14 +- src/Models/Setting.php | 90 +++++++--- src/Settings.php | 154 ++++++++++-------- src/SettingsServiceProvider.php | 4 +- tests/Feature/Drivers/DatabaseDriverTest.php | 34 ++-- tests/Feature/Drivers/EloquentDriverTest.php | 28 ++-- tests/Feature/SettingsTest.php | 9 +- tests/Feature/TeamsTest.php | 105 ++++++------ tests/Pest.php | 4 +- tests/Support/Drivers/CustomDriver.php | 12 +- tests/Support/Models/Team.php | 2 + .../migrations/create_test_tables.php | 2 +- 22 files changed, 346 insertions(+), 259 deletions(-) rename database/migrations/{add_settings_team_field.php.stub => add_settings_morphs_fields.php.stub} (64%) diff --git a/config/settings.php b/config/settings.php index 686df5a..f293337 100644 --- a/config/settings.php +++ b/config/settings.php @@ -84,34 +84,34 @@ /* |-------------------------------------------------------------------------- - | Teams + | Morphs |-------------------------------------------------------------------------- | - | When set to true the package implements teams using the `team_foreign_key`. + | When set to true the package implements morphs on the settings`. | - | If you want the migrations to register the `team_foreign_key`, you must + | If you want the migrations to register the morphs, you must | set this to true before running the migration. | | If you already ran the migrations, then you must make a new migration to - | add the `team_foreign_key` column to the settings table, and update the - | unique constraint on the table. See the `add_settings_team_field` migration + | add the morphs columns to the settings table, and update the + | unique constraint on the table. See the `add_settings_morphs_fields` migration | for how to do this. | */ - 'teams' => false, + 'morphs' => false, /* |-------------------------------------------------------------------------- - | Team Foreign Key + | Morph name |-------------------------------------------------------------------------- | - | When teams is set to true, our database/eloquent drivers will use this - | column as a team foreign key to scope queries to. + | When morphs is set to true, our database/eloquent drivers will use this + | name for columns to scope queries to. | - | The team id will also be included in a cache key when caching is enabled. + | The morphs will also be included in a cache key when caching is enabled. | */ - 'team_foreign_key' => 'team_id', + 'morph_name' => 'model', /* |-------------------------------------------------------------------------- diff --git a/database/migrations/add_settings_team_field.php.stub b/database/migrations/add_settings_morphs_fields.php.stub similarity index 64% rename from database/migrations/add_settings_team_field.php.stub rename to database/migrations/add_settings_morphs_fields.php.stub index 3dc64ee..9325615 100644 --- a/database/migrations/add_settings_team_field.php.stub +++ b/database/migrations/add_settings_morphs_fields.php.stub @@ -10,19 +10,19 @@ return new class extends Migration { public function up(): void { - if (! config('settings.teams')) { + if (! config('settings.morphs')) { return; } Schema::table(config('settings.table'), function (Blueprint $table) { - $table->unsignedBigInteger(config('settings.team_foreign_key'))->nullable()->after('id'); - $table->index(config('settings.team_foreign_key'), 'settings_team_id_index'); + $table->nullableUuidMorphs(config('settings.morph_name')); $table->dropUnique('settings_key_unique'); $table->unique([ 'key', - config('settings.team_foreign_key'), + config('settings.morph_name') . '_id', + config('settings.morph_name') . '_type', ], 'settings_key_team_id_unique'); }); } diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 5e7ef9c..1f4d939 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -10,4 +10,3 @@ parameters: tmpDir: build/phpstan checkOctaneCompatibility: true checkModelProperties: true - checkMissingIterableValueType: false diff --git a/src/Contracts/Driver.php b/src/Contracts/Driver.php index efdf382..6f7a2af 100644 --- a/src/Contracts/Driver.php +++ b/src/Contracts/Driver.php @@ -8,15 +8,15 @@ interface Driver { - public function forget($key, $teamId = null); + public function forget($key, $morphId = null, $morphType = null); - public function get(string $key, $default = null, $teamId = null); + public function get(string $key, $default = null, $morphId = null, $morphType = null); - public function all($teamId = null, $keys = null): array|Arrayable; + public function all($morphId = null, $morphType = null, $keys = null): array|Arrayable; - public function has($key, $teamId = null): bool; + public function has($key, $morphId = null, $morphType = null): bool; - public function set(string $key, $value = null, $teamId = null); + public function set(string $key, $value = null, $morphId = null, $morphType = null); - public function flush($teamId = null, $keys = null); + public function flush($morphId = null, $keys = null, $morphType = null); } diff --git a/src/Contracts/Setting.php b/src/Contracts/Setting.php index 2c5d53d..e580083 100644 --- a/src/Contracts/Setting.php +++ b/src/Contracts/Setting.php @@ -8,15 +8,15 @@ interface Setting { - public static function getValue(string $key, $default = null, $teamId = null); + public static function getValue(string $key, $default = null, $morphId = null, $morphType = null); - public static function getAll($teamId = null, $keys = null): array|Arrayable; + public static function getAll($morphId = null, $morphType = null, $keys = null): array|Arrayable; - public static function has($key, $teamId = null): bool; + public static function has($key, $morphId = null, $morphType = null): bool; - public static function removeSetting($key, $teamId = null); + public static function removeSetting($key, $morphId = null, $morphType = null): void; - public static function set(string $key, $value = null, $teamId = null); + public static function set(string $key, $value = null, $morphId = null, $morphType = null); - public static function flush($teamId = null, $keys = null); + public static function flush($morphId = null, $morphType = null, $keys = null); } diff --git a/src/Drivers/DatabaseDriver.php b/src/Drivers/DatabaseDriver.php index cebe2a9..c6a8366 100644 --- a/src/Drivers/DatabaseDriver.php +++ b/src/Drivers/DatabaseDriver.php @@ -16,65 +16,81 @@ class DatabaseDriver implements Driver public function __construct( protected Connection $connection, protected string $table, - protected ?string $teamForeignKey = null, + protected ?string $morphName = null, ) {} - public function forget($key, $teamId = null): void + public function forget($key, $morphId = null, $morphType = null): void { $this->db() ->where('key', $key) ->when( - $teamId !== false, - fn (Builder $query) => $query->where("{$this->table}.{$this->teamForeignKey}", $teamId) + $morphId !== false, + fn (Builder $query) => $query->where("{$this->table}.{$this->morphName}_id", $morphId) + ) + ->when( + $morphType !== false, + fn (Builder $query) => $query->where("{$this->table}.{$this->morphName}_type", $morphType) ) ->delete(); } - public function get(string $key, $default = null, $teamId = null) + public function get(string $key, $default = null, $morphId = null, $morphType = null) { $value = $this->db() ->where('key', $key) ->when( - $teamId !== false, - fn (Builder $query) => $query->where("{$this->table}.{$this->teamForeignKey}", $teamId) + $morphId !== false, + fn (Builder $query) => $query->where("{$this->table}.{$this->morphName}_id", $morphId) + ) + ->when( + $morphType !== false, + fn (Builder $query) => $query->where("{$this->table}.{$this->morphName}_type", $morphType) ) ->value('value'); return $value ?? $default; } - public function all($teamId = null, $keys = null): array|Arrayable + public function all($morphId = null, $morphType = null, $keys = null): array|Arrayable { - return $this->baseBulkQuery($teamId, $keys)->get(); + return $this->baseBulkQuery($morphId, $morphType, $keys)->get(); } - public function has($key, $teamId = null): bool + public function has($key, $morphId = null, $morphType = null): bool { return $this->db() ->where('key', $key) ->when( - $teamId !== false, - fn (Builder $query) => $query->where("{$this->table}.{$this->teamForeignKey}", $teamId) + $morphId !== false, + fn (Builder $query) => $query->where("{$this->table}.{$this->morphName}_id", $morphId) + ) + ->when( + $morphType !== false, + fn (Builder $query) => $query->where("{$this->table}.{$this->morphName}_type", $morphType) ) ->exists(); } - public function set(string $key, $value = null, $teamId = null): void + public function set(string $key, $value = null, $morphId = null, $morphType = null): void { $data = [ 'key' => $key, ]; - if ($teamId !== false) { - $data[$this->teamForeignKey] = $teamId; + if ($morphId !== false) { + $data["{$this->morphName}_id"] = $morphId; + } + + if ($morphType !== false) { + $data["{$this->morphName}_type"] = $morphType; } $this->db()->updateOrInsert($data, compact('value')); } - public function flush($teamId = null, $keys = null): void + public function flush($morphId = null, $keys = null, $morphType = null): void { - $this->baseBulkQuery($teamId, $keys)->delete(); + $this->baseBulkQuery($morphId, $morphType, $keys)->delete(); } protected function db(): Builder @@ -95,7 +111,7 @@ protected function normalizeKeys($keys): string|Collection|bool return collect($keys)->flatten()->filter(); } - private function baseBulkQuery($teamId, $keys): Builder + private function baseBulkQuery($morphId, $morphType, $keys): Builder { $keys = $this->normalizeKeys($keys); @@ -115,8 +131,12 @@ private function baseBulkQuery($teamId, $keys): Builder fn (Builder $query) => $query->whereIn('key', $keys), ) ->when( - $teamId !== false, - fn (Builder $query) => $query->where("{$this->table}.{$this->teamForeignKey}", $teamId) + $morphId !== false, + fn (Builder $query) => $query->where("{$this->table}.{$this->morphName}_id", $morphId) + ) + ->when( + $morphType !== false, + fn (Builder $query) => $query->where("{$this->table}.{$this->morphName}_type", $morphType) ); } } diff --git a/src/Drivers/EloquentDriver.php b/src/Drivers/EloquentDriver.php index b6bd9cd..8c1e19b 100644 --- a/src/Drivers/EloquentDriver.php +++ b/src/Drivers/EloquentDriver.php @@ -12,33 +12,33 @@ class EloquentDriver implements Driver { public function __construct(protected Setting $model) {} - public function forget($key, $teamId = null): void + public function forget($key, $morphId = null, $morphType = null): void { - $this->model::removeSetting($key, $teamId); + $this->model::removeSetting($key, $morphId, $morphType); } - public function get(string $key, $default = null, $teamId = null) + public function get(string $key, $default = null, $morphId = null, $morphType = null) { - return $this->model::getValue($key, $default, $teamId); + return $this->model::getValue($key, $default, $morphId, $morphType); } - public function all($teamId = null, $keys = null): array|Arrayable + public function all($morphId = null, $morphType = null, $keys = null): array|Arrayable { - return $this->model::getAll($teamId, $keys); + return $this->model::getAll($morphId, $morphType, $keys); } - public function has($key, $teamId = null): bool + public function has($key, $morphId = null, $morphType = null): bool { - return $this->model::has($key, $teamId); + return $this->model::has($key, $morphId, $morphType); } - public function set(string $key, $value = null, $teamId = null): void + public function set(string $key, $value = null, $morphId = null, $morphType = null): void { - $this->model::set($key, $value, $teamId); + $this->model::set($key, $value, $morphId, $morphType); } - public function flush($teamId = null, $keys = null): void + public function flush($morphId = null, $keys = null, $morphType = null): void { - $this->model::flush($teamId, $keys); + $this->model::flush($morphId, $morphType, $keys); } } diff --git a/src/Drivers/Factory.php b/src/Drivers/Factory.php index 4ccd09f..e3a2bfa 100644 --- a/src/Drivers/Factory.php +++ b/src/Drivers/Factory.php @@ -41,7 +41,7 @@ protected function createDatabaseDriver(array $config): DatabaseDriver return new DatabaseDriver( connection: $this->app['db']->connection(Arr::get($config, 'connection')), table: $this->app['config']['settings.table'], - teamForeignKey: $this->app['config']['settings.team_foreign_key'] ?? null, + morphName: $this->app['config']['settings.morph_name'] ?? null, ); } diff --git a/src/Events/SettingWasDeleted.php b/src/Events/SettingWasDeleted.php index 267f301..ffcc994 100644 --- a/src/Events/SettingWasDeleted.php +++ b/src/Events/SettingWasDeleted.php @@ -17,7 +17,8 @@ public function __construct( public string $key, public string $storageKey, public string $cacheKey, - public mixed $teamId, + public mixed $morphId, + public mixed $morphType, public bool|Context|null $context, ) {} } diff --git a/src/Events/SettingsFlushed.php b/src/Events/SettingsFlushed.php index f40d394..9317865 100644 --- a/src/Events/SettingsFlushed.php +++ b/src/Events/SettingsFlushed.php @@ -16,7 +16,8 @@ final class SettingsFlushed public function __construct( public bool|Collection|string $keys, - public mixed $teamId, + public mixed $modelId, + public mixed $modelType, public bool|Context|null $context, ) {} } diff --git a/src/Facades/Settings.php b/src/Facades/Settings.php index 89dded2..0ffe69e 100644 --- a/src/Facades/Settings.php +++ b/src/Facades/Settings.php @@ -23,13 +23,13 @@ * @method static self temporarilyDisableCache() * @method static self disableEncryption() * @method static self enableEncryption() - * @method static null|mixed getTeamId() - * @method static self setTeamId(mixed $id) - * @method static self enableTeams() - * @method static self disableTeams() - * @method static bool teamsAreEnabled() - * @method static self usingTeam(mixed $teamId) - * @method static self withoutTeams() + * @method static null|mixed getMorphId() + * @method static self setMorphs(mixed $id, mixed $morphType = null) + * @method static self enableMorphs() + * @method static self disableMorphs() + * @method static bool morphsAreEnabled() + * @method static self usingMorph(mixed $morphId, mixed $morphType = null) + * @method static self withoutMorphs() * @method static \Rawilk\Settings\Contracts\KeyGenerator getKeyGenerator() * @method static string cacheKeyForSetting(string $key) */ diff --git a/src/Models/Setting.php b/src/Models/Setting.php index 6c587cb..d7b2fa1 100644 --- a/src/Models/Setting.php +++ b/src/Models/Setting.php @@ -7,6 +7,7 @@ use Illuminate\Contracts\Support\Arrayable; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\MorphTo; use Illuminate\Support\Collection; use Rawilk\Settings\Contracts\Setting as SettingContract; use Rawilk\Settings\Facades\Settings; @@ -15,14 +16,13 @@ * @property int $id * @property string $key * @property mixed $value - * @property int|string|null $team_id + * @property int|string|null $model_id + * @property int|string|null $model_type */ class Setting extends Model implements SettingContract { public $timestamps = false; - protected ?string $teamForeignKey = null; - protected $guarded = ['id']; public function __construct(array $attributes = []) @@ -30,18 +30,24 @@ public function __construct(array $attributes = []) parent::__construct($attributes); $this->setTable(config('settings.table')); - $this->teamForeignKey = config('settings.team_foreign_key'); } - public static function getValue(string $key, $default = null, $teamId = null) + public static function getValue(string $key, $default = null, $morphId = null, $morphType = null) { $value = static::query() ->where('key', $key) ->when( - $teamId !== false, + $morphId !== false, + fn (Builder $query) => $query->where( + static::make()->getTable() . '.' . config('settings.morph_name') . '_id', + $morphId, + ), + ) + ->when( + $morphType !== false, fn (Builder $query) => $query->where( - static::make()->getTable() . '.' . config('settings.team_foreign_key'), - $teamId, + static::make()->getTable() . '.' . config('settings.morph_name') . '_type', + $morphType, ), ) ->value('value'); @@ -49,56 +55,79 @@ public static function getValue(string $key, $default = null, $teamId = null) return $value ?? $default; } - public static function getAll($teamId = null, $keys = null): array|Arrayable + public static function getAll($morphId = null, $morphType = null, $keys = null): array|Arrayable { - return static::baseBulkQuery($teamId, $keys)->get(); + return static::baseBulkQuery($morphId, $morphType, $keys)->get(); } - public static function has($key, $teamId = null): bool + public static function has($key, $morphId = null, $morphType = null): bool { return static::query() ->where('key', $key) ->when( - $teamId !== false, + $morphId !== false, fn (Builder $query) => $query->where( - static::make()->getTable() . '.' . config('settings.team_foreign_key'), - $teamId, + static::make()->getTable() . '.' . config('settings.morph_name') . '_id', + $morphId, + ), + ) + ->when( + $morphType !== false, + fn (Builder $query) => $query->where( + static::make()->getTable() . '.' . config('settings.morph_name') . '_type', + $morphType, ), ) ->exists(); } - public static function removeSetting($key, $teamId = null): void + public static function removeSetting($key, $morphId = null, $morphType = null): void { static::query() ->where('key', $key) ->when( - $teamId !== false, + $morphId !== false, fn (Builder $query) => $query->where( - static::make()->getTable() . '.' . config('settings.team_foreign_key'), - $teamId, + static::make()->getTable() . '.' . config('settings.morph_name') . '_id', + $morphId, + ), + ) + ->when( + $morphType !== false, + fn (Builder $query) => $query->where( + static::make()->getTable() . '.' . config('settings.morph_name') . '_type', + $morphType, ), ) ->delete(); } - public static function set(string $key, $value = null, $teamId = null) + public static function set(string $key, $value = null, $morphId = null, $morphType = null) { $data = ['key' => $key]; - if ($teamId !== false) { - $data[config('settings.team_foreign_key')] = $teamId; + if ($morphId !== false) { + $data[config('settings.morph_name') . '_id'] = $morphId; + } + + if ($morphType !== false) { + $data[config('settings.morph_name') . '_type'] = $morphType; } return static::updateOrCreate($data, compact('value')); } - public static function flush($teamId = null, $keys = null): void + public static function flush($morphId = null, $morphType = null, $keys = null): void + { + static::baseBulkQuery($morphId, $morphType, $keys)->delete(); + } + + public function model(): MorphTo { - static::baseBulkQuery($teamId, $keys)->delete(); + return $this->morphTo(); } - protected static function baseBulkQuery($teamId, $keys): Builder + protected static function baseBulkQuery($morphId, $morphType, $keys): Builder { $keys = static::normalizeKeys($keys); @@ -118,12 +147,19 @@ protected static function baseBulkQuery($teamId, $keys): Builder fn (Builder $query) => $query->whereIn('key', $keys), ) ->when( - $teamId !== false, + $morphId !== false, fn (Builder $query) => $query->where( - static::make()->getTable() . '.' . config('settings.team_foreign_key'), - $teamId, + static::make()->getTable() . '.' . config('settings.morph_name') . '_id', + $morphId, + ), + )->when( + $morphType !== false, + fn (Builder $query) => $query->where( + static::make()->getTable() . '.' . config('settings.morph_name') . '_type', + $morphType, ), ); + } protected static function normalizeKeys($keys): string|Collection|bool diff --git a/src/Settings.php b/src/Settings.php index 88d2a48..89e365b 100755 --- a/src/Settings.php +++ b/src/Settings.php @@ -51,15 +51,21 @@ class Settings */ protected bool $cacheDefaultValue = true; - protected bool $teams = false; + protected bool $morphs = false; /** @var null|string|int */ - protected mixed $teamId = null; + protected mixed $morphId = null; + + /** @var null|string|int */ + protected mixed $morphType = null; protected ?string $teamForeignKey = null; - // Allows us to use a team id for a single call. - protected mixed $temporaryTeamId = false; + // Allows us to use a model id for a single call. + protected mixed $temporaryMorphId = false; + + // Allows us to use a model type for a single call. + protected mixed $temporaryMorphType = false; protected string $cacheKeyPrefix = ''; @@ -86,53 +92,47 @@ public function context(Context|bool|null $context = null): self return $this; } - public function getTeamId(): mixed + public function getMorphId(): mixed { - return $this->teamId; + return $this->morphId; } /** * Set the team id for teams/groups support. This id is used when querying settings. * - * @param int|string|null|\Illuminate\Database\Eloquent\Model $id + * @param int|string|null|\Illuminate\Database\Eloquent\Model $morphId + * @param int|string|null $morphType */ - public function setTeamId(mixed $id): self + public function setMorphs(mixed $morphId, mixed $morphType = null): self { - if ($id instanceof Model) { - $id = $id->getKey(); + if ($morphId instanceof Model) { + $morphType = $morphId->getMorphClass(); + $morphId = $morphId->getKey(); } - $this->teamId = $id; + $this->morphId = $morphId; + $this->morphType = $morphType; return $this; } - public function usingTeam(mixed $teamId): self + public function usingMorph(mixed $morphId, mixed $morphType = null): self { - if ($teamId instanceof Model) { - $teamId = $teamId->getKey(); + if ($morphId instanceof Model) { + $morphType = $morphId->getMorphClass(); + $morphId = $morphId->getKey(); } - $this->temporaryTeamId = $teamId; + $this->temporaryMorphId = $morphId; + $this->temporaryMorphType = $morphType; return $this; } - public function withoutTeams(): self + public function withoutMorphs(): self { - $this->temporaryTeamId = null; - - return $this; - } - - public function getTeamForeignKey(): ?string - { - return $this->teamForeignKey; - } - - public function setTeamForeignKey(?string $foreignKey): self - { - $this->teamForeignKey = $foreignKey; + $this->temporaryMorphId = null; + $this->temporaryMorphType = null; return $this; } @@ -152,14 +152,16 @@ public function forget(string|BackedEnum $key) $driverResult = $this->driver->forget( key: $generatedKey, - teamId: $this->teams ? $this->teamId : false, + morphId: $this->morphs ? $this->morphId : false, + morphType: $this->morphs ? $this->morphType : false, ); SettingWasDeleted::dispatch( $key, $generatedKey, $this->getCacheKey($generatedKey), - $this->teams ? $this->teamId : false, + $this->morphs ? $this->morphId : false, + $this->morphs ? $this->morphType : false, $this->context, ); @@ -177,7 +179,7 @@ public function forget(string|BackedEnum $key) return $driverResult; } - public function get(string|BackedEnum $key, $default = null, bool $resetTempTeam = true) + public function get(string|BackedEnum $key, $default = null, bool $resetTempMorph = true) { $key = $this->normalizeKey($key); @@ -189,14 +191,16 @@ public function get(string|BackedEnum $key, $default = null, bool $resetTempTeam fn () => $this->driver->get( key: $generatedKey, default: $this->cacheDefaultValue ? $default : null, - teamId: $this->teamIdForCall(), + morphId: $this->morphIdForCall(), + morphType: $this->morphTypeForCall(), ) ); } else { $value = $this->driver->get( key: $generatedKey, default: $default, - teamId: $this->teamIdForCall(), + morphId: $this->morphIdForCall(), + morphType: $this->morphTypeForCall(), ); } @@ -211,8 +215,9 @@ public function get(string|BackedEnum $key, $default = null, bool $resetTempTeam $this->temporarilyDisableCache = false; $this->resetContext = true; - if ($resetTempTeam) { - $this->temporaryTeamId = false; + if ($resetTempMorph) { + $this->temporaryMorphId = false; + $this->temporaryMorphType = false; } return $value ?? $default; @@ -223,7 +228,8 @@ public function all($keys = null): Collection $keys = $this->normalizeBulkLookupKey($keys); $values = collect($this->driver->all( - teamId: $this->teamIdForCall(), + morphId: $this->morphIdForCall(), + morphType: $this->morphTypeForCall(), keys: $keys, ))->map(function (mixed $record): mixed { $record = $this->normalizeBulkRetrievedValue($record); @@ -246,18 +252,20 @@ public function all($keys = null): Collection $this->temporarilyDisableCache = false; $this->resetContext = true; - $this->temporaryTeamId = false; + $this->temporaryMorphId = false; + $this->temporaryMorphType = false; return $values; } - public function has(string|BackedEnum $key, bool $resetTempTeam = true): bool + public function has(string|BackedEnum $key, bool $resetTempMorph = true): bool { $key = $this->normalizeKey($key); $has = $this->driver->has( key: $this->getKeyForStorage($key), - teamId: $this->teamIdForCall(), + morphId: $this->morphIdForCall(), + morphType: $this->morphTypeForCall(), ); if ($this->resetContext) { @@ -267,8 +275,9 @@ public function has(string|BackedEnum $key, bool $resetTempTeam = true): bool $this->temporarilyDisableCache = false; $this->resetContext = true; - if ($resetTempTeam) { - $this->temporaryTeamId = false; + if ($resetTempMorph) { + $this->temporaryMorphId = false; + $this->temporaryMorphType = false; } return $has; @@ -292,7 +301,8 @@ public function set(string|BackedEnum $key, $value = null): mixed $driverResult = $this->driver->set( key: $generatedKey, value: $this->encryptionIsEnabled() ? $this->encrypter->encrypt($serializedValue) : $serializedValue, - teamId: $this->teamIdForCall(), + morphId: $this->morphIdForCall(), + morphType: $this->morphTypeForCall(), ); SettingWasStored::dispatch( @@ -300,7 +310,7 @@ public function set(string|BackedEnum $key, $value = null): mixed $generatedKey, $this->getCacheKey($generatedKey), $value, - $this->teamIdForCall(), + $this->morphIdForCall(), $this->context, ); @@ -310,7 +320,8 @@ public function set(string|BackedEnum $key, $value = null): mixed $this->context(); $this->temporarilyDisableCache = false; - $this->temporaryTeamId = false; + $this->temporaryMorphId = false; + $this->temporaryMorphType = false; return $driverResult; } @@ -334,13 +345,15 @@ public function flush($keys = null): mixed $keys = $this->normalizeBulkLookupKey($keys); $driverResult = $this->driver->flush( - teamId: $this->teamIdForCall(), + morphId: $this->morphIdForCall(), keys: $keys, + morphType: $this->morphTypeForCall(), ); SettingsFlushed::dispatch( $keys, - $this->teamIdForCall(), + $this->morphIdForCall(), + $this->morphTypeForCall(), $this->context, ); @@ -358,7 +371,8 @@ public function flush($keys = null): mixed $this->temporarilyDisableCache = false; $this->resetContext = true; - $this->temporaryTeamId = false; + $this->temporaryMorphId = false; + $this->temporaryMorphType = false; return $driverResult; } @@ -412,23 +426,23 @@ public function setEncrypter(Encrypter $encrypter): self return $this; } - public function enableTeams(): self + public function enableMorphs(): self { - $this->teams = true; + $this->morphs = true; return $this; } - public function disableTeams(): self + public function disableMorphs(): self { - $this->teams = false; + $this->morphs = false; return $this; } - public function teamsAreEnabled(): bool + public function morphsAreEnabled(): bool { - return $this->teams; + return $this->morphs; } public function useCacheKeyPrefix(string $prefix): self @@ -486,11 +500,14 @@ protected function getCacheKey(string $key): string { $cacheKey = $this->cacheKeyPrefix . $key; - $teamId = $this->teamIdForCall(); - if ($teamId !== false) { - $teamId = is_null($teamId) ? 'null' : $teamId; + $morphId = $this->morphIdForCall(); + $morphType = $this->morphTypeForCall(); - $cacheKey .= "::team:{$teamId}"; + if ($morphId !== false && $morphType !== false) { + $morphId = is_null($morphId) ? 'null' : $morphId; + $morphType = is_null($morphType) ? 'null' : $morphType; + + $cacheKey .= "::morphId:{$morphId}:morphType:{$morphType}"; } return $cacheKey; @@ -523,11 +540,11 @@ protected function shouldSetNewValue(string $key, $newValue): bool } // To prevent decryption errors, we will check if we have a setting set for the current context and key. - if (! $this->doNotResetContext()->has(key: $key, resetTempTeam: false)) { + if (! $this->doNotResetContext()->has(key: $key, resetTempMorph: false)) { return true; } - return $newValue !== $this->doNotResetContext()->get(key: $key, resetTempTeam: false); + return $newValue !== $this->doNotResetContext()->get(key: $key, resetTempMorph: false); } protected function cacheIsEnabled(): bool @@ -544,13 +561,22 @@ protected function encryptionIsEnabled(): bool return $this->encryptionEnabled && $this->encrypter !== null; } - protected function teamIdForCall(): mixed + protected function morphIdForCall(): mixed + { + if ($this->temporaryMorphId !== false) { + return $this->temporaryMorphId; + } + + return $this->morphs ? $this->morphId : false; + } + + protected function morphTypeForCall(): mixed { - if ($this->temporaryTeamId !== false) { - return $this->temporaryTeamId; + if ($this->temporaryMorphType !== false) { + return $this->temporaryMorphType; } - return $this->teams ? $this->teamId : false; + return $this->morphs ? $this->morphType : false; } protected function decryptValue($value) diff --git a/src/SettingsServiceProvider.php b/src/SettingsServiceProvider.php index f02e7d8..f691bd0 100644 --- a/src/SettingsServiceProvider.php +++ b/src/SettingsServiceProvider.php @@ -85,9 +85,7 @@ protected function registerSettings(): void $app['config']['settings.cache'] ? $settings->enableCache() : $settings->disableCache(); $app['config']['settings.encryption'] ? $settings->enableEncryption() : $settings->disableEncryption(); - $app['config']['settings.teams'] ? $settings->enableTeams() : $settings->disableTeams(); - - $settings->setTeamForeignKey($app['config']['settings.team_foreign_key'] ?? 'team_id'); + $app['config']['settings.morphs'] ? $settings->enableMorphs() : $settings->disableMorphs(); return $settings; }); diff --git a/tests/Feature/Drivers/DatabaseDriverTest.php b/tests/Feature/Drivers/DatabaseDriverTest.php index 2c21ea3..9b863fc 100644 --- a/tests/Feature/Drivers/DatabaseDriverTest.php +++ b/tests/Feature/Drivers/DatabaseDriverTest.php @@ -10,17 +10,17 @@ beforeEach(function () { config([ 'settings.driver' => 'database', - 'settings.teams' => true, - 'settings.team_foreign_key' => 'team_id', + 'settings.morphs' => true, + 'settings.morph_name' => 'model', ]); $this->driver = new DatabaseDriver( connection: app('db')->connection(), table: 'settings', - teamForeignKey: 'team_id', + morphName: 'model', ); - migrateTeams(); + migrateMorphs(); setDatabaseDriverConnection(); }); @@ -32,7 +32,7 @@ $this->assertDatabaseHas('settings', [ 'key' => 'foo', 'value' => 'bar', - 'team_id' => null, + 'model_id' => null, ]); }); @@ -44,7 +44,7 @@ $this->assertDatabaseHas('settings', [ 'key' => 'foo', 'value' => 'bar', - 'team_id' => 1, + 'model_id' => 1, ]); }); @@ -54,7 +54,7 @@ $this->assertDatabaseHas('settings', [ 'key' => 'foo', 'value' => 'bar', - 'team_id' => null, + 'model_id' => null, ]); $this->driver->set('foo', 'updated value', false); @@ -64,7 +64,7 @@ $this->assertDatabaseHas('settings', [ 'key' => 'foo', 'value' => 'updated value', - 'team_id' => null, + 'model_id' => null, ]); }); @@ -81,13 +81,13 @@ $this->assertDatabaseHas('settings', [ 'key' => 'foo', 'value' => 'no team value', - 'team_id' => null, + 'model_id' => null, ]); $this->assertDatabaseHas('settings', [ 'key' => 'foo', 'value' => 'updated team value', - 'team_id' => 1, + 'model_id' => 1, ]); }); @@ -110,24 +110,24 @@ it('gets a persisted setting value', function () { $this->driver->set('foo', 'some value', false); - expect($this->driver->get(key: 'foo', teamId: false))->toBe('some value'); + expect($this->driver->get(key: 'foo', morphId: false))->toBe('some value'); }); it('returns a default value for settings that are not persisted', function () { - expect($this->driver->get(key: 'foo', default: 'my default value', teamId: false))->toBe('my default value'); + expect($this->driver->get(key: 'foo', default: 'my default value', morphId: false))->toBe('my default value'); }); it('gets a persisted team value', function () { $this->driver->set('foo', 'no team value', null); $this->driver->set('foo', 'team value', 1); - expect($this->driver->get(key: 'foo', teamId: 1))->toBe('team value'); + expect($this->driver->get(key: 'foo', morphId: 1))->toBe('team value'); }); it('gets a default value for a team', function () { $this->driver->set('foo', 'no team value', null); - expect($this->driver->get(key: 'foo', default: 'my default', teamId: 1))->toBe('my default'); + expect($this->driver->get(key: 'foo', default: 'my default', morphId: 1))->toBe('my default'); }); it('removes persisted settings', function () { @@ -152,7 +152,7 @@ $this->assertDatabaseMissing('settings', [ 'key' => 'foo', - 'team_id' => 1, + 'model_id' => 1, ]); }); @@ -184,7 +184,7 @@ $this->driver->set('two', 'value 2', 1); $this->driver->set('one', 'team 2 value 1', 2); - $settings = $this->driver->all(teamId: 1); + $settings = $this->driver->all(morphId: 1); expect($settings)->toHaveCount(2) ->first()->value->toBe('value 1') @@ -221,7 +221,7 @@ $this->assertDatabaseCount('settings', 3); - $this->driver->flush(teamId: 1); + $this->driver->flush(morphId: 1); $this->assertDatabaseCount('settings', 1); }); diff --git a/tests/Feature/Drivers/EloquentDriverTest.php b/tests/Feature/Drivers/EloquentDriverTest.php index 71ecda8..75b9e7f 100644 --- a/tests/Feature/Drivers/EloquentDriverTest.php +++ b/tests/Feature/Drivers/EloquentDriverTest.php @@ -11,14 +11,14 @@ beforeEach(function () { config([ 'settings.driver' => 'eloquent', - 'settings.teams' => true, - 'settings.team_foreign_key' => 'team_id', + 'settings.morphs' => true, + 'settings.morph_name' => 'model', ]); $this->driver = new EloquentDriver(app(Setting::class)); $this->model = Setting::class; - migrateTeams(); + migrateMorphs(); }); it('creates new entries', function () { @@ -29,7 +29,7 @@ $this->assertDatabaseHas($this->model, [ 'key' => 'foo', 'value' => 'bar', - 'team_id' => null, + 'model_id' => null, ]); }); @@ -41,7 +41,7 @@ $this->assertDatabaseHas($this->model, [ 'key' => 'foo', 'value' => 'bar', - 'team_id' => 1, + 'model_id' => 1, ]); }); @@ -76,13 +76,13 @@ $this->assertDatabaseHas($this->model, [ 'key' => 'foo', 'value' => 'no team value', - 'team_id' => null, + 'model_id' => null, ]); $this->assertDatabaseHas($this->model, [ 'key' => 'foo', 'value' => 'updated team value', - 'team_id' => 1, + 'model_id' => 1, ]); }); @@ -105,24 +105,24 @@ it('gets a persisted setting value', function () { $this->driver->set('foo', 'bar', false); - expect($this->driver->get(key: 'foo', teamId: false))->toBe('bar'); + expect($this->driver->get(key: 'foo', morphId: false))->toBe('bar'); }); it('returns a default value for settings that are not persisted', function () { - expect($this->driver->get(key: 'foo', default: 'my default value', teamId: false))->toBe('my default value'); + expect($this->driver->get(key: 'foo', default: 'my default value', morphId: false))->toBe('my default value'); }); it('gets a persisted team value', function () { $this->driver->set('foo', 'no team value', null); $this->driver->set('foo', 'team value', 1); - expect($this->driver->get(key: 'foo', teamId: 1))->toBe('team value'); + expect($this->driver->get(key: 'foo', morphId: 1))->toBe('team value'); }); it('gets a default value for a team', function () { $this->driver->set('foo', 'no team value', null); - expect($this->driver->get(key: 'foo', default: 'my default', teamId: 1))->toBe('my default'); + expect($this->driver->get(key: 'foo', default: 'my default', morphId: 1))->toBe('my default'); }); it('removes persisted settings', function () { @@ -146,7 +146,7 @@ $this->assertDatabaseMissing('settings', [ 'key' => 'foo', - 'team_id' => 1, + 'morph_id' => 1, ]); }); @@ -178,7 +178,7 @@ $this->driver->set('two', 'value 2', 1); $this->driver->set('one', 'team 2 value 1', 2); - $settings = $this->driver->all(teamId: 1); + $settings = $this->driver->all(morphId: 1); expect($settings)->toHaveCount(2) ->first()->value->toBe('value 1') @@ -215,7 +215,7 @@ $this->assertDatabaseCount('settings', 3); - $this->driver->flush(teamId: 1); + $this->driver->flush(morphId: 1); $this->assertDatabaseCount('settings', 1); }); diff --git a/tests/Feature/SettingsTest.php b/tests/Feature/SettingsTest.php index ca4fdc4..e0542a9 100644 --- a/tests/Feature/SettingsTest.php +++ b/tests/Feature/SettingsTest.php @@ -461,7 +461,8 @@ Event::assertDispatched(function (SettingWasDeleted $event) { return $event->key === 'foo' - && $event->teamId === false + && $event->morphId === false + && $event->morphType === false && is_null($event->context); }); }); @@ -497,10 +498,10 @@ expect(SettingsFacade::cacheKeyForSetting('foo'))->toBe('settings.foo') ->and(SettingsFacade::context(new Context(['foo' => 'bar']))->cacheKeyForSetting('foo'))->toBe('settings.foo:c:::foo:bar'); - SettingsFacade::enableTeams(); - SettingsFacade::setTeamId(1); + SettingsFacade::enableMorphs(); + SettingsFacade::setMorphs(1); - expect(SettingsFacade::cacheKeyForSetting('foo'))->toBe('settings.foo::team:1'); + expect(SettingsFacade::cacheKeyForSetting('foo'))->toBe('settings.foo::morphId:1:morphType:null'); }); it('accepts a backed enum for a key instead of a string', function () { diff --git a/tests/Feature/TeamsTest.php b/tests/Feature/TeamsTest.php index 924622a..09a128e 100644 --- a/tests/Feature/TeamsTest.php +++ b/tests/Feature/TeamsTest.php @@ -16,51 +16,51 @@ 'settings.cache' => false, 'settings.cache_key_prefix' => 'settings.', 'settings.encryption' => false, - 'settings.teams' => true, - 'settings.team_foreign_key' => 'team_id', + 'settings.morphs' => true, ]); migrateTestTables(); - migrateTeams(); + migrateMorphs(); setDatabaseDriverConnection(); Team::factory()->create(); }); -test('teams can be enabled and disabled', function () { +test('morphs can be enabled and disabled', function () { // Should be enabled with the config value set to true - expect(SettingsFacade::teamsAreEnabled())->toBeTrue(); + expect(SettingsFacade::morphsAreEnabled())->toBeTrue(); - SettingsFacade::disableTeams(); - expect(SettingsFacade::teamsAreEnabled())->toBeFalse(); + SettingsFacade::disableMorphs(); + expect(SettingsFacade::morphsAreEnabled())->toBeFalse(); - SettingsFacade::enableTeams(); - expect(SettingsFacade::teamsAreEnabled())->toBeTrue(); + SettingsFacade::enableMorphs(); + expect(SettingsFacade::morphsAreEnabled())->toBeTrue(); }); test('team id can be set', function () { - expect(SettingsFacade::getTeamId())->toBeNull(); + expect(SettingsFacade::getMorphId())->toBeNull(); - SettingsFacade::setTeamId(1); + SettingsFacade::setMorphs(1); - expect(SettingsFacade::getTeamId())->toBe(1); + expect(SettingsFacade::getMorphId())->toBe(1); }); it('sets a team id when saving', function () { $team = Team::first(); - SettingsFacade::setTeamId($team); + SettingsFacade::setMorphs($team); SettingsFacade::set('foo', 'bar'); $setting = DB::table('settings')->first(); - expect($setting->team_id)->toBe($team->id); + expect($setting->model_id)->toBe($team->id) + ->and($setting->model_type)->toBe($team->getMorphClass()); }); it('updates team settings', function () { $team = Team::first(); - SettingsFacade::setTeamId($team); + SettingsFacade::setMorphs($team); SettingsFacade::set('foo', 'bar'); SettingsFacade::set('foo', 'updated'); @@ -70,7 +70,8 @@ $setting = DB::table('settings')->first(); $value = unserialize($setting->value); - expect($setting)->team_id->toBe($team->id) + expect($setting)->model_id->toBe($team->id) + ->and($setting->model_type)->toBe($team->getMorphClass()) ->and($value)->toBe('updated'); }); @@ -78,20 +79,22 @@ $team1 = Team::first(); $team2 = Team::factory()->create(); - SettingsFacade::setTeamId($team1); + SettingsFacade::setMorphs($team1); SettingsFacade::set('foo', 'team 1 value'); - SettingsFacade::setTeamId($team2); + SettingsFacade::setMorphs($team2); SettingsFacade::set('foo', 'team 2 value'); $this->assertDatabaseCount('settings', 2); - $setting1 = DB::table('settings')->where('team_id', $team1->id)->first(); - $setting2 = DB::table('settings')->where('team_id', $team2->id)->first(); + $setting1 = DB::table('settings')->where('model_id', $team1->id)->where('model_type', $team1->getMorphClass())->first(); + $setting2 = DB::table('settings')->where('model_id', $team2->id)->where('model_type', $team2->getMorphClass())->first(); - expect($setting1->team_id)->toBe($team1->id) + expect($setting1->model_id)->toBe($team1->id) + ->and($setting1->model_type)->toBe($team2->getMorphClass()) ->and(unserialize($setting1->value))->toBe('team 1 value') - ->and($setting2->team_id)->toBe($team2->id) + ->and($setting2->model_id)->toBe($team2->id) + ->and($setting2->model_type)->toBe($team2->getMorphClass()) ->and(unserialize($setting2->value))->toBe('team 2 value') ->and($setting1->key)->toBe($setting2->key); }); @@ -102,7 +105,7 @@ $team = Team::first(); - SettingsFacade::setTeamId($team); + SettingsFacade::setMorphs($team); expect(SettingsFacade::has('foo'))->toBeFalse(); SettingsFacade::set('foo', 'team value'); @@ -114,15 +117,15 @@ $team2 = Team::factory()->create(); // Also verify that no team id can be used - SettingsFacade::setTeamId(null); + SettingsFacade::setMorphs(null); SettingsFacade::set('foo', 'no team value'); expect(SettingsFacade::get('foo'))->toBe('no team value'); - SettingsFacade::setTeamId($team); + SettingsFacade::setMorphs($team); SettingsFacade::set('foo', 'team value'); expect(SettingsFacade::get('foo'))->toBe('team value'); - SettingsFacade::setTeamId($team2); + SettingsFacade::setMorphs($team2); SettingsFacade::set('foo', 'team 2 value'); expect(SettingsFacade::get('foo'))->toBe('team 2 value'); }); @@ -132,7 +135,7 @@ SettingsFacade::set('foo', 'no team value'); - SettingsFacade::setTeamId($team); + SettingsFacade::setMorphs($team); SettingsFacade::set('foo', 'team value'); $this->assertDatabaseCount('settings', 2); @@ -141,10 +144,10 @@ $this->assertDatabaseCount('settings', 1); $this->assertDatabaseMissing('settings', [ - 'team_id' => $team->id, + 'model_id' => $team->id, ]); $this->assertDatabaseHas('settings', [ - 'team_id' => null, + 'model_id' => null, ]); }); @@ -152,17 +155,17 @@ $team = Team::first(); $team2 = Team::factory()->create(); - SettingsFacade::setTeamId($team); + SettingsFacade::setMorphs($team); SettingsFacade::set('foo', 'team 1 value'); - SettingsFacade::setTeamId($team2); + SettingsFacade::setMorphs($team2); SettingsFacade::set('foo', 'team 2 value'); SettingsFacade::enableCache(); expect(SettingsFacade::get('foo'))->toBe('team 2 value'); - SettingsFacade::setTeamId($team); + SettingsFacade::setMorphs($team); expect(SettingsFacade::get('foo'))->toBe('team 1 value'); }); @@ -176,7 +179,7 @@ $settings->set('one', 'non-team value'); $settings->context(new Context(['id' => 'foo']))->set('one', 'non-team value context 1'); - $settings->setTeamId($team); + $settings->setMorphs($team); $settings->set('one', 'team value'); $settings->set('two', 'team value 2'); @@ -186,13 +189,13 @@ expect($storedSettings)->toHaveCount(3) ->and($storedSettings[0]->key)->toBe('one') - ->and($storedSettings[0]->team_id)->toBe($team->id) + ->and($storedSettings[0]->model_id)->toBe($team->id) ->and($storedSettings[0]->value)->toBe('team value') ->and($storedSettings[1]->key)->toBe('two') - ->and($storedSettings[1]->team_id)->toBe($team->id) + ->and($storedSettings[1]->model_id)->toBe($team->id) ->and($storedSettings[1]->value)->toBe('team value 2') ->and($storedSettings[2]->key)->toBe('one') - ->and($storedSettings[2]->team_id)->toBe($team->id) + ->and($storedSettings[2]->model_id)->toBe($team->id) ->and($storedSettings[2]->value)->toBe('team value context 1') ->and($storedSettings[2]->original_key)->toBe('one:c:::id:foo'); @@ -211,7 +214,7 @@ $settings->set('one', 'non-team value'); - $settings->setTeamId($team); + $settings->setMorphs($team); $settings->set('one', 'team value'); $this->assertDatabaseCount('settings', 2); @@ -221,7 +224,7 @@ $this->assertDatabaseCount('settings', 1); $this->assertDatabaseMissing('settings', [ - 'team_id' => $team->id, + 'model_id' => $team->id, ]); }); @@ -229,41 +232,41 @@ $team = Team::first(); $otherTeam = Team::factory()->create(); - SettingsFacade::setTeamId($team); + SettingsFacade::setMorphs($team); SettingsFacade::set('foo', 'team 1'); - SettingsFacade::usingTeam($otherTeam)->set('foo', 'team 2'); + SettingsFacade::usingMorph($otherTeam)->set('foo', 'team 2'); $this->assertDatabaseCount('settings', 2); - expect(SettingsFacade::getTeamId())->toBe($team->id) + expect(SettingsFacade::getMorphId())->toBe($team->id) ->and(SettingsFacade::get('foo'))->toBe('team 1') - ->and(SettingsFacade::usingTeam($otherTeam)->get('foo'))->toBe('team 2'); + ->and(SettingsFacade::usingMorph($otherTeam)->get('foo'))->toBe('team 2'); }); test('when a team is set, team id can be set to null temporarily', function () { $team = Team::first(); - SettingsFacade::setTeamId($team); + SettingsFacade::setMorphs($team); SettingsFacade::set('foo', 'team 1'); - SettingsFacade::withoutTeams()->set('foo', 'global'); + SettingsFacade::withoutMorphs()->set('foo', 'global'); $this->assertDatabaseCount('settings', 2); - expect(SettingsFacade::getTeamId())->toBe($team->id) + expect(SettingsFacade::getMorphId())->toBe($team->id) ->and(SettingsFacade::get('foo'))->toBe('team 1') - ->and(SettingsFacade::withoutTeams()->get('foo'))->toBe('global'); + ->and(SettingsFacade::withoutMorphs()->get('foo'))->toBe('global'); }); test('a team can be used temporarily when teams are not enabled', function () { - SettingsFacade::disableTeams(); + SettingsFacade::disableMorphs(); SettingsFacade::set('foo', 'global'); $team = Team::first(); - SettingsFacade::usingTeam($team)->set('foo', 'team 1'); + SettingsFacade::usingMorph($team)->set('foo', 'team 1'); $this->assertDatabaseCount('settings', 2); - expect(SettingsFacade::getTeamId())->toBeNull() - ->and(SettingsFacade::teamsAreEnabled())->toBeFalse() + expect(SettingsFacade::getMorphId())->toBeNull() + ->and(SettingsFacade::morphsAreEnabled())->toBeFalse() ->and(SettingsFacade::get('foo'))->toBe('global') - ->and(SettingsFacade::usingTeam($team)->get('foo'))->toBe('team 1'); + ->and(SettingsFacade::usingMorph($team)->get('foo'))->toBe('team 1'); }); diff --git a/tests/Pest.php b/tests/Pest.php index 8a2054b..35b8f16 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -31,8 +31,8 @@ function migrateTestTables(): void $migration->up(); } -function migrateTeams(): void +function migrateMorphs(): void { - $migration = include __DIR__ . '/../database/migrations/add_settings_team_field.php.stub'; + $migration = include __DIR__ . '/../database/migrations/add_settings_morphs_fields.php.stub'; $migration->up(); } diff --git a/tests/Support/Drivers/CustomDriver.php b/tests/Support/Drivers/CustomDriver.php index 07536b2..6a3e80e 100644 --- a/tests/Support/Drivers/CustomDriver.php +++ b/tests/Support/Drivers/CustomDriver.php @@ -9,30 +9,30 @@ final class CustomDriver implements Driver { - public function forget($key, $teamId = null): void + public function forget($key, $morphId = null, $morphType = null): void { // } - public function get(string $key, $default = null, $teamId = null) + public function get(string $key, $default = null, $morphId = null, $morphType = null) { return $default; } - public function has($key, $teamId = null): bool + public function has($key, $morphId = null, $morphType = null): bool { return true; } - public function set(string $key, $value = null, $teamId = null): void + public function set(string $key, $value = null, $morphId = null, $morphType = null): void { // } - public function all($teamId = null, $keys = null): array|Arrayable + public function all($morphId = null, $morphType = null, $keys = null): array|Arrayable { return []; } - public function flush($teamId = null, $keys = null): void {} + public function flush($morphId = null, $keys = null, $morphType = null): void {} } diff --git a/tests/Support/Models/Team.php b/tests/Support/Models/Team.php index 1b0fe99..f66fee4 100644 --- a/tests/Support/Models/Team.php +++ b/tests/Support/Models/Team.php @@ -4,6 +4,7 @@ namespace Rawilk\Settings\Tests\Support\Models; +use Illuminate\Database\Eloquent\Concerns\HasUuids; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Rawilk\Settings\Tests\Support\database\factories\TeamFactory; @@ -11,6 +12,7 @@ final class Team extends Model { use HasFactory; + use HasUuids; protected static function newFactory(): TeamFactory { diff --git a/tests/Support/database/migrations/create_test_tables.php b/tests/Support/database/migrations/create_test_tables.php index e5b7ead..c969baa 100644 --- a/tests/Support/database/migrations/create_test_tables.php +++ b/tests/Support/database/migrations/create_test_tables.php @@ -23,7 +23,7 @@ public function up(): void }); Schema::create('teams', function (Blueprint $table) { - $table->id(); + $table->uuid('id')->primary(); $table->string('name'); $table->timestamps(); });