mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-13 05:04:04 +00:00
Add and test DatabaseCacheBootstrapper
This commit is contained in:
parent
0e223e0484
commit
63b4efc028
3 changed files with 147 additions and 0 deletions
35
src/Bootstrappers/DatabaseCacheBootstrapper.php
Normal file
35
src/Bootstrappers/DatabaseCacheBootstrapper.php
Normal file
|
|
@ -0,0 +1,35 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Stancl\Tenancy\Bootstrappers;
|
||||||
|
|
||||||
|
use Illuminate\Config\Repository;
|
||||||
|
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
|
||||||
|
use Stancl\Tenancy\Contracts\Tenant;
|
||||||
|
use Illuminate\Cache\CacheManager;
|
||||||
|
|
||||||
|
class DatabaseCacheBootstrapper implements TenancyBootstrapper
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
protected Repository $config,
|
||||||
|
protected CacheManager $cache,
|
||||||
|
protected string|null $originalConnection = null,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
public function bootstrap(Tenant $tenant): void
|
||||||
|
{
|
||||||
|
$this->originalConnection = $this->config->get('cache.stores.database.connection');
|
||||||
|
|
||||||
|
$this->config->set('cache.stores.database.connection', 'tenant');
|
||||||
|
|
||||||
|
$this->cache->purge('database');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function revert(): void
|
||||||
|
{
|
||||||
|
$this->config->set('cache.stores.database.connection', $this->originalConnection);
|
||||||
|
|
||||||
|
$this->cache->purge('database');
|
||||||
|
}
|
||||||
|
}
|
||||||
77
tests/DbCacheBootstrapperTest.php
Normal file
77
tests/DbCacheBootstrapperTest.php
Normal file
|
|
@ -0,0 +1,77 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
use Stancl\Tenancy\Bootstrappers\DatabaseCacheBootstrapper;
|
||||||
|
use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper;
|
||||||
|
use Illuminate\Support\Facades\Event;
|
||||||
|
use Stancl\Tenancy\Events\TenantCreated;
|
||||||
|
use Stancl\JobPipeline\JobPipeline;
|
||||||
|
use Stancl\Tenancy\Jobs\CreateDatabase;
|
||||||
|
use Stancl\Tenancy\Tests\Etc\Tenant;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
use Stancl\Tenancy\Events\TenancyInitialized;
|
||||||
|
use Stancl\Tenancy\Listeners\BootstrapTenancy;
|
||||||
|
use Stancl\Tenancy\Events\TenancyEnded;
|
||||||
|
use Stancl\Tenancy\Listeners\RevertToCentralContext;
|
||||||
|
|
||||||
|
beforeEach(function () {
|
||||||
|
Event::listen(TenantCreated::class, JobPipeline::make([CreateDatabase::class])->send(function (TenantCreated $event) {
|
||||||
|
return $event->tenant;
|
||||||
|
})->toListener());
|
||||||
|
|
||||||
|
Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
|
||||||
|
Event::listen(TenancyEnded::class, RevertToCentralContext::class);
|
||||||
|
|
||||||
|
config([
|
||||||
|
'cache.default' => 'database',
|
||||||
|
'tenancy.bootstrappers' => [
|
||||||
|
DatabaseTenancyBootstrapper::class,
|
||||||
|
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);
|
||||||
|
|
||||||
|
$databaseCacheKey = config('cache.prefix') . 'foo';
|
||||||
|
$databaseCacheValue = fn () => DB::selectOne("SELECT * FROM `cache` WHERE `key` = '{$databaseCacheKey}'")?->value;
|
||||||
|
|
||||||
|
$tenant = Tenant::create();
|
||||||
|
$tenant2 = Tenant::create();
|
||||||
|
|
||||||
|
pest()->artisan('tenants:migrate', [
|
||||||
|
'--path' => __DIR__ . '/Etc/cache_migrations',
|
||||||
|
'--realpath' => true,
|
||||||
|
])->assertExitCode(0);
|
||||||
|
|
||||||
|
// Write to cache in central context
|
||||||
|
cache(['foo' => 'CENTRAL']);
|
||||||
|
|
||||||
|
tenancy()->initialize($tenant);
|
||||||
|
|
||||||
|
// Write to cache in tenant context
|
||||||
|
cache(['foo' => 'TENANT']);
|
||||||
|
|
||||||
|
tenancy()->end();
|
||||||
|
|
||||||
|
// The 'foo' cache value in the central database should be 'CENTRAL'
|
||||||
|
expect(cache('foo'))->toBe('CENTRAL');
|
||||||
|
expect(str($databaseCacheValue())->contains('CENTRAL'))->toBeTrue();
|
||||||
|
|
||||||
|
tenancy()->initialize($tenant);
|
||||||
|
|
||||||
|
// The 'foo' cache value in the tenant database should be 'TENANT'
|
||||||
|
expect(cache('foo'))->toBe('TENANT');
|
||||||
|
expect(str($databaseCacheValue())->contains('TENANT'))->toBeTrue();
|
||||||
|
|
||||||
|
tenancy()->initialize($tenant2);
|
||||||
|
|
||||||
|
// The 'foo' cache value in another tenant's database should be null
|
||||||
|
expect(cache('foo'))->toBeNull();
|
||||||
|
expect(DB::select('select * from cache'))->toHaveCount(0);
|
||||||
|
});
|
||||||
|
|
@ -0,0 +1,35 @@
|
||||||
|
<?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