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

Make 'redis' the only tenant cache store in beforeEach, test that tenantCacheStores works

This commit is contained in:
lukinovec 2023-01-05 16:24:37 +01:00
parent 42381d00e2
commit 7f10b9af41

View file

@ -19,9 +19,11 @@ beforeEach(function () {
'tenancy.bootstrappers' => [ 'tenancy.bootstrappers' => [
PrefixCacheTenancyBootstrapper::class PrefixCacheTenancyBootstrapper::class
], ],
'cache.default' => 'redis', 'cache.default' => $cacheDriver = 'redis',
]); ]);
PrefixCacheTenancyBootstrapper::$tenantCacheStores = [$cacheDriver];
TenancyCacheManager::$addTags = false; TenancyCacheManager::$addTags = false;
Event::listen(TenancyInitialized::class, BootstrapTenancy::class); Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
@ -204,38 +206,58 @@ test('stores other than the default one are not prefixed', function () {
expect(cache()->driver('redis2')->get('key'))->toBe($tenant2->getTenantKey()); expect(cache()->driver('redis2')->get('key'))->toBe($tenant2->getTenantKey());
}); });
test('drivers specified in the nonTenantCacheDrivers property do not get prefixed', function() { test('stores specified in tenantCacheStores get prefixed', function() {
$defaultPrefix = config('cache.prefix'); // Make the currently used store ('redis') the only store in $tenantCacheStores
PrefixCacheTenancyBootstrapper::$nonTenantCacheDrivers[] = config('cache.default'); PrefixCacheTenancyBootstrapper::$tenantCacheStores = ['redis'];
$expectPrefixToBeAnEmptyString = fn() => expect($defaultPrefix . ':')
->toBe(app('cache')->getPrefix()) app()->make(CacheService::class)->handle();
->toBe(app('cache.store')->getPrefix()); expect(cache('key'))->toBe($centralValue = 'central-value');
$tenant1 = Tenant::create();
$tenant2 = Tenant::create();
tenancy()->initialize($tenant1);
expect(cache('key'))->toBeNull();
app()->make(CacheService::class)->handle();
expect(cache('key'))->toBe($tenant1->getTenantKey());
tenancy()->initialize($tenant2);
expect(cache('key'))->toBeNull();
app()->make(CacheService::class)->handle();
expect(cache('key'))->toBe($tenant2->getTenantKey());
tenancy()->end();
expect(cache('key'))->toBe($centralValue);
});
test('stores that are not specified in tenantCacheStores do not get prefixed', function() {
config(['cache.stores.redis2' => config('cache.stores.redis')]);
config(['cache.default' => 'redis2']);
// Make 'redis' the only store in $tenantCacheStores so that the current store ('redis2') doesn't get prefixed
PrefixCacheTenancyBootstrapper::$tenantCacheStores = ['redis'];
$this->app->singleton(CacheService::class); $this->app->singleton(CacheService::class);
app()->make(CacheService::class)->handle(); app()->make(CacheService::class)->handle();
$expectPrefixToBeAnEmptyString();
expect(cache('key'))->toBe('central-value'); expect(cache('key'))->toBe('central-value');
$tenant1 = Tenant::create(); $tenant1 = Tenant::create();
$tenant2 = Tenant::create(); $tenant2 = Tenant::create();
tenancy()->initialize($tenant1); tenancy()->initialize($tenant1);
// The cache isn't prefixed, so it isn't separated
expect(cache('key'))->toBe('central-value'); expect(cache('key'))->toBe('central-value');
app()->make(CacheService::class)->handle(); app()->make(CacheService::class)->handle();
$expectPrefixToBeAnEmptyString();
expect(cache('key'))->toBe($tenant1->getTenantKey()); expect(cache('key'))->toBe($tenant1->getTenantKey());
tenancy()->initialize($tenant2); tenancy()->initialize($tenant2);
expect(cache('key'))->toBe($tenant1->getTenantKey()); expect(cache('key'))->toBe($tenant1->getTenantKey());
app()->make(CacheService::class)->handle(); app()->make(CacheService::class)->handle();
$expectPrefixToBeAnEmptyString();
expect(cache('key'))->toBe($tenant2->getTenantKey()); expect(cache('key'))->toBe($tenant2->getTenantKey());
tenancy()->end(); tenancy()->end();
$expectPrefixToBeAnEmptyString();
expect(cache('key'))->toBe($tenant2->getTenantKey()); expect(cache('key'))->toBe($tenant2->getTenantKey());
}); });