|
| 1 | +<?php |
| 2 | + |
| 3 | +use Illuminate\Auth\Events\Authenticated; |
| 4 | +use Illuminate\Foundation\Auth\User; |
| 5 | +use Illuminate\Support\Facades\Broadcast; |
| 6 | +use Illuminate\Support\Facades\Event; |
| 7 | +use Illuminate\Support\Facades\Mail; |
| 8 | +use Spatie\Multitenancy\Exceptions\CurrentTenantCouldNotBeDeterminedInTenantAwareJob; |
| 9 | +use Spatie\Multitenancy\Models\Tenant; |
| 10 | +use Spatie\Multitenancy\Tests\Feature\TenantAwareJobs\TestClasses\BroadcastNotTenantAware; |
| 11 | +use Spatie\Multitenancy\Tests\Feature\TenantAwareJobs\TestClasses\BroadcastTenantAware; |
| 12 | +use Spatie\Multitenancy\Tests\Feature\TenantAwareJobs\TestClasses\ListenerNotTenantAware; |
| 13 | +use Spatie\Multitenancy\Tests\Feature\TenantAwareJobs\TestClasses\ListenerTenantAware; |
| 14 | +use Spatie\Multitenancy\Tests\Feature\TenantAwareJobs\TestClasses\MailableNotTenantAware; |
| 15 | +use Spatie\Multitenancy\Tests\Feature\TenantAwareJobs\TestClasses\MailableTenantAware; |
| 16 | +use Spatie\Multitenancy\Tests\Feature\TenantAwareJobs\TestClasses\TestEvent; |
| 17 | + |
| 18 | +beforeEach(function () { |
| 19 | + config()->set('multitenancy.queues_are_tenant_aware_by_default', true); |
| 20 | + config()->set('queue.default', 'sync'); |
| 21 | + config()->set('mail.default', 'log'); |
| 22 | + |
| 23 | + $this->tenant = Tenant::factory()->create(); |
| 24 | +}); |
| 25 | + |
| 26 | +it('will fail when no tenant is present and listeners are tenant aware by default', function () { |
| 27 | + config()->set('multitenancy.queues_are_tenant_aware_by_default', true); |
| 28 | + |
| 29 | + Event::listen(TestEvent::class, ListenerTenantAware::class); |
| 30 | + |
| 31 | + Broadcast::event(new BroadcastTenantAware("Hello world!")); |
| 32 | +})->throws(CurrentTenantCouldNotBeDeterminedInTenantAwareJob::class); |
| 33 | + |
| 34 | +it('will not fail when no tenant is present and listeners are tenant aware by default', function () { |
| 35 | + config()->set('multitenancy.queues_are_tenant_aware_by_default', true); |
| 36 | + |
| 37 | + Event::listen(TestEvent::class, ListenerNotTenantAware::class); |
| 38 | + Broadcast::event(new BroadcastNotTenantAware("Hello world!")); |
| 39 | + |
| 40 | + $this->expectExceptionMessage("Method Illuminate\Events\Dispatcher::assertDispatchedTimes does not exist."); |
| 41 | + |
| 42 | + Event::assertDispatchedTimes(TestEvent::class); |
| 43 | +}); |
| 44 | + |
| 45 | +it('will inject the current tenant id', function () { |
| 46 | + config()->set('multitenancy.queues_are_tenant_aware_by_default', true); |
| 47 | + |
| 48 | + $this->tenant->makeCurrent(); |
| 49 | + |
| 50 | + Event::listen(TestEvent::class, ListenerNotTenantAware::class); |
| 51 | + |
| 52 | + expect( |
| 53 | + Broadcast::event(new BroadcastTenantAware("Hello world!")) |
| 54 | + )->toBeInstanceOf(\Illuminate\Broadcasting\PendingBroadcast::class); |
| 55 | +}); |
0 commit comments