diff --git a/tests/BootstrapperTest.php b/tests/BootstrapperTest.php index a71b54d4..9e5f8c72 100644 --- a/tests/BootstrapperTest.php +++ b/tests/BootstrapperTest.php @@ -36,6 +36,8 @@ use Stancl\Tenancy\Middleware\InitializeTenancyBySubdomain; 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; @@ -82,12 +84,19 @@ test('database data is separated', function () { expect(DB::table('users')->first()->name)->toBe('Foo'); }); -test('cache data is separated', function () { +test('cache data is separated', function (string $bootstrapper) { + $cacheDriver = 'redis'; + + if ($bootstrapper === PrefixCacheTenancyBootstrapper::class) { + CacheManager::$addTags = false; + PrefixCacheTenancyBootstrapper::$tenantCacheStores = [$cacheDriver]; + } else { + CacheManager::$addTags = true; + } + config([ - 'tenancy.bootstrappers' => [ - CacheTagsBootstrapper::class, - ], - 'cache.default' => 'redis', + 'tenancy.bootstrappers' => [$bootstrapper], + 'cache.default' => $cacheDriver, ]); $tenant1 = Tenant::create(); @@ -121,7 +130,13 @@ test('cache data is separated', function () { // Asset central is still the same expect(Cache::get('foo'))->toBe('central'); -}); + + // Reset the static property + CacheManager::$addTags = false; +})->with([ + 'tagging' => CacheTagsBootstrapper::class, + 'prefixing' => PrefixCacheTenancyBootstrapper::class, +])->group('bootstrapper'); test('redis data is separated', function () { config(['tenancy.bootstrappers' => [ diff --git a/tests/GlobalCacheTest.php b/tests/GlobalCacheTest.php index 6106a13c..44e14dfe 100644 --- a/tests/GlobalCacheTest.php +++ b/tests/GlobalCacheTest.php @@ -2,6 +2,7 @@ declare(strict_types=1); +use Stancl\Tenancy\CacheManager; use Stancl\Tenancy\Tests\Etc\Tenant; use Illuminate\Support\Facades\Event; use Stancl\Tenancy\Events\TenancyEnded; @@ -10,15 +11,28 @@ use Stancl\Tenancy\Events\TenancyInitialized; use Stancl\Tenancy\Listeners\BootstrapTenancy; use Stancl\Tenancy\Listeners\RevertToCentralContext; use Stancl\Tenancy\Bootstrappers\CacheTagsBootstrapper; +use Stancl\Tenancy\Bootstrappers\PrefixCacheTenancyBootstrapper; beforeEach(function () { - config(['tenancy.bootstrappers' => [CacheTagsBootstrapper::class]]); - Event::listen(TenancyInitialized::class, BootstrapTenancy::class); Event::listen(TenancyEnded::class, RevertToCentralContext::class); }); -test('global cache manager stores data in global cache', function () { +test('global cache manager stores data in global cache', function (string $bootstrapper) { + $cacheDriver = 'redis'; + + if ($bootstrapper === PrefixCacheTenancyBootstrapper::class) { + CacheManager::$addTags = false; + PrefixCacheTenancyBootstrapper::$tenantCacheStores = [$cacheDriver]; + } else { + CacheManager::$addTags = true; + } + + config([ + 'tenancy.bootstrappers' => [$bootstrapper], + 'cache.default' => $cacheDriver, + ]); + expect(cache('foo'))->toBe(null); GlobalCache::put(['foo' => 'bar'], 1); expect(GlobalCache::get('foo'))->toBe('bar'); @@ -46,9 +60,26 @@ test('global cache manager stores data in global cache', function () { tenancy()->initialize($tenant1); expect(cache('def'))->toBe('ghi'); -}); +})->with([ + 'tagging' => CacheTagsBootstrapper::class, + 'prefixing' => PrefixCacheTenancyBootstrapper::class, +]); + +test('the global_cache helper supports the same syntax as the cache helper', function (string $bootstrapper) { + $cacheDriver = 'redis'; + + if ($bootstrapper === PrefixCacheTenancyBootstrapper::class) { + CacheManager::$addTags = false; + PrefixCacheTenancyBootstrapper::$tenantCacheStores = [$cacheDriver]; + } else { + CacheManager::$addTags = true; + } + + config([ + 'tenancy.bootstrappers' => [$bootstrapper], + 'cache.default' => $cacheDriver, + ]); -test('the global_cache helper supports the same syntax as the cache helper', function () { $tenant = Tenant::create(); $tenant->enter(); @@ -61,4 +92,7 @@ test('the global_cache helper supports the same syntax as the cache helper', fun expect(global_cache()->get('foo'))->toBe('baz'); expect(cache('foo'))->toBe(null); // tenant cache is not affected -}); +})->with([ + 'tagging' => CacheTagsBootstrapper::class, + 'prefixing' => PrefixCacheTenancyBootstrapper::class, +]);