mirror of
https://github.com/archtechx/tenancy.git
synced 2026-05-06 16:24:03 +00:00
Merge ddd8c68fbd into 23b18c93a0
This commit is contained in:
commit
3e32ec2118
6 changed files with 386 additions and 245 deletions
|
|
@ -7,11 +7,19 @@ namespace Stancl\Tenancy\Bootstrappers;
|
||||||
use Illuminate\Broadcasting\BroadcastManager;
|
use Illuminate\Broadcasting\BroadcastManager;
|
||||||
use Illuminate\Config\Repository;
|
use Illuminate\Config\Repository;
|
||||||
use Illuminate\Contracts\Broadcasting\Broadcaster;
|
use Illuminate\Contracts\Broadcasting\Broadcaster;
|
||||||
|
use Illuminate\Contracts\Broadcasting\Factory as BroadcastingFactory;
|
||||||
use Illuminate\Foundation\Application;
|
use Illuminate\Foundation\Application;
|
||||||
|
use Illuminate\Support\Facades\Broadcast;
|
||||||
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
|
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
|
||||||
use Stancl\Tenancy\Contracts\Tenant;
|
use Stancl\Tenancy\Contracts\Tenant;
|
||||||
use Stancl\Tenancy\Overrides\TenancyBroadcastManager;
|
use Stancl\Tenancy\Overrides\TenancyBroadcastManager;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Maps tenant properties to broadcasting config and overrides
|
||||||
|
* the BroadcastManager binding with TenancyBroadcastManager.
|
||||||
|
*
|
||||||
|
* @see TenancyBroadcastManager
|
||||||
|
*/
|
||||||
class BroadcastingConfigBootstrapper implements TenancyBootstrapper
|
class BroadcastingConfigBootstrapper implements TenancyBootstrapper
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
|
|
@ -21,6 +29,8 @@ class BroadcastingConfigBootstrapper implements TenancyBootstrapper
|
||||||
* [
|
* [
|
||||||
* 'config.key.name' => 'tenant_property',
|
* 'config.key.name' => 'tenant_property',
|
||||||
* ]
|
* ]
|
||||||
|
*
|
||||||
|
* $tenant->tenant_property will be mapped to config('config.key.name') when tenancy is initialized.
|
||||||
*/
|
*/
|
||||||
public static array $credentialsMap = [];
|
public static array $credentialsMap = [];
|
||||||
|
|
||||||
|
|
@ -54,7 +64,7 @@ class BroadcastingConfigBootstrapper implements TenancyBootstrapper
|
||||||
protected Application $app
|
protected Application $app
|
||||||
) {
|
) {
|
||||||
static::$broadcaster ??= $config->get('broadcasting.default');
|
static::$broadcaster ??= $config->get('broadcasting.default');
|
||||||
static::$credentialsMap = array_merge(static::$credentialsMap, static::$mapPresets[static::$broadcaster] ?? []);
|
static::$credentialsMap = array_merge(static::$mapPresets[static::$broadcaster] ?? [], static::$credentialsMap);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function bootstrap(Tenant $tenant): void
|
public function bootstrap(Tenant $tenant): void
|
||||||
|
|
@ -64,18 +74,49 @@ class BroadcastingConfigBootstrapper implements TenancyBootstrapper
|
||||||
|
|
||||||
$this->setConfig($tenant);
|
$this->setConfig($tenant);
|
||||||
|
|
||||||
// Make BroadcastManager resolve to a custom BroadcastManager which makes the broadcasters use the tenant credentials
|
// Make BroadcastManager resolve to TenancyBroadcastManager. The manager:
|
||||||
|
// - resolves fresh broadcasters so that the updated (tenant) broadcasting config is used while broadcasting
|
||||||
|
// - makes the tenant broadcasters inherit the channels of the original (central) broadcaster
|
||||||
|
// (since newly resolved broadcasters don't receive any channels by default, broadcasting on
|
||||||
|
// channels registered in central context, e.g. in routes/channels.php, would otherwise not
|
||||||
|
// work with the tenant broadcasters)
|
||||||
$this->app->extend(BroadcastManager::class, function (BroadcastManager $broadcastManager) {
|
$this->app->extend(BroadcastManager::class, function (BroadcastManager $broadcastManager) {
|
||||||
return new TenancyBroadcastManager($this->app);
|
$originalCustomCreators = invade($broadcastManager)->customCreators;
|
||||||
|
$tenantBroadcastManager = new TenancyBroadcastManager($this->app);
|
||||||
|
|
||||||
|
// Make TenancyBroadcastManager inherit the custom driver creators registered in the central context
|
||||||
|
// so that custom drivers work in tenant context without having to re-register the creators manually.
|
||||||
|
foreach ($originalCustomCreators as $driver => $closure) {
|
||||||
|
$tenantBroadcastManager->extend($driver, $closure);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $tenantBroadcastManager;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Swap currently bound Broadcaster instance for one that's resolved through the tenant BroadcastManager.
|
||||||
|
// Note that updating broadcasting config (credentials) in tenant context doesn't update the credentials
|
||||||
|
// used by the bound Broadcaster instance. If you need to e.g. send a notification in response to
|
||||||
|
// updating tenant's broadcasting credentials in tenant context, it's recommended to
|
||||||
|
// reinitialize tenancy after updating the credentials.
|
||||||
|
$this->app->extend(Broadcaster::class, function () {
|
||||||
|
return $this->app->make(BroadcastManager::class)->connection();
|
||||||
|
});
|
||||||
|
|
||||||
|
// Clear the resolved Broadcast facade's Illuminate\Contracts\Broadcasting\Factory instance
|
||||||
|
// so that it gets re-resolved as TenancyBroadcastManager instead of the central BroadcastManager
|
||||||
|
// when used. E.g. the Broadcast::auth() call in BroadcastController::authenticate (/broadcasting/auth).
|
||||||
|
Broadcast::clearResolvedInstance(BroadcastingFactory::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function revert(): void
|
public function revert(): void
|
||||||
{
|
{
|
||||||
// Change the BroadcastManager and Broadcaster singletons back to what they were before initializing tenancy
|
// Revert the bound BroadcastManager and Broadcaster singletons back to their original state
|
||||||
$this->app->singleton(BroadcastManager::class, fn (Application $app) => $this->originalBroadcastManager);
|
$this->app->singleton(BroadcastManager::class, fn (Application $app): ?BroadcastManager => $this->originalBroadcastManager);
|
||||||
$this->app->singleton(Broadcaster::class, fn (Application $app) => $this->originalBroadcaster);
|
$this->app->singleton(Broadcaster::class, fn (Application $app) => $this->originalBroadcaster);
|
||||||
|
|
||||||
|
// Clear the resolved Broadcast facade instance so that it gets re-resolved as the central BroadcastManager
|
||||||
|
Broadcast::clearResolvedInstance(BroadcastingFactory::class);
|
||||||
|
|
||||||
$this->unsetConfig();
|
$this->unsetConfig();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -7,26 +7,38 @@ namespace Stancl\Tenancy\Overrides;
|
||||||
use Illuminate\Broadcasting\Broadcasters\Broadcaster;
|
use Illuminate\Broadcasting\Broadcasters\Broadcaster;
|
||||||
use Illuminate\Broadcasting\BroadcastManager;
|
use Illuminate\Broadcasting\BroadcastManager;
|
||||||
use Illuminate\Contracts\Broadcasting\Broadcaster as BroadcasterContract;
|
use Illuminate\Contracts\Broadcasting\Broadcaster as BroadcasterContract;
|
||||||
use Illuminate\Contracts\Foundation\Application;
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* BroadcastManager override that always re-resolves the broadcasters in static::$tenantBroadcasters
|
||||||
|
* when attempting to retrieve them so that they use the updated tenant-specific config
|
||||||
|
* and passes the channels of the original (central) broadcaster
|
||||||
|
* to the newly resolved (tenant) broadcasters.
|
||||||
|
*
|
||||||
|
* Affects calls that use BroadcastManager's get() method.
|
||||||
|
*
|
||||||
|
* @see Stancl\Tenancy\Bootstrappers\BroadcastingConfigBootstrapper
|
||||||
|
*/
|
||||||
class TenancyBroadcastManager extends BroadcastManager
|
class TenancyBroadcastManager extends BroadcastManager
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Names of broadcasters to always recreate using $this->resolve() (even when they're
|
* Names of broadcasters that
|
||||||
* cached and available in the $broadcasters property).
|
* - should always be recreated using $this->resolve(), even when they're cached and available
|
||||||
*
|
* in $this->drivers so that when you update broadcasting config in the tenant context,
|
||||||
* The reason for recreating the broadcasters is
|
* the updated config/credentials will be used for broadcasting immediately.
|
||||||
* to make your app use the correct broadcaster credentials when tenancy is initialized.
|
* Note that in cases like this, only direct config changes are reflected right away.
|
||||||
|
* For the broadcasters to reflect tenant property changes made in tenant context,
|
||||||
|
* you still have to reinitialize tenancy after updating the tenant properties intended
|
||||||
|
* to be mapped to broadcasting config, since the properties are only mapped to config
|
||||||
|
* on BroadcastingConfigBootstrapper::bootstrap().
|
||||||
|
* - should inherit the original broadcaster's channels (= the channels registered in
|
||||||
|
* the central context, e.g. in routes/channels.php, before this manager overrides the bound BroadcastManager).
|
||||||
*/
|
*/
|
||||||
public static array $tenantBroadcasters = ['pusher', 'ably'];
|
public static array $tenantBroadcasters = ['pusher', 'ably', 'reverb'];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Override the get method so that the broadcasters in $tenantBroadcasters
|
* Override the get method so that the broadcasters in static::$tenantBroadcasters
|
||||||
* always get freshly resolved even when they're cached and available in the $broadcasters property,
|
* - receive the original (central) broadcaster's channels
|
||||||
* and that the resolved broadcaster will override the BroadcasterContract::class singleton.
|
* - always get freshly resolved.
|
||||||
*
|
|
||||||
* If there's a cached broadcaster with the same name as $name,
|
|
||||||
* give its channels to the newly resolved bootstrapper.
|
|
||||||
*/
|
*/
|
||||||
protected function get($name)
|
protected function get($name)
|
||||||
{
|
{
|
||||||
|
|
@ -35,24 +47,27 @@ class TenancyBroadcastManager extends BroadcastManager
|
||||||
$originalBroadcaster = $this->app->make(BroadcasterContract::class);
|
$originalBroadcaster = $this->app->make(BroadcasterContract::class);
|
||||||
$newBroadcaster = $this->resolve($name);
|
$newBroadcaster = $this->resolve($name);
|
||||||
|
|
||||||
// If there is a current broadcaster, give its channels to the newly resolved one
|
// Give the channels of the original (central) broadcaster to the newly resolved one.
|
||||||
|
//
|
||||||
// Broadcasters only have to implement the Illuminate\Contracts\Broadcasting\Broadcaster contract
|
// Broadcasters only have to implement the Illuminate\Contracts\Broadcasting\Broadcaster contract
|
||||||
// Which doesn't require the channels property
|
// which doesn't require the channels property, so we only pass the channels to
|
||||||
// So passing the channels is only needed for Illuminate\Broadcasting\Broadcasters\Broadcaster instances
|
// Illuminate\Broadcasting\Broadcasters\Broadcaster instances (= all the default broadcasters, e.g. PusherBroadcaster).
|
||||||
if ($originalBroadcaster instanceof Broadcaster && $newBroadcaster instanceof Broadcaster) {
|
if ($originalBroadcaster instanceof Broadcaster && $newBroadcaster instanceof Broadcaster) {
|
||||||
$this->passChannelsFromOriginalBroadcaster($originalBroadcaster, $newBroadcaster);
|
$this->passChannelsFromOriginalBroadcaster($originalBroadcaster, $newBroadcaster);
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->app->singleton(BroadcasterContract::class, fn (Application $app) => $newBroadcaster);
|
|
||||||
|
|
||||||
return $newBroadcaster;
|
return $newBroadcaster;
|
||||||
}
|
}
|
||||||
|
|
||||||
return parent::get($name);
|
return parent::get($name);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Because, unlike the original broadcaster, the newly resolved broadcaster won't have the channels registered using routes/channels.php
|
/**
|
||||||
// Using it for broadcasting won't work, unless we make it have the original broadcaster's channels
|
* The newly resolved broadcasters don't automatically receive the channels registered
|
||||||
|
* in central context (e.g. Broadcast::channel() in routes/channels.php), so the channels
|
||||||
|
* have to be obtained from the original (central) broadcaster and manually passed to the new broadcasters
|
||||||
|
* (broadcasting using a broadcaster with no channels results in a 403 error on Broadcast::auth()).
|
||||||
|
*/
|
||||||
protected function passChannelsFromOriginalBroadcaster(Broadcaster $originalBroadcaster, Broadcaster $newBroadcaster): void
|
protected function passChannelsFromOriginalBroadcaster(Broadcaster $originalBroadcaster, Broadcaster $newBroadcaster): void
|
||||||
{
|
{
|
||||||
// invade() because channels can't be retrieved through any of the broadcaster's public methods
|
// invade() because channels can't be retrieved through any of the broadcaster's public methods
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,9 @@ use Stancl\Tenancy\Listeners\RevertToCentralContext;
|
||||||
use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper;
|
use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper;
|
||||||
use Stancl\Tenancy\Bootstrappers\BroadcastChannelPrefixBootstrapper;
|
use Stancl\Tenancy\Bootstrappers\BroadcastChannelPrefixBootstrapper;
|
||||||
use function Stancl\Tenancy\Tests\pest;
|
use function Stancl\Tenancy\Tests\pest;
|
||||||
|
use Illuminate\Broadcasting\Broadcasters\NullBroadcaster;
|
||||||
|
use Illuminate\Support\Facades\Broadcast;
|
||||||
|
use Illuminate\Support\Collection;
|
||||||
|
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
|
Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
|
||||||
|
|
@ -137,3 +140,96 @@ test('BroadcastChannelPrefixBootstrapper prefixes the channels events are broadc
|
||||||
expect(app(BroadcastManager::class)->driver())->toBe($broadcaster);
|
expect(app(BroadcastManager::class)->driver())->toBe($broadcaster);
|
||||||
expect(invade(app(BroadcastManager::class)->driver())->formatChannels($channelNames))->toEqual($channelNames);
|
expect(invade(app(BroadcastManager::class)->driver())->formatChannels($channelNames))->toEqual($channelNames);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('broadcasting channel helpers register channels correctly', function() {
|
||||||
|
config([
|
||||||
|
'broadcasting.default' => $driver = 'testing',
|
||||||
|
'broadcasting.connections.testing.driver' => $driver,
|
||||||
|
]);
|
||||||
|
|
||||||
|
config(['tenancy.bootstrappers' => [DatabaseTenancyBootstrapper::class]]);
|
||||||
|
|
||||||
|
Schema::create('users', function (Blueprint $table) {
|
||||||
|
$table->increments('id');
|
||||||
|
$table->string('name');
|
||||||
|
$table->string('email')->unique();
|
||||||
|
$table->string('password');
|
||||||
|
$table->rememberToken();
|
||||||
|
$table->timestamps();
|
||||||
|
});
|
||||||
|
|
||||||
|
$centralUser = User::create(['name' => 'central', 'email' => 'test@central.cz', 'password' => 'test']);
|
||||||
|
$tenant = Tenant::create();
|
||||||
|
|
||||||
|
migrateTenants();
|
||||||
|
|
||||||
|
tenancy()->initialize($tenant);
|
||||||
|
|
||||||
|
// Same ID as $centralUser
|
||||||
|
$tenantUser = User::create(['name' => 'tenant', 'email' => 'test@tenant.cz', 'password' => 'test']);
|
||||||
|
|
||||||
|
tenancy()->end();
|
||||||
|
|
||||||
|
/** @var BroadcastManager $broadcastManager */
|
||||||
|
$broadcastManager = app(BroadcastManager::class);
|
||||||
|
|
||||||
|
// Use a driver with no channels
|
||||||
|
$broadcastManager->extend($driver, fn () => new NullBroadcaster);
|
||||||
|
|
||||||
|
$getChannels = fn (): Collection => $broadcastManager->driver($driver)->getChannels();
|
||||||
|
|
||||||
|
expect($getChannels())->toBeEmpty();
|
||||||
|
|
||||||
|
// Basic channel registration
|
||||||
|
Broadcast::channel($channelName = 'user.{userName}', $channelClosure = function ($user, $userName) {
|
||||||
|
return User::firstWhere('name', $userName)?->is($user) ?? false;
|
||||||
|
});
|
||||||
|
|
||||||
|
// Check if the channel is registered
|
||||||
|
$centralChannelClosure = $getChannels()->first(fn ($closure, $name) => $name === $channelName);
|
||||||
|
expect($centralChannelClosure)->not()->toBeNull();
|
||||||
|
|
||||||
|
// Channel closures work as expected (running in central context)
|
||||||
|
expect($centralChannelClosure($centralUser, $centralUser->name))->toBeTrue();
|
||||||
|
expect($centralChannelClosure($centralUser, $tenantUser->name))->toBeFalse();
|
||||||
|
|
||||||
|
// Register a tenant broadcasting channel (almost identical to the original channel, just able to accept the tenant key)
|
||||||
|
tenant_channel($channelName, $channelClosure);
|
||||||
|
|
||||||
|
// Tenant channel registered – its name is correctly prefixed ("{tenant}.user.{userId}")
|
||||||
|
$tenantChannelClosure = $getChannels()->first(fn ($closure, $name) => $name === "{tenant}.$channelName");
|
||||||
|
expect($tenantChannelClosure)->toBe($centralChannelClosure);
|
||||||
|
|
||||||
|
// The tenant channels are prefixed with '{tenant}.'
|
||||||
|
// They accept the tenant key, but their closures only run in tenant context when tenancy is initialized
|
||||||
|
// The regular channels don't accept the tenant key, but they also respect the current context
|
||||||
|
// The tenant key is used solely for the name prefixing – the closures can still run in the central context
|
||||||
|
tenant_channel($channelName, $tenantChannelClosure = function ($user, $tenant, $userName) {
|
||||||
|
return User::firstWhere('name', $userName)?->is($user) ?? false;
|
||||||
|
});
|
||||||
|
|
||||||
|
expect($tenantChannelClosure)->not()->toBe($centralChannelClosure);
|
||||||
|
|
||||||
|
expect($tenantChannelClosure($centralUser, $tenant->getTenantKey(), $centralUser->name))->toBeTrue();
|
||||||
|
expect($tenantChannelClosure($centralUser, $tenant->getTenantKey(), $tenantUser->name))->toBeFalse();
|
||||||
|
|
||||||
|
tenancy()->initialize($tenant);
|
||||||
|
|
||||||
|
// The channel closure runs in the tenant context
|
||||||
|
// Only the tenant user is available
|
||||||
|
expect($tenantChannelClosure($centralUser, $tenant->getTenantKey(), $tenantUser->name))->toBeFalse();
|
||||||
|
expect($tenantChannelClosure($tenantUser, $tenant->getTenantKey(), $tenantUser->name))->toBeTrue();
|
||||||
|
|
||||||
|
// Use a new channel instance to delete the previously registered channels before testing the global_channel helper
|
||||||
|
$broadcastManager->purge($driver);
|
||||||
|
$broadcastManager->extend($driver, fn () => new NullBroadcaster);
|
||||||
|
|
||||||
|
expect($getChannels())->toBeEmpty();
|
||||||
|
|
||||||
|
// Global channel helper prefixes the channel name with 'global__'
|
||||||
|
global_channel($channelName, $channelClosure);
|
||||||
|
|
||||||
|
// Channel prefixed with 'global__' found
|
||||||
|
$foundChannelClosure = $getChannels()->first(fn ($closure, $name) => $name === 'global__' . $channelName);
|
||||||
|
expect($foundChannelClosure)->not()->toBeNull();
|
||||||
|
});
|
||||||
|
|
|
||||||
|
|
@ -10,24 +10,48 @@ use Stancl\Tenancy\Tests\Etc\TestingBroadcaster;
|
||||||
use Stancl\Tenancy\Listeners\RevertToCentralContext;
|
use Stancl\Tenancy\Listeners\RevertToCentralContext;
|
||||||
use Stancl\Tenancy\Overrides\TenancyBroadcastManager;
|
use Stancl\Tenancy\Overrides\TenancyBroadcastManager;
|
||||||
use Stancl\Tenancy\Bootstrappers\BroadcastingConfigBootstrapper;
|
use Stancl\Tenancy\Bootstrappers\BroadcastingConfigBootstrapper;
|
||||||
|
use Illuminate\Support\Facades\Broadcast;
|
||||||
|
use Illuminate\Contracts\Broadcasting\Broadcaster as BroadcasterContract;
|
||||||
|
|
||||||
beforeEach(function () {
|
$cleanup = function () {
|
||||||
|
BroadcastingConfigBootstrapper::$broadcaster = null;
|
||||||
|
BroadcastingConfigBootstrapper::$credentialsMap = [];
|
||||||
|
BroadcastingConfigBootstrapper::$mapPresets = [
|
||||||
|
'pusher' => [
|
||||||
|
'broadcasting.connections.pusher.key' => 'pusher_key',
|
||||||
|
'broadcasting.connections.pusher.secret' => 'pusher_secret',
|
||||||
|
'broadcasting.connections.pusher.app_id' => 'pusher_app_id',
|
||||||
|
'broadcasting.connections.pusher.options.cluster' => 'pusher_cluster',
|
||||||
|
],
|
||||||
|
'reverb' => [
|
||||||
|
'broadcasting.connections.reverb.key' => 'reverb_key',
|
||||||
|
'broadcasting.connections.reverb.secret' => 'reverb_secret',
|
||||||
|
'broadcasting.connections.reverb.app_id' => 'reverb_app_id',
|
||||||
|
'broadcasting.connections.reverb.options.cluster' => 'reverb_cluster',
|
||||||
|
],
|
||||||
|
'ably' => [
|
||||||
|
'broadcasting.connections.ably.key' => 'ably_key',
|
||||||
|
'broadcasting.connections.ably.public' => 'ably_public',
|
||||||
|
],
|
||||||
|
];
|
||||||
|
TenancyBroadcastManager::$tenantBroadcasters = ['pusher', 'ably', 'reverb'];
|
||||||
|
};
|
||||||
|
|
||||||
|
beforeEach(function () use ($cleanup) {
|
||||||
Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
|
Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
|
||||||
Event::listen(TenancyEnded::class, RevertToCentralContext::class);
|
Event::listen(TenancyEnded::class, RevertToCentralContext::class);
|
||||||
|
|
||||||
BroadcastingConfigBootstrapper::$credentialsMap = [];
|
$cleanup();
|
||||||
TenancyBroadcastManager::$tenantBroadcasters = ['pusher', 'ably'];
|
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(function () {
|
afterEach($cleanup);
|
||||||
BroadcastingConfigBootstrapper::$credentialsMap = [];
|
|
||||||
TenancyBroadcastManager::$tenantBroadcasters = ['pusher', 'ably'];
|
|
||||||
});
|
|
||||||
|
|
||||||
test('BroadcastingConfigBootstrapper binds TenancyBroadcastManager to BroadcastManager and reverts the binding when tenancy is ended', function() {
|
test('BroadcastingConfigBootstrapper binds TenancyBroadcastManager to BroadcastManager and reverts the binding when tenancy is ended', function() {
|
||||||
config(['tenancy.bootstrappers' => [BroadcastingConfigBootstrapper::class]]);
|
config(['tenancy.bootstrappers' => [BroadcastingConfigBootstrapper::class]]);
|
||||||
|
|
||||||
expect(app(BroadcastManager::class))->toBeInstanceOf(BroadcastManager::class);
|
expect(app(BroadcastManager::class))
|
||||||
|
->toBeInstanceOf(BroadcastManager::class)
|
||||||
|
->not()->toBeInstanceOf(TenancyBroadcastManager::class);
|
||||||
|
|
||||||
tenancy()->initialize(Tenant::create());
|
tenancy()->initialize(Tenant::create());
|
||||||
|
|
||||||
|
|
@ -35,71 +59,209 @@ test('BroadcastingConfigBootstrapper binds TenancyBroadcastManager to BroadcastM
|
||||||
|
|
||||||
tenancy()->end();
|
tenancy()->end();
|
||||||
|
|
||||||
expect(app(BroadcastManager::class))->toBeInstanceOf(BroadcastManager::class);
|
expect(app(BroadcastManager::class))
|
||||||
|
->toBeInstanceOf(BroadcastManager::class)
|
||||||
|
->not()->toBeInstanceOf(TenancyBroadcastManager::class);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('BroadcastingConfigBootstrapper maps tenant broadcaster credentials to config as specified in the $credentialsMap property and reverts the config after ending tenancy', function() {
|
test('BroadcastingConfigBootstrapper maps tenant properties to broadcaster credentials correctly', function(string $driver) {
|
||||||
config([
|
config([
|
||||||
'broadcasting.connections.testing.driver' => 'testing',
|
'broadcasting.default' => $driver,
|
||||||
'broadcasting.connections.testing.message' => $defaultMessage = 'default',
|
"broadcasting.connections.{$driver}.key" => 'central_key',
|
||||||
'tenancy.bootstrappers' => [BroadcastingConfigBootstrapper::class],
|
'tenancy.bootstrappers' => [
|
||||||
|
BroadcastingConfigBootstrapper::class,
|
||||||
|
],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
BroadcastingConfigBootstrapper::$credentialsMap = [
|
if ($driver === 'custom') {
|
||||||
'broadcasting.connections.testing.message' => 'testing_broadcaster_message',
|
config(['broadcasting.connections.custom.driver' => 'custom']);
|
||||||
];
|
|
||||||
|
|
||||||
$tenant = Tenant::create(['testing_broadcaster_message' => $tenantMessage = 'first testing']);
|
// Custom driver, not included in TenancyBroadcastManager::$tenantBroadcasters by default
|
||||||
$tenant2 = Tenant::create(['testing_broadcaster_message' => $secondTenantMessage = 'second testing']);
|
TenancyBroadcastManager::$tenantBroadcasters = ['custom'];
|
||||||
|
}
|
||||||
|
|
||||||
tenancy()->initialize($tenant);
|
BroadcastingConfigBootstrapper::$credentialsMap["broadcasting.connections.{$driver}.key"] = 'testing_key';
|
||||||
|
|
||||||
expect(array_key_exists('testing_broadcaster_message', tenant()->getAttributes()))->toBeTrue();
|
app(BroadcastManager::class)->extend($driver, fn ($app, $config) => new TestingBroadcaster('testing', $config));
|
||||||
expect(config('broadcasting.connections.testing.message'))->toBe($tenantMessage);
|
|
||||||
|
$tenant1 = Tenant::create(['testing_key' => 'tenant1_key']);
|
||||||
|
$tenant2 = Tenant::create(['testing_key' => 'tenant2_key']);
|
||||||
|
|
||||||
|
expect(config("broadcasting.connections.{$driver}.key"))->toBe('central_key');
|
||||||
|
expect(app(BroadcastManager::class)->driver()->config['key'])->toBe('central_key');
|
||||||
|
expect(app(BroadcasterContract::class)->config['key'])->toBe('central_key');
|
||||||
|
expect(Broadcast::driver()->config['key'])->toBe('central_key');
|
||||||
|
|
||||||
|
tenancy()->initialize($tenant1);
|
||||||
|
|
||||||
|
// Tenant's testing_key property is mapped to the current broadcasting connection key
|
||||||
|
expect(config("broadcasting.connections.{$driver}.key"))->toBe('tenant1_key');
|
||||||
|
expect(app(BroadcastManager::class)->driver()->config['key'])->toBe('tenant1_key');
|
||||||
|
// Switching to tenant context makes the currently bound Broadcaster instance use the tenant's config
|
||||||
|
expect(app(BroadcasterContract::class)->config['key'])->toBe('tenant1_key');
|
||||||
|
// The Broadcast facade (used in BroadcastController::authenticate) uses the broadcaster with tenant config
|
||||||
|
// instead of the stale broadcaster instance resolved before tenancy was initialized
|
||||||
|
expect(Broadcast::driver()->config['key'])->toBe('tenant1_key');
|
||||||
|
|
||||||
tenancy()->initialize($tenant2);
|
tenancy()->initialize($tenant2);
|
||||||
|
|
||||||
expect(config('broadcasting.connections.testing.message'))->toBe($secondTenantMessage);
|
expect(config("broadcasting.connections.{$driver}.key"))->toBe('tenant2_key');
|
||||||
|
// Switching to another tenant context makes the current broadcaster use the new tenant's config
|
||||||
|
expect(app(BroadcastManager::class)->driver()->config['key'])->toBe('tenant2_key');
|
||||||
|
expect(app(BroadcasterContract::class)->config['key'])->toBe('tenant2_key');
|
||||||
|
expect(Broadcast::driver()->config['key'])->toBe('tenant2_key');
|
||||||
|
|
||||||
|
$tenant2->update(['testing_key' => 'new_tenant2_key']);
|
||||||
|
|
||||||
|
// Reinitialize tenancy to apply the tenant property update to config
|
||||||
|
tenancy()->end();
|
||||||
|
tenancy()->initialize($tenant2);
|
||||||
|
|
||||||
|
expect(config("broadcasting.connections.{$driver}.key"))->toBe('new_tenant2_key');
|
||||||
|
expect(app(BroadcastManager::class)->driver()->config['key'])->toBe('new_tenant2_key');
|
||||||
|
expect(app(BroadcasterContract::class)->config['key'])->toBe('new_tenant2_key');
|
||||||
|
expect(Broadcast::driver()->config['key'])->toBe('new_tenant2_key');
|
||||||
|
|
||||||
|
tenancy()->initialize($tenant1);
|
||||||
|
|
||||||
|
// When updating tenant properties without reinitializing, the tenant property update doesn't update the config,
|
||||||
|
// so the config has to be modified manually. Only methods that use TenancyBroadcastManager::get()
|
||||||
|
// will use the updated credentials without needing to reinitialize tenancy (e.g. the bound
|
||||||
|
// BroadcasterContract instance will still use the original credentials, even after config gets updated directly).
|
||||||
|
config(["broadcasting.connections.{$driver}.key" => 'new_tenant1_key']);
|
||||||
|
|
||||||
|
expect(app(BroadcastManager::class)->driver()->config['key'])->toBe('new_tenant1_key');
|
||||||
|
expect(Broadcast::driver()->config['key'])->toBe('new_tenant1_key');
|
||||||
|
|
||||||
tenancy()->end();
|
tenancy()->end();
|
||||||
|
|
||||||
expect(config('broadcasting.connections.testing.message'))->toBe($defaultMessage);
|
expect(config("broadcasting.connections.{$driver}.key"))->toBe('central_key');
|
||||||
});
|
// Ending tenancy reverts the broadcaster changes
|
||||||
|
expect(app(BroadcastManager::class)->driver()->config['key'])->toBe('central_key');
|
||||||
|
expect(app(BroadcasterContract::class)->config['key'])->toBe('central_key');
|
||||||
|
expect(Broadcast::driver()->config['key'])->toBe('central_key');
|
||||||
|
})->with([
|
||||||
|
'pusher',
|
||||||
|
'ably',
|
||||||
|
'reverb',
|
||||||
|
'custom', // Except for this custom driver, assume that the drivers are included in TenancyBroadcastManager::$tenantBroadcasters by default
|
||||||
|
]);
|
||||||
|
|
||||||
test('BroadcastingConfigBootstrapper makes the app use broadcasters with the correct credentials', function() {
|
test('tenant broadcast manager receives the custom driver creators of the central broadcast manager', function() {
|
||||||
config([
|
config([
|
||||||
'broadcasting.default' => 'testing',
|
'tenancy.bootstrappers' => [
|
||||||
'broadcasting.connections.testing.driver' => 'testing',
|
BroadcastingConfigBootstrapper::class,
|
||||||
'broadcasting.connections.testing.message' => $defaultMessage = 'default',
|
],
|
||||||
'tenancy.bootstrappers' => [BroadcastingConfigBootstrapper::class],
|
|
||||||
]);
|
]);
|
||||||
|
|
||||||
TenancyBroadcastManager::$tenantBroadcasters[] = 'testing';
|
$tenant = Tenant::create();
|
||||||
BroadcastingConfigBootstrapper::$credentialsMap = [
|
$tenant2 = Tenant::create();
|
||||||
'broadcasting.connections.testing.message' => 'testing_broadcaster_message',
|
|
||||||
];
|
|
||||||
|
|
||||||
$registerTestingBroadcaster = fn() => app(BroadcastManager::class)->extend('testing', fn ($app, $config) => new TestingBroadcaster($config['message']));
|
app(BroadcastManager::class)->extend('testing', fn($app, $config) => new TestingBroadcaster('testing', $config));
|
||||||
|
|
||||||
$registerTestingBroadcaster();
|
$originalDrivers = array_keys(invade(app(BroadcastManager::class))->customCreators);
|
||||||
|
|
||||||
expect(invade(app(BroadcastManager::class)->driver())->message)->toBe($defaultMessage);
|
expect($originalDrivers)->toContain('testing');
|
||||||
|
|
||||||
$tenant = Tenant::create(['testing_broadcaster_message' => $tenantMessage = 'first testing']);
|
|
||||||
$tenant2 = Tenant::create(['testing_broadcaster_message' => $secondTenantMessage = 'second testing']);
|
|
||||||
|
|
||||||
tenancy()->initialize($tenant);
|
tenancy()->initialize($tenant);
|
||||||
$registerTestingBroadcaster();
|
|
||||||
|
|
||||||
expect(invade(app(BroadcastManager::class)->driver())->message)->toBe($tenantMessage);
|
app(BroadcastManager::class)->extend(
|
||||||
|
'testing-tenant1',
|
||||||
|
fn($app, $config) => new TestingBroadcaster('testing-tenant1', $config)
|
||||||
|
);
|
||||||
|
|
||||||
|
// Current BroadcastManager instance has the original custom creators plus the newly registered testing-tenant1 creator
|
||||||
|
expect(array_keys(invade(app(BroadcastManager::class))->customCreators))->toEqualCanonicalizing([...$originalDrivers, 'testing-tenant1']);
|
||||||
|
|
||||||
tenancy()->initialize($tenant2);
|
tenancy()->initialize($tenant2);
|
||||||
$registerTestingBroadcaster();
|
|
||||||
|
|
||||||
expect(invade(app(BroadcastManager::class)->driver())->message)->toBe($secondTenantMessage);
|
// Current BroadcastManager only has the original custom creators,
|
||||||
|
// the creator added in the previous tenant's context doesn't persist.
|
||||||
|
expect(array_keys(invade(app(BroadcastManager::class))->customCreators))->toEqualCanonicalizing($originalDrivers);
|
||||||
|
|
||||||
tenancy()->end();
|
tenancy()->end();
|
||||||
$registerTestingBroadcaster();
|
|
||||||
|
|
||||||
expect(invade(app(BroadcastManager::class)->driver())->message)->toBe($defaultMessage);
|
// Ending tenancy reverts the BroadcastManager binding back to the original state,
|
||||||
|
// the creator registered in the tenant context doesn't persist.
|
||||||
|
expect(array_keys(invade(app(BroadcastManager::class))->customCreators))->toEqualCanonicalizing($originalDrivers);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('tenant broadcasters receive the channels from the broadcaster bound in central context', function(string $driver) {
|
||||||
|
config([
|
||||||
|
'tenancy.bootstrappers' => [BroadcastingConfigBootstrapper::class],
|
||||||
|
'broadcasting.default' => $driver,
|
||||||
|
]);
|
||||||
|
|
||||||
|
if ($driver === 'custom') {
|
||||||
|
config(['broadcasting.connections.custom.driver' => 'custom']);
|
||||||
|
|
||||||
|
// Custom driver, not included in TenancyBroadcastManager::$tenantBroadcasters by default
|
||||||
|
TenancyBroadcastManager::$tenantBroadcasters = ['custom'];
|
||||||
|
}
|
||||||
|
|
||||||
|
$tenant1 = Tenant::create();
|
||||||
|
$tenant2 = Tenant::create();
|
||||||
|
|
||||||
|
app(BroadcastManager::class)->extend($driver, fn($app, $config) => new TestingBroadcaster('testing'));
|
||||||
|
$getCurrentChannelsFromBoundBroadcaster = fn() => array_keys(invade(app(BroadcasterContract::class))->channels);
|
||||||
|
$getCurrentChannelsThroughManager = fn() => array_keys(invade(app(BroadcastManager::class)->driver())->channels);
|
||||||
|
|
||||||
|
Broadcast::channel($channel = 'testing-channel', fn() => true);
|
||||||
|
|
||||||
|
expect($channel)
|
||||||
|
->toBeIn($getCurrentChannelsThroughManager())
|
||||||
|
->toBeIn($getCurrentChannelsFromBoundBroadcaster());
|
||||||
|
|
||||||
|
tenancy()->initialize($tenant1);
|
||||||
|
|
||||||
|
expect($channel)
|
||||||
|
->toBeIn($getCurrentChannelsThroughManager())
|
||||||
|
->toBeIn($getCurrentChannelsFromBoundBroadcaster());
|
||||||
|
|
||||||
|
tenancy()->initialize($tenant2);
|
||||||
|
|
||||||
|
expect($channel)
|
||||||
|
->toBeIn($getCurrentChannelsThroughManager())
|
||||||
|
->toBeIn($getCurrentChannelsFromBoundBroadcaster());
|
||||||
|
|
||||||
|
tenancy()->end();
|
||||||
|
|
||||||
|
expect($channel)
|
||||||
|
->toBeIn($getCurrentChannelsThroughManager())
|
||||||
|
->toBeIn($getCurrentChannelsFromBoundBroadcaster());
|
||||||
|
})->with([
|
||||||
|
'pusher',
|
||||||
|
'ably',
|
||||||
|
'reverb',
|
||||||
|
'custom', // Except for this custom driver, assume that the drivers are included in TenancyBroadcastManager::$tenantBroadcasters by default
|
||||||
|
]);
|
||||||
|
|
||||||
|
test('mappings specified in credentialsMap override default mapPresets', function($driver) {
|
||||||
|
config([
|
||||||
|
'tenancy.bootstrappers' => [BroadcastingConfigBootstrapper::class],
|
||||||
|
'broadcasting.default' => $driver,
|
||||||
|
]);
|
||||||
|
|
||||||
|
// Preset used for broadcasters included in TenancyBroadcastManager::$tenantBroadcasters by default.
|
||||||
|
// This is the default for all tenant broadcasters, but we set it here for clarity.
|
||||||
|
BroadcastingConfigBootstrapper::$mapPresets[$driver]["broadcasting.connections.{$driver}.key"] = "{$driver}_key";
|
||||||
|
|
||||||
|
// Custom mapping specified in credentialsMap should override the preset mapping for the tested broadcaster
|
||||||
|
BroadcastingConfigBootstrapper::$credentialsMap["broadcasting.connections.{$driver}.key"] = 'broadcasting_key';
|
||||||
|
|
||||||
|
app(BroadcastManager::class)->extend($driver, fn($app, $config) => new TestingBroadcaster('testing'));
|
||||||
|
|
||||||
|
$tenant = Tenant::create([
|
||||||
|
"{$driver}_key" => 'preset_value',
|
||||||
|
'broadcasting_key' => 'custom_value',
|
||||||
|
]);
|
||||||
|
|
||||||
|
|
||||||
|
tenancy()->initialize($tenant);
|
||||||
|
|
||||||
|
expect(config("broadcasting.connections.{$driver}.key"))->toBe('custom_value');
|
||||||
|
})->with([
|
||||||
|
'pusher',
|
||||||
|
'ably',
|
||||||
|
'reverb',
|
||||||
|
]);
|
||||||
|
|
|
||||||
|
|
@ -1,174 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
use Illuminate\Support\Collection;
|
|
||||||
use Stancl\Tenancy\Tests\Etc\Tenant;
|
|
||||||
use Illuminate\Support\Facades\Event;
|
|
||||||
use Illuminate\Support\Facades\Schema;
|
|
||||||
use Stancl\Tenancy\Events\TenancyEnded;
|
|
||||||
use Illuminate\Database\Schema\Blueprint;
|
|
||||||
use Illuminate\Support\Facades\Broadcast;
|
|
||||||
use Illuminate\Broadcasting\BroadcastManager;
|
|
||||||
use Stancl\Tenancy\Events\TenancyInitialized;
|
|
||||||
use Stancl\Tenancy\Listeners\BootstrapTenancy;
|
|
||||||
use Stancl\Tenancy\Tests\Etc\TestingBroadcaster;
|
|
||||||
use Stancl\Tenancy\Listeners\RevertToCentralContext;
|
|
||||||
use Stancl\Tenancy\Overrides\TenancyBroadcastManager;
|
|
||||||
use Illuminate\Broadcasting\Broadcasters\NullBroadcaster;
|
|
||||||
use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper;
|
|
||||||
use Stancl\Tenancy\Bootstrappers\BroadcastingConfigBootstrapper;
|
|
||||||
use Illuminate\Contracts\Broadcasting\Broadcaster as BroadcasterContract;
|
|
||||||
use function Stancl\Tenancy\Tests\withTenantDatabases;
|
|
||||||
|
|
||||||
beforeEach(function () {
|
|
||||||
withTenantDatabases();
|
|
||||||
TenancyBroadcastManager::$tenantBroadcasters = ['pusher', 'ably'];
|
|
||||||
|
|
||||||
Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
|
|
||||||
Event::listen(TenancyEnded::class, RevertToCentralContext::class);
|
|
||||||
});
|
|
||||||
|
|
||||||
afterEach(function () {
|
|
||||||
TenancyBroadcastManager::$tenantBroadcasters = ['pusher', 'ably'];
|
|
||||||
});
|
|
||||||
|
|
||||||
test('bound broadcaster instance is the same before initializing tenancy and after ending it', function() {
|
|
||||||
config(['tenancy.bootstrappers' => [BroadcastingConfigBootstrapper::class]]);
|
|
||||||
config(['broadcasting.default' => 'null']);
|
|
||||||
TenancyBroadcastManager::$tenantBroadcasters[] = 'null';
|
|
||||||
|
|
||||||
$originalBroadcaster = app(BroadcasterContract::class);
|
|
||||||
|
|
||||||
tenancy()->initialize(Tenant::create());
|
|
||||||
|
|
||||||
// TenancyBroadcastManager binds new broadcaster
|
|
||||||
$tenantBroadcaster = app(BroadcastManager::class)->driver();
|
|
||||||
|
|
||||||
expect($tenantBroadcaster)->not()->toBe($originalBroadcaster);
|
|
||||||
|
|
||||||
tenancy()->end();
|
|
||||||
|
|
||||||
expect($originalBroadcaster)->toBe(app(BroadcasterContract::class));
|
|
||||||
});
|
|
||||||
|
|
||||||
test('new broadcasters get the channels from the previously bound broadcaster', function() {
|
|
||||||
config(['tenancy.bootstrappers' => [BroadcastingConfigBootstrapper::class]]);
|
|
||||||
config([
|
|
||||||
'broadcasting.default' => $driver = 'testing',
|
|
||||||
'broadcasting.connections.testing.driver' => $driver,
|
|
||||||
]);
|
|
||||||
|
|
||||||
TenancyBroadcastManager::$tenantBroadcasters[] = $driver;
|
|
||||||
|
|
||||||
$registerTestingBroadcaster = fn() => 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());
|
|
||||||
});
|
|
||||||
|
|
||||||
test('broadcasting channel helpers register channels correctly', function() {
|
|
||||||
config([
|
|
||||||
'broadcasting.default' => $driver = 'testing',
|
|
||||||
'broadcasting.connections.testing.driver' => $driver,
|
|
||||||
]);
|
|
||||||
|
|
||||||
config(['tenancy.bootstrappers' => [DatabaseTenancyBootstrapper::class]]);
|
|
||||||
|
|
||||||
Schema::create('users', function (Blueprint $table) {
|
|
||||||
$table->increments('id');
|
|
||||||
$table->string('name');
|
|
||||||
$table->string('email')->unique();
|
|
||||||
$table->string('password');
|
|
||||||
$table->rememberToken();
|
|
||||||
$table->timestamps();
|
|
||||||
});
|
|
||||||
|
|
||||||
$centralUser = User::create(['name' => 'central', 'email' => 'test@central.cz', 'password' => 'test']);
|
|
||||||
$tenant = Tenant::create();
|
|
||||||
|
|
||||||
migrateTenants();
|
|
||||||
|
|
||||||
tenancy()->initialize($tenant);
|
|
||||||
|
|
||||||
// Same ID as $centralUser
|
|
||||||
$tenantUser = User::create(['name' => 'tenant', 'email' => 'test@tenant.cz', 'password' => 'test']);
|
|
||||||
|
|
||||||
tenancy()->end();
|
|
||||||
|
|
||||||
/** @var BroadcastManager $broadcastManager */
|
|
||||||
$broadcastManager = app(BroadcastManager::class);
|
|
||||||
|
|
||||||
// Use a driver with no channels
|
|
||||||
$broadcastManager->extend($driver, fn () => new NullBroadcaster);
|
|
||||||
|
|
||||||
$getChannels = fn (): Collection => $broadcastManager->driver($driver)->getChannels();
|
|
||||||
|
|
||||||
expect($getChannels())->toBeEmpty();
|
|
||||||
|
|
||||||
// Basic channel registration
|
|
||||||
Broadcast::channel($channelName = 'user.{userName}', $channelClosure = function ($user, $userName) {
|
|
||||||
return User::firstWhere('name', $userName)?->is($user) ?? false;
|
|
||||||
});
|
|
||||||
|
|
||||||
// Check if the channel is registered
|
|
||||||
$centralChannelClosure = $getChannels()->first(fn ($closure, $name) => $name === $channelName);
|
|
||||||
expect($centralChannelClosure)->not()->toBeNull();
|
|
||||||
|
|
||||||
// Channel closures work as expected (running in central context)
|
|
||||||
expect($centralChannelClosure($centralUser, $centralUser->name))->toBeTrue();
|
|
||||||
expect($centralChannelClosure($centralUser, $tenantUser->name))->toBeFalse();
|
|
||||||
|
|
||||||
// Register a tenant broadcasting channel (almost identical to the original channel, just able to accept the tenant key)
|
|
||||||
tenant_channel($channelName, $channelClosure);
|
|
||||||
|
|
||||||
// Tenant channel registered – its name is correctly prefixed ("{tenant}.user.{userId}")
|
|
||||||
$tenantChannelClosure = $getChannels()->first(fn ($closure, $name) => $name === "{tenant}.$channelName");
|
|
||||||
expect($tenantChannelClosure)->toBe($centralChannelClosure);
|
|
||||||
|
|
||||||
// The tenant channels are prefixed with '{tenant}.'
|
|
||||||
// They accept the tenant key, but their closures only run in tenant context when tenancy is initialized
|
|
||||||
// The regular channels don't accept the tenant key, but they also respect the current context
|
|
||||||
// The tenant key is used solely for the name prefixing – the closures can still run in the central context
|
|
||||||
tenant_channel($channelName, $tenantChannelClosure = function ($user, $tenant, $userName) {
|
|
||||||
return User::firstWhere('name', $userName)?->is($user) ?? false;
|
|
||||||
});
|
|
||||||
|
|
||||||
expect($tenantChannelClosure)->not()->toBe($centralChannelClosure);
|
|
||||||
|
|
||||||
expect($tenantChannelClosure($centralUser, $tenant->getTenantKey(), $centralUser->name))->toBeTrue();
|
|
||||||
expect($tenantChannelClosure($centralUser, $tenant->getTenantKey(), $tenantUser->name))->toBeFalse();
|
|
||||||
|
|
||||||
tenancy()->initialize($tenant);
|
|
||||||
|
|
||||||
// The channel closure runs in the central context
|
|
||||||
// Only the central user is available
|
|
||||||
expect($tenantChannelClosure($centralUser, $tenant->getTenantKey(), $tenantUser->name))->toBeFalse();
|
|
||||||
expect($tenantChannelClosure($tenantUser, $tenant->getTenantKey(), $tenantUser->name))->toBeTrue();
|
|
||||||
|
|
||||||
// Use a new channel instance to delete the previously registered channels before testing the univeresal_channel helper
|
|
||||||
$broadcastManager->purge($driver);
|
|
||||||
$broadcastManager->extend($driver, fn () => new NullBroadcaster);
|
|
||||||
|
|
||||||
expect($getChannels())->toBeEmpty();
|
|
||||||
|
|
||||||
// Global channel helper prefixes the channel name with 'global__'
|
|
||||||
global_channel($channelName, $channelClosure);
|
|
||||||
|
|
||||||
// Channel prefixed with 'global__' found
|
|
||||||
$foundChannelClosure = $getChannels()->first(fn ($closure, $name) => $name === 'global__' . $channelName);
|
|
||||||
expect($foundChannelClosure)->not()->toBeNull();
|
|
||||||
});
|
|
||||||
|
|
@ -6,7 +6,8 @@ use Illuminate\Broadcasting\Broadcasters\Broadcaster;
|
||||||
|
|
||||||
class TestingBroadcaster extends Broadcaster {
|
class TestingBroadcaster extends Broadcaster {
|
||||||
public function __construct(
|
public function __construct(
|
||||||
public string $message = 'nothing'
|
public string $message = 'nothing',
|
||||||
|
public array $config = [],
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
public function auth($request)
|
public function auth($request)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue