diff --git a/src/Bootstrappers/CacheTenancyBootstrapper.php b/src/Bootstrappers/CacheTenancyBootstrapper.php index 29547fae..6c963115 100644 --- a/src/Bootstrappers/CacheTenancyBootstrapper.php +++ b/src/Bootstrappers/CacheTenancyBootstrapper.php @@ -7,7 +7,6 @@ namespace Stancl\Tenancy\Bootstrappers; use Illuminate\Cache\CacheManager; use Illuminate\Contracts\Foundation\Application; use Illuminate\Support\Facades\Cache; -use Stancl\Tenancy\CacheManager as TenantCacheManager; use Stancl\Tenancy\Contracts\TenancyBootstrapper; use Stancl\Tenancy\Contracts\Tenant; @@ -25,19 +24,12 @@ class CacheTenancyBootstrapper implements TenancyBootstrapper $this->resetFacadeCache(); $this->originalCache = $this->originalCache ?? $this->app['cache']; - $this->app->extend('cache', function () { - return new TenantCacheManager($this->app); - }); } public function revert(): void { $this->resetFacadeCache(); - $this->app->extend('cache', function () { - return $this->originalCache; - }); - $this->originalCache = null; } diff --git a/src/TenancyServiceProvider.php b/src/TenancyServiceProvider.php index ded96f35..4884d07b 100644 --- a/src/TenancyServiceProvider.php +++ b/src/TenancyServiceProvider.php @@ -4,16 +4,17 @@ declare(strict_types=1); namespace Stancl\Tenancy; +use Stancl\Tenancy\Enums\LogMode; use Illuminate\Cache\CacheManager; -use Illuminate\Database\Console\Migrations\FreshCommand; -use Illuminate\Support\Facades\Event; -use Illuminate\Support\ServiceProvider; -use Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper; use Stancl\Tenancy\Contracts\Domain; use Stancl\Tenancy\Contracts\Tenant; -use Stancl\Tenancy\Enums\LogMode; +use Illuminate\Support\Facades\Event; +use Illuminate\Support\ServiceProvider; use Stancl\Tenancy\Events\Contracts\TenancyEvent; use Stancl\Tenancy\Resolvers\DomainTenantResolver; +use Stancl\Tenancy\CacheManager as TenantCacheManager; +use Illuminate\Database\Console\Migrations\FreshCommand; +use Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper; class TenancyServiceProvider extends ServiceProvider { @@ -97,6 +98,10 @@ class TenancyServiceProvider extends ServiceProvider return new Commands\MigrateFreshOverride; }); + $this->app->singleton('cache', function ($app) { + return new TenantCacheManager($app); + }); + $this->publishes([ __DIR__ . '/../assets/config.php' => config_path('tenancy.php'), ], 'config'); diff --git a/tests/PrefixCacheBootstrapperTest.php b/tests/PrefixCacheBootstrapperTest.php index 5db60aae..41e0643c 100644 --- a/tests/PrefixCacheBootstrapperTest.php +++ b/tests/PrefixCacheBootstrapperTest.php @@ -2,13 +2,15 @@ declare(strict_types=1); +use Illuminate\Cache\CacheManager; use Illuminate\Support\Facades\Event; -use Stancl\Tenancy\Bootstrappers\PrefixCacheTenancyBootstrapper; use Stancl\Tenancy\Events\TenancyEnded; +use Stancl\Tenancy\Tests\Etc\CacheService; use Stancl\Tenancy\Events\TenancyInitialized; use Stancl\Tenancy\Listeners\BootstrapTenancy; use Stancl\Tenancy\Listeners\RevertToCentralContext; -use Stancl\Tenancy\Tests\Etc\CacheService; +use Stancl\Tenancy\CacheManager as TenancyCacheManager; +use Stancl\Tenancy\Bootstrappers\PrefixCacheTenancyBootstrapper; beforeEach(function () { config([ @@ -22,6 +24,26 @@ beforeEach(function () { Event::listen(TenancyEnded::class, RevertToCentralContext::class); }); +test('Tenancy overrides CacheManager', function() { + expect(app('cache')::class)->toBe(TenancyCacheManager::class); + expect(app(CacheManager::class)::class)->toBe(TenancyCacheManager::class); + + tenancy()->initialize(Tenant::create(['id' => 'first'])); + + expect(app('cache')::class)->toBe(TenancyCacheManager::class); + expect(app(CacheManager::class)::class)->toBe(TenancyCacheManager::class); + + tenancy()->initialize(Tenant::create(['id' => 'second'])); + + expect(app('cache')::class)->toBe(TenancyCacheManager::class); + expect(app(CacheManager::class)::class)->toBe(TenancyCacheManager::class); + + tenancy()->end(); + + expect(app('cache')::class)->toBe(TenancyCacheManager::class); + expect(app(CacheManager::class)::class)->toBe(TenancyCacheManager::class); +}); + test('cache prefix is separate for each tenant', function () { $originalPrefix = config('cache.prefix'); $prefixBase = config('tenancy.cache.prefix_base'); @@ -45,21 +67,21 @@ test('cache prefix is separate for each tenant', function () { $tenantTwoPrefix = $originalPrefix . $prefixBase . $tenant2->getTenantKey(); tenancy()->initialize($tenant2); + cache()->set('key', 'tenanttwo-value'); expect($tenantTwoPrefix . ':') ->toBe(app('cache')->getPrefix()) ->toBe(app('cache.store')->getPrefix()); - // Assert tenants' data is accessible using the prefix from the central context - tenancy()->end(); + // Assert tenants' data is accessible using the prefix from the central context tenancy()->end(); config(['cache.prefix' => null]); // stop prefixing cache keys in central so we can provide prefix manually app('cache')->forgetDriver(config('cache.default')); expect(cache($tenantOnePrefix . ':key'))->toBe('tenantone-value'); expect(cache($tenantTwoPrefix . ':key'))->toBe('tenanttwo-value'); -}); +})->group('prefix'); test('cache is persisted when reidentification is used', function () { $tenant1 = Tenant::create();