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

Update prefix generator logic + tests

This commit is contained in:
lukinovec 2023-04-13 12:46:33 +02:00
parent 8ac4d87e94
commit 740d4e78d8
2 changed files with 33 additions and 31 deletions

View file

@ -22,7 +22,7 @@ beforeEach(function () {
]);
PrefixCacheTenancyBootstrapper::$tenantCacheStores = [$cacheDriver];
PrefixCacheTenancyBootstrapper::$prefixGenerators = [];
PrefixCacheTenancyBootstrapper::$prefixGenerator = null;
TenancyCacheManager::$addTags = false;
@ -32,7 +32,7 @@ beforeEach(function () {
afterEach(function () {
PrefixCacheTenancyBootstrapper::$tenantCacheStores = [];
PrefixCacheTenancyBootstrapper::$prefixGenerators = [];
PrefixCacheTenancyBootstrapper::$prefixGenerator = null;
TenancyCacheManager::$addTags = true;
});
@ -74,9 +74,12 @@ test('correct cache prefix is used in all contexts', function () {
$tenantOnePrefix = $originalPrefix . $prefixBase . $tenant1->getTenantKey();
tenancy()->initialize($tenant1);
// Default prefix generator
$prefixGenerator = PrefixCacheTenancyBootstrapper::$prefixGenerator;
cache()->set('key', 'tenantone-value');
$expectPrefixToBe($tenantOnePrefix);
$expectPrefixToBe($prefixGenerator($tenant1));
$tenantTwoPrefix = $originalPrefix . $prefixBase . $tenant2->getTenantKey();
@ -84,7 +87,7 @@ test('correct cache prefix is used in all contexts', function () {
cache()->set('key', 'tenanttwo-value');
$expectPrefixToBe($tenantTwoPrefix);
$expectPrefixToBe($prefixGenerator($tenant2));
// Prefix gets reverted to default after ending tenancy
tenancy()->end();
@ -287,11 +290,11 @@ test('non default stores get prefixed too', function () {
// The prefix is the same for both drivers in the central context
$tenant = Tenant::create();
$defaultPrefix = cache()->store()->getPrefix();
$expectedPrefix = config('cache.prefix', '') . config('tenancy.cache.prefix_base', '') . $tenant->getTenantKey();
expect(cache()->store('redis')->getPrefix())->toBe($defaultPrefix);
tenancy()->initialize($tenant);
$expectedPrefix = (PrefixCacheTenancyBootstrapper::$prefixGenerator)($tenant);
// 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($expectedPrefix . ':');
@ -305,19 +308,17 @@ 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) {
PrefixCacheTenancyBootstrapper::generatePrefixUsing($customPrefixGenerator = function (Tenant $tenant) {
return 'redis_tenant_cache_' . $tenant->getTenantKey();
});
expect(PrefixCacheTenancyBootstrapper::$prefixGenerators)->toHaveKey('redis');
expect(PrefixCacheTenancyBootstrapper::$prefixGenerator)->toBe($customPrefixGenerator);
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) . ':');
expect(cache()->store('redis')->getPrefix())->toBe($customPrefixGenerator($tenant) . ':');
tenancy()->end();
});
@ -327,15 +328,18 @@ test('stores get prefixed using the default way if the store does not have a cor
// Make 'redis2' the default cache driver
config(['cache.default' => 'redis2']);
$tenant = Tenant::create();
$expectedPrefix = config('cache.prefix', '') . config('tenancy.cache.prefix_base', '') . $tenant->getTenantKey();
PrefixCacheTenancyBootstrapper::$tenantCacheStores = ['redis', 'redis2'];
// Don't add a generator for 'redis2'
// Let the prefix get created using the default approach
tenancy()->initialize($tenant);
$expectedPrefix = (PrefixCacheTenancyBootstrapper::$prefixGenerator)($tenant);
expect(cache()->store()->getPrefix())->toBe($expectedPrefix . ':');
// Other stores without a prefix generator use the default generator too
expect(cache()->store('redis')->getPrefix())->toBe($expectedPrefix . ':');
tenancy()->end();
});