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

Improve tests

This commit is contained in:
lukinovec 2023-01-06 08:34:56 +01:00
parent caa0c87153
commit 8deeef25be

View file

@ -49,13 +49,16 @@ test('Tenancy overrides CacheManager', function() {
expect(app(CacheManager::class)::class)->toBe(TenancyCacheManager::class); expect(app(CacheManager::class)::class)->toBe(TenancyCacheManager::class);
}); });
test('cache prefix is different for each tenant', function () { test('correct cache prefix is used in all contexts', function () {
$originalPrefix = config('cache.prefix'); $originalPrefix = config('cache.prefix');
$prefixBase = config('tenancy.cache.prefix_base'); $prefixBase = config('tenancy.cache.prefix_base');
$expectPrefixToBe = function(string $prefix) {
expect($originalPrefix . ':') // RedisStore suffixes prefix with ':' expect($prefix . ':') // RedisStore suffixes prefix with ':'
->toBe(app('cache')->getPrefix()) ->toBe(app('cache')->getPrefix())
->toBe(app('cache.store')->getPrefix()); ->toBe(app('cache.store')->getPrefix());
};
$expectPrefixToBe($originalPrefix);
$tenant1 = Tenant::create(); $tenant1 = Tenant::create();
$tenant2 = Tenant::create(); $tenant2 = Tenant::create();
@ -65,9 +68,7 @@ test('cache prefix is different for each tenant', function () {
tenancy()->initialize($tenant1); tenancy()->initialize($tenant1);
cache()->set('key', 'tenantone-value'); cache()->set('key', 'tenantone-value');
expect($tenantOnePrefix . ':') $expectPrefixToBe($tenantOnePrefix);
->toBe(app('cache')->getPrefix())
->toBe(app('cache.store')->getPrefix());
$tenantTwoPrefix = $originalPrefix . $prefixBase . $tenant2->getTenantKey(); $tenantTwoPrefix = $originalPrefix . $prefixBase . $tenant2->getTenantKey();
@ -75,9 +76,7 @@ test('cache prefix is different for each tenant', function () {
cache()->set('key', 'tenanttwo-value'); cache()->set('key', 'tenanttwo-value');
expect($tenantTwoPrefix . ':') $expectPrefixToBe($tenantTwoPrefix);
->toBe(app('cache')->getPrefix())
->toBe(app('cache.store')->getPrefix());
// Assert tenants' data is accessible using the prefix from the central context tenancy()->end(); // Assert tenants' data is accessible using the prefix from the central context tenancy()->end();
@ -103,7 +102,7 @@ test('cache is persisted when reidentification is used', function () {
expect(cache('foo'))->toBe('bar'); expect(cache('foo'))->toBe('bar');
}); });
test('prefix separate cache well enough', function () { test('prefixing separates the cache', function () {
$tenant1 = Tenant::create(); $tenant1 = Tenant::create();
tenancy()->initialize($tenant1); tenancy()->initialize($tenant1);
@ -238,7 +237,7 @@ test('stores specified in tenantCacheStores get prefixed', function() {
expect(cache('key'))->toBe($centralValue); expect(cache('key'))->toBe($centralValue);
}); });
test('stores that are not specified in tenantCacheStores do not get prefixed', function() { test('stores not specified in tenantCacheStores do not get prefixed', function() {
config(['cache.stores.redis2' => config('cache.stores.redis')]); config(['cache.stores.redis2' => config('cache.stores.redis')]);
config(['cache.default' => 'redis2']); config(['cache.default' => 'redis2']);
// Make 'redis' the only store in $tenantCacheStores so that the current store ('redis2') doesn't get prefixed // Make 'redis' the only store in $tenantCacheStores so that the current store ('redis2') doesn't get prefixed