-
-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #492 from masterix21/fix-queued-closures
Add support to queued closures
- Loading branch information
Showing
8 changed files
with
119 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
use Spatie\Multitenancy\Models\Tenant; | ||
use Spatie\Valuestore\Valuestore; | ||
|
||
beforeEach(function () { | ||
config()->set('multitenancy.queues_are_tenant_aware_by_default', false); | ||
|
||
$this->tenant = Tenant::factory()->create(); | ||
}); | ||
|
||
it('succeeds with closure job when queues are tenant aware by default', function () { | ||
$valuestore = Valuestore::make(tempFile('tenantAware.json'))->flush(); | ||
|
||
config()->set('multitenancy.queues_are_tenant_aware_by_default', true); | ||
|
||
$this->tenant->makeCurrent(); | ||
|
||
dispatch(function () use ($valuestore) { | ||
$tenant = Tenant::current(); | ||
|
||
$valuestore->put('tenantId', $tenant?->getKey()); | ||
$valuestore->put('tenantName', $tenant?->name); | ||
}); | ||
|
||
$this->artisan('queue:work --once')->assertExitCode(0); | ||
|
||
expect($valuestore->get('tenantId'))->toBe($this->tenant->getKey()) | ||
->and($valuestore->get('tenantName'))->toBe($this->tenant->name); | ||
}); | ||
|
||
it('fails with closure job when queues are not tenant aware by default', function () { | ||
$valuestore = Valuestore::make(tempFile('tenantAware.json'))->flush(); | ||
|
||
$this->tenant->makeCurrent(); | ||
|
||
dispatch(function () use ($valuestore) { | ||
$tenant = Tenant::current(); | ||
|
||
$valuestore->put('tenantId', $tenant?->getKey()); | ||
$valuestore->put('tenantName', $tenant?->name); | ||
}); | ||
|
||
$this->artisan('queue:work --once')->assertExitCode(0); | ||
|
||
expect($valuestore->get('tenantId'))->toBeNull() | ||
->and($valuestore->get('tenantName'))->toBeNull(); | ||
}); | ||
|
||
it('succeeds with closure job when a tenant is specified', function () { | ||
$valuestore = Valuestore::make(tempFile('tenantAware.json'))->flush(); | ||
|
||
$currentTenant = $this->tenant; | ||
|
||
dispatch(function () use ($valuestore, $currentTenant) { | ||
$currentTenant->makeCurrent(); | ||
|
||
$tenant = Tenant::current(); | ||
|
||
$valuestore->put('tenantId', $tenant?->getKey()); | ||
$valuestore->put('tenantName', $tenant?->name); | ||
|
||
$currentTenant->forget(); | ||
}); | ||
|
||
$this->artisan('queue:work --once')->assertExitCode(0); | ||
|
||
expect($valuestore->get('tenantId'))->toBe($this->tenant->getKey()) | ||
->and($valuestore->get('tenantName'))->toBe($this->tenant->name); | ||
}); |