From a2d5dd3397707be167fd27a23af0da133f7579f0 Mon Sep 17 00:00:00 2001 From: Sami Mazouz Date: Tue, 5 Jan 2021 07:28:25 +0100 Subject: [PATCH] Add default value to Settings extender (#2495) --- src/Extend/Settings.php | 7 +-- tests/integration/extenders/SettingsTest.php | 50 ++++++++++++++++++++ 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/src/Extend/Settings.php b/src/Extend/Settings.php index a2c66b219..c8a3e452c 100644 --- a/src/Extend/Settings.php +++ b/src/Extend/Settings.php @@ -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; } @@ -45,7 +46,7 @@ class Settings implements ExtenderInterface $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); diff --git a/tests/integration/extenders/SettingsTest.php b/tests/integration/extenders/SettingsTest.php index 854c01465..6a8f0b142 100644 --- a/tests/integration/extenders/SettingsTest.php +++ b/tests/integration/extenders/SettingsTest.php @@ -122,6 +122,56 @@ class SettingsTest extends TestCase $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