[PrefixCacheTenancyBootstrapper::class], 'cache.default' => 'redis', ]); Event::listen(TenancyInitialized::class, BootstrapTenancy::class); Event::listen(TenancyEnded::class, RevertToCentralContext::class); }); test('cache prefix is separate for each tenant', function () { $originalPrefix = config('cache.prefix') . ':'; expect($originalPrefix) ->toBe(app('cache')->getPrefix()) ->toBe(app('cache.store')->getPrefix()); $tenant1 = Tenant::create(); $tenant2 = Tenant::create(); $tenantOnePrefix = 'tenant_' . $tenant1->id . ':'; tenancy()->initialize($tenant1); expect($tenantOnePrefix) ->toBe(app('cache')->getPrefix()) ->toBe(app('cache.store')->getPrefix()); $tenantTwoPrefix = 'tenant_' . $tenant2->id . ':'; tenancy()->initialize($tenant2); expect($tenantTwoPrefix) ->toBe(app('cache')->getPrefix()) ->toBe(app('cache.store')->getPrefix()); }); test('cache is persisted when reidentification is used', function () { $tenant1 = Tenant::create(); $tenant2 = Tenant::create(); tenancy()->initialize($tenant1); cache(['foo' => 'bar'], 10); expect(cache('foo'))->toBe('bar'); tenancy()->initialize($tenant2); tenancy()->end(); tenancy()->initialize($tenant1); expect(cache('foo'))->toBe('bar'); }); test('prefix separate cache well enough', function () { $tenant1 = Tenant::create(); tenancy()->initialize($tenant1); cache()->put('foo', 'bar', 1); expect(cache()->get('foo'))->toBe('bar'); $tenant2 = Tenant::create(); tenancy()->initialize($tenant2); pest()->assertNotSame('bar', cache()->get('foo')); cache()->put('foo', 'xyz', 1); expect(cache()->get('foo'))->toBe('xyz'); }); test('central cache is persisted', function () { cache()->put('key', 'central'); $tenant1 = Tenant::create(); tenancy()->initialize($tenant1); cache()->put('key', 'tenant'); expect(cache()->get('key'))->toBe('tenant'); tenancy()->end(); cache()->put('key2', 'central-two'); expect(cache()->get('key'))->toBe('central'); expect(cache()->get('key2'))->toBe('central-two'); }); test('cache base prefix is customizable', function () { config([ 'tenancy.cache.prefix_base' => 'custom_' ]); $tenant1 = Tenant::create(); tenancy()->initialize($tenant1); expect('custom_' . $tenant1->id . ':') ->toBe(app('cache')->getPrefix()) ->toBe(app('cache.store')->getPrefix()); });