Files
portal/app/tests/Unit/External/SupplierBalanceProviderTest.php
T
Дмитрий 88e816c576 feat(балансы): backend плитки балансов внешних сервисов
Ежедневный контроль баланса DaData/Поставщик/Yandex Cloud плиткой дашборда.

- Таблица external_service_balances (pgsql_supplier, BYPASSRLS, last-value upsert)
- BalanceHealth: чистая логика светофора (red <floor или <3д; amber <floor или <7д)
- BalanceProvider+DTO; провайдеры DaData(API)/YC(OAuth→IAM→billing)/Supplier(Playwright)
- RefreshExternalBalancesJob: изоляция провайдеров (try/catch), расписание 06:30 МСК
- AdminDashboardController::balances() + плитка в summary + topup_url (кнопка «Пополнить»)
- Тесты: BalanceHealth, 3 провайдера, джоба, endpoint (102 теста зелёные)

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-28 07:12:14 +03:00

47 lines
1.7 KiB
PHP

<?php
declare(strict_types=1);
use App\Services\External\SupplierBalanceProvider;
use App\Services\Supplier\PlaywrightBridge;
use Tests\TestCase;
uses(TestCase::class); // нужен booted-app: config()/app()
beforeEach(function () {
config()->set('services.supplier.login', 'u');
config()->set('services.supplier.password', 'p');
config()->set('services.supplier.portal_url', 'https://crm.bp-gr.ru');
});
it('читает баланс кабинета через Playwright-мост', function () {
$bridge = Mockery::mock(PlaywrightBridge::class);
$bridge->shouldReceive('run')->once()->andReturn(['balance' => 12400, 'currency' => 'RUB']);
app()->instance(PlaywrightBridge::class, $bridge);
$r = app(SupplierBalanceProvider::class)->fetch();
expect($r->ok)->toBeTrue();
expect($r->balance)->toBe(12400.0);
expect($r->serviceKey)->toBe('supplier');
});
it('мост бросил → fail, не пробрасывает исключение', function () {
$bridge = Mockery::mock(PlaywrightBridge::class);
$bridge->shouldReceive('run')->andThrow(new RuntimeException('exit 2: balance not found'));
app()->instance(PlaywrightBridge::class, $bridge);
$r = app(SupplierBalanceProvider::class)->fetch();
expect($r->ok)->toBeFalse();
expect($r->error)->toContain('exit 2');
});
it('нет логина → fail без запуска моста', function () {
config()->set('services.supplier.login', '');
$bridge = Mockery::mock(PlaywrightBridge::class);
$bridge->shouldNotReceive('run');
app()->instance(PlaywrightBridge::class, $bridge);
$r = app(SupplierBalanceProvider::class)->fetch();
expect($r->ok)->toBeFalse();
});