1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-13 09:44:03 +00:00

Clean up global state (static properties) in before/afterEach

This commit is contained in:
lukinovec 2023-04-19 13:37:06 +02:00
parent 52d10d36f9
commit 53b2181779
5 changed files with 30 additions and 5 deletions

View file

@ -37,12 +37,16 @@ use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper;
use Stancl\Tenancy\Bootstrappers\BroadcastTenancyBootstrapper; use Stancl\Tenancy\Bootstrappers\BroadcastTenancyBootstrapper;
use Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper; use Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper;
use Stancl\Tenancy\Bootstrappers\PrefixCacheTenancyBootstrapper; use Stancl\Tenancy\Bootstrappers\PrefixCacheTenancyBootstrapper;
use Stancl\Tenancy\CacheManager;
beforeEach(function () { beforeEach(function () {
$this->mockConsoleOutput = false; $this->mockConsoleOutput = false;
config(['cache.default' => $cacheDriver = 'redis']); config(['cache.default' => $cacheDriver = 'redis']);
PrefixCacheTenancyBootstrapper::$tenantCacheStores = [$cacheDriver]; PrefixCacheTenancyBootstrapper::$tenantCacheStores = [$cacheDriver];
// Reset static properties of classes used in this test file to their default values
BroadcastTenancyBootstrapper::$credentialsMap = [];
TenancyBroadcastManager::$tenantBroadcasters = ['pusher', 'ably'];
UrlTenancyBootstrapper::$rootUrlOverride = null;
Event::listen( Event::listen(
TenantCreated::class, TenantCreated::class,
@ -56,7 +60,11 @@ beforeEach(function () {
}); });
afterEach(function () { afterEach(function () {
// Reset static properties of classes used in this test file to their default values
UrlTenancyBootstrapper::$rootUrlOverride = null;
PrefixCacheTenancyBootstrapper::$tenantCacheStores = []; PrefixCacheTenancyBootstrapper::$tenantCacheStores = [];
TenancyBroadcastManager::$tenantBroadcasters = ['pusher', 'ably'];
BroadcastTenancyBootstrapper::$credentialsMap = [];
}); });
test('database data is separated', function () { test('database data is separated', function () {

View file

@ -13,12 +13,17 @@ use Stancl\Tenancy\Tests\Etc\TestingBroadcaster;
use Stancl\Tenancy\Listeners\RevertToCentralContext; use Stancl\Tenancy\Listeners\RevertToCentralContext;
use Illuminate\Contracts\Broadcasting\Broadcaster as BroadcasterContract; use Illuminate\Contracts\Broadcasting\Broadcaster as BroadcasterContract;
beforeEach(function() { beforeEach(function () {
withTenantDatabases(); withTenantDatabases();
TenancyBroadcastManager::$tenantBroadcasters = ['pusher', 'ably'];
Event::listen(TenancyInitialized::class, BootstrapTenancy::class); Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
Event::listen(TenancyEnded::class, RevertToCentralContext::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() { test('bound broadcaster instance is the same before initializing tenancy and after ending it', function() {
config(['broadcasting.default' => 'null']); config(['broadcasting.default' => 'null']);
TenancyBroadcastManager::$tenantBroadcasters[] = 'null'; TenancyBroadcastManager::$tenantBroadcasters[] = 'null';

View file

@ -12,6 +12,8 @@ use Stancl\Tenancy\Middleware\InitializeTenancyByDomain;
use Stancl\Tenancy\Resolvers\DomainTenantResolver; use Stancl\Tenancy\Resolvers\DomainTenantResolver;
beforeEach(function () { beforeEach(function () {
InitializeTenancyByDomain::$onFail = null;
Route::group([ Route::group([
'middleware' => InitializeTenancyByDomain::class, 'middleware' => InitializeTenancyByDomain::class,
], function () { ], function () {
@ -23,6 +25,10 @@ beforeEach(function () {
config(['tenancy.models.tenant' => DomainTenant::class]); config(['tenancy.models.tenant' => DomainTenant::class]);
}); });
afterEach(function () {
InitializeTenancyByDomain::$onFail = null;
});
test('tenant can be identified using hostname', function () { test('tenant can be identified using hostname', function () {
$tenant = DomainTenant::create(); $tenant = DomainTenant::create();
@ -89,9 +95,6 @@ test('onfail logic can be customized', function () {
}); });
test('throw correct exception when onFail is null and universal routes are enabled', function () { test('throw correct exception when onFail is null and universal routes are enabled', function () {
// un-define onFail logic
InitializeTenancyByDomain::$onFail = null;
// Enable UniversalRoute feature // Enable UniversalRoute feature
Route::middlewareGroup('universal', []); Route::middlewareGroup('universal', []);

View file

@ -21,6 +21,10 @@ beforeEach(function () {
Event::listen(TenancyEnded::class, RevertToCentralContext::class); Event::listen(TenancyEnded::class, RevertToCentralContext::class);
}); });
afterEach(function () {
PrefixCacheTenancyBootstrapper::$tenantCacheStores = [];
});
test('global cache manager stores data in global cache', function (string $bootstrapper) { test('global cache manager stores data in global cache', function (string $bootstrapper) {
config(['tenancy.bootstrappers' => [$bootstrapper]]); config(['tenancy.bootstrappers' => [$bootstrapper]]);

View file

@ -12,11 +12,16 @@ use Stancl\Tenancy\Bootstrappers\MailTenancyBootstrapper;
beforeEach(function() { beforeEach(function() {
config(['mail.default' => 'smtp']); config(['mail.default' => 'smtp']);
MailTenancyBootstrapper::$credentialsMap = [];
Event::listen(TenancyInitialized::class, BootstrapTenancy::class); Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
Event::listen(TenancyEnded::class, RevertToCentralContext::class); Event::listen(TenancyEnded::class, RevertToCentralContext::class);
}); });
afterEach(function () {
MailTenancyBootstrapper::$credentialsMap = [];
});
// Initialize tenancy as $tenant and assert that the smtp mailer's transport has the correct password // Initialize tenancy as $tenant and assert that the smtp mailer's transport has the correct password
function assertMailerTransportUsesPassword(string|null $password) { function assertMailerTransportUsesPassword(string|null $password) {
$manager = app(MailManager::class); $manager = app(MailManager::class);