-
-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for not tenant aware jobs in a default tenant aware applica…
…tion + fix them
- Loading branch information
Showing
14 changed files
with
315 additions
and
3 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
55 changes: 55 additions & 0 deletions
55
tests/Feature/TenantAwareJobs/QueuedBroadcastEventTest.php
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,55 @@ | ||
<?php | ||
|
||
use Illuminate\Auth\Events\Authenticated; | ||
use Illuminate\Foundation\Auth\User; | ||
use Illuminate\Support\Facades\Broadcast; | ||
use Illuminate\Support\Facades\Event; | ||
use Illuminate\Support\Facades\Mail; | ||
use Spatie\Multitenancy\Exceptions\CurrentTenantCouldNotBeDeterminedInTenantAwareJob; | ||
use Spatie\Multitenancy\Models\Tenant; | ||
use Spatie\Multitenancy\Tests\Feature\TenantAwareJobs\TestClasses\BroadcastNotTenantAware; | ||
use Spatie\Multitenancy\Tests\Feature\TenantAwareJobs\TestClasses\BroadcastTenantAware; | ||
use Spatie\Multitenancy\Tests\Feature\TenantAwareJobs\TestClasses\ListenerNotTenantAware; | ||
use Spatie\Multitenancy\Tests\Feature\TenantAwareJobs\TestClasses\ListenerTenantAware; | ||
use Spatie\Multitenancy\Tests\Feature\TenantAwareJobs\TestClasses\MailableNotTenantAware; | ||
use Spatie\Multitenancy\Tests\Feature\TenantAwareJobs\TestClasses\MailableTenantAware; | ||
use Spatie\Multitenancy\Tests\Feature\TenantAwareJobs\TestClasses\TestEvent; | ||
|
||
beforeEach(function () { | ||
config()->set('multitenancy.queues_are_tenant_aware_by_default', true); | ||
config()->set('queue.default', 'sync'); | ||
config()->set('mail.default', 'log'); | ||
|
||
$this->tenant = Tenant::factory()->create(); | ||
}); | ||
|
||
it('will fail when no tenant is present and listeners are tenant aware by default', function () { | ||
config()->set('multitenancy.queues_are_tenant_aware_by_default', true); | ||
|
||
Event::listen(TestEvent::class, ListenerTenantAware::class); | ||
|
||
Broadcast::event(new BroadcastTenantAware("Hello world!")); | ||
})->throws(CurrentTenantCouldNotBeDeterminedInTenantAwareJob::class); | ||
|
||
it('will not fail when no tenant is present and listeners are tenant aware by default', function () { | ||
config()->set('multitenancy.queues_are_tenant_aware_by_default', true); | ||
|
||
Event::listen(TestEvent::class, ListenerNotTenantAware::class); | ||
Broadcast::event(new BroadcastNotTenantAware("Hello world!")); | ||
|
||
$this->expectExceptionMessage("Method Illuminate\Events\Dispatcher::assertDispatchedTimes does not exist."); | ||
|
||
Event::assertDispatchedTimes(TestEvent::class); | ||
}); | ||
|
||
it('will inject the current tenant id', function () { | ||
config()->set('multitenancy.queues_are_tenant_aware_by_default', true); | ||
|
||
$this->tenant->makeCurrent(); | ||
|
||
Event::listen(TestEvent::class, ListenerNotTenantAware::class); | ||
|
||
expect( | ||
Broadcast::event(new BroadcastTenantAware("Hello world!")) | ||
)->toBeInstanceOf(\Illuminate\Broadcasting\PendingBroadcast::class); | ||
}); |
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,52 @@ | ||
<?php | ||
|
||
use Illuminate\Auth\Events\Authenticated; | ||
use Illuminate\Foundation\Auth\User; | ||
use Illuminate\Support\Facades\Event; | ||
use Illuminate\Support\Facades\Mail; | ||
use Spatie\Multitenancy\Exceptions\CurrentTenantCouldNotBeDeterminedInTenantAwareJob; | ||
use Spatie\Multitenancy\Models\Tenant; | ||
use Spatie\Multitenancy\Tests\Feature\TenantAwareJobs\TestClasses\ListenerNotTenantAware; | ||
use Spatie\Multitenancy\Tests\Feature\TenantAwareJobs\TestClasses\ListenerTenantAware; | ||
use Spatie\Multitenancy\Tests\Feature\TenantAwareJobs\TestClasses\MailableNotTenantAware; | ||
use Spatie\Multitenancy\Tests\Feature\TenantAwareJobs\TestClasses\MailableTenantAware; | ||
use Spatie\Multitenancy\Tests\Feature\TenantAwareJobs\TestClasses\TestEvent; | ||
|
||
beforeEach(function () { | ||
config()->set('multitenancy.queues_are_tenant_aware_by_default', true); | ||
config()->set('queue.default', 'sync'); | ||
config()->set('mail.default', 'log'); | ||
|
||
$this->tenant = Tenant::factory()->create(); | ||
}); | ||
|
||
it('will fail when no tenant is present and listeners are tenant aware by default', function () { | ||
config()->set('multitenancy.queues_are_tenant_aware_by_default', true); | ||
|
||
Event::listen(TestEvent::class, ListenerTenantAware::class); | ||
|
||
Event::dispatch(new TestEvent("Hello world!")); | ||
})->throws(CurrentTenantCouldNotBeDeterminedInTenantAwareJob::class); | ||
|
||
it('will not fail when no tenant is present and listeners are tenant aware by default', function () { | ||
config()->set('multitenancy.queues_are_tenant_aware_by_default', true); | ||
|
||
Event::listen(TestEvent::class, ListenerNotTenantAware::class); | ||
Event::dispatch(new TestEvent("Hello world!")); | ||
|
||
$this->expectExceptionMessage("Method Illuminate\Events\Dispatcher::assertDispatchedTimes does not exist."); | ||
|
||
Event::assertDispatchedTimes(TestEvent::class); | ||
}); | ||
|
||
it('will inject the current tenant id', function () { | ||
config()->set('multitenancy.queues_are_tenant_aware_by_default', true); | ||
|
||
$this->tenant->makeCurrent(); | ||
|
||
Event::listen(TestEvent::class, ListenerNotTenantAware::class); | ||
|
||
expect( | ||
Event::dispatch(new TestEvent("Hello world!")) | ||
)->toEqual([0 => null]); | ||
}); |
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 |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
use Illuminate\Support\Facades\Mail; | ||
use Spatie\Multitenancy\Exceptions\CurrentTenantCouldNotBeDeterminedInTenantAwareJob; | ||
use Spatie\Multitenancy\Models\Tenant; | ||
use Spatie\Multitenancy\Tests\Feature\TenantAwareJobs\TestClasses\MailableNotTenantAware; | ||
use Spatie\Multitenancy\Tests\Feature\TenantAwareJobs\TestClasses\MailableTenantAware; | ||
|
||
beforeEach(function () { | ||
|
@@ -19,6 +20,16 @@ | |
Mail::to('[email protected]')->queue(new MailableTenantAware()); | ||
})->throws(CurrentTenantCouldNotBeDeterminedInTenantAwareJob::class); | ||
|
||
it('will not fail when no tenant is present and mailables are tenant aware by default', function () { | ||
config()->set('multitenancy.queues_are_tenant_aware_by_default', true); | ||
|
||
Mail::to('[email protected]')->queue(new MailableNotTenantAware()); | ||
|
||
$this->expectExceptionMessage("Method Illuminate\Mail\Mailer::assertSentCount does not exist."); | ||
|
||
Mail::assertSentCount(1); | ||
}); | ||
|
||
it('will inject the current tenant id', function () { | ||
config()->set('multitenancy.queues_are_tenant_aware_by_default', true); | ||
|
||
|
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
24 changes: 24 additions & 0 deletions
24
tests/Feature/TenantAwareJobs/TestClasses/BroadcastNotTenantAware.php
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,24 @@ | ||
<?php | ||
|
||
namespace Spatie\Multitenancy\Tests\Feature\TenantAwareJobs\TestClasses; | ||
|
||
use Illuminate\Broadcasting\Channel; | ||
use Illuminate\Broadcasting\InteractsWithSockets; | ||
use Illuminate\Contracts\Broadcasting\ShouldBroadcast; | ||
use Illuminate\Foundation\Events\Dispatchable; | ||
use Illuminate\Queue\SerializesModels; | ||
use Spatie\Multitenancy\Jobs\NotTenantAware; | ||
|
||
class BroadcastNotTenantAware implements ShouldBroadcast, NotTenantAware | ||
{ | ||
public function __construct( | ||
public string $message, | ||
) {} | ||
|
||
public function broadcastOn() | ||
{ | ||
return [ | ||
new Channel('test-channel'), | ||
]; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
tests/Feature/TenantAwareJobs/TestClasses/BroadcastTenantAware.php
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,25 @@ | ||
<?php | ||
|
||
namespace Spatie\Multitenancy\Tests\Feature\TenantAwareJobs\TestClasses; | ||
|
||
use Illuminate\Broadcasting\Channel; | ||
use Illuminate\Broadcasting\InteractsWithSockets; | ||
use Illuminate\Contracts\Broadcasting\ShouldBroadcast; | ||
use Illuminate\Foundation\Events\Dispatchable; | ||
use Illuminate\Queue\SerializesModels; | ||
use Spatie\Multitenancy\Jobs\NotTenantAware; | ||
use Spatie\Multitenancy\Jobs\TenantAware; | ||
|
||
class BroadcastTenantAware implements ShouldBroadcast, TenantAware | ||
{ | ||
public function __construct( | ||
public string $message, | ||
) {} | ||
|
||
public function broadcastOn() | ||
{ | ||
return [ | ||
new Channel('test-channel'), | ||
]; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
tests/Feature/TenantAwareJobs/TestClasses/ListenerNotTenantAware.php
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,14 @@ | ||
<?php | ||
|
||
namespace Spatie\Multitenancy\Tests\Feature\TenantAwareJobs\TestClasses; | ||
|
||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Spatie\Multitenancy\Jobs\NotTenantAware; | ||
|
||
class ListenerNotTenantAware implements ShouldQueue, NotTenantAware | ||
{ | ||
public function handle(TestEvent $event): void | ||
{ | ||
|
||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
tests/Feature/TenantAwareJobs/TestClasses/ListenerTenantAware.php
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,14 @@ | ||
<?php | ||
|
||
namespace Spatie\Multitenancy\Tests\Feature\TenantAwareJobs\TestClasses; | ||
|
||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Spatie\Multitenancy\Jobs\TenantAware; | ||
|
||
class ListenerTenantAware implements ShouldQueue, TenantAware | ||
{ | ||
public function handle(TestEvent $event): void | ||
{ | ||
|
||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
tests/Feature/TenantAwareJobs/TestClasses/MailableNotTenantAware.php
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,15 @@ | ||
<?php | ||
|
||
namespace Spatie\Multitenancy\Tests\Feature\TenantAwareJobs\TestClasses; | ||
|
||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Mail\Mailable; | ||
use Spatie\Multitenancy\Jobs\NotTenantAware; | ||
|
||
class MailableNotTenantAware extends Mailable implements ShouldQueue, NotTenantAware | ||
{ | ||
public function build(): Mailable | ||
{ | ||
return $this->view('mailable'); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
tests/Feature/TenantAwareJobs/TestClasses/NotificationNotTenantAware.php
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,33 @@ | ||
<?php | ||
|
||
namespace Spatie\Multitenancy\Tests\Feature\TenantAwareJobs\TestClasses; | ||
|
||
use Illuminate\Bus\Queueable; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Notifications\Messages\MailMessage; | ||
use Illuminate\Notifications\Notification; | ||
use Spatie\Multitenancy\Jobs\NotTenantAware; | ||
use Spatie\Multitenancy\Jobs\TenantAware; | ||
|
||
class NotificationNotTenantAware extends Notification implements ShouldQueue, NotTenantAware | ||
{ | ||
use Queueable; | ||
|
||
public function via($notifiable) | ||
{ | ||
return ['mail']; | ||
} | ||
|
||
public function toMail($notifiable) | ||
{ | ||
return (new MailMessage()) | ||
->subject('Message') | ||
->greeting('Hello!') | ||
->line('Say goodbye!'); | ||
} | ||
|
||
public function toArray($notifiable) | ||
{ | ||
return [ ]; | ||
} | ||
} |
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,16 @@ | ||
<?php | ||
|
||
namespace Spatie\Multitenancy\Tests\Feature\TenantAwareJobs\TestClasses; | ||
|
||
use Illuminate\Broadcasting\InteractsWithSockets; | ||
use Illuminate\Foundation\Events\Dispatchable; | ||
use Illuminate\Queue\SerializesModels; | ||
|
||
class TestEvent | ||
{ | ||
use Dispatchable, InteractsWithSockets, SerializesModels; | ||
|
||
public function __construct( | ||
public string $message, | ||
) {} | ||
} |