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

Make Tenancy override CacheManager

This commit is contained in:
lukinovec 2022-12-12 17:05:53 +01:00
parent dc9c8fcf68
commit 9ec0b6dae3
3 changed files with 37 additions and 18 deletions

View file

@ -2,13 +2,15 @@
declare(strict_types=1);
use Illuminate\Cache\CacheManager;
use Illuminate\Support\Facades\Event;
use Stancl\Tenancy\Bootstrappers\PrefixCacheTenancyBootstrapper;
use Stancl\Tenancy\Events\TenancyEnded;
use Stancl\Tenancy\Tests\Etc\CacheService;
use Stancl\Tenancy\Events\TenancyInitialized;
use Stancl\Tenancy\Listeners\BootstrapTenancy;
use Stancl\Tenancy\Listeners\RevertToCentralContext;
use Stancl\Tenancy\Tests\Etc\CacheService;
use Stancl\Tenancy\CacheManager as TenancyCacheManager;
use Stancl\Tenancy\Bootstrappers\PrefixCacheTenancyBootstrapper;
beforeEach(function () {
config([
@ -22,6 +24,26 @@ beforeEach(function () {
Event::listen(TenancyEnded::class, RevertToCentralContext::class);
});
test('Tenancy overrides CacheManager', function() {
expect(app('cache')::class)->toBe(TenancyCacheManager::class);
expect(app(CacheManager::class)::class)->toBe(TenancyCacheManager::class);
tenancy()->initialize(Tenant::create(['id' => 'first']));
expect(app('cache')::class)->toBe(TenancyCacheManager::class);
expect(app(CacheManager::class)::class)->toBe(TenancyCacheManager::class);
tenancy()->initialize(Tenant::create(['id' => 'second']));
expect(app('cache')::class)->toBe(TenancyCacheManager::class);
expect(app(CacheManager::class)::class)->toBe(TenancyCacheManager::class);
tenancy()->end();
expect(app('cache')::class)->toBe(TenancyCacheManager::class);
expect(app(CacheManager::class)::class)->toBe(TenancyCacheManager::class);
});
test('cache prefix is separate for each tenant', function () {
$originalPrefix = config('cache.prefix');
$prefixBase = config('tenancy.cache.prefix_base');
@ -45,21 +67,21 @@ test('cache prefix is separate for each tenant', function () {
$tenantTwoPrefix = $originalPrefix . $prefixBase . $tenant2->getTenantKey();
tenancy()->initialize($tenant2);
cache()->set('key', 'tenanttwo-value');
expect($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();
config(['cache.prefix' => null]); // stop prefixing cache keys in central so we can provide prefix manually
app('cache')->forgetDriver(config('cache.default'));
expect(cache($tenantOnePrefix . ':key'))->toBe('tenantone-value');
expect(cache($tenantTwoPrefix . ':key'))->toBe('tenanttwo-value');
});
})->group('prefix');
test('cache is persisted when reidentification is used', function () {
$tenant1 = Tenant::create();