1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 15:54:03 +00:00
This commit is contained in:
lukinovec 2023-01-04 15:08:09 +01:00
parent 8a54e19644
commit a59d5a1069
2 changed files with 68 additions and 0 deletions

View file

@ -0,0 +1,27 @@
<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Tests\Etc;
use Illuminate\Cache\CacheManager;
use Illuminate\Cache\Repository;
class CacheManagerService
{
public Repository|null $cache = null;
public function __construct(CacheManager $cacheManager)
{
$this->cache = $cacheManager->driver('redis2');
}
public function handle(): void
{
if (tenancy()->initialized) {
$this->cache->put('key', tenant()->getTenantKey());
} else {
$this->cache->put('key', 'central-value');
}
}
}

View file

@ -2,12 +2,14 @@
declare(strict_types=1); declare(strict_types=1);
use Illuminate\Cache\RedisStore;
use Illuminate\Cache\CacheManager; use Illuminate\Cache\CacheManager;
use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Event;
use Stancl\Tenancy\Events\TenancyEnded; use Stancl\Tenancy\Events\TenancyEnded;
use Stancl\Tenancy\Tests\Etc\CacheService; use Stancl\Tenancy\Tests\Etc\CacheService;
use Stancl\Tenancy\Events\TenancyInitialized; use Stancl\Tenancy\Events\TenancyInitialized;
use Stancl\Tenancy\Listeners\BootstrapTenancy; use Stancl\Tenancy\Listeners\BootstrapTenancy;
use Stancl\Tenancy\Tests\Etc\CacheManagerService;
use Stancl\Tenancy\Listeners\RevertToCentralContext; use Stancl\Tenancy\Listeners\RevertToCentralContext;
use Stancl\Tenancy\CacheManager as TenancyCacheManager; use Stancl\Tenancy\CacheManager as TenancyCacheManager;
use Stancl\Tenancy\Bootstrappers\PrefixCacheTenancyBootstrapper; use Stancl\Tenancy\Bootstrappers\PrefixCacheTenancyBootstrapper;
@ -174,3 +176,42 @@ test('prefix separate cache well enough using CacheManager dependency injection'
expect(cache('key'))->toBe('central-value'); expect(cache('key'))->toBe('central-value');
}); });
test('stores other than the default one are not prefixed', function () {
config(['cache.default' => 'redis']);
config(['cache.stores.redis2' => config('cache.stores.redis')]);
app(CacheManager::class)->extend('redis2', function($config) {
$redis = $this->app['redis'];
$connection = $config['connection'] ?? 'default';
$store = new RedisStore($redis, $this->getPrefix($config), $connection);
return $this->repository(
$store->setLockConnection($config['lock_connection'] ?? $connection)
);
});
$this->app->singleton(CacheManagerService::class);
app()->make(CacheManagerService::class)->handle();
expect(cache()->driver('redis2')->get('key'))->toBe('central-value');
$tenant1 = Tenant::create();
$tenant2 = Tenant::create();
tenancy()->initialize($tenant1);
expect(cache()->driver('redis2')->get('key'))->toBe('central-value');
app()->make(CacheManagerService::class)->handle();
expect(cache()->driver('redis2')->get('key'))->toBe($tenant1->getTenantKey());
tenancy()->initialize($tenant2);
expect(cache()->driver('redis2')->get('key'))->toBe($tenant1->getTenantKey());
app()->make(CacheManagerService::class)->handle();
expect(cache()->driver('redis2')->get('key'))->toBe($tenant2->getTenantKey());
tenancy()->end();
expect(cache()->driver('redis2')->get('key'))->toBe($tenant2->getTenantKey());
});