Skip to content

Commit

Permalink
Add SettingsChanged event (#39)
Browse files Browse the repository at this point in the history
* Add SettingsUpdated event

* "Updated" → "Changed"
  • Loading branch information
bakerkretzmar authored Nov 18, 2020
1 parent c9d896f commit 19443a5
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 26 deletions.
15 changes: 15 additions & 0 deletions src/Events/SettingsChanged.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Bakerkretzmar\NovaSettingsTool\Events;

class SettingsChanged
{
public $settings;
public $oldSettings;

public function __construct(array $settings, array $oldSettings)
{
$this->settings = $settings;
$this->oldSettings = $oldSettings;
}
}
5 changes: 5 additions & 0 deletions src/Http/Controllers/SettingsToolController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Bakerkretzmar\NovaSettingsTool\Http\Controllers;

use Bakerkretzmar\NovaSettingsTool\Events\SettingsChanged;
use Illuminate\Http\Request;
use Spatie\Valuestore\Valuestore;

Expand Down Expand Up @@ -46,10 +47,14 @@ public function read()

public function write(Request $request)
{
$oldSettings = $this->store->all();

foreach ($request->all() as $key => $value) {
$this->store->put($key, $value);
}

event(new SettingsChanged($this->store->all(), $oldSettings));

return response()->json();
}
}
65 changes: 39 additions & 26 deletions tests/SettingsToolControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@

namespace Bakerkretzmar\NovaSettingsTool\Tests;

use Bakerkretzmar\NovaSettingsTool\Events\SettingsChanged;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Storage;

class SettingsToolControllerTest extends TestCase
{
/** @test */
public function can_read_settings()
{
$response = $this->get('nova-vendor/settings-tool');

$response->assertSuccessful();
$response->assertJsonFragment([
'key' => 'test_setting',
'value' => 'https://example.com',
]);
$this->get('nova-vendor/settings-tool')
->assertSuccessful()
->assertJsonFragment([
'key' => 'test_setting',
'value' => 'https://example.com',
]);
}

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

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

$response = $this->get('nova-vendor/settings-tool');

$response->assertSuccessful();
$response->assertJsonFragment([
'key' => 'test_setting',
'value' => 'https://example.com',
]);
$this->get('nova-vendor/settings-tool')
->assertSuccessful()
->assertJsonFragment([
'key' => 'test_setting',
'value' => 'https://example.com',
]);
}

/** @test */
public function can_fill_default_setting_metadata_automatically()
{
$response = $this->get('nova-vendor/settings-tool');

$response->assertJsonFragment([
'key' => 'setting_with_no_metadata',
'type' => 'text',
'label' => 'Setting_with_no_metadata',
'value' => null,
]);
$this->get('nova-vendor/settings-tool')
->assertJsonFragment([
'key' => 'setting_with_no_metadata',
'type' => 'text',
'label' => 'Setting_with_no_metadata',
'value' => null,
]);
}

/** @test */
public function can_write_settings()
{
$response = $this->postJson('nova-vendor/settings-tool', [
$this->postJson('nova-vendor/settings-tool', [
'test_setting' => 'http://google.ca',
]);

$response->assertSuccessful();
])->assertSuccessful();
$this->assertArrayHasKey('test_setting', json_decode(Storage::get('settings.json'), true));
$this->assertSame('http://google.ca', json_decode(Storage::get('settings.json'), true)['test_setting']);
}

/** @test */
public function can_emit_event_when_settings_updated()
{
Event::fake();

$this->postJson('nova-vendor/settings-tool', [
'test_setting' => 'http://google.ca',
])->assertSuccessful();

Event::assertDispatched(function (SettingsChanged $event) {
$this->assertSame('http://google.ca', $event->settings['test_setting']);
$this->assertSame('https://example.com', $event->oldSettings['test_setting']);
return true;
});
}
}

0 comments on commit 19443a5

Please sign in to comment.