mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 08:04:03 +00:00
* Initial implementation (lukinovec) * Make sure DatabaseCacheBootstrapper runs after DatabaseTenancyBootstrapper, misc wip changes * Fix withTenantDatabases() * Add failing test (GlobalCacheTest) * Configure globalCache's DB stores to use central connection instead of default connection every time it's reinstantiated * Make GlobalCache facade not cached. Even though it wasn't causing issues in our existing tests, it likely was flaky, and making it not $cached makes it now consistent with global_cache() - always getting a new CacheManager from the globalCache container binding * Add database connection assertions in GlobalCacheTest * Run all cached resolver/global cache tests with DatabaseCacheBootstrapper * Reset adjustCacheManagerUsing in revert() and TestCase * Reset static $stores property * Finalize GlobalCache-related changes * tests: remove pointless cache TTLs * Refactor DatabaseCacheBootstrapper * Refactor tests Co-authored-by: lukinovec <lukinovec@gmail.com>
This commit is contained in:
parent
3984d64cfa
commit
ecc3374293
14 changed files with 600 additions and 38 deletions
|
|
@ -11,6 +11,11 @@ use Stancl\Tenancy\Listeners\BootstrapTenancy;
|
|||
use Stancl\Tenancy\Listeners\RevertToCentralContext;
|
||||
use Stancl\Tenancy\Bootstrappers\CacheTagsBootstrapper;
|
||||
use Stancl\Tenancy\Bootstrappers\CacheTenancyBootstrapper;
|
||||
use Stancl\Tenancy\Bootstrappers\DatabaseCacheBootstrapper;
|
||||
use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper;
|
||||
|
||||
use function Stancl\Tenancy\Tests\withCacheTables;
|
||||
use function Stancl\Tenancy\Tests\withTenantDatabases;
|
||||
|
||||
beforeEach(function () {
|
||||
config([
|
||||
|
|
@ -20,26 +25,40 @@ beforeEach(function () {
|
|||
|
||||
Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
|
||||
Event::listen(TenancyEnded::class, RevertToCentralContext::class);
|
||||
|
||||
withCacheTables();
|
||||
});
|
||||
|
||||
test('global cache manager stores data in global cache', function (string $bootstrapper) {
|
||||
config(['tenancy.bootstrappers' => [$bootstrapper]]);
|
||||
test('global cache manager stores data in global cache', function (string $store, array $bootstrappers) {
|
||||
config([
|
||||
'cache.default' => $store,
|
||||
'tenancy.bootstrappers' => $bootstrappers,
|
||||
]);
|
||||
|
||||
if ($store === 'database') withTenantDatabases(true);
|
||||
|
||||
expect(cache('foo'))->toBe(null);
|
||||
GlobalCache::put(['foo' => 'bar'], 1);
|
||||
GlobalCache::put('foo', 'bar');
|
||||
expect(GlobalCache::get('foo'))->toBe('bar');
|
||||
|
||||
$tenant1 = Tenant::create();
|
||||
tenancy()->initialize($tenant1);
|
||||
expect(GlobalCache::get('foo'))->toBe('bar');
|
||||
|
||||
GlobalCache::put(['abc' => 'xyz'], 1);
|
||||
cache(['def' => 'ghi'], 10);
|
||||
GlobalCache::put('abc', 'xyz');
|
||||
cache(['def' => 'ghi']);
|
||||
expect(cache('def'))->toBe('ghi');
|
||||
|
||||
// different stores, same underlying connection. the prefix is set ON THE STORE
|
||||
expect(cache()->store()->getStore() === GlobalCache::store()->getStore())->toBeFalse();
|
||||
expect(cache()->store()->getStore()->connection() === GlobalCache::store()->getStore()->connection())->toBeTrue();
|
||||
// different stores
|
||||
expect(cache()->store()->getStore() !== GlobalCache::store()->getStore())->toBeTrue();
|
||||
if ($store === 'redis') {
|
||||
// same underlying connection. the prefix is set ON THE STORE
|
||||
expect(cache()->store()->getStore()->connection() === GlobalCache::store()->getStore()->connection())->toBeTrue();
|
||||
} else {
|
||||
// different connections
|
||||
expect(cache()->store()->getStore()->getConnection()->getName())->toBe('tenant');
|
||||
expect(GlobalCache::store()->getStore()->getConnection()->getName())->toBe('central');
|
||||
}
|
||||
|
||||
tenancy()->end();
|
||||
expect(GlobalCache::get('abc'))->toBe('xyz');
|
||||
|
|
@ -51,25 +70,129 @@ test('global cache manager stores data in global cache', function (string $boots
|
|||
expect(GlobalCache::get('abc'))->toBe('xyz');
|
||||
expect(GlobalCache::get('foo'))->toBe('bar');
|
||||
expect(cache('def'))->toBe(null);
|
||||
cache(['def' => 'xxx'], 1);
|
||||
cache(['def' => 'xxx']);
|
||||
expect(cache('def'))->toBe('xxx');
|
||||
|
||||
tenancy()->initialize($tenant1);
|
||||
expect(cache('def'))->toBe('ghi');
|
||||
})->with([
|
||||
CacheTagsBootstrapper::class,
|
||||
CacheTenancyBootstrapper::class,
|
||||
['redis', [CacheTagsBootstrapper::class]],
|
||||
['redis', [CacheTenancyBootstrapper::class]],
|
||||
['database', [DatabaseTenancyBootstrapper::class, DatabaseCacheBootstrapper::class]],
|
||||
]);
|
||||
|
||||
test('the global_cache helper supports the same syntax as the cache helper', function (string $bootstrapper) {
|
||||
config(['tenancy.bootstrappers' => [$bootstrapper]]);
|
||||
test('global cache facade is not persistent', function () {
|
||||
$oldId = spl_object_id(GlobalCache::getFacadeRoot());
|
||||
|
||||
$_ = new class {};
|
||||
|
||||
expect(spl_object_id(GlobalCache::getFacadeRoot()))->not()->toBe($oldId);
|
||||
});
|
||||
|
||||
test('global cache is always central', function (string $store, array $bootstrappers, string $initialCentralCall) {
|
||||
config([
|
||||
'cache.default' => $store,
|
||||
'tenancy.bootstrappers' => $bootstrappers,
|
||||
]);
|
||||
|
||||
if ($store === 'database') {
|
||||
withTenantDatabases(true);
|
||||
}
|
||||
|
||||
// This tells us which "accessor" for the global cache should be instantiated first, before we go
|
||||
// into the tenant context. We make sure to not touch the other one here. This tests that whether
|
||||
// a particular accessor is used "early" makes no difference in the later behavior.
|
||||
if ($initialCentralCall === 'helper') {
|
||||
if ($store === 'database') expect(global_cache()->store()->getStore()->getConnection()->getName())->toBe('central');
|
||||
global_cache()->put('central-helper', true);
|
||||
} else if ($initialCentralCall === 'facade') {
|
||||
if ($store === 'database') expect(GlobalCache::store()->getStore()->getConnection()->getName())->toBe('central');
|
||||
GlobalCache::put('central-facade', true);
|
||||
} else if ($initialCentralCall === 'both') {
|
||||
if ($store === 'database') expect(global_cache()->store()->getStore()->getConnection()->getName())->toBe('central');
|
||||
global_cache()->put('central-helper', true);
|
||||
if ($store === 'database') expect(GlobalCache::store()->getStore()->getConnection()->getName())->toBe('central');
|
||||
GlobalCache::put('central-facade', true);
|
||||
}
|
||||
|
||||
$tenant = Tenant::create();
|
||||
$tenant->enter();
|
||||
|
||||
// different stores, same underlying connection. the prefix is set ON THE STORE
|
||||
expect(cache()->store()->getStore() === global_cache()->store()->getStore())->toBeFalse();
|
||||
expect(cache()->store()->getStore()->connection() === global_cache()->store()->getStore()->connection())->toBeTrue();
|
||||
// Here we use both the helper and the facade to ensure the value is accessible via either one
|
||||
if ($initialCentralCall === 'helper') {
|
||||
if ($store === 'database') expect(global_cache()->store()->getStore()->getConnection()->getName())->toBe('central');
|
||||
if ($store === 'database') expect(GlobalCache::store()->getStore()->getConnection()->getName())->toBe('central');
|
||||
expect(global_cache('central-helper'))->toBe(true);
|
||||
expect(GlobalCache::get('central-helper'))->toBe(true);
|
||||
} else if ($initialCentralCall === 'facade') {
|
||||
if ($store === 'database') expect(global_cache()->store()->getStore()->getConnection()->getName())->toBe('central');
|
||||
if ($store === 'database') expect(GlobalCache::store()->getStore()->getConnection()->getName())->toBe('central');
|
||||
expect(global_cache('central-facade'))->toBe(true);
|
||||
expect(GlobalCache::get('central-facade'))->toBe(true);
|
||||
} else if ($initialCentralCall === 'both') {
|
||||
if ($store === 'database') expect(global_cache()->store()->getStore()->getConnection()->getName())->toBe('central');
|
||||
if ($store === 'database') expect(GlobalCache::store()->getStore()->getConnection()->getName())->toBe('central');
|
||||
expect(global_cache('central-helper'))->toBe(true);
|
||||
expect(GlobalCache::get('central-helper'))->toBe(true);
|
||||
expect(global_cache('central-facade'))->toBe(true);
|
||||
expect(GlobalCache::get('central-facade'))->toBe(true);
|
||||
}
|
||||
|
||||
global_cache()->put('tenant-helper', true);
|
||||
GlobalCache::put('tenant-facade', true);
|
||||
|
||||
tenancy()->end();
|
||||
|
||||
if ($store === 'database') expect(global_cache()->store()->getStore()->getConnection()->getName())->toBe('central');
|
||||
if ($store === 'database') expect(GlobalCache::store()->getStore()->getConnection()->getName())->toBe('central');
|
||||
|
||||
expect(global_cache('tenant-helper'))->toBe(true);
|
||||
expect(GlobalCache::get('tenant-helper'))->toBe(true);
|
||||
expect(global_cache('tenant-facade'))->toBe(true);
|
||||
expect(GlobalCache::get('tenant-facade'))->toBe(true);
|
||||
|
||||
if ($initialCentralCall === 'helper') {
|
||||
expect(GlobalCache::get('central-helper'))->toBe(true);
|
||||
} else if ($initialCentralCall === 'facade') {
|
||||
expect(global_cache('central-facade'))->toBe(true);
|
||||
} else if ($initialCentralCall === 'both') {
|
||||
expect(global_cache('central-helper'))->toBe(true);
|
||||
expect(GlobalCache::get('central-helper'))->toBe(true);
|
||||
expect(global_cache('central-facade'))->toBe(true);
|
||||
expect(GlobalCache::get('central-facade'))->toBe(true);
|
||||
}
|
||||
})->with([
|
||||
['redis', [CacheTagsBootstrapper::class]],
|
||||
['redis', [CacheTenancyBootstrapper::class]],
|
||||
['database', [DatabaseTenancyBootstrapper::class, DatabaseCacheBootstrapper::class]],
|
||||
])->with([
|
||||
'helper',
|
||||
'facade',
|
||||
'both',
|
||||
'none',
|
||||
]);
|
||||
|
||||
test('the global_cache helper supports the same syntax as the cache helper', function (string $store, array $bootstrappers) {
|
||||
config([
|
||||
'cache.default' => $store,
|
||||
'tenancy.bootstrappers' => $bootstrappers,
|
||||
]);
|
||||
|
||||
if ($store === 'database') withTenantDatabases(true);
|
||||
|
||||
$tenant = Tenant::create();
|
||||
$tenant->enter();
|
||||
|
||||
// different stores
|
||||
expect(cache()->store()->getStore() !== GlobalCache::store()->getStore())->toBeTrue();
|
||||
if ($store === 'redis') {
|
||||
// same underlying connection. the prefix is set ON THE STORE
|
||||
expect(cache()->store()->getStore()->connection() === global_cache()->store()->getStore()->connection())->toBeTrue();
|
||||
} else {
|
||||
// different connections
|
||||
expect(cache()->store()->getStore()->getConnection()->getName())->toBe('tenant');
|
||||
expect(global_cache()->store()->getStore()->getConnection()->getName())->toBe('central');
|
||||
}
|
||||
|
||||
expect(cache('foo'))->toBe(null); // tenant cache is empty
|
||||
|
||||
|
|
@ -81,6 +204,7 @@ test('the global_cache helper supports the same syntax as the cache helper', fun
|
|||
|
||||
expect(cache('foo'))->toBe(null); // tenant cache is not affected
|
||||
})->with([
|
||||
CacheTagsBootstrapper::class,
|
||||
CacheTenancyBootstrapper::class,
|
||||
['redis', [CacheTagsBootstrapper::class]],
|
||||
['redis', [CacheTenancyBootstrapper::class]],
|
||||
['database', [DatabaseTenancyBootstrapper::class, DatabaseCacheBootstrapper::class]],
|
||||
]);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue