From 44803ab54fe2ad51588d88f55b0ef0bf7000bcba Mon Sep 17 00:00:00 2001 From: MGatner Date: Mon, 15 Nov 2021 16:47:46 +0000 Subject: [PATCH] Require configuration --- src/Config/Services.php | 10 +++++++--- src/Settings.php | 5 +---- tests/SettingsTest.php | 32 ++++++++++++++++---------------- 3 files changed, 24 insertions(+), 23 deletions(-) diff --git a/src/Config/Services.php b/src/Config/Services.php index 6cc927c..2baf619 100644 --- a/src/Config/Services.php +++ b/src/Config/Services.php @@ -3,6 +3,7 @@ namespace Sparks\Settings\Config; use CodeIgniter\Config\BaseService; +use Sparks\Settings\Config\Settings as SettingsConfig; use Sparks\Settings\Settings; /** @@ -23,12 +24,15 @@ class Services extends BaseService /** * Returns the Settings manager class. */ - public static function settings(bool $getShared = true): Settings + public static function settings(?SettingsConfig $config = null, bool $getShared = true): Settings { if ($getShared) { - return static::getSharedInstance('settings'); + return static::getSharedInstance('settings', $config); } - return new Settings(config('Settings')); + /** @var SettingsConfig $config */ + $config = $config ?? config('Settings'); + + return new Settings($config); } } diff --git a/src/Settings.php b/src/Settings.php index c596ff5..232081b 100644 --- a/src/Settings.php +++ b/src/Settings.php @@ -31,11 +31,8 @@ class Settings /** * Grabs instances of our handlers. */ - public function __construct(?SettingsConfig $config = null) + public function __construct(SettingsConfig $config) { - /** @var SettingsConfig $config */ - $config = $config ?? config('Settings'); - foreach ($config->handlers as $handler) { $class = $config->{$handler}['class'] ?? null; diff --git a/tests/SettingsTest.php b/tests/SettingsTest.php index c41f3db..6fe105a 100644 --- a/tests/SettingsTest.php +++ b/tests/SettingsTest.php @@ -29,21 +29,21 @@ public function testSettingsUsesParameter() public function testSettingsGetsFromConfig() { - $settings = new Settings(); + $settings = new Settings(config('Settings')); $this->assertSame(config('Test')->siteName, $settings->get('Test.siteName')); } public function testSettingsDatabaseNotFound() { - $settings = new Settings(); + $settings = new Settings(config('Settings')); $this->assertSame(config('Test')->siteName, $settings->get('Test.siteName')); } public function testSetInsertsNewRows() { - $settings = new Settings(); + $settings = new Settings(config('Settings')); $results = $settings->set('Test.siteName', 'Foo'); @@ -58,7 +58,7 @@ public function testSetInsertsNewRows() public function testSetInsertsBoolTrue() { - $settings = new Settings(); + $settings = new Settings(config('Settings')); $results = $settings->set('Test.siteName', true); @@ -75,7 +75,7 @@ public function testSetInsertsBoolTrue() public function testSetInsertsBoolFalse() { - $settings = new Settings(); + $settings = new Settings(config('Settings')); $results = $settings->set('Test.siteName', false); @@ -92,7 +92,7 @@ public function testSetInsertsBoolFalse() public function testSetInsertsNull() { - $settings = new Settings(); + $settings = new Settings(config('Settings')); $results = $settings->set('Test.siteName', null); @@ -109,7 +109,7 @@ public function testSetInsertsNull() public function testSetInsertsArray() { - $settings = new Settings(); + $settings = new Settings(config('Settings')); $data = ['foo' => 'bar']; $results = $settings->set('Test.siteName', $data); @@ -127,7 +127,7 @@ public function testSetInsertsArray() public function testSetInsertsObject() { - $settings = new Settings(); + $settings = new Settings(config('Settings')); $data = (object) ['foo' => 'bar']; $results = $settings->set('Test.siteName', $data); @@ -145,7 +145,7 @@ public function testSetInsertsObject() public function testSetUpdatesExistingRows() { - $settings = new Settings(); + $settings = new Settings(config('Settings')); $this->hasInDatabase($this->table, [ 'class' => 'Tests\Support\Config\Test', @@ -167,7 +167,7 @@ public function testSetUpdatesExistingRows() public function testWorksWithoutConfigClass() { - $settings = new Settings(); + $settings = new Settings(config('Settings')); $results = $settings->set('Nada.siteName', 'Bar'); @@ -183,7 +183,7 @@ public function testWorksWithoutConfigClass() public function testForgetSuccess() { - $settings = new Settings(); + $settings = new Settings(config('Settings')); $this->hasInDatabase($this->table, [ 'class' => 'Tests\Support\Config\Test', @@ -204,7 +204,7 @@ public function testForgetSuccess() public function testForgetWithNoStoredRecord() { - $settings = new Settings(); + $settings = new Settings(config('Settings')); $results = $settings->forget('Test.siteName'); @@ -213,7 +213,7 @@ public function testForgetWithNoStoredRecord() public function testSetWithContext() { - $settings = new Settings(); + $settings = new Settings(config('Settings')); $results = $settings->set('Test.siteName', 'Banana', 'environment:test'); @@ -229,7 +229,7 @@ public function testSetWithContext() public function testGetWithContext() { - $settings = new Settings(); + $settings = new Settings(config('Settings')); $settings->set('Test.siteName', 'NoContext'); $settings->set('Test.siteName', 'YesContext', 'testing:true'); @@ -240,7 +240,7 @@ public function testGetWithContext() public function testGetWithoutContextUsesGlobal() { - $settings = new Settings(); + $settings = new Settings(config('Settings')); $settings->set('Test.siteName', 'NoContext'); @@ -249,7 +249,7 @@ public function testGetWithoutContextUsesGlobal() public function testForgetWithContext() { - $settings = new Settings(); + $settings = new Settings(config('Settings')); $settings->set('Test.siteName', 'Bar'); $settings->set('Test.siteName', 'Amnesia', 'category:disease');