mirror of
https://github.com/archtechx/tenancy.git
synced 2026-05-06 18:04:03 +00:00
Throw exception if override closure doesn't return array
This commit is contained in:
parent
cdea112ad5
commit
8fda84fcee
2 changed files with 17 additions and 3 deletions
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue