mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 14:14:04 +00:00
[4.x] Make broadcasting work with Tenancy (#1027)
* Add BroadcastTenancyBootstrapper and TenancyBroadcastManager * Fix code style (php-cs-fixer) * Bind original BroadcastManager again on `revert()` * Fix code style (php-cs-fixer) * Move manager to correct directory * Fix property type * Make BroadcastTenancyBootstrapper a singleton in tests * Fix code style (php-cs-fixer) * Bind the original broadcaster instance on `revert()` * Instead of just forgetting the old broadcaster instance, bind the new one * Add BroadcastTenancyBootstrapper tests * Separate the test * Fix code style (php-cs-fixer) * Add bootstrapper test * Add broadcaster channels test * Clean up BootstrapperTest * Fix BroadcastingTest * Add comments to TenancyBroadcastManager * Add BroadcastTenancyBootstrapper comments * Simplify BroadcastManager extension, remove setDriver method * Add comment * Fix PHPStan errors * Fix PHPStan errors * Remove duplicate import * Fix test * Delete `::class` from test name Co-authored-by: Samuel Štancl <samuel.stancl@gmail.com> * Create databases for newly created tenants in BroadcastingTest * move spatie/invade to require --------- Co-authored-by: PHP CS Fixer <phpcsfixer@example.com> Co-authored-by: Samuel Štancl <samuel.stancl@gmail.com>
This commit is contained in:
parent
fbdb13f392
commit
d7a4982cd3
7 changed files with 336 additions and 2 deletions
|
|
@ -18,11 +18,14 @@ use Stancl\Tenancy\Jobs\CreateDatabase;
|
|||
use Stancl\Tenancy\Events\TenantCreated;
|
||||
use Stancl\Tenancy\Events\TenantDeleted;
|
||||
use Stancl\Tenancy\Events\DeletingTenant;
|
||||
use Stancl\Tenancy\TenancyBroadcastManager;
|
||||
use Illuminate\Filesystem\FilesystemAdapter;
|
||||
use Illuminate\Broadcasting\BroadcastManager;
|
||||
use Stancl\Tenancy\Events\TenancyInitialized;
|
||||
use Stancl\Tenancy\Jobs\CreateStorageSymlinks;
|
||||
use Stancl\Tenancy\Jobs\RemoveStorageSymlinks;
|
||||
use Stancl\Tenancy\Listeners\BootstrapTenancy;
|
||||
use Stancl\Tenancy\Tests\Etc\TestingBroadcaster;
|
||||
use Stancl\Tenancy\Listeners\DeleteTenantStorage;
|
||||
use Stancl\Tenancy\Listeners\RevertToCentralContext;
|
||||
use Stancl\Tenancy\Bootstrappers\UrlTenancyBootstrapper;
|
||||
|
|
@ -31,6 +34,7 @@ use Stancl\Tenancy\Bootstrappers\CacheTenancyBootstrapper;
|
|||
use Stancl\Tenancy\Bootstrappers\RedisTenancyBootstrapper;
|
||||
use Stancl\Tenancy\Middleware\InitializeTenancyBySubdomain;
|
||||
use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper;
|
||||
use Stancl\Tenancy\Bootstrappers\BroadcastTenancyBootstrapper;
|
||||
use Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper;
|
||||
|
||||
beforeEach(function () {
|
||||
|
|
@ -331,6 +335,82 @@ test('local storage public urls are generated correctly', function() {
|
|||
expect(File::isDirectory($tenantStoragePath))->toBeFalse();
|
||||
});
|
||||
|
||||
test('BroadcastTenancyBootstrapper binds TenancyBroadcastManager to BroadcastManager and reverts the binding when tenancy is ended', function() {
|
||||
expect(app(BroadcastManager::class))->toBeInstanceOf(BroadcastManager::class);
|
||||
|
||||
tenancy()->initialize(Tenant::create());
|
||||
|
||||
expect(app(BroadcastManager::class))->toBeInstanceOf(TenancyBroadcastManager::class);
|
||||
|
||||
tenancy()->end();
|
||||
|
||||
expect(app(BroadcastManager::class))->toBeInstanceOf(BroadcastManager::class);
|
||||
});
|
||||
|
||||
test('BroadcastTenancyBootstrapper maps tenant broadcaster credentials to config as specified in the $credentialsMap property and reverts the config after ending tenancy', function() {
|
||||
config([
|
||||
'broadcasting.connections.testing.driver' => 'testing',
|
||||
'broadcasting.connections.testing.message' => $defaultMessage = 'default',
|
||||
]);
|
||||
|
||||
BroadcastTenancyBootstrapper::$credentialsMap = [
|
||||
'broadcasting.connections.testing.message' => 'testing_broadcaster_message',
|
||||
];
|
||||
|
||||
$tenant = Tenant::create(['testing_broadcaster_message' => $tenantMessage = 'first testing']);
|
||||
$tenant2 = Tenant::create(['testing_broadcaster_message' => $secondTenantMessage = 'second testing']);
|
||||
|
||||
tenancy()->initialize($tenant);
|
||||
|
||||
expect(array_key_exists('testing_broadcaster_message', tenant()->getAttributes()))->toBeTrue();
|
||||
expect(config('broadcasting.connections.testing.message'))->toBe($tenantMessage);
|
||||
|
||||
tenancy()->initialize($tenant2);
|
||||
|
||||
expect(config('broadcasting.connections.testing.message'))->toBe($secondTenantMessage);
|
||||
|
||||
tenancy()->end();
|
||||
|
||||
expect(config('broadcasting.connections.testing.message'))->toBe($defaultMessage);
|
||||
});
|
||||
|
||||
test('BroadcastTenancyBootstrapper makes the app use broadcasters with the correct credentials', function() {
|
||||
config([
|
||||
'broadcasting.default' => 'testing',
|
||||
'broadcasting.connections.testing.driver' => 'testing',
|
||||
'broadcasting.connections.testing.message' => $defaultMessage = 'default',
|
||||
]);
|
||||
|
||||
TenancyBroadcastManager::$tenantBroadcasters[] = 'testing';
|
||||
BroadcastTenancyBootstrapper::$credentialsMap = [
|
||||
'broadcasting.connections.testing.message' => 'testing_broadcaster_message',
|
||||
];
|
||||
|
||||
$registerTestingBroadcaster = fn() => app(BroadcastManager::class)->extend('testing', fn($app, $config) => new TestingBroadcaster($config['message']));
|
||||
|
||||
$registerTestingBroadcaster();
|
||||
|
||||
expect(invade(app(BroadcastManager::class)->driver())->message)->toBe($defaultMessage);
|
||||
|
||||
$tenant = Tenant::create(['testing_broadcaster_message' => $tenantMessage = 'first testing']);
|
||||
$tenant2 = Tenant::create(['testing_broadcaster_message' => $secondTenantMessage = 'second testing']);
|
||||
|
||||
tenancy()->initialize($tenant);
|
||||
$registerTestingBroadcaster();
|
||||
|
||||
expect(invade(app(BroadcastManager::class)->driver())->message)->toBe($tenantMessage);
|
||||
|
||||
tenancy()->initialize($tenant2);
|
||||
$registerTestingBroadcaster();
|
||||
|
||||
expect(invade(app(BroadcastManager::class)->driver())->message)->toBe($secondTenantMessage);
|
||||
|
||||
tenancy()->end();
|
||||
$registerTestingBroadcaster();
|
||||
|
||||
expect(invade(app(BroadcastManager::class)->driver())->message)->toBe($defaultMessage);
|
||||
});
|
||||
|
||||
test('MailTenancyBootstrapper maps tenant mail credentials to config as specified in the $credentialsMap property and makes the mailer use tenant credentials', function() {
|
||||
MailTenancyBootstrapper::$credentialsMap = [
|
||||
'mail.mailers.smtp.username' => 'smtp_username',
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue