From c89a27e921c21ee06d7b2da231c5674a8cc22347 Mon Sep 17 00:00:00 2001 From: MGatner Date: Fri, 12 Nov 2021 15:12:38 +0000 Subject: [PATCH] Allow passing config --- src/Config/Services.php | 2 +- src/Settings.php | 13 +++++++++---- tests/SettingsTest.php | 11 +++++++++++ 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/Config/Services.php b/src/Config/Services.php index a49f92b..6cc927c 100644 --- a/src/Config/Services.php +++ b/src/Config/Services.php @@ -29,6 +29,6 @@ public static function settings(bool $getShared = true): Settings return static::getSharedInstance('settings'); } - return new Settings(); + return new Settings(config('Settings')); } } diff --git a/src/Settings.php b/src/Settings.php index 908eff0..806caf5 100644 --- a/src/Settings.php +++ b/src/Settings.php @@ -2,6 +2,8 @@ namespace Sparks\Settings; +use Sparks\Settings\Config\Settings as SettingsConfig; + /** * Allows developers a single location to store and * retrieve settings that were original set in config files @@ -27,10 +29,13 @@ class Settings /** * Grabs instances of our handlers. */ - public function __construct() + public function __construct(?SettingsConfig $config = null) { - foreach (config('Settings')->handlers as $handler) { - $class = config('Settings')->{$handler}['class'] ?? null; + /** @var SettingsConfig $config */ + $config = $config ?? config('Settings'); + + foreach ($config->handlers as $handler) { + $class = $config->{$handler}['class'] ?? null; if ($class === null) { continue; @@ -38,7 +43,7 @@ public function __construct() $this->handlers[$handler] = new $class(); - $writeable = config('Settings')->{$handler}['writeable'] ?? null; + $writeable = $config->{$handler}['writeable'] ?? null; if ($writeable) { $this->writeHandler = $handler; diff --git a/tests/SettingsTest.php b/tests/SettingsTest.php index eaecbc2..e017aeb 100644 --- a/tests/SettingsTest.php +++ b/tests/SettingsTest.php @@ -16,6 +16,17 @@ final class SettingsTest extends TestCase { use DatabaseTestTrait; + public function testSettingsUsesParameter() + { + $config = config('Settings'); + $config->handlers = []; + + $settings = new Settings($config); + $result = $this->getPrivateProperty($settings, 'handlers'); + + $this->assertSame([], $result); + } + public function testSettingsGetsFromConfig() { $settings = new Settings();