From a59d5a1069e9cc9c5b78126dd0a9719263aa0a18 Mon Sep 17 00:00:00 2001 From: lukinovec Date: Wed, 4 Jan 2023 15:08:09 +0100 Subject: [PATCH] Add test --- tests/Etc/CacheManagerService.php | 27 ++++++++++++++++++ tests/PrefixCacheBootstrapperTest.php | 41 +++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 tests/Etc/CacheManagerService.php diff --git a/tests/Etc/CacheManagerService.php b/tests/Etc/CacheManagerService.php new file mode 100644 index 00000000..c41e3560 --- /dev/null +++ b/tests/Etc/CacheManagerService.php @@ -0,0 +1,27 @@ +cache = $cacheManager->driver('redis2'); + } + + public function handle(): void + { + if (tenancy()->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 94b0ea67..420f6a88 100644 --- a/tests/PrefixCacheBootstrapperTest.php +++ b/tests/PrefixCacheBootstrapperTest.php @@ -2,12 +2,14 @@ declare(strict_types=1); +use Illuminate\Cache\RedisStore; use Illuminate\Cache\CacheManager; use Illuminate\Support\Facades\Event; use Stancl\Tenancy\Events\TenancyEnded; use Stancl\Tenancy\Tests\Etc\CacheService; use Stancl\Tenancy\Events\TenancyInitialized; use Stancl\Tenancy\Listeners\BootstrapTenancy; +use Stancl\Tenancy\Tests\Etc\CacheManagerService; use Stancl\Tenancy\Listeners\RevertToCentralContext; use Stancl\Tenancy\CacheManager as TenancyCacheManager; use Stancl\Tenancy\Bootstrappers\PrefixCacheTenancyBootstrapper; @@ -174,3 +176,42 @@ test('prefix separate cache well enough using CacheManager dependency injection' expect(cache('key'))->toBe('central-value'); }); + +test('stores other than the default one are not prefixed', function () { + config(['cache.default' => 'redis']); + config(['cache.stores.redis2' => config('cache.stores.redis')]); + + app(CacheManager::class)->extend('redis2', function($config) { + $redis = $this->app['redis']; + + $connection = $config['connection'] ?? 'default'; + + $store = new RedisStore($redis, $this->getPrefix($config), $connection); + + return $this->repository( + $store->setLockConnection($config['lock_connection'] ?? $connection) + ); + }); + + $this->app->singleton(CacheManagerService::class); + + app()->make(CacheManagerService::class)->handle(); + expect(cache()->driver('redis2')->get('key'))->toBe('central-value'); + + $tenant1 = Tenant::create(); + $tenant2 = Tenant::create(); + tenancy()->initialize($tenant1); + + expect(cache()->driver('redis2')->get('key'))->toBe('central-value'); + app()->make(CacheManagerService::class)->handle(); + expect(cache()->driver('redis2')->get('key'))->toBe($tenant1->getTenantKey()); + + tenancy()->initialize($tenant2); + + expect(cache()->driver('redis2')->get('key'))->toBe($tenant1->getTenantKey()); + app()->make(CacheManagerService::class)->handle(); + expect(cache()->driver('redis2')->get('key'))->toBe($tenant2->getTenantKey()); + + tenancy()->end(); + expect(cache()->driver('redis2')->get('key'))->toBe($tenant2->getTenantKey()); +});