mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-05 09:54:05 +00:00
Use withtenantDatabases where needed
This commit is contained in:
parent
cda2a57fb0
commit
23b028da36
5 changed files with 31 additions and 11 deletions
|
|
@ -50,6 +50,8 @@ test('context is switched when tenancy is reinitialized', function () {
|
||||||
});
|
});
|
||||||
|
|
||||||
test('central helper runs callbacks in the central state', function () {
|
test('central helper runs callbacks in the central state', function () {
|
||||||
|
withTenantDatabases();
|
||||||
|
|
||||||
tenancy()->initialize($tenant = Tenant::create());
|
tenancy()->initialize($tenant = Tenant::create());
|
||||||
|
|
||||||
tenancy()->central(function () {
|
tenancy()->central(function () {
|
||||||
|
|
@ -60,6 +62,8 @@ test('central helper runs callbacks in the central state', function () {
|
||||||
});
|
});
|
||||||
|
|
||||||
test('central helper returns the value from the callback', function () {
|
test('central helper returns the value from the callback', function () {
|
||||||
|
withTenantDatabases();
|
||||||
|
|
||||||
tenancy()->initialize(Tenant::create());
|
tenancy()->initialize(Tenant::create());
|
||||||
|
|
||||||
pest()->assertSame('foo', tenancy()->central(function () {
|
pest()->assertSame('foo', tenancy()->central(function () {
|
||||||
|
|
@ -68,6 +72,8 @@ test('central helper returns the value from the callback', function () {
|
||||||
});
|
});
|
||||||
|
|
||||||
test('central helper reverts back to tenant context', function () {
|
test('central helper reverts back to tenant context', function () {
|
||||||
|
withTenantDatabases();
|
||||||
|
|
||||||
tenancy()->initialize($tenant = Tenant::create());
|
tenancy()->initialize($tenant = Tenant::create());
|
||||||
|
|
||||||
tenancy()->central(function () {
|
tenancy()->central(function () {
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,8 @@ beforeEach(function () {
|
||||||
});
|
});
|
||||||
|
|
||||||
test('batch repository is set to tenant connection and reverted', function () {
|
test('batch repository is set to tenant connection and reverted', function () {
|
||||||
|
withTenantDatabases();
|
||||||
|
|
||||||
$tenant = Tenant::create();
|
$tenant = Tenant::create();
|
||||||
$tenant2 = Tenant::create();
|
$tenant2 = Tenant::create();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -27,6 +27,8 @@ function assertMailerTransportUsesPassword(string|null $password) {
|
||||||
};
|
};
|
||||||
|
|
||||||
test('mailer transport uses the correct credentials', function() {
|
test('mailer transport uses the correct credentials', function() {
|
||||||
|
withTenantDatabases();
|
||||||
|
|
||||||
config(['mail.default' => 'smtp', 'mail.mailers.smtp.password' => $defaultPassword = 'DEFAULT']);
|
config(['mail.default' => 'smtp', 'mail.mailers.smtp.password' => $defaultPassword = 'DEFAULT']);
|
||||||
MailTenancyBootstrapper::$credentialsMap = ['mail.mailers.smtp.password' => 'smtp_password'];
|
MailTenancyBootstrapper::$credentialsMap = ['mail.mailers.smtp.password' => 'smtp_password'];
|
||||||
|
|
||||||
|
|
@ -52,6 +54,8 @@ test('mailer transport uses the correct credentials', function() {
|
||||||
|
|
||||||
|
|
||||||
test('initializing and ending tenancy binds a fresh MailManager instance without cached mailers', function() {
|
test('initializing and ending tenancy binds a fresh MailManager instance without cached mailers', function() {
|
||||||
|
withTenantDatabases();
|
||||||
|
|
||||||
$mailers = fn() => invade(app(MailManager::class))->mailers;
|
$mailers = fn() => invade(app(MailManager::class))->mailers;
|
||||||
|
|
||||||
app(MailManager::class)->mailer('smtp');
|
app(MailManager::class)->mailer('smtp');
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,10 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
use Stancl\Tenancy\Tests\TestCase;
|
use Stancl\Tenancy\Tests\TestCase;
|
||||||
|
use Stancl\JobPipeline\JobPipeline;
|
||||||
|
use Illuminate\Support\Facades\Event;
|
||||||
|
use Stancl\Tenancy\Jobs\CreateDatabase;
|
||||||
|
use Stancl\Tenancy\Events\TenantCreated;
|
||||||
|
|
||||||
uses(TestCase::class)->in(__DIR__);
|
uses(TestCase::class)->in(__DIR__);
|
||||||
|
|
||||||
|
|
@ -8,3 +12,10 @@ function pest(): TestCase
|
||||||
{
|
{
|
||||||
return Pest\TestSuite::getInstance()->test;
|
return Pest\TestSuite::getInstance()->test;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function withTenantDatabases()
|
||||||
|
{
|
||||||
|
Event::listen(TenantCreated::class, JobPipeline::make([CreateDatabase::class])->send(function (TenantCreated $event) {
|
||||||
|
return $event->tenant;
|
||||||
|
})->toListener());
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,23 +3,23 @@
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
use Illuminate\Bus\Queueable;
|
use Illuminate\Bus\Queueable;
|
||||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
||||||
use Illuminate\Foundation\Bus\Dispatchable;
|
|
||||||
use Illuminate\Queue\InteractsWithQueue;
|
|
||||||
use Illuminate\Queue\SerializesModels;
|
|
||||||
use Spatie\Valuestore\Valuestore;
|
use Spatie\Valuestore\Valuestore;
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
use Stancl\Tenancy\Tests\Etc\User;
|
use Stancl\Tenancy\Tests\Etc\User;
|
||||||
use Stancl\JobPipeline\JobPipeline;
|
use Stancl\JobPipeline\JobPipeline;
|
||||||
use Stancl\Tenancy\Tests\Etc\Tenant;
|
use Stancl\Tenancy\Tests\Etc\Tenant;
|
||||||
use Illuminate\Support\Facades\Event;
|
use Illuminate\Support\Facades\Event;
|
||||||
|
use Illuminate\Queue\SerializesModels;
|
||||||
use Illuminate\Support\Facades\Schema;
|
use Illuminate\Support\Facades\Schema;
|
||||||
use Stancl\Tenancy\Events\TenancyEnded;
|
use Stancl\Tenancy\Events\TenancyEnded;
|
||||||
use Stancl\Tenancy\Jobs\CreateDatabase;
|
use Stancl\Tenancy\Jobs\CreateDatabase;
|
||||||
|
use Illuminate\Queue\InteractsWithQueue;
|
||||||
use Stancl\Tenancy\Events\TenantCreated;
|
use Stancl\Tenancy\Events\TenantCreated;
|
||||||
use Illuminate\Database\Schema\Blueprint;
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
use Illuminate\Queue\Events\JobProcessed;
|
use Illuminate\Queue\Events\JobProcessed;
|
||||||
use Illuminate\Queue\Events\JobProcessing;
|
use Illuminate\Queue\Events\JobProcessing;
|
||||||
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||||
|
use Illuminate\Foundation\Bus\Dispatchable;
|
||||||
use Stancl\Tenancy\Events\TenancyInitialized;
|
use Stancl\Tenancy\Events\TenancyInitialized;
|
||||||
use Stancl\Tenancy\Listeners\BootstrapTenancy;
|
use Stancl\Tenancy\Listeners\BootstrapTenancy;
|
||||||
use Stancl\Tenancy\Listeners\RevertToCentralContext;
|
use Stancl\Tenancy\Listeners\RevertToCentralContext;
|
||||||
|
|
@ -48,6 +48,8 @@ afterEach(function () {
|
||||||
});
|
});
|
||||||
|
|
||||||
test('tenant id is passed to tenant queues', function () {
|
test('tenant id is passed to tenant queues', function () {
|
||||||
|
withTenantDatabases();
|
||||||
|
|
||||||
config(['queue.default' => 'sync']);
|
config(['queue.default' => 'sync']);
|
||||||
|
|
||||||
$tenant = Tenant::create();
|
$tenant = Tenant::create();
|
||||||
|
|
@ -64,6 +66,8 @@ test('tenant id is passed to tenant queues', function () {
|
||||||
});
|
});
|
||||||
|
|
||||||
test('tenant id is not passed to central queues', function () {
|
test('tenant id is not passed to central queues', function () {
|
||||||
|
withTenantDatabases();
|
||||||
|
|
||||||
$tenant = Tenant::create();
|
$tenant = Tenant::create();
|
||||||
|
|
||||||
tenancy()->initialize($tenant);
|
tenancy()->initialize($tenant);
|
||||||
|
|
@ -217,13 +221,6 @@ function withUsers()
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function withTenantDatabases()
|
|
||||||
{
|
|
||||||
Event::listen(TenantCreated::class, JobPipeline::make([CreateDatabase::class])->send(function (TenantCreated $event) {
|
|
||||||
return $event->tenant;
|
|
||||||
})->toListener());
|
|
||||||
}
|
|
||||||
|
|
||||||
class TestJob implements ShouldQueue
|
class TestJob implements ShouldQueue
|
||||||
{
|
{
|
||||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue