mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 19:14:04 +00:00
Test cache prefixing customization
This commit is contained in:
parent
8348389fb3
commit
7b91e3fdec
1 changed files with 65 additions and 6 deletions
|
|
@ -22,6 +22,7 @@ beforeEach(function () {
|
||||||
]);
|
]);
|
||||||
|
|
||||||
PrefixCacheTenancyBootstrapper::$tenantCacheStores = [$cacheDriver];
|
PrefixCacheTenancyBootstrapper::$tenantCacheStores = [$cacheDriver];
|
||||||
|
PrefixCacheTenancyBootstrapper::$prefixGenerators = [];
|
||||||
|
|
||||||
TenancyCacheManager::$addTags = false;
|
TenancyCacheManager::$addTags = false;
|
||||||
|
|
||||||
|
|
@ -29,6 +30,10 @@ beforeEach(function () {
|
||||||
Event::listen(TenancyEnded::class, RevertToCentralContext::class);
|
Event::listen(TenancyEnded::class, RevertToCentralContext::class);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
afterEach(function () {
|
||||||
|
PrefixCacheTenancyBootstrapper::$tenantCacheStores = [];
|
||||||
|
});
|
||||||
|
|
||||||
test('Tenancy overrides CacheManager', function() {
|
test('Tenancy overrides CacheManager', function() {
|
||||||
expect(app('cache')::class)->toBe(TenancyCacheManager::class);
|
expect(app('cache')::class)->toBe(TenancyCacheManager::class);
|
||||||
expect(app(CacheManager::class)::class)->toBe(TenancyCacheManager::class);
|
expect(app(CacheManager::class)::class)->toBe(TenancyCacheManager::class);
|
||||||
|
|
@ -270,20 +275,74 @@ test('stores not specified in tenantCacheStores do not get prefixed', function()
|
||||||
expect(cache('key'))->toBe($tenant2->getTenantKey());
|
expect(cache('key'))->toBe($tenant2->getTenantKey());
|
||||||
});
|
});
|
||||||
|
|
||||||
test('stores that are not default get prefixed too', function () {
|
test('non default stores get prefixed too', function () {
|
||||||
config(['cache.stores.redis2' => config('cache.stores.redis')]);
|
config(['cache.stores.redis2' => config('cache.stores.redis')]);
|
||||||
|
// Make 'redis2' the default cache driver
|
||||||
config(['cache.default' => 'redis2']);
|
config(['cache.default' => 'redis2']);
|
||||||
PrefixCacheTenancyBootstrapper::$tenantCacheStores = ['redis', 'redis2'];
|
PrefixCacheTenancyBootstrapper::$tenantCacheStores = ['redis', 'redis2'];
|
||||||
|
|
||||||
|
// The prefix is the same for both drivers in the central context
|
||||||
|
$originalConfigPrefix = config('cache.prefix');
|
||||||
$defaultPrefix = cache()->store()->getPrefix();
|
$defaultPrefix = cache()->store()->getPrefix();
|
||||||
|
|
||||||
expect(cache()->store('redis')->getPrefix())->toBe($defaultPrefix);
|
expect(cache()->store('redis')->getPrefix())->toBe($defaultPrefix);
|
||||||
|
|
||||||
tenancy()->initialize($tenant = Tenant::create());
|
tenancy()->initialize($tenant = Tenant::create());
|
||||||
$generateTenantPrefix = fn (Tenant $tenant) => str($defaultPrefix)->beforeLast(':') . 'tenant_' . $tenant->getTenantKey() . ':';
|
|
||||||
|
|
||||||
// Default store
|
// We didn't add a prefix generator for our 'redis' driver, so we expect the prefix to be generated using the 'default' generator
|
||||||
expect(cache()->store()->getPrefix())->toBe($generateTenantPrefix($tenant));
|
expect(cache()->store()->getPrefix())->toBe($prefix = PrefixCacheTenancyBootstrapper::defaultPrefixGenerator($originalConfigPrefix)($tenant) . ':');
|
||||||
// Non-default store
|
// Non-default store
|
||||||
expect(cache()->store('redis')->getPrefix())->toBe($generateTenantPrefix($tenant));
|
expect(cache()->store('redis')->getPrefix())->toBe($prefix);
|
||||||
|
|
||||||
|
tenancy()->end();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('cache store prefix generation can be customized', function() {
|
||||||
|
config(['cache.default' => 'redis']);
|
||||||
|
PrefixCacheTenancyBootstrapper::$tenantCacheStores = ['redis'];
|
||||||
|
|
||||||
|
expect(PrefixCacheTenancyBootstrapper::$prefixGenerators)->not()->toHaveKey('redis');
|
||||||
|
|
||||||
|
// Add custom prefix generator for the 'redis' store
|
||||||
|
PrefixCacheTenancyBootstrapper::generatePrefixUsing('redis', $generateTenantPrefix = function (Tenant $tenant) {
|
||||||
|
return 'redis_tenant_cache_' . $tenant->getTenantKey();
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(PrefixCacheTenancyBootstrapper::$prefixGenerators)->toHaveKey('redis');
|
||||||
|
|
||||||
|
tenancy()->initialize($tenant = Tenant::create());
|
||||||
|
|
||||||
|
// Expect the 'redis' store to use the prefix generated by the custom generator
|
||||||
|
expect(cache()->store('redis')->getPrefix())->toBe($generateTenantPrefix($tenant) . ':');
|
||||||
|
|
||||||
|
tenancy()->end();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('stores get prefixed by the default prefix generator if the store does not have a corresponding generator', function() {
|
||||||
|
config(['cache.stores.redis2' => config('cache.stores.redis')]);
|
||||||
|
// Make 'redis2' the default cache driver
|
||||||
|
config(['cache.default' => 'redis2']);
|
||||||
|
|
||||||
|
PrefixCacheTenancyBootstrapper::$tenantCacheStores = ['redis', 'redis2'];
|
||||||
|
|
||||||
|
// Don't add a generator for 'redis2'
|
||||||
|
// Let the default generator generate the prefix
|
||||||
|
// The default generator is created for you in bootstrap()
|
||||||
|
tenancy()->initialize($tenant = Tenant::create());
|
||||||
|
$defaultGenerator = PrefixCacheTenancyBootstrapper::$prefixGenerators['default'];
|
||||||
|
expect(cache()->store()->getPrefix())->toBe($defaultGenerator($tenant) . ':');
|
||||||
|
// Other stores without a prefix generator use the default generator too
|
||||||
|
expect(cache()->store('redis')->getPrefix())->toBe($defaultGenerator($tenant) . ':');
|
||||||
|
tenancy()->end();
|
||||||
|
|
||||||
|
// You can override the default prefix generator
|
||||||
|
PrefixCacheTenancyBootstrapper::generatePrefixUsing('default', $newDefaultGenerator = function (Tenant $tenant) {
|
||||||
|
return 'new_' . $tenant->getTenantKey();
|
||||||
|
});
|
||||||
|
|
||||||
|
tenancy()->initialize($tenant = Tenant::create());
|
||||||
|
// The store gets prefixed using the new default generator
|
||||||
|
expect(cache()->store()->getPrefix())->toBe($newDefaultGenerator($tenant) . ':');
|
||||||
|
expect(cache()->store('redis')->getPrefix())->toBe($newDefaultGenerator($tenant) . ':');
|
||||||
|
|
||||||
|
tenancy()->end();
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue