From 87efdd29542fb7548b9356e41b63385d47399bad Mon Sep 17 00:00:00 2001 From: lukinovec Date: Tue, 13 Dec 2022 05:25:57 +0100 Subject: [PATCH] Toggle cache tags --- src/CacheManager.php | 5 +++-- tests/BootstrapperTest.php | 5 ++++- tests/CacheManagerTest.php | 7 +++++-- tests/GlobalCacheTest.php | 3 +++ tests/PrefixCacheBootstrapperTest.php | 2 ++ 5 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/CacheManager.php b/src/CacheManager.php index 41a2bdd6..14965de9 100644 --- a/src/CacheManager.php +++ b/src/CacheManager.php @@ -11,6 +11,8 @@ use Illuminate\Cache\Repository; class CacheManager extends BaseCacheManager { + public static bool $addTags = false; + /** * Add tags and forward the call to the inner cache store. * @@ -19,8 +21,7 @@ class CacheManager extends BaseCacheManager */ public function __call($method, $parameters) { - // todo0 Make PrefixCacheTenancyBootstrapper work with tags - if (! tenancy()->initialized) { + if (! tenancy()->initialized || ! static::$addTags) { return parent::__call($method, $parameters); } diff --git a/tests/BootstrapperTest.php b/tests/BootstrapperTest.php index 8a1d0bff..7ad65c48 100644 --- a/tests/BootstrapperTest.php +++ b/tests/BootstrapperTest.php @@ -28,6 +28,7 @@ use Stancl\Tenancy\Bootstrappers\CacheTenancyBootstrapper; use Stancl\Tenancy\Bootstrappers\RedisTenancyBootstrapper; use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper; use Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper; +use Stancl\Tenancy\CacheManager; beforeEach(function () { $this->mockConsoleOutput = false; @@ -75,6 +76,8 @@ test('database data is separated', function () { }); test('cache data is separated', function (string $bootstrapper) { + CacheManager::$addTags = true; + config([ 'tenancy.bootstrappers' => [$bootstrapper], 'cache.default' => 'redis', @@ -114,7 +117,7 @@ test('cache data is separated', function (string $bootstrapper) { })->with([ 'CacheTenancyBootstrapper' => CacheTenancyBootstrapper::class, 'PrefixCacheTenancyBootstrapper' => PrefixCacheTenancyBootstrapper::class, -]); +])->group('bootstrapper'); test('redis data is separated', function () { config(['tenancy.bootstrappers' => [ diff --git a/tests/CacheManagerTest.php b/tests/CacheManagerTest.php index 03580fe1..49f4dfe9 100644 --- a/tests/CacheManagerTest.php +++ b/tests/CacheManagerTest.php @@ -2,17 +2,20 @@ declare(strict_types=1); +use Stancl\Tenancy\CacheManager; +use Stancl\Tenancy\Tests\Etc\Tenant; use Illuminate\Support\Facades\Event; -use Stancl\Tenancy\Bootstrappers\CacheTenancyBootstrapper; use Stancl\Tenancy\Events\TenancyInitialized; use Stancl\Tenancy\Listeners\BootstrapTenancy; -use Stancl\Tenancy\Tests\Etc\Tenant; +use Stancl\Tenancy\Bootstrappers\CacheTenancyBootstrapper; beforeEach(function () { config(['tenancy.bootstrappers' => [ CacheTenancyBootstrapper::class, ]]); + CacheManager::$addTags = true; + Event::listen(TenancyInitialized::class, BootstrapTenancy::class); }); diff --git a/tests/GlobalCacheTest.php b/tests/GlobalCacheTest.php index ea38341b..fad1764a 100644 --- a/tests/GlobalCacheTest.php +++ b/tests/GlobalCacheTest.php @@ -4,6 +4,7 @@ declare(strict_types=1); use Illuminate\Support\Facades\Event; use Stancl\Tenancy\Bootstrappers\CacheTenancyBootstrapper; +use Stancl\Tenancy\CacheManager; use Stancl\Tenancy\Events\TenancyEnded; use Stancl\Tenancy\Events\TenancyInitialized; use Stancl\Tenancy\Facades\GlobalCache; @@ -16,6 +17,8 @@ beforeEach(function () { CacheTenancyBootstrapper::class, ]]); + CacheManager::$addTags = true; + Event::listen(TenancyInitialized::class, BootstrapTenancy::class); Event::listen(TenancyEnded::class, RevertToCentralContext::class); }); diff --git a/tests/PrefixCacheBootstrapperTest.php b/tests/PrefixCacheBootstrapperTest.php index 41e0643c..94b0ea67 100644 --- a/tests/PrefixCacheBootstrapperTest.php +++ b/tests/PrefixCacheBootstrapperTest.php @@ -20,6 +20,8 @@ beforeEach(function () { 'cache.default' => 'redis', ]); + TenancyCacheManager::$addTags = false; + Event::listen(TenancyInitialized::class, BootstrapTenancy::class); Event::listen(TenancyEnded::class, RevertToCentralContext::class); });