diff --git a/tests/BroadcastingTest.php b/tests/BroadcastingTest.php index 69e7fb3b..4455bfe9 100644 --- a/tests/BroadcastingTest.php +++ b/tests/BroadcastingTest.php @@ -94,6 +94,45 @@ test('broadcasting config bootstrapper maps the config to broadcaster credential expect(Broadcast::driver()->config['key'])->toBe('central_key'); }); +test('tenant broadcast manager receives the custom driver creators of the central broadcast manager', function() { + config([ + 'tenancy.bootstrappers' => [ + BroadcastingConfigBootstrapper::class, + ], + ]); + + $tenant = Tenant::create(); + $tenant2 = Tenant::create(); + + app(BroadcastManager::class)->extend('testing', $testingClosure = fn($app, $config) => new TestingBroadcaster('testing', $config)); + + $originalCustomCreators = invade(app(BroadcastManager::class))->customCreators; + + expect($originalCustomCreators['testing'])->toBe($testingClosure); + + tenancy()->initialize($tenant); + + app(BroadcastManager::class)->extend( + 'testing-tenant1', + $testingTenant1Closure = fn($app, $config) => new TestingBroadcaster('testing-tenant1', $config) + ); + + // Current BroadcastManager instance has the original custom creators plus the newly registered testing-tenant1 creator + expect(invade(app(BroadcastManager::class))->customCreators)->toBe($originalCustomCreators + ['testing-tenant1' => $testingTenant1Closure]); + + tenancy()->initialize($tenant2); + + // Current BroadcastManager only has the original custom creators, + // the creator added in the previous tenant's context doesn't persist. + expect(invade(app(BroadcastManager::class))->customCreators)->toBe($originalCustomCreators); + + tenancy()->end(); + + // Ending tenancy reverts the BroadcastManager binding back to the original state, + // the creator registered in the tenant context doesn't persist. + expect(invade(app(BroadcastManager::class))->customCreators)->toBe($originalCustomCreators); +}); + test('new broadcasters get the channels from the previously bound broadcaster', function() { config(['tenancy.bootstrappers' => [BroadcastingConfigBootstrapper::class]]); config([ @@ -103,21 +142,18 @@ test('new broadcasters get the channels from the previously bound broadcaster', TenancyBroadcastManager::$tenantBroadcasters[] = $driver; - $registerTestingBroadcaster = fn() => app(BroadcastManager::class)->extend('testing', fn($app, $config) => new TestingBroadcaster('testing')); + app(BroadcastManager::class)->extend('testing', fn($app, $config) => new TestingBroadcaster('testing')); $getCurrentChannels = fn() => array_keys(invade(app(BroadcastManager::class)->driver())->channels); - $registerTestingBroadcaster(); Broadcast::channel($channel = 'testing-channel', fn() => true); expect($channel)->toBeIn($getCurrentChannels()); tenancy()->initialize(Tenant::create()); - $registerTestingBroadcaster(); expect($channel)->toBeIn($getCurrentChannels()); tenancy()->end(); - $registerTestingBroadcaster(); expect($channel)->toBeIn($getCurrentChannels()); });