Skip to content

Commit

Permalink
test: Add tests for the jobprocessed listener that makes jobs tenant-…
Browse files Browse the repository at this point in the history
…aware
  • Loading branch information
ollieread committed Sep 27, 2024
1 parent 1cebaa4 commit d2087f5
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 0 deletions.
76 changes: 76 additions & 0 deletions tests/Listeners/SetCurrentTenantForJobTest.php
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()));
}
}
30 changes: 30 additions & 0 deletions workbench/app/Jobs/TestTenantJob.php
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
{
//
}
}

0 comments on commit d2087f5

Please sign in to comment.