diff --git a/tests/AutomaticModeTest.php b/tests/AutomaticModeTest.php index fc740fc1..1a0948ea 100644 --- a/tests/AutomaticModeTest.php +++ b/tests/AutomaticModeTest.php @@ -50,6 +50,8 @@ test('context is switched when tenancy is reinitialized', function () { }); test('central helper runs callbacks in the central state', function () { + withTenantDatabases(); + tenancy()->initialize($tenant = Tenant::create()); 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 () { + withTenantDatabases(); + tenancy()->initialize(Tenant::create()); 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 () { + withTenantDatabases(); + tenancy()->initialize($tenant = Tenant::create()); tenancy()->central(function () { diff --git a/tests/BatchTest.php b/tests/BatchTest.php index 629a4e61..24cb7c59 100644 --- a/tests/BatchTest.php +++ b/tests/BatchTest.php @@ -23,6 +23,8 @@ beforeEach(function () { }); test('batch repository is set to tenant connection and reverted', function () { + withTenantDatabases(); + $tenant = Tenant::create(); $tenant2 = Tenant::create(); diff --git a/tests/MailTest.php b/tests/MailTest.php index 544fda1b..c530b7e8 100644 --- a/tests/MailTest.php +++ b/tests/MailTest.php @@ -27,6 +27,8 @@ function assertMailerTransportUsesPassword(string|null $password) { }; test('mailer transport uses the correct credentials', function() { + withTenantDatabases(); + config(['mail.default' => 'smtp', 'mail.mailers.smtp.password' => $defaultPassword = 'DEFAULT']); 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() { + withTenantDatabases(); + $mailers = fn() => invade(app(MailManager::class))->mailers; app(MailManager::class)->mailer('smtp'); diff --git a/tests/Pest.php b/tests/Pest.php index d7ca8c22..5380da0a 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -1,6 +1,10 @@ in(__DIR__); @@ -8,3 +12,10 @@ function pest(): TestCase { return Pest\TestSuite::getInstance()->test; } + +function withTenantDatabases() +{ + Event::listen(TenantCreated::class, JobPipeline::make([CreateDatabase::class])->send(function (TenantCreated $event) { + return $event->tenant; + })->toListener()); +} diff --git a/tests/QueueTest.php b/tests/QueueTest.php index c1fa24b8..158daf20 100644 --- a/tests/QueueTest.php +++ b/tests/QueueTest.php @@ -3,23 +3,23 @@ declare(strict_types=1); 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 Illuminate\Support\Facades\DB; use Stancl\Tenancy\Tests\Etc\User; use Stancl\JobPipeline\JobPipeline; use Stancl\Tenancy\Tests\Etc\Tenant; use Illuminate\Support\Facades\Event; +use Illuminate\Queue\SerializesModels; use Illuminate\Support\Facades\Schema; use Stancl\Tenancy\Events\TenancyEnded; use Stancl\Tenancy\Jobs\CreateDatabase; +use Illuminate\Queue\InteractsWithQueue; use Stancl\Tenancy\Events\TenantCreated; use Illuminate\Database\Schema\Blueprint; use Illuminate\Queue\Events\JobProcessed; use Illuminate\Queue\Events\JobProcessing; +use Illuminate\Contracts\Queue\ShouldQueue; +use Illuminate\Foundation\Bus\Dispatchable; use Stancl\Tenancy\Events\TenancyInitialized; use Stancl\Tenancy\Listeners\BootstrapTenancy; use Stancl\Tenancy\Listeners\RevertToCentralContext; @@ -48,6 +48,8 @@ afterEach(function () { }); test('tenant id is passed to tenant queues', function () { + withTenantDatabases(); + config(['queue.default' => 'sync']); $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 () { + withTenantDatabases(); + $tenant = Tenant::create(); 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 { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;