Files
portal/app/tests/Unit/External/YooKassaLivenessProbeTest.php
T

45 lines
1.9 KiB
PHP

<?php
declare(strict_types=1);
use App\Services\External\YooKassaLivenessProbe;
use Illuminate\Support\Facades\Http;
use Tests\TestCase;
uses(TestCase::class); // нужен booted-app: config()/Http::fake()
beforeEach(function () {
config()->set('services.yookassa.shop_id', '1392092');
config()->set('services.yookassa.secret_key', 'test_secret');
config()->set('services.yookassa.api_url', 'https://api.yookassa.ru/v3');
});
it('зелёный при 200 от /me (с ключами)', function () {
Http::fake(['api.yookassa.ru/v3/me' => Http::response(['account_id' => '1392092'], 200)]);
$r = (new YooKassaLivenessProbe)->check();
expect($r->serviceKey)->toBe('yookassa');
expect($r->light)->toBe('green');
});
it('зелёный при 401 — шлюз на связи (нам важна ЖИВОСТЬ, не деньги/авторизация)', function () {
Http::fake(['api.yookassa.ru/v3/me' => Http::response(['type' => 'error'], 401)]);
expect((new YooKassaLivenessProbe)->check()->light)->toBe('green');
});
it('зелёный БЕЗ ключей — всё равно пингует шлюз, 401 = на связи', function () {
config()->set('services.yookassa.shop_id', '');
config()->set('services.yookassa.secret_key', '');
Http::fake(['api.yookassa.ru/v3/me' => Http::response('unauthorized', 401)]);
expect((new YooKassaLivenessProbe)->check()->light)->toBe('green');
});
it('красный при 5xx (шлюз лёг)', function () {
Http::fake(['api.yookassa.ru/v3/me' => Http::response('err', 502)]);
expect((new YooKassaLivenessProbe)->check()->light)->toBe('red');
});
it('красный при сетевой ошибке (проба не бросает)', function () {
Http::fake(fn () => throw new RuntimeException('network down'));
expect((new YooKassaLivenessProbe)->check()->light)->toBe('red');
});