From 53b2181779baa8541400dd014043636e213e6259 Mon Sep 17 00:00:00 2001 From: lukinovec Date: Wed, 19 Apr 2023 13:37:06 +0200 Subject: [PATCH] Clean up global state (static properties) in before/afterEach --- tests/BootstrapperTest.php | 10 +++++++++- tests/BroadcastingTest.php | 7 ++++++- tests/DomainTest.php | 9 ++++++--- tests/GlobalCacheTest.php | 4 ++++ tests/MailTest.php | 5 +++++ 5 files changed, 30 insertions(+), 5 deletions(-) diff --git a/tests/BootstrapperTest.php b/tests/BootstrapperTest.php index d74079e7..9260c9c9 100644 --- a/tests/BootstrapperTest.php +++ b/tests/BootstrapperTest.php @@ -37,12 +37,16 @@ use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper; use Stancl\Tenancy\Bootstrappers\BroadcastTenancyBootstrapper; use Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper; use Stancl\Tenancy\Bootstrappers\PrefixCacheTenancyBootstrapper; -use Stancl\Tenancy\CacheManager; beforeEach(function () { $this->mockConsoleOutput = false; + config(['cache.default' => $cacheDriver = 'redis']); 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( TenantCreated::class, @@ -56,7 +60,11 @@ beforeEach(function () { }); afterEach(function () { + // Reset static properties of classes used in this test file to their default values + UrlTenancyBootstrapper::$rootUrlOverride = null; PrefixCacheTenancyBootstrapper::$tenantCacheStores = []; + TenancyBroadcastManager::$tenantBroadcasters = ['pusher', 'ably']; + BroadcastTenancyBootstrapper::$credentialsMap = []; }); test('database data is separated', function () { diff --git a/tests/BroadcastingTest.php b/tests/BroadcastingTest.php index aeb70de2..9505b537 100644 --- a/tests/BroadcastingTest.php +++ b/tests/BroadcastingTest.php @@ -13,12 +13,17 @@ use Stancl\Tenancy\Tests\Etc\TestingBroadcaster; use Stancl\Tenancy\Listeners\RevertToCentralContext; use Illuminate\Contracts\Broadcasting\Broadcaster as BroadcasterContract; -beforeEach(function() { +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(['broadcasting.default' => 'null']); TenancyBroadcastManager::$tenantBroadcasters[] = 'null'; diff --git a/tests/DomainTest.php b/tests/DomainTest.php index 02459914..cb104532 100644 --- a/tests/DomainTest.php +++ b/tests/DomainTest.php @@ -12,6 +12,8 @@ use Stancl\Tenancy\Middleware\InitializeTenancyByDomain; use Stancl\Tenancy\Resolvers\DomainTenantResolver; beforeEach(function () { + InitializeTenancyByDomain::$onFail = null; + Route::group([ 'middleware' => InitializeTenancyByDomain::class, ], function () { @@ -23,6 +25,10 @@ beforeEach(function () { config(['tenancy.models.tenant' => DomainTenant::class]); }); +afterEach(function () { + InitializeTenancyByDomain::$onFail = null; +}); + test('tenant can be identified using hostname', function () { $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 () { - // un-define onFail logic - InitializeTenancyByDomain::$onFail = null; - // Enable UniversalRoute feature Route::middlewareGroup('universal', []); diff --git a/tests/GlobalCacheTest.php b/tests/GlobalCacheTest.php index 1ffc2ee1..22fc2641 100644 --- a/tests/GlobalCacheTest.php +++ b/tests/GlobalCacheTest.php @@ -21,6 +21,10 @@ beforeEach(function () { Event::listen(TenancyEnded::class, RevertToCentralContext::class); }); +afterEach(function () { + PrefixCacheTenancyBootstrapper::$tenantCacheStores = []; +}); + test('global cache manager stores data in global cache', function (string $bootstrapper) { config(['tenancy.bootstrappers' => [$bootstrapper]]); diff --git a/tests/MailTest.php b/tests/MailTest.php index c530b7e8..dc48648a 100644 --- a/tests/MailTest.php +++ b/tests/MailTest.php @@ -12,11 +12,16 @@ use Stancl\Tenancy\Bootstrappers\MailTenancyBootstrapper; beforeEach(function() { config(['mail.default' => 'smtp']); + MailTenancyBootstrapper::$credentialsMap = []; Event::listen(TenancyInitialized::class, BootstrapTenancy::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 function assertMailerTransportUsesPassword(string|null $password) { $manager = app(MailManager::class);