init
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Unit;
|
||||
|
||||
use App\Support\MissingTenantContextException;
|
||||
use App\Support\TenantContext;
|
||||
use InvalidArgumentException;
|
||||
use Tests\TestCase;
|
||||
|
||||
class TenantContextTest extends TestCase
|
||||
{
|
||||
public function test_code_requires_a_tenant(): void
|
||||
{
|
||||
$this->expectException(MissingTenantContextException::class);
|
||||
|
||||
app(TenantContext::class)->code();
|
||||
}
|
||||
|
||||
public function test_empty_tenant_code_is_rejected(): void
|
||||
{
|
||||
$this->expectException(InvalidArgumentException::class);
|
||||
|
||||
app(TenantContext::class)->set('');
|
||||
}
|
||||
|
||||
public function test_cache_and_lock_keys_include_the_tenant(): void
|
||||
{
|
||||
$context = app(TenantContext::class);
|
||||
$context->set('acme');
|
||||
|
||||
$this->assertSame('tenant:acme:sku:1', $context->cacheKey('sku:1'));
|
||||
$this->assertSame('lock:tenant:acme:inventory:1', $context->lockKey('inventory:1'));
|
||||
}
|
||||
|
||||
public function test_run_restores_the_previous_tenant(): void
|
||||
{
|
||||
$context = app(TenantContext::class);
|
||||
$context->set('tenant-a');
|
||||
|
||||
$seen = $context->run('tenant-b', fn (): string => $context->code());
|
||||
|
||||
$this->assertSame('tenant-b', $seen);
|
||||
$this->assertSame('tenant-a', $context->code());
|
||||
}
|
||||
|
||||
public function test_run_clears_context_when_none_was_set(): void
|
||||
{
|
||||
$context = app(TenantContext::class);
|
||||
|
||||
$context->run('tenant-b', fn (): string => $context->code());
|
||||
|
||||
$this->assertFalse($context->has());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user