From 222686ec1d6772048ded1b6de8e73fac96347bbf Mon Sep 17 00:00:00 2001 From: Abrar Ahmad Date: Thu, 1 Dec 2022 13:53:45 +0500 Subject: [PATCH] CacheManager dependency injection test --- .../PrefixCacheTenancyBootstrapper.php | 2 +- tests/Etc/CacheAction.php | 24 +++++++++++++++++++ tests/PrefixCacheBootstrapperTest.php | 17 +++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 tests/Etc/CacheAction.php diff --git a/src/Bootstrappers/PrefixCacheTenancyBootstrapper.php b/src/Bootstrappers/PrefixCacheTenancyBootstrapper.php index de4346a1..cb14fc05 100644 --- a/src/Bootstrappers/PrefixCacheTenancyBootstrapper.php +++ b/src/Bootstrappers/PrefixCacheTenancyBootstrapper.php @@ -40,7 +40,7 @@ class PrefixCacheTenancyBootstrapper implements TenancyBootstrapper { $this->config->set('cache.prefix', $prefix); - $this->app['cache']->forgetDriver($this->storeName); + // $this->app['cache']->forgetDriver($this->storeName); // The CacheManager will have the $app['config'] array cached with old prefixes on the 'cache' instance // This call will forget the 'cache' instance diff --git a/tests/Etc/CacheAction.php b/tests/Etc/CacheAction.php new file mode 100644 index 00000000..01da11e8 --- /dev/null +++ b/tests/Etc/CacheAction.php @@ -0,0 +1,24 @@ +initialized) { + $this->cache->put('key', tenant()->getTenantKey()); + } else { + $this->cache->put('key', 'central-value'); + } + } +} diff --git a/tests/PrefixCacheBootstrapperTest.php b/tests/PrefixCacheBootstrapperTest.php index 09d7f6fb..13eeeaf9 100644 --- a/tests/PrefixCacheBootstrapperTest.php +++ b/tests/PrefixCacheBootstrapperTest.php @@ -8,6 +8,7 @@ use Stancl\Tenancy\Events\TenancyEnded; use Stancl\Tenancy\Events\TenancyInitialized; use Stancl\Tenancy\Listeners\BootstrapTenancy; use Stancl\Tenancy\Listeners\RevertToCentralContext; +use Stancl\Tenancy\Tests\Etc\CacheAction; beforeEach(function () { config([ @@ -120,3 +121,19 @@ test('cache base prefix is customizable', function () { ->toBe(app('cache.store')->getPrefix()); }); +test('prefix separate cache well enough using CacheManager dependency injection', function () { + app()->make(CacheAction::class)->handle(); + + expect(cache('key'))->toBe('central-value'); + + $tenant = Tenant::create(); + tenancy()->initialize($tenant); + + expect(cache('key'))->toBeNull(); + app()->make(CacheAction::class)->handle(); + expect(cache('key'))->toBe($tenant->getTenantKey()); + + tenancy()->end(); + + expect(cache('key'))->toBe('central-value'); +});