mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-05 13:14:05 +00:00
Clean up BootstrapperTest
This commit is contained in:
parent
6b066f8496
commit
aa2fcdf9bc
3 changed files with 90 additions and 71 deletions
|
|
@ -2,7 +2,6 @@
|
||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
use Illuminate\Broadcasting\Broadcasters\Broadcaster;
|
|
||||||
use Illuminate\Support\Str;
|
use Illuminate\Support\Str;
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
use Stancl\JobPipeline\JobPipeline;
|
use Stancl\JobPipeline\JobPipeline;
|
||||||
|
|
@ -20,12 +19,11 @@ use Stancl\Tenancy\Events\DeletingTenant;
|
||||||
use Stancl\Tenancy\TenancyBroadcastManager;
|
use Stancl\Tenancy\TenancyBroadcastManager;
|
||||||
use Illuminate\Filesystem\FilesystemAdapter;
|
use Illuminate\Filesystem\FilesystemAdapter;
|
||||||
use Illuminate\Broadcasting\BroadcastManager;
|
use Illuminate\Broadcasting\BroadcastManager;
|
||||||
use Illuminate\Contracts\Broadcasting\Broadcaster as BroadcasterContract;
|
|
||||||
use Illuminate\Support\Facades\Broadcast;
|
|
||||||
use Stancl\Tenancy\Events\TenancyInitialized;
|
use Stancl\Tenancy\Events\TenancyInitialized;
|
||||||
use Stancl\Tenancy\Jobs\CreateStorageSymlinks;
|
use Stancl\Tenancy\Jobs\CreateStorageSymlinks;
|
||||||
use Stancl\Tenancy\Jobs\RemoveStorageSymlinks;
|
use Stancl\Tenancy\Jobs\RemoveStorageSymlinks;
|
||||||
use Stancl\Tenancy\Listeners\BootstrapTenancy;
|
use Stancl\Tenancy\Listeners\BootstrapTenancy;
|
||||||
|
use Stancl\Tenancy\Tests\Etc\TestingBroadcaster;
|
||||||
use Stancl\Tenancy\Listeners\DeleteTenantStorage;
|
use Stancl\Tenancy\Listeners\DeleteTenantStorage;
|
||||||
use Stancl\Tenancy\Listeners\RevertToCentralContext;
|
use Stancl\Tenancy\Listeners\RevertToCentralContext;
|
||||||
use Stancl\Tenancy\Bootstrappers\CacheTenancyBootstrapper;
|
use Stancl\Tenancy\Bootstrappers\CacheTenancyBootstrapper;
|
||||||
|
|
@ -344,22 +342,6 @@ test('BroadcastTenancyBootstrapper binds TenancyBroadcastManager to BroadcastMan
|
||||||
expect(app(BroadcastManager::class))->toBeInstanceOf(BroadcastManager::class);
|
expect(app(BroadcastManager::class))->toBeInstanceOf(BroadcastManager::class);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('bound broadcaster instance is the same before initializing tenancy and after ending it', function() {
|
|
||||||
config(['broadcasting.default' => 'null']);
|
|
||||||
TenancyBroadcastManager::$tenantBroadcasters[] = 'null';
|
|
||||||
|
|
||||||
$originalBroadcaster = app(BroadcasterContract::class);
|
|
||||||
|
|
||||||
tenancy()->initialize(Tenant::create());
|
|
||||||
|
|
||||||
// TenancyBroadcastManager binds new broadcaster
|
|
||||||
app(BroadcastManager::class)->driver();
|
|
||||||
|
|
||||||
tenancy()->end();
|
|
||||||
|
|
||||||
expect($originalBroadcaster)->toBe(app(BroadcasterContract::class));
|
|
||||||
});
|
|
||||||
|
|
||||||
test('BroadcastTenancyBootstrapper maps tenant broadcaster credentials to config as specified in the $credentialsMap property and reverts the config after ending tenancy', function() {
|
test('BroadcastTenancyBootstrapper maps tenant broadcaster credentials to config as specified in the $credentialsMap property and reverts the config after ending tenancy', function() {
|
||||||
config([
|
config([
|
||||||
'broadcasting.connections.testing.driver' => 'testing',
|
'broadcasting.connections.testing.driver' => 'testing',
|
||||||
|
|
@ -387,7 +369,7 @@ test('BroadcastTenancyBootstrapper maps tenant broadcaster credentials to config
|
||||||
expect(config('broadcasting.connections.testing.message'))->toBe($defaultMessage);
|
expect(config('broadcasting.connections.testing.message'))->toBe($defaultMessage);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('broadcasters are created with the correct credentials', function() {
|
test('BroadcastTenancyBootstrapper makes the app use broadcasters with the correct credentials', function() {
|
||||||
config([
|
config([
|
||||||
'broadcasting.default' => 'testing',
|
'broadcasting.default' => 'testing',
|
||||||
'broadcasting.connections.testing.driver' => 'testing',
|
'broadcasting.connections.testing.driver' => 'testing',
|
||||||
|
|
@ -424,37 +406,6 @@ test('broadcasters are created with the correct credentials', function() {
|
||||||
expect(invade(app(BroadcastManager::class)->driver())->message)->toBe($defaultMessage);
|
expect(invade(app(BroadcastManager::class)->driver())->message)->toBe($defaultMessage);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('new broadcasters get the channels from the previously bound broadcaster', function() {
|
|
||||||
config([
|
|
||||||
'broadcasting.default' => $driver = 'testing',
|
|
||||||
'broadcasting.connections.testing.driver' => $driver,
|
|
||||||
'broadcasting.connections.testing.message' => $defaultMessage = 'default',
|
|
||||||
]);
|
|
||||||
|
|
||||||
TenancyBroadcastManager::$tenantBroadcasters[] = $driver;
|
|
||||||
BroadcastTenancyBootstrapper::$credentialsMap = [
|
|
||||||
'broadcasting.connections.testing.message' => 'testing_broadcaster_message',
|
|
||||||
];
|
|
||||||
|
|
||||||
$registerTestingBroadcaster = fn() => app(BroadcastManager::class)->extend('testing', fn($app, $config) => new TestingBroadcaster($config['message']));
|
|
||||||
$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());
|
|
||||||
});
|
|
||||||
|
|
||||||
function getDiskPrefix(string $disk): string
|
function getDiskPrefix(string $disk): string
|
||||||
{
|
{
|
||||||
/** @var FilesystemAdapter $disk */
|
/** @var FilesystemAdapter $disk */
|
||||||
|
|
@ -472,23 +423,3 @@ function getDiskPrefix(string $disk): string
|
||||||
|
|
||||||
return $prefix->getValue($prefixer);
|
return $prefix->getValue($prefixer);
|
||||||
}
|
}
|
||||||
|
|
||||||
class TestingBroadcaster extends Broadcaster {
|
|
||||||
public function __construct(
|
|
||||||
public string $message
|
|
||||||
) {}
|
|
||||||
|
|
||||||
public function auth($request)
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function validAuthenticationResponse($request, $result)
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function broadcast(array $channels, $event, array $payload = [])
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
||||||
63
tests/BroadcastingTest.php
Normal file
63
tests/BroadcastingTest.php
Normal file
|
|
@ -0,0 +1,63 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
use Illuminate\Support\Facades\Event;
|
||||||
|
use Stancl\Tenancy\Events\TenancyEnded;
|
||||||
|
use Illuminate\Support\Facades\Broadcast;
|
||||||
|
use Stancl\Tenancy\TenancyBroadcastManager;
|
||||||
|
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\Bootstrappers\BroadcastTenancyBootstrapper;
|
||||||
|
use Illuminate\Contracts\Broadcasting\Broadcaster as BroadcasterContract;
|
||||||
|
|
||||||
|
beforeEach(function() {
|
||||||
|
Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
|
||||||
|
Event::listen(TenancyEnded::class, RevertToCentralContext::class);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('bound broadcaster instance is the same before initializing tenancy and after ending it', function() {
|
||||||
|
config(['broadcasting.default' => 'testing']);
|
||||||
|
TenancyBroadcastManager::$tenantBroadcasters[] = 'testing';
|
||||||
|
|
||||||
|
$originalBroadcaster = app(BroadcasterContract::class);
|
||||||
|
|
||||||
|
tenancy()->initialize(Tenant::create());
|
||||||
|
|
||||||
|
// TenancyBroadcastManager binds new broadcaster
|
||||||
|
app(BroadcastManager::class)->driver();
|
||||||
|
|
||||||
|
tenancy()->end();
|
||||||
|
|
||||||
|
expect($originalBroadcaster)->toBe(app(BroadcasterContract::class));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('new broadcasters get the channels from the previously bound broadcaster', function() {
|
||||||
|
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($config['message']));
|
||||||
|
$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());
|
||||||
|
});
|
||||||
25
tests/Etc/TestingBroadcaster.php
Normal file
25
tests/Etc/TestingBroadcaster.php
Normal file
|
|
@ -0,0 +1,25 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Stancl\Tenancy\Tests\Etc;
|
||||||
|
|
||||||
|
use Illuminate\Broadcasting\Broadcasters\Broadcaster;
|
||||||
|
|
||||||
|
class TestingBroadcaster extends Broadcaster {
|
||||||
|
public function __construct(
|
||||||
|
public string $message
|
||||||
|
) {}
|
||||||
|
|
||||||
|
public function auth($request)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function validAuthenticationResponse($request, $result)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function broadcast(array $channels, $event, array $payload = [])
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue