diff --git a/src/Bootstrappers/LogTenancyBootstrapper.php b/src/Bootstrappers/LogTenancyBootstrapper.php index 5631e5bc..ea1f3393 100644 --- a/src/Bootstrappers/LogTenancyBootstrapper.php +++ b/src/Bootstrappers/LogTenancyBootstrapper.php @@ -44,7 +44,9 @@ class LogTenancyBootstrapper implements TenancyBootstrapper * * Examples: * - Array mapping (the default approach): ['slack' => ['url' => 'webhookUrl']] maps $tenant->webhookUrl to slack.url (if $tenant->webhookUrl is not null, otherwise, the override is ignored) - * - Closure: ['slack' => fn (Tenant $tenant, array $channel) => array_merge($channel, ['url' => $tenant->slackUrl])] + * - Closure: ['slack' => fn (Tenant $tenant, array $channel) => array_merge($channel, ['url' => $tenant->slackUrl])] (the closure should return the whole channel's config) + * + * In both cases, the override should be an array. */ public static array $channelOverrides = []; @@ -140,7 +142,13 @@ class LogTenancyBootstrapper implements TenancyBootstrapper } elseif ($override instanceof Closure) { $channelConfigKey = "logging.channels.{$channel}"; - $this->config->set($channelConfigKey, $override($tenant, $this->config->get($channelConfigKey))); + $result = $override($tenant, $this->config->get($channelConfigKey)); + + if (! is_array($result)) { + throw new \InvalidArgumentException("Channel override closure for '{$channel}' must return an array."); + } + + $this->config->set($channelConfigKey, $result); } } diff --git a/tests/Bootstrappers/LogTenancyBootstrapperTest.php b/tests/Bootstrappers/LogTenancyBootstrapperTest.php index dd530dfc..d738d1d5 100644 --- a/tests/Bootstrappers/LogTenancyBootstrapperTest.php +++ b/tests/Bootstrappers/LogTenancyBootstrapperTest.php @@ -140,6 +140,11 @@ test('channel overrides work correctly with both arrays and closures', function $tenant = Tenant::create(['webhookUrl' => 'tenant-webhook']); + // Channel override closures must return an array, otherwise an exception is thrown + LogTenancyBootstrapper::$channelOverrides['slack'] = fn (Tenant $tenant, array $channel) => 'invalid override'; + + expect(fn() => tenancy()->initialize($tenant))->toThrow(InvalidArgumentException::class); + // Test both array mapping and closure-based overrides LogTenancyBootstrapper::$channelOverrides = [ 'slack' => ['url' => 'webhookUrl'], // slack.url will be mapped to $tenant->webhookUrl @@ -148,7 +153,8 @@ test('channel overrides work correctly with both arrays and closures', function }, ]; - tenancy()->initialize($tenant); + // Reinitialize tenancy to apply the new overrides + tenancy()->reinitialize(); // Array mapping overrides work expect(config('logging.channels.slack.url'))->toBe($tenant->webhookUrl);