From 63b4efc02886ddf28c38a45286affcc2ca1f3d3c Mon Sep 17 00:00:00 2001 From: lukinovec Date: Mon, 13 Jan 2025 11:56:04 +0100 Subject: [PATCH 1/2] Add and test DatabaseCacheBootstrapper --- .../DatabaseCacheBootstrapper.php | 35 +++++++++ tests/DbCacheBootstrapperTest.php | 77 +++++++++++++++++++ .../0001_01_01_000001_create_cache_table.php | 35 +++++++++ 3 files changed, 147 insertions(+) create mode 100644 src/Bootstrappers/DatabaseCacheBootstrapper.php create mode 100644 tests/DbCacheBootstrapperTest.php create mode 100644 tests/Etc/cache_migrations/0001_01_01_000001_create_cache_table.php diff --git a/src/Bootstrappers/DatabaseCacheBootstrapper.php b/src/Bootstrappers/DatabaseCacheBootstrapper.php new file mode 100644 index 00000000..74267138 --- /dev/null +++ b/src/Bootstrappers/DatabaseCacheBootstrapper.php @@ -0,0 +1,35 @@ +originalConnection = $this->config->get('cache.stores.database.connection'); + + $this->config->set('cache.stores.database.connection', 'tenant'); + + $this->cache->purge('database'); + } + + public function revert(): void + { + $this->config->set('cache.stores.database.connection', $this->originalConnection); + + $this->cache->purge('database'); + } +} diff --git a/tests/DbCacheBootstrapperTest.php b/tests/DbCacheBootstrapperTest.php new file mode 100644 index 00000000..38f26184 --- /dev/null +++ b/tests/DbCacheBootstrapperTest.php @@ -0,0 +1,77 @@ +send(function (TenantCreated $event) { + return $event->tenant; + })->toListener()); + + Event::listen(TenancyInitialized::class, BootstrapTenancy::class); + Event::listen(TenancyEnded::class, RevertToCentralContext::class); + + config([ + 'cache.default' => 'database', + 'tenancy.bootstrappers' => [ + DatabaseTenancyBootstrapper::class, + DatabaseCacheBootstrapper::class, // Used instead of CacheTenancyBootstrapper + ], + ]); +}); + +test('DatabaseCacheBootstrapper uses tenant connection for saving cache', function() { + pest()->artisan('migrate', [ + '--path' => __DIR__ . '/Etc/cache_migrations', + '--realpath' => true, + ])->assertExitCode(0); + + $databaseCacheKey = config('cache.prefix') . 'foo'; + $databaseCacheValue = fn () => DB::selectOne("SELECT * FROM `cache` WHERE `key` = '{$databaseCacheKey}'")?->value; + + $tenant = Tenant::create(); + $tenant2 = Tenant::create(); + + pest()->artisan('tenants:migrate', [ + '--path' => __DIR__ . '/Etc/cache_migrations', + '--realpath' => true, + ])->assertExitCode(0); + + // Write to cache in central context + cache(['foo' => 'CENTRAL']); + + tenancy()->initialize($tenant); + + // Write to cache in tenant context + cache(['foo' => 'TENANT']); + + tenancy()->end(); + + // The 'foo' cache value in the central database should be 'CENTRAL' + expect(cache('foo'))->toBe('CENTRAL'); + expect(str($databaseCacheValue())->contains('CENTRAL'))->toBeTrue(); + + tenancy()->initialize($tenant); + + // The 'foo' cache value in the tenant database should be 'TENANT' + expect(cache('foo'))->toBe('TENANT'); + expect(str($databaseCacheValue())->contains('TENANT'))->toBeTrue(); + + tenancy()->initialize($tenant2); + + // The 'foo' cache value in another tenant's database should be null + expect(cache('foo'))->toBeNull(); + expect(DB::select('select * from cache'))->toHaveCount(0); +}); diff --git a/tests/Etc/cache_migrations/0001_01_01_000001_create_cache_table.php b/tests/Etc/cache_migrations/0001_01_01_000001_create_cache_table.php new file mode 100644 index 00000000..b9c106be --- /dev/null +++ b/tests/Etc/cache_migrations/0001_01_01_000001_create_cache_table.php @@ -0,0 +1,35 @@ +string('key')->primary(); + $table->mediumText('value'); + $table->integer('expiration'); + }); + + Schema::create('cache_locks', function (Blueprint $table) { + $table->string('key')->primary(); + $table->string('owner'); + $table->integer('expiration'); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('cache'); + Schema::dropIfExists('cache_locks'); + } +}; From f7aaae07daa4aba30af403832426c3a4fbb9c49f Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Mon, 13 Jan 2025 10:56:33 +0000 Subject: [PATCH 2/2] Fix code style (php-cs-fixer) --- src/Bootstrappers/DatabaseCacheBootstrapper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Bootstrappers/DatabaseCacheBootstrapper.php b/src/Bootstrappers/DatabaseCacheBootstrapper.php index 74267138..a1183049 100644 --- a/src/Bootstrappers/DatabaseCacheBootstrapper.php +++ b/src/Bootstrappers/DatabaseCacheBootstrapper.php @@ -4,10 +4,10 @@ namespace Stancl\Tenancy\Bootstrappers; +use Illuminate\Cache\CacheManager; use Illuminate\Config\Repository; use Stancl\Tenancy\Contracts\TenancyBootstrapper; use Stancl\Tenancy\Contracts\Tenant; -use Illuminate\Cache\CacheManager; class DatabaseCacheBootstrapper implements TenancyBootstrapper {