mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 17:44:04 +00:00
Add test
This commit is contained in:
parent
8a54e19644
commit
a59d5a1069
2 changed files with 68 additions and 0 deletions
27
tests/Etc/CacheManagerService.php
Normal file
27
tests/Etc/CacheManagerService.php
Normal 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');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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());
|
||||||
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue