Skip to content

Commit 19443a5

Browse files
Add SettingsChanged event (#39)
* Add SettingsUpdated event * "Updated" → "Changed"
1 parent c9d896f commit 19443a5

File tree

3 files changed

+59
-26
lines changed

3 files changed

+59
-26
lines changed

src/Events/SettingsChanged.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Bakerkretzmar\NovaSettingsTool\Events;
4+
5+
class SettingsChanged
6+
{
7+
public $settings;
8+
public $oldSettings;
9+
10+
public function __construct(array $settings, array $oldSettings)
11+
{
12+
$this->settings = $settings;
13+
$this->oldSettings = $oldSettings;
14+
}
15+
}

src/Http/Controllers/SettingsToolController.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Bakerkretzmar\NovaSettingsTool\Http\Controllers;
44

5+
use Bakerkretzmar\NovaSettingsTool\Events\SettingsChanged;
56
use Illuminate\Http\Request;
67
use Spatie\Valuestore\Valuestore;
78

@@ -46,10 +47,14 @@ public function read()
4647

4748
public function write(Request $request)
4849
{
50+
$oldSettings = $this->store->all();
51+
4952
foreach ($request->all() as $key => $value) {
5053
$this->store->put($key, $value);
5154
}
5255

56+
event(new SettingsChanged($this->store->all(), $oldSettings));
57+
5358
return response()->json();
5459
}
5560
}

tests/SettingsToolControllerTest.php

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,21 @@
22

33
namespace Bakerkretzmar\NovaSettingsTool\Tests;
44

5+
use Bakerkretzmar\NovaSettingsTool\Events\SettingsChanged;
6+
use Illuminate\Support\Facades\Event;
57
use Illuminate\Support\Facades\Storage;
68

79
class SettingsToolControllerTest extends TestCase
810
{
911
/** @test */
1012
public function can_read_settings()
1113
{
12-
$response = $this->get('nova-vendor/settings-tool');
13-
14-
$response->assertSuccessful();
15-
$response->assertJsonFragment([
16-
'key' => 'test_setting',
17-
'value' => 'https://example.com',
18-
]);
14+
$this->get('nova-vendor/settings-tool')
15+
->assertSuccessful()
16+
->assertJsonFragment([
17+
'key' => 'test_setting',
18+
'value' => 'https://example.com',
19+
]);
1920
}
2021

2122
/** @test */
@@ -28,37 +29,49 @@ public function can_read_settings_from_custom_path()
2829

2930
config(['nova-settings-tool.path' => base_path() . '/storage/app/public/custom/configurations.json']);
3031

31-
$response = $this->get('nova-vendor/settings-tool');
32-
33-
$response->assertSuccessful();
34-
$response->assertJsonFragment([
35-
'key' => 'test_setting',
36-
'value' => 'https://example.com',
37-
]);
32+
$this->get('nova-vendor/settings-tool')
33+
->assertSuccessful()
34+
->assertJsonFragment([
35+
'key' => 'test_setting',
36+
'value' => 'https://example.com',
37+
]);
3838
}
3939

4040
/** @test */
4141
public function can_fill_default_setting_metadata_automatically()
4242
{
43-
$response = $this->get('nova-vendor/settings-tool');
44-
45-
$response->assertJsonFragment([
46-
'key' => 'setting_with_no_metadata',
47-
'type' => 'text',
48-
'label' => 'Setting_with_no_metadata',
49-
'value' => null,
50-
]);
43+
$this->get('nova-vendor/settings-tool')
44+
->assertJsonFragment([
45+
'key' => 'setting_with_no_metadata',
46+
'type' => 'text',
47+
'label' => 'Setting_with_no_metadata',
48+
'value' => null,
49+
]);
5150
}
5251

5352
/** @test */
5453
public function can_write_settings()
5554
{
56-
$response = $this->postJson('nova-vendor/settings-tool', [
55+
$this->postJson('nova-vendor/settings-tool', [
5756
'test_setting' => 'http://google.ca',
58-
]);
59-
60-
$response->assertSuccessful();
57+
])->assertSuccessful();
6158
$this->assertArrayHasKey('test_setting', json_decode(Storage::get('settings.json'), true));
6259
$this->assertSame('http://google.ca', json_decode(Storage::get('settings.json'), true)['test_setting']);
6360
}
61+
62+
/** @test */
63+
public function can_emit_event_when_settings_updated()
64+
{
65+
Event::fake();
66+
67+
$this->postJson('nova-vendor/settings-tool', [
68+
'test_setting' => 'http://google.ca',
69+
])->assertSuccessful();
70+
71+
Event::assertDispatched(function (SettingsChanged $event) {
72+
$this->assertSame('http://google.ca', $event->settings['test_setting']);
73+
$this->assertSame('https://example.com', $event->oldSettings['test_setting']);
74+
return true;
75+
});
76+
}
6477
}

0 commit comments

Comments
 (0)