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

Improve tests

This commit is contained in:
lukinovec 2023-04-17 18:00:03 +02:00
parent 2309b83327
commit ca907e68be

View file

@ -19,6 +19,7 @@ beforeEach(function () {
PrefixCacheTenancyBootstrapper::class PrefixCacheTenancyBootstrapper::class
], ],
'cache.default' => $cacheDriver = 'redis', 'cache.default' => $cacheDriver = 'redis',
'cache.stores.redis2' => config('cache.stores.redis'),
]); ]);
PrefixCacheTenancyBootstrapper::$tenantCacheStores = [$cacheDriver]; PrefixCacheTenancyBootstrapper::$tenantCacheStores = [$cacheDriver];
@ -161,6 +162,7 @@ test('cache base prefix is customizable', function () {
tenancy()->initialize($tenant1); tenancy()->initialize($tenant1);
expect($originalPrefix . $prefixBase . $tenant1->getTenantKey() . ':') expect($originalPrefix . $prefixBase . $tenant1->getTenantKey() . ':')
->toBe(cache()->getPrefix())
->toBe(app('cache')->getPrefix()) ->toBe(app('cache')->getPrefix())
->toBe(app('cache.store')->getPrefix()); ->toBe(app('cache.store')->getPrefix());
}); });
@ -195,7 +197,6 @@ test('cache is prefixed correctly when using a repository injected in a singleto
test('specific central cache store can be used inside a service', function () { test('specific central cache store can be used inside a service', function () {
config(['cache.default' => 'redis']); config(['cache.default' => 'redis']);
config(['cache.stores.redis2' => config('cache.stores.redis')]);
$cacheStore = 'redis2'; // Name of the non-default, central cache store that we'll use using cache()->store($cacheStore) $cacheStore = 'redis2'; // Name of the non-default, central cache store that we'll use using cache()->store($cacheStore)
// Service uses the 'redis2' store which is central/not prefixed (not present in PrefixCacheTenancyBootstrapper::$tenantCacheStores) // Service uses the 'redis2' store which is central/not prefixed (not present in PrefixCacheTenancyBootstrapper::$tenantCacheStores)
@ -226,8 +227,6 @@ 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() {
config(['cache.stores.redis2' => config('cache.stores.redis')]);
// Make the currently used store ('redis') the only store in $tenantCacheStores // Make the currently used store ('redis') the only store in $tenantCacheStores
PrefixCacheTenancyBootstrapper::$tenantCacheStores = ['redis']; PrefixCacheTenancyBootstrapper::$tenantCacheStores = ['redis'];
@ -304,7 +303,6 @@ test('non default stores get prefixed too when specified in tenantCacheStores',
return app(PrefixCacheTenancyBootstrapper::class)->generatePrefix($tenant); return app(PrefixCacheTenancyBootstrapper::class)->generatePrefix($tenant);
}; };
config(['cache.stores.redis2' => config('cache.stores.redis')]);
// Make 'redis2' the default cache driver // Make 'redis2' the default cache driver
config(['cache.default' => 'redis2']); config(['cache.default' => 'redis2']);
PrefixCacheTenancyBootstrapper::$tenantCacheStores = ['redis', 'redis2']; PrefixCacheTenancyBootstrapper::$tenantCacheStores = ['redis', 'redis2'];
@ -329,25 +327,42 @@ test('non default stores get prefixed too when specified in tenantCacheStores',
test('cache store prefix generation can be customized', function() { test('cache store prefix generation can be customized', function() {
config(['cache.default' => 'redis']); config(['cache.default' => 'redis']);
PrefixCacheTenancyBootstrapper::$tenantCacheStores = ['redis']; PrefixCacheTenancyBootstrapper::$tenantCacheStores = ['redis', 'redis2'];
// Add custom prefix generator for the 'redis' store // Use custom prefix generator
PrefixCacheTenancyBootstrapper::generatePrefixUsing($customPrefixGenerator = function (Tenant $tenant) { PrefixCacheTenancyBootstrapper::generatePrefixUsing($customPrefixGenerator = function (Tenant $tenant) {
return 'redis_tenant_cache_' . $tenant->getTenantKey(); return 'redis_tenant_cache_' . $tenant->getTenantKey();
}); });
expect(PrefixCacheTenancyBootstrapper::$prefixGenerator)->toBe($customPrefixGenerator); expect(PrefixCacheTenancyBootstrapper::$prefixGenerator)->toBe($customPrefixGenerator);
expect(app(PrefixCacheTenancyBootstrapper::class)->generatePrefix($tenant = Tenant::create()))
->toBe($customPrefixGenerator($tenant));
tenancy()->initialize($tenant = Tenant::create()); tenancy()->initialize($tenant = Tenant::create());
// Expect the 'redis' store to use the prefix generated by the custom generator // Expect the 'redis' store to use the prefix generated by the custom generator
expect(cache()->store('redis')->getPrefix())->toBe($customPrefixGenerator($tenant) . ':'); expect($customPrefixGenerator($tenant) . ':')
->toBe(cache()->getPrefix())
->toBe(app('cache')->getPrefix())
->toBe(app('cache.store')->getPrefix());
config(['cache.default' => 'redis2']);
PrefixCacheTenancyBootstrapper::generatePrefixUsing($customPrefixGenerator = function (Tenant $tenant) {
return 'redis2_tenant_cache_' . $tenant->getTenantKey();
});
tenancy()->initialize($tenant = Tenant::create());
expect($customPrefixGenerator($tenant) . ':')
->toBe(cache()->getPrefix())
->toBe(app('cache')->getPrefix())
->toBe(app('cache.store')->getPrefix());
tenancy()->end(); tenancy()->end();
}); });
test('stores get prefixed using the default way if the store does not have a corresponding generator', function() { test('stores get prefixed using the default way if no prefix generator is specified', function() {
config(['cache.stores.redis2' => config('cache.stores.redis')]);
// Make 'redis2' the default cache driver // Make 'redis2' the default cache driver
config(['cache.default' => 'redis2']); config(['cache.default' => 'redis2']);
$originalPrefix = config('cache.prefix'); $originalPrefix = config('cache.prefix');
@ -362,9 +377,11 @@ test('stores get prefixed using the default way if the store does not have a cor
// Let the prefix get created using the default approach // Let the prefix get created using the default approach
tenancy()->initialize($tenant); tenancy()->initialize($tenant);
expect(cache()->store()->getPrefix())->toBe($defaultPrefix . ':');
// Other stores without a prefix generator use the default generator too // Other stores without a prefix generator use the default generator too
expect(cache()->store('redis')->getPrefix())->toBe($defaultPrefix . ':'); expect($defaultPrefix . ':')
->toBe(cache()->getPrefix())
->toBe(app('cache')->getPrefix())
->toBe(app('cache.store')->getPrefix());
tenancy()->end(); tenancy()->end();
}); });