From 3c183e45d9365e3805f79c8f61b7549aac8554ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Sat, 4 Jan 2025 15:57:01 +0100 Subject: [PATCH] fix #1277: consume PendingDispatch return values in Tenancy::run() --- src/Tenancy.php | 4 ++++ tests/QueueTest.php | 20 +++++++++++++++++--- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/Tenancy.php b/src/Tenancy.php index 673c55cc..bc88dc67 100644 --- a/src/Tenancy.php +++ b/src/Tenancy.php @@ -80,6 +80,10 @@ class Tenancy $this->initialize($tenant); $result = $callback($tenant); + if ($result instanceof \Illuminate\Foundation\Bus\PendingDispatch) { // #1277 + $result = null; + } + if ($originalTenant) { $this->initialize($originalTenant); } else { diff --git a/tests/QueueTest.php b/tests/QueueTest.php index a4b3595f..4d3f5ce0 100644 --- a/tests/QueueTest.php +++ b/tests/QueueTest.php @@ -6,15 +6,12 @@ use Illuminate\Bus\Queueable; 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; @@ -229,6 +226,23 @@ test('tenant connections do not persist after tenant jobs get processed', functi expect(collect(DB::select('SHOW FULL PROCESSLIST'))->pluck('db'))->not()->toContain($tenant->database()->getName()); }); +// Regression test for #1277 +test('dispatching a job from a tenant run arrow function dispatches it immediately', function () { + withTenantDatabases(); + + $tenant = Tenant::create(); + + $result = $tenant->run(fn () => dispatch(new TestJob(pest()->valuestore))); + expect($result)->toBe(null); + + expect(tenant())->toBe(null); + expect(pest()->valuestore->has('tenant_id'))->toBeFalse(); + pest()->artisan('queue:work --once'); + expect(tenant())->toBe(null); + + expect(pest()->valuestore->get('tenant_id'))->toBe('The current tenant id is: ' . $tenant->getTenantKey()); +}); + function createValueStore(): void { $valueStorePath = __DIR__ . '/Etc/tmp/queuetest.json';