From ca907e68bed47cf44809ed721ee7d112422997d8 Mon Sep 17 00:00:00 2001 From: lukinovec Date: Mon, 17 Apr 2023 18:00:03 +0200 Subject: [PATCH] Improve tests --- tests/PrefixCacheBootstrapperTest.php | 39 +++++++++++++++++++-------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/tests/PrefixCacheBootstrapperTest.php b/tests/PrefixCacheBootstrapperTest.php index 490b8626..818bdbe7 100644 --- a/tests/PrefixCacheBootstrapperTest.php +++ b/tests/PrefixCacheBootstrapperTest.php @@ -19,6 +19,7 @@ beforeEach(function () { PrefixCacheTenancyBootstrapper::class ], 'cache.default' => $cacheDriver = 'redis', + 'cache.stores.redis2' => config('cache.stores.redis'), ]); PrefixCacheTenancyBootstrapper::$tenantCacheStores = [$cacheDriver]; @@ -161,6 +162,7 @@ test('cache base prefix is customizable', function () { tenancy()->initialize($tenant1); expect($originalPrefix . $prefixBase . $tenant1->getTenantKey() . ':') + ->toBe(cache()->getPrefix()) ->toBe(app('cache')->getPrefix()) ->toBe(app('cache.store')->getPrefix()); }); @@ -195,7 +197,6 @@ test('cache is prefixed correctly when using a repository injected in a singleto test('specific central cache store can be used inside a service', function () { config(['cache.default' => 'redis']); - config(['cache.stores.redis2' => config('cache.stores.redis')]); $cacheStore = 'redis2'; // Name of the non-default, central cache store that we'll use using cache()->store($cacheStore) // Service uses the 'redis2' store which is central/not prefixed (not present in PrefixCacheTenancyBootstrapper::$tenantCacheStores) @@ -226,8 +227,6 @@ test('specific central cache store can be used inside a service', function () { }); test('only the stores specified in tenantCacheStores get prefixed', function() { - config(['cache.stores.redis2' => config('cache.stores.redis')]); - // Make the currently used store ('redis') the only store in $tenantCacheStores PrefixCacheTenancyBootstrapper::$tenantCacheStores = ['redis']; @@ -304,7 +303,6 @@ test('non default stores get prefixed too when specified in tenantCacheStores', return app(PrefixCacheTenancyBootstrapper::class)->generatePrefix($tenant); }; - config(['cache.stores.redis2' => config('cache.stores.redis')]); // Make 'redis2' the default cache driver config(['cache.default' => 'redis2']); PrefixCacheTenancyBootstrapper::$tenantCacheStores = ['redis', 'redis2']; @@ -329,25 +327,42 @@ test('non default stores get prefixed too when specified in tenantCacheStores', test('cache store prefix generation can be customized', function() { config(['cache.default' => 'redis']); - PrefixCacheTenancyBootstrapper::$tenantCacheStores = ['redis']; + PrefixCacheTenancyBootstrapper::$tenantCacheStores = ['redis', 'redis2']; - // Add custom prefix generator for the 'redis' store + // Use custom prefix generator PrefixCacheTenancyBootstrapper::generatePrefixUsing($customPrefixGenerator = function (Tenant $tenant) { return 'redis_tenant_cache_' . $tenant->getTenantKey(); }); expect(PrefixCacheTenancyBootstrapper::$prefixGenerator)->toBe($customPrefixGenerator); + expect(app(PrefixCacheTenancyBootstrapper::class)->generatePrefix($tenant = Tenant::create())) + ->toBe($customPrefixGenerator($tenant)); tenancy()->initialize($tenant = Tenant::create()); // Expect the 'redis' store to use the prefix generated by the custom generator - expect(cache()->store('redis')->getPrefix())->toBe($customPrefixGenerator($tenant) . ':'); + expect($customPrefixGenerator($tenant) . ':') + ->toBe(cache()->getPrefix()) + ->toBe(app('cache')->getPrefix()) + ->toBe(app('cache.store')->getPrefix()); + + config(['cache.default' => 'redis2']); + + PrefixCacheTenancyBootstrapper::generatePrefixUsing($customPrefixGenerator = function (Tenant $tenant) { + return 'redis2_tenant_cache_' . $tenant->getTenantKey(); + }); + + tenancy()->initialize($tenant = Tenant::create()); + + expect($customPrefixGenerator($tenant) . ':') + ->toBe(cache()->getPrefix()) + ->toBe(app('cache')->getPrefix()) + ->toBe(app('cache.store')->getPrefix()); tenancy()->end(); }); -test('stores get prefixed using the default way if the store does not have a corresponding generator', function() { - config(['cache.stores.redis2' => config('cache.stores.redis')]); +test('stores get prefixed using the default way if no prefix generator is specified', function() { // Make 'redis2' the default cache driver config(['cache.default' => 'redis2']); $originalPrefix = config('cache.prefix'); @@ -362,9 +377,11 @@ test('stores get prefixed using the default way if the store does not have a cor // Let the prefix get created using the default approach tenancy()->initialize($tenant); - expect(cache()->store()->getPrefix())->toBe($defaultPrefix . ':'); // Other stores without a prefix generator use the default generator too - expect(cache()->store('redis')->getPrefix())->toBe($defaultPrefix . ':'); + expect($defaultPrefix . ':') + ->toBe(cache()->getPrefix()) + ->toBe(app('cache')->getPrefix()) + ->toBe(app('cache.store')->getPrefix()); tenancy()->end(); });