1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 16:44:04 +00:00

Reset static $stores property

This commit is contained in:
Samuel Štancl 2025-08-06 20:44:05 +02:00
parent 6a8358fd80
commit a760c52ef2
3 changed files with 16 additions and 11 deletions

View file

@ -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']));
}
};
}

View file

@ -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');

View file

@ -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();