Skip to content

Commit b374118

Browse files
committed
Refactor type hints and add LazySetting tests
Standardize type hints for nullable parameters and add comprehensive tests for key functionalities of the LazySetting class. This includes validation, cache behavior, and default value handling.
1 parent 668cc3e commit b374118

File tree

2 files changed

+53
-6
lines changed

2 files changed

+53
-6
lines changed

src/LazySetting.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Support\Collection;
66
use Illuminate\Support\Facades\Log;
77
use InvalidArgumentException;
8+
use JsonException;
89
use Step2Dev\LazySetting\Models\Setting;
910
use Throwable;
1011

@@ -41,7 +42,7 @@ public static function getCacheKey(): string
4142
return config('lazy-setting.cache_prefix').'settings';
4243
}
4344

44-
public static function getCacheTtl(): int
45+
public static function getCacheTtl(): int|null
4546
{
4647
return config('lazy-setting.cache_ttl');
4748
}
@@ -93,12 +94,12 @@ public function getKeyAndGroup(string $key): array
9394
return compact('key', 'group');
9495
}
9596

96-
public function get(string $key, mixed $default = null): ?string
97+
public function get(string $key, mixed $default = null): string|null
9798
{
9899
return $this->getConfig($key)?->value ?? $default;
99100
}
100101

101-
public function getConfig(string $key): ?Setting
102+
public function getConfig(string $key): Setting|null
102103
{
103104
try {
104105
['group' => $group, 'key' => $key] = $this->getKeyAndGroup($key);
@@ -116,7 +117,7 @@ public function getConfig(string $key): ?Setting
116117
/**
117118
* @throws Throwable
118119
*/
119-
public function set(string $key, mixed $data, ?string $type = null): Setting
120+
public function set(string $key, mixed $data, string|null $type = null): Setting
120121
{
121122
if ($setting = $this->getConfig($key)) {
122123
$this->update($setting, $data);
@@ -132,6 +133,9 @@ public function all(): Collection
132133
return $this->getSettings();
133134
}
134135

136+
/**
137+
* @throws JsonException
138+
*/
135139
public function update(Setting $setting, mixed $data): Setting
136140
{
137141
$setting->update($this->formatData($data, $setting->type));
@@ -144,7 +148,7 @@ public function update(Setting $setting, mixed $data): Setting
144148
/**
145149
* @throws Throwable
146150
*/
147-
public function createIfNotExists(string $key, mixed $data, ?string $type = null): Setting
151+
public function createIfNotExists(string $key, mixed $data, string|null $type = null): Setting
148152
{
149153
$setting = Setting::firstOrCreate($this->getKeyAndGroup($key), $this->formatData($data, $type));
150154

@@ -169,7 +173,10 @@ public function getFieldType(string $type = ''): string
169173
};
170174
}
171175

172-
final protected function formatData(array|string $data, ?string $type = null, ?array $options = []): array
176+
/**
177+
* @throws JsonException
178+
*/
179+
final protected function formatData(array|string $data, string|null $type = null, array|null $options = []): array
173180
{
174181
$type = $this->getFieldType($type);
175182
$result = compact('type');

tests/Feature/LazySettingTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
use Step2Dev\LazySetting\Facades\LazySetting;
4+
5+
it('can get a setting', function () {
6+
LazySetting::set('site_name', 'My Website');
7+
$setting = LazySetting::get('site_name');
8+
expect($setting)->toBe('My Website');
9+
});
10+
11+
it('can set a new setting', function () {
12+
LazySetting::set('site_description', 'Best website');
13+
$setting = LazySetting::get('site_description');
14+
expect($setting)->toBe('Best website');
15+
});
16+
17+
it('can update an existing setting', function () {
18+
LazySetting::set('site_name', 'Old Name');
19+
LazySetting::set('site_name', 'New Name');
20+
$setting = LazySetting::get('site_name');
21+
expect($setting)->toBe('New Name');
22+
});
23+
24+
it('returns default value if setting does not exist', function () {
25+
$setting = LazySetting::get('non_existing_key', 'Default Value');
26+
expect($setting)->toBe('Default Value');
27+
});
28+
29+
it('validates the key and value when setting a setting', function () {
30+
$this->expectException(InvalidArgumentException::class);
31+
LazySetting::set('', 'Invalid Key');
32+
});
33+
34+
it('clears the cache when setting a new value', function () {
35+
LazySetting::set('cached_key', 'Cached Value');
36+
$cachedValue = LazySetting::get('cached_key');
37+
LazySetting::set('cached_key', 'New Value');
38+
$newValue = LazySetting::get('cached_key');
39+
expect($newValue)->toBe('New Value');
40+
});

0 commit comments

Comments
 (0)