diff --git a/src/Actions/MakeQueueTenantAwareAction.php b/src/Actions/MakeQueueTenantAwareAction.php index ca1749c..02cd776 100644 --- a/src/Actions/MakeQueueTenantAwareAction.php +++ b/src/Actions/MakeQueueTenantAwareAction.php @@ -2,6 +2,7 @@ namespace Spatie\Multitenancy\Actions; +use Illuminate\Queue\Events\JobProcessed; use Illuminate\Queue\Events\JobProcessing; use Spatie\Multitenancy\Jobs\NotTenantAware; use Spatie\Multitenancy\Jobs\TenantAware; @@ -39,7 +40,7 @@ protected function listenForJobsBeingQueued(): self protected function listenForJobsBeingProcessed(): self { app('events')->listen(JobProcessing::class, function (JobProcessing $event) { - $tenantId = $event->job->payload()['tenantId']; + $tenantId = $event->job->payload()['tenantId'] ?? null; if (! $tenantId) { return; diff --git a/tests/Feature/TenantAwareJobs/QueueIsTenantAwareByDefaultTest.php b/tests/Feature/TenantAwareJobs/QueueIsTenantAwareByDefaultTest.php index fd30858..8322f74 100644 --- a/tests/Feature/TenantAwareJobs/QueueIsTenantAwareByDefaultTest.php +++ b/tests/Feature/TenantAwareJobs/QueueIsTenantAwareByDefaultTest.php @@ -3,6 +3,7 @@ namespace Spatie\Multitenancy\Tests\Feature\TenantAwareJobs; use Illuminate\Contracts\Bus\Dispatcher; +use Illuminate\Queue\Events\JobFailed; use Spatie\Multitenancy\Models\Tenant; use Spatie\Multitenancy\Tests\Feature\TenantAwareJobs\TestClasses\NotTenantAwareTestJob; use Spatie\Multitenancy\Tests\Feature\TenantAwareJobs\TestClasses\TenantAwareTestJob; @@ -25,6 +26,8 @@ public function setUp(): void $this->tenant = factory(Tenant::class)->create(); $this->valuestore = Valuestore::make($this->tempFile('tenantAware.json'))->flush(); + + $this->doesntExpectEvents(JobFailed::class); } /** @test */ @@ -49,8 +52,6 @@ public function it_will_not_break_when_no_tenant_is_set() $job = new TestJob($this->valuestore); app(Dispatcher::class)->dispatch($job); - $this->tenant->makeCurrent(); - $this->artisan('queue:work --once')->assertExitCode(0); $currentTenantIdInJob = $this->valuestore->get('tenantId'); @@ -94,7 +95,7 @@ public function it_will_not_make_jobs_tenant_aware_if_the_config_setting_is_set_ $this->artisan('queue:work --once')->assertExitCode(0); - $currentTenantIdInJob = $this->valuestore->get('tenantId'); + $currentTenantIdInJob = $this->valuestore->get('tenantIdInPayload'); $this->assertNull($currentTenantIdInJob); } @@ -126,7 +127,7 @@ public function it_will_not_make_a_job_tenant_aware_if_it_implement_NotTenantAwa $this->artisan('queue:work --once')->assertExitCode(0); - $currentTenantIdInJob = $this->valuestore->get('tenantId'); + $currentTenantIdInJob = $this->valuestore->get('tenantIdInPayload'); $this->assertNull($currentTenantIdInJob); } } diff --git a/tests/Feature/TenantAwareJobs/TestClasses/TestJob.php b/tests/Feature/TenantAwareJobs/TestClasses/TestJob.php index e4bd682..a503ba5 100644 --- a/tests/Feature/TenantAwareJobs/TestClasses/TestJob.php +++ b/tests/Feature/TenantAwareJobs/TestClasses/TestJob.php @@ -3,11 +3,14 @@ namespace Spatie\Multitenancy\Tests\Feature\TenantAwareJobs\TestClasses; use Illuminate\Contracts\Queue\ShouldQueue; +use Illuminate\Queue\InteractsWithQueue; use Spatie\Multitenancy\Models\Tenant; use Spatie\Valuestore\Valuestore; class TestJob implements ShouldQueue { + use InteractsWithQueue; + public Valuestore $valuestore; public function __construct(Valuestore $valuestore) @@ -17,6 +20,7 @@ public function __construct(Valuestore $valuestore) public function handle() { - $this->valuestore->put('tenantId', Tenant::current()->id); + $this->valuestore->put('tenantIdInPayload', $this->job->payload()['tenantId'] ?? null); + $this->valuestore->put('tenantId', optional(Tenant::current())->id); } }