1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 23:54:04 +00:00

Update cache tests so that both prefixing and tagging is covered

This commit is contained in:
lukinovec 2023-04-13 13:36:58 +02:00
parent 651302943f
commit 9e15110ad9
2 changed files with 61 additions and 12 deletions

View file

@ -36,6 +36,8 @@ use Stancl\Tenancy\Middleware\InitializeTenancyBySubdomain;
use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper; 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\CacheManager;
beforeEach(function () { beforeEach(function () {
$this->mockConsoleOutput = false; $this->mockConsoleOutput = false;
@ -82,12 +84,19 @@ test('database data is separated', function () {
expect(DB::table('users')->first()->name)->toBe('Foo'); 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([ config([
'tenancy.bootstrappers' => [ 'tenancy.bootstrappers' => [$bootstrapper],
CacheTagsBootstrapper::class, 'cache.default' => $cacheDriver,
],
'cache.default' => 'redis',
]); ]);
$tenant1 = Tenant::create(); $tenant1 = Tenant::create();
@ -121,7 +130,13 @@ test('cache data is separated', function () {
// Asset central is still the same // Asset central is still the same
expect(Cache::get('foo'))->toBe('central'); 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 () { test('redis data is separated', function () {
config(['tenancy.bootstrappers' => [ config(['tenancy.bootstrappers' => [

View file

@ -2,6 +2,7 @@
declare(strict_types=1); declare(strict_types=1);
use Stancl\Tenancy\CacheManager;
use Stancl\Tenancy\Tests\Etc\Tenant; use Stancl\Tenancy\Tests\Etc\Tenant;
use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Event;
use Stancl\Tenancy\Events\TenancyEnded; use Stancl\Tenancy\Events\TenancyEnded;
@ -10,15 +11,28 @@ use Stancl\Tenancy\Events\TenancyInitialized;
use Stancl\Tenancy\Listeners\BootstrapTenancy; use Stancl\Tenancy\Listeners\BootstrapTenancy;
use Stancl\Tenancy\Listeners\RevertToCentralContext; use Stancl\Tenancy\Listeners\RevertToCentralContext;
use Stancl\Tenancy\Bootstrappers\CacheTagsBootstrapper; use Stancl\Tenancy\Bootstrappers\CacheTagsBootstrapper;
use Stancl\Tenancy\Bootstrappers\PrefixCacheTenancyBootstrapper;
beforeEach(function () { beforeEach(function () {
config(['tenancy.bootstrappers' => [CacheTagsBootstrapper::class]]);
Event::listen(TenancyInitialized::class, BootstrapTenancy::class); Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
Event::listen(TenancyEnded::class, RevertToCentralContext::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); expect(cache('foo'))->toBe(null);
GlobalCache::put(['foo' => 'bar'], 1); GlobalCache::put(['foo' => 'bar'], 1);
expect(GlobalCache::get('foo'))->toBe('bar'); expect(GlobalCache::get('foo'))->toBe('bar');
@ -46,9 +60,26 @@ test('global cache manager stores data in global cache', function () {
tenancy()->initialize($tenant1); tenancy()->initialize($tenant1);
expect(cache('def'))->toBe('ghi'); 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 = Tenant::create();
$tenant->enter(); $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(global_cache()->get('foo'))->toBe('baz');
expect(cache('foo'))->toBe(null); // tenant cache is not affected expect(cache('foo'))->toBe(null); // tenant cache is not affected
}); })->with([
'tagging' => CacheTagsBootstrapper::class,
'prefixing' => PrefixCacheTenancyBootstrapper::class,
]);