From 2c0a8f4ef950c941be14327f8dac3cb10a11cee7 Mon Sep 17 00:00:00 2001 From: Abrar Ahmad Date: Wed, 23 Nov 2022 12:59:51 +0500 Subject: [PATCH] Update PrefixCacheBootstrapperTest.php --- tests/PrefixCacheBootstrapperTest.php | 31 +++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/tests/PrefixCacheBootstrapperTest.php b/tests/PrefixCacheBootstrapperTest.php index 388cb1e0..bdd54f44 100644 --- a/tests/PrefixCacheBootstrapperTest.php +++ b/tests/PrefixCacheBootstrapperTest.php @@ -4,8 +4,10 @@ declare(strict_types=1); use Illuminate\Support\Facades\Event; use Stancl\Tenancy\Bootstrappers\PrefixCacheTenancyBootstrapper; +use Stancl\Tenancy\Events\TenancyEnded; use Stancl\Tenancy\Events\TenancyInitialized; use Stancl\Tenancy\Listeners\BootstrapTenancy; +use Stancl\Tenancy\Listeners\RevertToCentralContext; beforeEach(function () { config([ @@ -14,6 +16,7 @@ beforeEach(function () { ]); Event::listen(TenancyInitialized::class, BootstrapTenancy::class); + Event::listen(TenancyEnded::class, RevertToCentralContext::class); }); test('cache prefix is separate for each tenant', function () { @@ -72,3 +75,31 @@ test('prefix separate cache well enough', function () { expect(cache()->get('foo'))->toBe('xyz'); }); +test('central cache is not broke', function () { + cache()->put('key', 'central'); + + $tenant1 = Tenant::create(); + tenancy()->initialize($tenant1); + + cache()->put('key', 'tenant'); + + expect(cache()->get('key'))->toBe('tenant'); + + tenancy()->end(); + + expect(cache()->get('key'))->toBe('central'); +}); + +test('cache base prefix can be customized', 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()); +}); +