Skip to content

Commit 251a52c

Browse files
committed
refactor: Streamlined tests to Pest style
feat: Added command tests feat: Added type tests fix: Return typing
1 parent b9ab0bc commit 251a52c

File tree

4 files changed

+66
-16
lines changed

4 files changed

+66
-16
lines changed

src/Facades/Setting.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
namespace ElipZis\Setting\Facades;
44

55
use ElipZis\Setting\Repositories\SettingRepository;
6+
use Illuminate\Database\Eloquent\Collection;
67
use Illuminate\Support\Facades\Facade;
78

89
/**
910
* @method static \ElipZis\Setting\Models\Setting|bool get(string $key)
1011
* @method static mixed getValue(string $key, mixed $default = null, mixed $devValue = null)
1112
* @method static \ElipZis\Setting\Models\Setting set(string $key, mixed $value, string $type = null)
12-
* @method static array all()
13+
* @method static Collection|\Illuminate\Support\Collection all()
1314
* @method static void storeConfig(?string $filename = null, mixed $data = null)
1415
*
1516
* @see SettingRepository

src/Repositories/SettingRepository.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Illuminate\Config\Repository;
88
use Illuminate\Contracts\Filesystem\Filesystem;
99
use Illuminate\Contracts\Foundation\Application;
10+
use Illuminate\Database\Eloquent\Collection;
1011
use Illuminate\Support\Facades\App;
1112
use Illuminate\Support\Facades\Cache;
1213
use Illuminate\Support\Facades\Storage;
@@ -39,7 +40,7 @@ public function storeConfig(?string $filename = null, mixed $data = null)
3940
/**
4041
* Return all settings from the database, keyed by the setting key
4142
*
42-
* @return mixed[string]
43+
* @return Collection|\Illuminate\Support\Collection
4344
*/
4445
public function all()
4546
{
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
use ElipZis\Setting\Commands\SyncSettingsCommand;
4+
use ElipZis\Setting\Models\Setting;
5+
use Illuminate\Foundation\Testing\RefreshDatabase;
6+
use Illuminate\Support\Facades\Storage;
7+
use function Pest\Laravel\artisan;
8+
9+
uses(RefreshDatabase::class);
10+
11+
beforeEach(function () {
12+
Setting::factory()->count(10)->create();
13+
});
14+
15+
it('throws no exceptions when running', function () {
16+
artisan(SyncSettingsCommand::class)->assertSuccessful();
17+
});
18+
19+
it('exports the settings to the disc', function () {
20+
artisan(SyncSettingsCommand::class)->assertSuccessful();
21+
expect(Storage::exists('settings.json'))->toBeTrue();
22+
});
23+
24+
it('has the correct settings written to the disc', function () {
25+
artisan(SyncSettingsCommand::class)->assertSuccessful();
26+
$settingsStr = Storage::get('settings.json');
27+
expect($settingsStr)->not()->toBeNull();
28+
expect($settingsStr)->toBeString();
29+
$settings = json_decode($settingsStr, true, 512, JSON_THROW_ON_ERROR);
30+
expect($settings)->toBeArray();
31+
});

tests/Facades/SettingTest.php

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
use Carbon\Carbon;
34
use ElipZis\Setting\Facades\Setting;
45
use ElipZis\Setting\Models\Setting as Model;
56
use Illuminate\Foundation\Testing\RefreshDatabase;
@@ -12,31 +13,47 @@
1213

1314
it('has many settings', function () {
1415
$settings = Setting::all();
15-
$this->assertNotEmpty($settings);
16-
$this->assertGreaterThanOrEqual(1, count($settings));
17-
18-
$this->assertIsString($settings->keys()->first());
19-
$this->assertNotNull($settings->first());
16+
expect($settings)->isNotEmpty()->toBeGreaterThanOrEqual(1);
17+
expect($settings->keys()->first())->toBeString();
18+
expect($settings->first())->not()->toBeNull();
2019
});
2120

22-
it('can return a setting', function () {
21+
it('can set and return a setting', function () {
2322
$setting = Setting::get('simple.setting');
24-
$this->assertNull($setting);
23+
expect($setting)->toBeNull();
2524

2625
Setting::set('simple.setting', 1);
2726
$setting = Setting::get('simple.setting');
28-
$this->assertNotNull($setting);
29-
$this->assertEquals(1, $setting->value);
27+
expect($setting)->not()->toBeNull();
28+
expect($setting->value)->toBe(1);
3029
});
3130

32-
it('can return a setting value', function () {
31+
it('can set and return a setting value', function () {
3332
$setting = Setting::getValue('simple.setting.value');
34-
$this->assertNull($setting);
33+
expect($setting)->toBeNull();
3534
$setting = Setting::getValue('simple.setting.value', 1);
36-
$this->assertEquals(1, $setting);
35+
expect($setting)->toBe(1);
3736

3837
Setting::set('simple.setting.value', 2);
3938
$setting = Setting::getValue('simple.setting.value');
40-
expect($setting)->not()->toBeNull();
41-
expect($setting)->toBe(2);
39+
expect($setting)->not()->toBeNull()->toBe(2);
40+
});
41+
42+
it('can set an array and return it', function () {
43+
Setting::set('simple.setting.array', ['key' => 'value']);
44+
$setting = Setting::getValue('simple.setting.array');
45+
expect($setting)->not()->toBeNull()->toBeArray()->toBe(['key' => 'value']);
46+
});
47+
48+
it('can set a date and return it', function () {
49+
$now = Carbon::now();
50+
Setting::set('simple.setting.datetime', $now);
51+
$setting = Setting::getValue('simple.setting.datetime');
52+
expect($setting)->not()->toBeNull()->toBeInstanceOf(Carbon::class);
53+
});
54+
55+
it('can set a string and return it', function () {
56+
Setting::set('simple.setting.string', 'string');
57+
$setting = Setting::getValue('simple.setting.string');
58+
expect($setting)->not()->toBeNull()->toBeString();
4259
});

0 commit comments

Comments
 (0)