mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-05 07:54:03 +00:00
misc improvements
This commit is contained in:
parent
4b4d808e30
commit
07c1d77148
1 changed files with 18 additions and 47 deletions
|
|
@ -54,13 +54,15 @@ test('correct cache prefix is used in all contexts', function () {
|
||||||
|
|
||||||
tenancy()->initialize($tenant1);
|
tenancy()->initialize($tenant1);
|
||||||
cache()->set('key', 'tenantone-value');
|
cache()->set('key', 'tenantone-value');
|
||||||
expect($bootstrapper->generatePrefix($tenant1))->toBe($tenantOnePrefix = $getDefaultPrefixForTenant($tenant1));
|
$tenantOnePrefix = $getDefaultPrefixForTenant($tenant1);
|
||||||
$expectCachePrefixToBe($tenantOnePrefix);
|
$expectCachePrefixToBe($tenantOnePrefix);
|
||||||
|
expect($bootstrapper->generatePrefix($tenant1))->toBe($tenantOnePrefix);
|
||||||
|
|
||||||
tenancy()->initialize($tenant2);
|
tenancy()->initialize($tenant2);
|
||||||
cache()->set('key', 'tenanttwo-value');
|
cache()->set('key', 'tenanttwo-value');
|
||||||
expect($bootstrapper->generatePrefix($tenant2))->toBe($tenantTwoPrefix = $getDefaultPrefixForTenant($tenant2));
|
$tenantTwoPrefix = $getDefaultPrefixForTenant($tenant2);
|
||||||
$expectCachePrefixToBe($tenantTwoPrefix);
|
$expectCachePrefixToBe($tenantTwoPrefix);
|
||||||
|
expect($bootstrapper->generatePrefix($tenant2))->toBe($tenantTwoPrefix);
|
||||||
|
|
||||||
// Prefix gets reverted to default after ending tenancy
|
// Prefix gets reverted to default after ending tenancy
|
||||||
tenancy()->end();
|
tenancy()->end();
|
||||||
|
|
@ -83,7 +85,7 @@ test('cache is persisted when reidentification is used', function () {
|
||||||
expect(cache('foo'))->toBe('bar');
|
expect(cache('foo'))->toBe('bar');
|
||||||
|
|
||||||
tenancy()->initialize($tenant2);
|
tenancy()->initialize($tenant2);
|
||||||
expect(cache('foo'))->not()->toBe('bar');
|
expect(cache('foo'))->toBeNull();
|
||||||
tenancy()->end();
|
tenancy()->end();
|
||||||
|
|
||||||
tenancy()->initialize($tenant1);
|
tenancy()->initialize($tenant1);
|
||||||
|
|
@ -100,7 +102,7 @@ test('prefixing separates the cache', function () {
|
||||||
$tenant2 = Tenant::create();
|
$tenant2 = Tenant::create();
|
||||||
tenancy()->initialize($tenant2);
|
tenancy()->initialize($tenant2);
|
||||||
|
|
||||||
expect(cache()->get('foo'))->not()->toBe('bar');
|
expect(cache()->get('foo'))->toBeNull();
|
||||||
|
|
||||||
cache()->put('foo', 'xyz');
|
cache()->put('foo', 'xyz');
|
||||||
expect(cache()->get('foo'))->toBe('xyz');
|
expect(cache()->get('foo'))->toBe('xyz');
|
||||||
|
|
@ -125,6 +127,10 @@ test('central cache is persisted', function () {
|
||||||
|
|
||||||
expect(cache()->get('key'))->toBe('central');
|
expect(cache()->get('key'))->toBe('central');
|
||||||
expect(cache()->get('key2'))->toBe('central-two');
|
expect(cache()->get('key2'))->toBe('central-two');
|
||||||
|
|
||||||
|
tenancy()->initialize($tenant1);
|
||||||
|
expect(cache()->get('key'))->toBe('tenant');
|
||||||
|
expect(cache()->get('key2'))->toBeNull();
|
||||||
});
|
});
|
||||||
|
|
||||||
test('cache base prefix is customizable', function () {
|
test('cache base prefix is customizable', function () {
|
||||||
|
|
@ -193,7 +199,7 @@ test('specific central cache store can be used inside a service', function () {
|
||||||
tenancy()->initialize($tenant1);
|
tenancy()->initialize($tenant1);
|
||||||
|
|
||||||
// The store isn't prefixed, so the cache isn't separated – the values persist from one context to another
|
// The store isn't prefixed, so the cache isn't separated – the values persist from one context to another
|
||||||
// Also check if the cache key SpecificCacheStoreService sets using the Repository singleton is set correctly
|
// Also assert that the value of 'key' is set correctly inside SpecificCacheStoreService according to the current context
|
||||||
expect(cache()->store($cacheStore)->get('key'))->toBe('central-value');
|
expect(cache()->store($cacheStore)->get('key'))->toBe('central-value');
|
||||||
$this->app->make(SpecificCacheStoreService::class)->handle();
|
$this->app->make(SpecificCacheStoreService::class)->handle();
|
||||||
expect(cache()->store($cacheStore)->get('key'))->toBe($tenant1->getTenantKey());
|
expect(cache()->store($cacheStore)->get('key'))->toBe($tenant1->getTenantKey());
|
||||||
|
|
@ -205,10 +211,11 @@ test('specific central cache store can be used inside a service', function () {
|
||||||
expect(cache()->store($cacheStore)->get('key'))->toBe($tenant2->getTenantKey());
|
expect(cache()->store($cacheStore)->get('key'))->toBe($tenant2->getTenantKey());
|
||||||
|
|
||||||
tenancy()->end();
|
tenancy()->end();
|
||||||
|
// We last executed handle() in tenant2's context, so the value should persist as tenant2's id
|
||||||
expect(cache()->store($cacheStore)->get('key'))->toBe($tenant2->getTenantKey());
|
expect(cache()->store($cacheStore)->get('key'))->toBe($tenant2->getTenantKey());
|
||||||
});
|
});
|
||||||
|
|
||||||
test('only the stores specified in tenantCacheStores get prefixed', function() {
|
test('only the stores specified in tenantCacheStores get prefixed', function () {
|
||||||
// Make sure the currently used store ('redis') is the only store in $tenantCacheStores
|
// Make sure the currently used store ('redis') is the only store in $tenantCacheStores
|
||||||
PrefixCacheTenancyBootstrapper::$tenantCacheStores = [$prefixedStore = 'redis'];
|
PrefixCacheTenancyBootstrapper::$tenantCacheStores = [$prefixedStore = 'redis'];
|
||||||
|
|
||||||
|
|
@ -250,34 +257,12 @@ test('only the stores specified in tenantCacheStores get prefixed', function() {
|
||||||
|
|
||||||
$this->app->make(CacheService::class)->handle();
|
$this->app->make(CacheService::class)->handle();
|
||||||
expect(cache('key'))->toBe($centralValue);
|
expect(cache('key'))->toBe($centralValue);
|
||||||
|
|
||||||
$tenant1 = Tenant::create();
|
|
||||||
$tenant2 = Tenant::create();
|
|
||||||
|
|
||||||
tenancy()->initialize($tenant1);
|
|
||||||
|
|
||||||
expect(cache('key'))->toBeNull();
|
|
||||||
$this->app->make(CacheService::class)->handle();
|
|
||||||
expect(cache('key'))->toBe($tenant1->getTenantKey());
|
|
||||||
|
|
||||||
$assertStoreIsNotPrefixed('redis2');
|
|
||||||
|
|
||||||
tenancy()->initialize($tenant2);
|
|
||||||
|
|
||||||
expect(cache('key'))->toBeNull();
|
|
||||||
$this->app->make(CacheService::class)->handle();
|
|
||||||
expect(cache('key'))->toBe($tenant2->getTenantKey());
|
|
||||||
|
|
||||||
$assertStoreIsNotPrefixed('redis2');
|
|
||||||
|
|
||||||
tenancy()->end();
|
|
||||||
expect(cache('key'))->toBe($centralValue);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('non default stores get prefixed too when specified in tenantCacheStores', function () {
|
test('non default stores get prefixed too when specified in tenantCacheStores', function () {
|
||||||
// In beforeEach, we set $tenantCacheStores to ['redis', 'redis2']
|
// In beforeEach, we set $tenantCacheStores to ['redis', 'redis2']
|
||||||
// Make 'redis2' the default cache driver
|
// Make 'redis' the default cache driver
|
||||||
config(['cache.default' => 'redis2']);
|
config(['cache.default' => 'redis']);
|
||||||
|
|
||||||
$tenant = Tenant::create();
|
$tenant = Tenant::create();
|
||||||
$defaultPrefix = cache()->store()->getPrefix();
|
$defaultPrefix = cache()->store()->getPrefix();
|
||||||
|
|
@ -285,13 +270,14 @@ test('non default stores get prefixed too when specified in tenantCacheStores',
|
||||||
|
|
||||||
// The prefix is the same for both drivers in the central context
|
// The prefix is the same for both drivers in the central context
|
||||||
expect(cache()->store('redis')->getPrefix())->toBe($defaultPrefix);
|
expect(cache()->store('redis')->getPrefix())->toBe($defaultPrefix);
|
||||||
|
expect(cache()->store('redis2')->getPrefix())->toBe($defaultPrefix);
|
||||||
|
|
||||||
tenancy()->initialize($tenant);
|
tenancy()->initialize($tenant);
|
||||||
|
|
||||||
// We didn't add a prefix generator for our 'redis' driver, so we expect the prefix to be generated using the 'default' generator
|
// We didn't add a prefix generator for our 'redis2' driver, so we expect the prefix to be generated using the 'default' generator
|
||||||
expect($bootstrapper->generatePrefix($tenant) . ':')
|
expect($bootstrapper->generatePrefix($tenant) . ':')
|
||||||
->toBe(cache()->getPrefix())
|
->toBe(cache()->getPrefix())
|
||||||
->toBe(cache()->store('redis')->getPrefix()); // Non-default store
|
->toBe(cache()->store('redis2')->getPrefix()); // Non-default store
|
||||||
|
|
||||||
tenancy()->end();
|
tenancy()->end();
|
||||||
});
|
});
|
||||||
|
|
@ -315,21 +301,6 @@ test('cache store prefix generation can be customized', function() {
|
||||||
->toBe(app('cache')->getPrefix())
|
->toBe(app('cache')->getPrefix())
|
||||||
->toBe(app('cache.store')->getPrefix());
|
->toBe(app('cache.store')->getPrefix());
|
||||||
|
|
||||||
config(['cache.default' => 'redis2']);
|
|
||||||
|
|
||||||
// Use different prefix generator
|
|
||||||
PrefixCacheTenancyBootstrapper::generatePrefixUsing($customPrefixGenerator = function (Tenant $tenant) {
|
|
||||||
return 'redis2_tenant_cache_' . $tenant->getTenantKey();
|
|
||||||
});
|
|
||||||
|
|
||||||
tenancy()->initialize($tenant = Tenant::create());
|
|
||||||
|
|
||||||
expect($customPrefixGenerator($tenant) . ':')
|
|
||||||
->toBe(cache()->getPrefix())
|
|
||||||
->toBe(cache()->store('redis')->getPrefix())
|
|
||||||
->toBe(app('cache')->getPrefix())
|
|
||||||
->toBe(app('cache.store')->getPrefix());
|
|
||||||
|
|
||||||
tenancy()->end();
|
tenancy()->end();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue