1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-08-06 05:54:03 +00:00

minor test improvements

the main change here is adding similar assertions related to
*re-resolving* channels to the 'stack channels that include any
configured channel are re-resolved' test, similar to the 'channels are
forgotten and re-resolved during bootstrap and revert' test
This commit is contained in:
Samuel Stancl 2026-07-03 20:18:40 -07:00
parent ce5b36372f
commit f2b7c7d5ed
No known key found for this signature in database
GPG key ID: BA146259A1E16C57

View file

@ -12,7 +12,7 @@ use Stancl\Tenancy\Bootstrappers\LogTenancyBootstrapper;
use Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper; use Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
$cleanup = function () { afterEach($cleanup = function () {
LogTenancyBootstrapper::$channelOverrides = []; LogTenancyBootstrapper::$channelOverrides = [];
LogTenancyBootstrapper::$storagePathChannels = ['single', 'daily']; LogTenancyBootstrapper::$storagePathChannels = ['single', 'daily'];
@ -26,7 +26,7 @@ $cleanup = function () {
foreach ($logFiles as $path) { foreach ($logFiles as $path) {
@unlink($path); @unlink($path);
} }
}; });
beforeEach(function () use ($cleanup) { beforeEach(function () use ($cleanup) {
config([ config([
@ -42,8 +42,6 @@ beforeEach(function () use ($cleanup) {
Event::listen(TenancyEnded::class, RevertToCentralContext::class); Event::listen(TenancyEnded::class, RevertToCentralContext::class);
}); });
afterEach($cleanup);
test('storage path channels get tenant-specific paths by default', function () { test('storage path channels get tenant-specific paths by default', function () {
// Note that for LogTenancyBootstrapper to change the paths correctly by default, // Note that for LogTenancyBootstrapper to change the paths correctly by default,
// the bootstrapper MUST run after FilesystemTenancyBootstrapper. // the bootstrapper MUST run after FilesystemTenancyBootstrapper.
@ -77,7 +75,7 @@ test('storage path channels get tenant-specific paths by default', function () {
} }
}); });
test('all channels included in the log stack get processed correctly', function () { test('all channels included in a stack get processed correctly', function () {
config([ config([
'tenancy.bootstrappers' => [ 'tenancy.bootstrappers' => [
FilesystemTenancyBootstrapper::class, FilesystemTenancyBootstrapper::class,
@ -181,6 +179,8 @@ test('channel config keys remain unchanged if the specified tenant override attr
test('channel overrides take precedence over the default storage path channel updating logic', function () { test('channel overrides take precedence over the default storage path channel updating logic', function () {
$tenant = Tenant::create(['id' => 'tenant1']); $tenant = Tenant::create(['id' => 'tenant1']);
LogTenancyBootstrapper::$storagePathChannels = ['single'];
LogTenancyBootstrapper::$channelOverrides = [ LogTenancyBootstrapper::$channelOverrides = [
'single' => function (Tenant $tenant, array $channel) { 'single' => function (Tenant $tenant, array $channel) {
return array_merge($channel, ['path' => storage_path("logs/override-{$tenant->id}.log")]); return array_merge($channel, ['path' => storage_path("logs/override-{$tenant->id}.log")]);
@ -189,7 +189,7 @@ test('channel overrides take precedence over the default storage path channel up
tenancy()->initialize($tenant); tenancy()->initialize($tenant);
// Should use override, not the default storage path updating behavior // Should use channel override, not the storage path updating behavior
expect(config('logging.channels.single.path'))->toEndWith('storage/logs/override-tenant1.log'); expect(config('logging.channels.single.path'))->toEndWith('storage/logs/override-tenant1.log');
}); });
@ -365,17 +365,23 @@ test('stack channels that include any configured channel are re-resolved', funct
$tenant = Tenant::create(['id' => 'stack-tenant']); $tenant = Tenant::create(['id' => 'stack-tenant']);
$centralLogPath = storage_path('logs/laravel.log'); $centralLogPath = storage_path('logs/laravel.log');
$logManager = app('log');
// Resolve the stack channel in the central context first // Resolve the stack channel in the central context first
// (this caches the stack with its members still pointing at the central logs). // (this caches the stack with its members still pointing at the central logs).
Log::channel('custom_stack')->info('central log message'); $originalStackChannel = $logManager->channel('custom_stack');
$originalStackChannel->info('central log message');
expect(file_get_contents($centralLogPath))->toContain('central log message'); expect(file_get_contents($centralLogPath))->toContain('central log message');
tenancy()->initialize($tenant); tenancy()->initialize($tenant);
Log::channel('custom_stack')->info('tenant log message');
// The stack channel should have been re-resolved with the // The stack channel should have been re-resolved with the
// updated (tenant) config for its member channels, // updated (tenant) config for its member channels
$tenantStackChannel = $logManager->channel('custom_stack');
expect($tenantStackChannel)->not()->toBe($originalStackChannel);
$tenantStackChannel->info('tenant log message');
// so 'tenant log message' should be logged to the tenant log, // so 'tenant log message' should be logged to the tenant log,
// not the central log. // not the central log.
expect(file_get_contents($centralLogPath)) expect(file_get_contents($centralLogPath))
@ -389,7 +395,11 @@ test('stack channels that include any configured channel are re-resolved', funct
tenancy()->end(); tenancy()->end();
Log::channel('custom_stack')->info('central after revert'); // After revert, the stack channel should get re-resolved again with the original config
$currentStackChannel = $logManager->channel('custom_stack');
expect($currentStackChannel)->not()->toBe($tenantStackChannel);
$currentStackChannel->info('central after revert');
expect(file_get_contents($centralLogPath)) expect(file_get_contents($centralLogPath))
->toContain('central log message') ->toContain('central log message')