mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 18:04:03 +00:00
Simplify low-level DatabaseCacheBootstrapper test, delete migration file
This commit is contained in:
parent
b00a037ea8
commit
d482bb9e2d
2 changed files with 33 additions and 53 deletions
|
|
@ -14,15 +14,22 @@ use Stancl\Tenancy\Events\TenancyInitialized;
|
||||||
use Stancl\Tenancy\Listeners\BootstrapTenancy;
|
use Stancl\Tenancy\Listeners\BootstrapTenancy;
|
||||||
use Stancl\Tenancy\Events\TenancyEnded;
|
use Stancl\Tenancy\Events\TenancyEnded;
|
||||||
use Stancl\Tenancy\Listeners\RevertToCentralContext;
|
use Stancl\Tenancy\Listeners\RevertToCentralContext;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
Event::listen(TenantCreated::class, JobPipeline::make([CreateDatabase::class])->send(function (TenantCreated $event) {
|
Event::listen(
|
||||||
return $event->tenant;
|
TenantCreated::class,
|
||||||
})->toListener());
|
JobPipeline::make([CreateDatabase::class])->send(function (TenantCreated $event) {
|
||||||
|
return $event->tenant;
|
||||||
|
})->toListener()
|
||||||
|
);
|
||||||
|
|
||||||
Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
|
Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
|
||||||
Event::listen(TenancyEnded::class, RevertToCentralContext::class);
|
Event::listen(TenancyEnded::class, RevertToCentralContext::class);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('DatabaseCacheBootstrapper makes cache use the tenant connection', function() {
|
||||||
config([
|
config([
|
||||||
'cache.default' => 'database',
|
'cache.default' => 'database',
|
||||||
'tenancy.bootstrappers' => [
|
'tenancy.bootstrappers' => [
|
||||||
|
|
@ -30,24 +37,35 @@ beforeEach(function () {
|
||||||
DatabaseCacheBootstrapper::class, // Used instead of CacheTenancyBootstrapper
|
DatabaseCacheBootstrapper::class, // Used instead of CacheTenancyBootstrapper
|
||||||
],
|
],
|
||||||
]);
|
]);
|
||||||
});
|
|
||||||
|
|
||||||
test('DatabaseCacheBootstrapper uses tenant connection for saving cache', function() {
|
|
||||||
pest()->artisan('migrate', [
|
|
||||||
'--path' => __DIR__ . '/Etc/cache_migrations',
|
|
||||||
'--realpath' => true,
|
|
||||||
])->assertExitCode(0);
|
|
||||||
|
|
||||||
|
// DB query for verifying that the cache is stored in DB
|
||||||
|
// 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;
|
$databaseCacheValue = fn () => DB::selectOne("SELECT * FROM `cache` WHERE `key` = '{$databaseCacheKey}'")?->value;
|
||||||
|
|
||||||
|
$createCacheTables = function () {
|
||||||
|
Schema::create('cache', function (Blueprint $table) {
|
||||||
|
$table->string('key')->primary();
|
||||||
|
$table->mediumText('value');
|
||||||
|
$table->integer('expiration');
|
||||||
|
});
|
||||||
|
|
||||||
|
Schema::create('cache_locks', function (Blueprint $table) {
|
||||||
|
$table->string('key')->primary();
|
||||||
|
$table->string('owner');
|
||||||
|
$table->integer('expiration');
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
// Create cache tables in central DB
|
||||||
|
$createCacheTables();
|
||||||
|
|
||||||
$tenant = Tenant::create();
|
$tenant = Tenant::create();
|
||||||
$tenant2 = Tenant::create();
|
$tenant2 = Tenant::create();
|
||||||
|
|
||||||
pest()->artisan('tenants:migrate', [
|
// Create cache tables in tenant DBs
|
||||||
'--path' => __DIR__ . '/Etc/cache_migrations',
|
// With this bootstrapper, cache will be saved in these tenant DBs instead of the central DB
|
||||||
'--realpath' => true,
|
tenancy()->runForMultiple([$tenant, $tenant2], $createCacheTables);
|
||||||
])->assertExitCode(0);
|
|
||||||
|
|
||||||
// Write to cache in central context
|
// Write to cache in central context
|
||||||
cache(['foo' => 'CENTRAL']);
|
cache(['foo' => 'CENTRAL']);
|
||||||
|
|
@ -60,18 +78,15 @@ test('DatabaseCacheBootstrapper uses tenant connection for saving cache', functi
|
||||||
tenancy()->end();
|
tenancy()->end();
|
||||||
|
|
||||||
// The 'foo' cache value in the central database should be 'CENTRAL'
|
// The 'foo' cache value in the central database should be 'CENTRAL'
|
||||||
expect(cache('foo'))->toBe('CENTRAL');
|
|
||||||
expect(str($databaseCacheValue())->contains('CENTRAL'))->toBeTrue();
|
expect(str($databaseCacheValue())->contains('CENTRAL'))->toBeTrue();
|
||||||
|
|
||||||
tenancy()->initialize($tenant);
|
tenancy()->initialize($tenant);
|
||||||
|
|
||||||
// The 'foo' cache value in the tenant database should be 'TENANT'
|
// The 'foo' cache value in the tenant database should be 'TENANT'
|
||||||
expect(cache('foo'))->toBe('TENANT');
|
|
||||||
expect(str($databaseCacheValue())->contains('TENANT'))->toBeTrue();
|
expect(str($databaseCacheValue())->contains('TENANT'))->toBeTrue();
|
||||||
|
|
||||||
tenancy()->initialize($tenant2);
|
tenancy()->initialize($tenant2);
|
||||||
|
|
||||||
// The 'foo' cache value in another tenant's database should be null
|
// The 'foo' cache key in another tenant's database shouldn't exist
|
||||||
expect(cache('foo'))->toBeNull();
|
|
||||||
expect(DB::select('select * from cache'))->toHaveCount(0);
|
expect(DB::select('select * from cache'))->toHaveCount(0);
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -1,35 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
use Illuminate\Database\Migrations\Migration;
|
|
||||||
use Illuminate\Database\Schema\Blueprint;
|
|
||||||
use Illuminate\Support\Facades\Schema;
|
|
||||||
|
|
||||||
return new class extends Migration
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* Run the migrations.
|
|
||||||
*/
|
|
||||||
public function up(): void
|
|
||||||
{
|
|
||||||
Schema::create('cache', function (Blueprint $table) {
|
|
||||||
$table->string('key')->primary();
|
|
||||||
$table->mediumText('value');
|
|
||||||
$table->integer('expiration');
|
|
||||||
});
|
|
||||||
|
|
||||||
Schema::create('cache_locks', function (Blueprint $table) {
|
|
||||||
$table->string('key')->primary();
|
|
||||||
$table->string('owner');
|
|
||||||
$table->integer('expiration');
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Reverse the migrations.
|
|
||||||
*/
|
|
||||||
public function down(): void
|
|
||||||
{
|
|
||||||
Schema::dropIfExists('cache');
|
|
||||||
Schema::dropIfExists('cache_locks');
|
|
||||||
}
|
|
||||||
};
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue