Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add default value to Settings extender #2495

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/Extend/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ class Settings implements ExtenderInterface
* @param string $attributeName: The attribute name to be used in the ForumSerializer attributes array.
* @param string $key: The key of the setting.
* @param string|callable|null $callback: Optional callback to modify the value before serialization.
* @param mixed $default: Optional default serialized value. Will be run through the optional callback.
* @return $this
*/
public function serializeToForum(string $attributeName, string $key, $callback = null)
public function serializeToForum(string $attributeName, string $key, $callback = null, $default = null)
{
$this->settings[$key] = compact('attributeName', 'callback');
$this->settings[$key] = compact('attributeName', 'callback', 'default');

return $this;
}
Expand All @@ -45,7 +46,7 @@ function () use ($container) {
$attributes = [];

foreach ($this->settings as $key => $setting) {
$value = $settings->get($key, null);
$value = $settings->get($key, $setting['default']);

if (isset($setting['callback'])) {
$callback = ContainerUtil::wrapCallback($setting['callback'], $container);
Expand Down
50 changes: 50 additions & 0 deletions tests/integration/extenders/SettingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,56 @@ public function custom_setting_callback_works_with_invokable_class()
$this->assertArrayHasKey('customPrefix.customSetting2', $payload['data']['attributes']);
$this->assertEquals('customValueModifiedByInvokable', $payload['data']['attributes']['customPrefix.customSetting2']);
}

/**
* @test
*/
public function custom_setting_falls_back_to_default()
{
$this->extend(
(new Extend\Settings())
->serializeToForum('customPrefix.noCustomSetting', 'custom-prefix.no_custom_setting', null, 'customDefault')
);

$this->prepDb();

$response = $this->send(
$this->request('GET', '/api', [
'authenticatedAs' => 1,
])
);

$payload = json_decode($response->getBody(), true);

$this->assertArrayHasKey('customPrefix.noCustomSetting', $payload['data']['attributes']);
$this->assertEquals('customDefault', $payload['data']['attributes']['customPrefix.noCustomSetting']);
}

/**
* @test
*/
public function custom_setting_default_passed_to_callback()
{
$this->extend(
(new Extend\Settings())
->serializeToForum('customPrefix.noCustomSetting', 'custom-prefix.no_custom_setting', function ($value) {
return $value.'Modified2';
}, 'customDefault')
);

$this->prepDb();

$response = $this->send(
$this->request('GET', '/api', [
'authenticatedAs' => 1,
])
);

$payload = json_decode($response->getBody(), true);

$this->assertArrayHasKey('customPrefix.noCustomSetting', $payload['data']['attributes']);
$this->assertEquals('customDefaultModified2', $payload['data']['attributes']['customPrefix.noCustomSetting']);
}
}

class CustomInvokableClass
Expand Down