-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Add tests for the jobprocessed listener that makes jobs tenant-…
…aware
- Loading branch information
Showing
2 changed files
with
106 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Sprout\Tests\Listeners; | ||
|
||
use Illuminate\Config\Repository; | ||
use Illuminate\Foundation\Testing\RefreshDatabase; | ||
use Illuminate\Queue\QueueManager; | ||
use Illuminate\Support\Facades\Context; | ||
use Orchestra\Testbench\Concerns\WithWorkbench; | ||
use Orchestra\Testbench\TestCase; | ||
use PHPUnit\Framework\Attributes\Group; | ||
use PHPUnit\Framework\Attributes\Test; | ||
use Sprout\Managers\TenancyManager; | ||
use Sprout\TenancyOptions; | ||
use Workbench\App\Jobs\TestTenantJob; | ||
use Workbench\App\Models\TenantModel; | ||
|
||
#[Group('listeners')] | ||
class SetCurrentTenantForJobTest extends TestCase | ||
{ | ||
use WithWorkbench, RefreshDatabase; | ||
|
||
protected $enablesPackageDiscoveries = true; | ||
|
||
protected function defineEnvironment($app): void | ||
{ | ||
tap($app['config'], static function (Repository $config) { | ||
$config->set('multitenancy.providers.tenants.model', TenantModel::class); | ||
}); | ||
} | ||
|
||
#[Test] | ||
public function doesNotSetCurrentTenantForJobWithoutOption(): void | ||
{ | ||
/** @var \Sprout\Contracts\Tenancy<*> $tenancy */ | ||
$tenancy = app(TenancyManager::class)->get(); | ||
$tenancy->removeOption(TenancyOptions::makeJobsTenantAware()); | ||
|
||
$this->assertFalse($tenancy->check()); | ||
|
||
$tenant = TenantModel::factory()->createOne(); | ||
|
||
Context::add('sprout.tenants', [$tenancy->getName() => $tenant->getKey()]); | ||
|
||
$this->assertTrue(Context::has('sprout.tenants')); | ||
$this->assertSame([$tenancy->getName() => $tenant->getKey()], Context::get('sprout.tenants')); | ||
|
||
TestTenantJob::dispatchSync(); | ||
|
||
$this->assertFalse($tenancy->check()); | ||
} | ||
|
||
#[Test] | ||
public function setsCurrentTenantForJobWithOption(): void | ||
{ | ||
/** @var \Sprout\Contracts\Tenancy<*> $tenancy */ | ||
$tenancy = app(TenancyManager::class)->get(); | ||
$tenancy->addOption(TenancyOptions::makeJobsTenantAware()); | ||
|
||
$this->assertFalse($tenancy->check()); | ||
|
||
$tenant = TenantModel::factory()->createOne(); | ||
|
||
Context::add('sprout.tenants', [$tenancy->getName() => $tenant->getKey()]); | ||
|
||
$this->assertTrue(Context::has('sprout.tenants')); | ||
$this->assertSame([$tenancy->getName() => $tenant->getKey()], Context::get('sprout.tenants')); | ||
|
||
TestTenantJob::dispatchSync(); | ||
|
||
$this->assertTrue($tenancy->check()); | ||
$this->assertSame($tenant->getKey(), $tenancy->key()); | ||
$this->assertTrue($tenant->is($tenancy->tenant())); | ||
} | ||
} |
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,30 @@ | ||
<?php | ||
|
||
namespace Workbench\App\Jobs; | ||
|
||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Foundation\Bus\Dispatchable; | ||
use Illuminate\Foundation\Queue\Queueable; | ||
use Illuminate\Queue\InteractsWithQueue; | ||
use Illuminate\Queue\SerializesModels; | ||
|
||
class TestTenantJob implements ShouldQueue | ||
{ | ||
use Queueable; | ||
|
||
/** | ||
* Create a new job instance. | ||
*/ | ||
public function __construct() | ||
{ | ||
// | ||
} | ||
|
||
/** | ||
* Execute the job. | ||
*/ | ||
public function handle(): void | ||
{ | ||
// | ||
} | ||
} |