diff --git a/src/Bootstrappers/DatabaseCacheBootstrapper.php b/src/Bootstrappers/DatabaseCacheBootstrapper.php index 27754f9e..6e331e4b 100644 --- a/src/Bootstrappers/DatabaseCacheBootstrapper.php +++ b/src/Bootstrappers/DatabaseCacheBootstrapper.php @@ -52,8 +52,6 @@ class DatabaseCacheBootstrapper implements TenancyBootstrapper $this->originalConnections[$storeName] = $this->config->get("cache.stores.{$storeName}.connection"); $this->originalLockConnections[$storeName] = $this->config->get("cache.stores.{$storeName}.lock_connection"); - // todo0 does this handle *already resolved* stores? - $this->config->set("cache.stores.{$storeName}.connection", 'tenant'); $this->config->set("cache.stores.{$storeName}.lock_connection", 'tenant'); @@ -64,17 +62,18 @@ class DatabaseCacheBootstrapper implements TenancyBootstrapper // pull it into the closure, and execute it there. But such a naive approach would lead to existing callbacks // *from here* being executed repeatedly in a loop on reinitialization. For that reason we do not do that // (this is our only use of $adjustCacheManagerUsing anyway) but ideally at some point we'd have a better solution. - TenancyServiceProvider::$adjustCacheManagerUsing = function (CacheManager $manager) use ($stores) { - foreach ($stores as $storeName) { + $originalConnections = array_combine($stores, array_map(fn (string $storeName) => [ + 'connection' => $this->originalConnections[$storeName] ?? config('tenancy.database.central_connection'), + 'lockConnection' => $this->originalLockConnections[$storeName] ?? config('tenancy.database.central_connection'), + ], $stores)); + + TenancyServiceProvider::$adjustCacheManagerUsing = static function (CacheManager $manager) use ($originalConnections) { + foreach ($originalConnections as $storeName => $connections) { // @phpstan-ignore-next-line method.notFound - $manager->store($storeName)->getStore()->setConnection( - DB::connection($this->originalConnections[$storeName] ?? config('tenancy.database.central_connection')) - ); + $manager->store($storeName)->getStore()->setConnection(DB::connection($connections['connection'])); // @phpstan-ignore-next-line method.notFound - $manager->store($storeName)->getStore()->setLockConnection( - DB::connection($this->originalLockConnections[$storeName] ?? config('tenancy.database.central_connection')) - ); + $manager->store($storeName)->getStore()->setLockConnection(DB::connection($connections['lockConnection'])); } }; } diff --git a/tests/DatabaseCacheBootstrapperTest.php b/tests/DatabaseCacheBootstrapperTest.php index 7463af93..d6895ce4 100644 --- a/tests/DatabaseCacheBootstrapperTest.php +++ b/tests/DatabaseCacheBootstrapperTest.php @@ -29,6 +29,8 @@ beforeEach(function () { Event::listen(TenancyInitialized::class, BootstrapTenancy::class); Event::listen(TenancyEnded::class, RevertToCentralContext::class); + DatabaseCacheBootstrapper::$stores = null; + config([ 'cache.stores.database.connection' => 'central', // Explicitly set cache DB connection name in config 'cache.stores.database.lock_connection' => 'central', // Also set lock connection name @@ -40,6 +42,10 @@ beforeEach(function () { ]); }); +afterEach(function () { + DatabaseCacheBootstrapper::$stores = null; +}); + test('DatabaseCacheBootstrapper switches the database cache store connections correctly', function () { // Original connections (store and lock) are 'central' in the config expect(config('cache.stores.database.connection'))->toBe('central'); diff --git a/tests/TestCase.php b/tests/TestCase.php index 6039134e..d4f2657b 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -39,8 +39,8 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase ini_set('memory_limit', '1G'); - TenancyServiceProvider::$configure = null; TenancyServiceProvider::$registerForgetTenantParameterListener = true; + TenancyServiceProvider::$migrateFreshOverride = true; TenancyServiceProvider::$adjustCacheManagerUsing = null; Redis::connection('default')->flushdb();