From 1ea3cefa1d1499211e8659a0b2e050b05ff0c4fe Mon Sep 17 00:00:00 2001 From: George Bishop Date: Sun, 21 Aug 2022 15:43:01 +0100 Subject: [PATCH] Add support for nested tenant config override (#920) * feat: add support for nested tenant config override * test: ensure nested tenant values are mapped --- src/Features/TenantConfig.php | 3 ++- tests/Features/TenantConfigTest.php | 24 ++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/Features/TenantConfig.php b/src/Features/TenantConfig.php index fb09a871..95691f0d 100644 --- a/src/Features/TenantConfig.php +++ b/src/Features/TenantConfig.php @@ -6,6 +6,7 @@ namespace Stancl\Tenancy\Features; use Illuminate\Contracts\Config\Repository; use Illuminate\Database\Eloquent\Model; +use Illuminate\Support\Arr; use Illuminate\Support\Facades\Event; use Stancl\Tenancy\Contracts\Feature; use Stancl\Tenancy\Contracts\Tenant; @@ -45,7 +46,7 @@ class TenantConfig implements Feature { /** @var Tenant|Model $tenant */ foreach (static::$storageToConfigMap as $storageKey => $configKey) { - $override = $tenant->getAttribute($storageKey); + $override = Arr::get($tenant, $storageKey); if (! is_null($override)) { if (is_array($configKey)) { diff --git a/tests/Features/TenantConfigTest.php b/tests/Features/TenantConfigTest.php index 37e26198..904e420b 100644 --- a/tests/Features/TenantConfigTest.php +++ b/tests/Features/TenantConfigTest.php @@ -22,6 +22,30 @@ class TenantConfigTest extends TestCase parent::tearDown(); } + /** @test */ + public function nested_tenant_values_are_merged() + { + $this->assertSame(null, config('whitelabel.theme')); + config([ + 'tenancy.features' => [TenantConfig::class], + 'tenancy.bootstrappers' => [], + ]); + Event::listen(TenancyInitialized::class, BootstrapTenancy::class); + Event::listen(TenancyEnded::class, RevertToCentralContext::class); + + TenantConfig::$storageToConfigMap = [ + 'whitelabel.config.theme' => 'whitelabel.theme', + ]; + + $tenant = Tenant::create([ + 'whitelabel' => ['config' => ['theme' => 'dark']], + ]); + + tenancy()->initialize($tenant); + $this->assertSame('dark', config('whitelabel.theme')); + tenancy()->end(); + } + /** @test */ public function config_is_merged_and_removed() {