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

Extract duplicate assertions into a closure

This commit is contained in:
lukinovec 2023-04-18 09:42:43 +02:00
parent e4a4940e59
commit 46f94d15a5

View file

@ -230,13 +230,21 @@ test('specific central cache store can be used inside a service', function () {
}); });
test('only the stores specified in tenantCacheStores get prefixed', function() { test('only the stores specified in tenantCacheStores get prefixed', function() {
// Make the currently used store ('redis') the only store in $tenantCacheStores // Make sure the currently used store ('redis') is the only store in $tenantCacheStores
PrefixCacheTenancyBootstrapper::$tenantCacheStores = ['redis']; PrefixCacheTenancyBootstrapper::$tenantCacheStores = [$prefixedStore = 'redis'];
$centralValue = 'central-value';
$assertStoreIsNotPrefixed = function (string $unprefixedStore) use ($prefixedStore, $centralValue) {
// Switch to the unprefixed store
config(['cache.default' => $unprefixedStore]);
expect(cache('key'))->toBe($centralValue);
// Switch back to the prefixed store
config(['cache.default' => $prefixedStore]);
};
$this->app->singleton(CacheService::class); $this->app->singleton(CacheService::class);
$this->app->make(CacheService::class)->handle(); $this->app->make(CacheService::class)->handle();
expect(cache('key'))->toBe($centralValue = 'central-value'); expect(cache('key'))->toBe($centralValue);
$tenant1 = Tenant::create(); $tenant1 = Tenant::create();
$tenant2 = Tenant::create(); $tenant2 = Tenant::create();
@ -247,10 +255,7 @@ 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($tenant1->getTenantKey()); expect(cache('key'))->toBe($tenant1->getTenantKey());
// Switch to an unprefixed store $assertStoreIsNotPrefixed('redis2');
config(['cache.default' => 'redis2']);
expect(cache('key'))->toBe($centralValue);
config(['cache.default' => 'redis']);
tenancy()->initialize($tenant2); tenancy()->initialize($tenant2);
@ -258,11 +263,7 @@ 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($tenant2->getTenantKey()); expect(cache('key'))->toBe($tenant2->getTenantKey());
// Switch to an unprefixed store $assertStoreIsNotPrefixed('redis2');
config(['cache.default' => 'redis2']);
expect(cache('key'))->toBe($centralValue);
// Switch back to the prefixed store
config(['cache.default' => 'redis']);
tenancy()->end(); tenancy()->end();
expect(cache('key'))->toBe($centralValue); expect(cache('key'))->toBe($centralValue);
@ -279,11 +280,7 @@ 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($tenant1->getTenantKey()); expect(cache('key'))->toBe($tenant1->getTenantKey());
// Switch to an unprefixed store $assertStoreIsNotPrefixed('redis2');
config(['cache.default' => 'redis2']);
expect(cache('key'))->toBe($centralValue);
// Switch back to the prefixed store
config(['cache.default' => 'redis']);
tenancy()->initialize($tenant2); tenancy()->initialize($tenant2);
@ -291,11 +288,7 @@ 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($tenant2->getTenantKey()); expect(cache('key'))->toBe($tenant2->getTenantKey());
// Switch to an unprefixed store $assertStoreIsNotPrefixed('redis2');
config(['cache.default' => 'redis2']);
expect(cache('key'))->toBe($centralValue);
// Switch back to the prefixed store
config(['cache.default' => 'redis']);
tenancy()->end(); tenancy()->end();
expect(cache('key'))->toBe($centralValue); expect(cache('key'))->toBe($centralValue);