mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-13 04:24:03 +00:00
Add high-level assertions to DatabaseCacheBootstrapperTest, make the test follow the structure of the "cache data is separated" test
This commit is contained in:
parent
ba0c710e4e
commit
1be203b886
1 changed files with 31 additions and 13 deletions
|
|
@ -16,6 +16,7 @@ use Stancl\Tenancy\Events\TenancyEnded;
|
||||||
use Stancl\Tenancy\Listeners\RevertToCentralContext;
|
use Stancl\Tenancy\Listeners\RevertToCentralContext;
|
||||||
use Illuminate\Support\Facades\Schema;
|
use Illuminate\Support\Facades\Schema;
|
||||||
use Illuminate\Database\Schema\Blueprint;
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Cache;
|
||||||
|
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
Event::listen(
|
Event::listen(
|
||||||
|
|
@ -41,7 +42,7 @@ test('DatabaseCacheBootstrapper makes cache use the tenant connection', function
|
||||||
// DB query for verifying that the cache is stored in DB
|
// DB query for verifying that the cache is stored in DB
|
||||||
// under the 'foo' key (prefixed by the default cache prefix).
|
// under the 'foo' key (prefixed by the default cache prefix).
|
||||||
$databaseCacheKey = config('cache.prefix') . 'foo';
|
$databaseCacheKey = config('cache.prefix') . 'foo';
|
||||||
$databaseCacheValue = fn () => DB::selectOne("SELECT * FROM `cache` WHERE `key` = '{$databaseCacheKey}'")?->value;
|
$retrieveFooCacheUsingDbQuery = fn () => DB::selectOne("SELECT * FROM `cache` WHERE `key` = '{$databaseCacheKey}'")?->value;
|
||||||
|
|
||||||
$createCacheTables = function () {
|
$createCacheTables = function () {
|
||||||
Schema::create('cache', function (Blueprint $table) {
|
Schema::create('cache', function (Blueprint $table) {
|
||||||
|
|
@ -68,25 +69,42 @@ test('DatabaseCacheBootstrapper makes cache use the tenant connection', function
|
||||||
tenancy()->runForMultiple([$tenant, $tenant2], $createCacheTables);
|
tenancy()->runForMultiple([$tenant, $tenant2], $createCacheTables);
|
||||||
|
|
||||||
// Write to cache in central context
|
// Write to cache in central context
|
||||||
cache(['foo' => 'CENTRAL']);
|
cache()->set('foo', 'central');
|
||||||
|
expect(Cache::get('foo'))->toBe('central');
|
||||||
|
expect(str($retrieveFooCacheUsingDbQuery())->contains('central'))->toBeTrue();
|
||||||
|
|
||||||
tenancy()->initialize($tenant);
|
tenancy()->initialize($tenant);
|
||||||
|
|
||||||
// Write to cache in tenant context
|
// Central cache doesn't leak to tenant context
|
||||||
cache(['foo' => 'TENANT']);
|
expect(Cache::has('foo'))->toBeFalse();
|
||||||
|
// The 'foo' cache key in the tenant's database shouldn't exist
|
||||||
|
expect(DB::select('select * from cache'))->toHaveCount(0);
|
||||||
|
|
||||||
tenancy()->end();
|
cache()->set('foo', 'bar');
|
||||||
|
expect(Cache::get('foo'))->toBe('bar');
|
||||||
// The 'foo' cache value in the central database should be 'CENTRAL'
|
expect(str($retrieveFooCacheUsingDbQuery())->contains('bar'))->toBeTrue();
|
||||||
expect(str($databaseCacheValue())->contains('CENTRAL'))->toBeTrue();
|
|
||||||
|
|
||||||
tenancy()->initialize($tenant);
|
|
||||||
|
|
||||||
// The 'foo' cache value in the tenant database should be 'TENANT'
|
|
||||||
expect(str($databaseCacheValue())->contains('TENANT'))->toBeTrue();
|
|
||||||
|
|
||||||
tenancy()->initialize($tenant2);
|
tenancy()->initialize($tenant2);
|
||||||
|
|
||||||
|
// Assert one tenant's data doesn't leak to another tenant
|
||||||
|
expect(Cache::has('foo'))->toBeFalse();
|
||||||
// The 'foo' cache key in another tenant's database shouldn't exist
|
// The 'foo' cache key in another tenant's database shouldn't exist
|
||||||
expect(DB::select('select * from cache'))->toHaveCount(0);
|
expect(DB::select('select * from cache'))->toHaveCount(0);
|
||||||
|
|
||||||
|
cache()->set('foo', 'xyz');
|
||||||
|
expect(Cache::get('foo'))->toBe('xyz');
|
||||||
|
expect(str($retrieveFooCacheUsingDbQuery())->contains('xyz'))->toBeTrue();
|
||||||
|
|
||||||
|
tenancy()->initialize($tenant);
|
||||||
|
|
||||||
|
// Assert data didn't leak to original tenant
|
||||||
|
expect(Cache::get('foo'))->toBe('bar');
|
||||||
|
expect(str($retrieveFooCacheUsingDbQuery())->contains('bar'))->toBeTrue();
|
||||||
|
|
||||||
|
tenancy()->end();
|
||||||
|
|
||||||
|
// Assert central is still the same
|
||||||
|
// The 'foo' cache value in the central database should be 'central'
|
||||||
|
expect(Cache::get('foo'))->toBe('central');
|
||||||
|
expect(str($retrieveFooCacheUsingDbQuery())->contains('central'))->toBeTrue();
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue