Files
portal/app/tests/Unit/Middleware/UseAdminConnectionTest.php
T
Дмитрий 9d0999d49a
Accessibility (Pa11y live) / a11y (push) Has been cancelled
SAST — Semgrep / Semgrep SAST scan (push) Has been cancelled
style(админка): pint — new UseAdminConnection без скобок в тесте
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-27 07:17:39 +03:00

43 lines
1.1 KiB
PHP

<?php
declare(strict_types=1);
use App\Http\Middleware\UseAdminConnection;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;
uses(TestCase::class);
it('switches default connection to pgsql_admin during the request', function () {
$original = DB::getDefaultConnection();
$seen = null;
$response = (new UseAdminConnection)->handle(
Request::create('/api/admin/tenants'),
function () use (&$seen) {
$seen = DB::getDefaultConnection();
return response('ok');
}
);
expect($seen)->toBe('pgsql_admin');
expect($response->getContent())->toBe('ok');
expect(DB::getDefaultConnection())->toBe($original); // восстановлено
});
it('restores the default connection even when downstream throws', function () {
$original = DB::getDefaultConnection();
$call = fn () => (new UseAdminConnection)->handle(
Request::create('/api/admin/tenants'),
function () {
throw new RuntimeException('boom');
}
);
expect($call)->toThrow(RuntimeException::class);
expect(DB::getDefaultConnection())->toBe($original);
});