You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/web/tests/PaymentRateServiceTest.php

85 lines
4.5 KiB
PHP

<?php
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Services\Payment\PaymentRateService;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Database\Schema\Blueprint;
function requirePaymentRate(bool $ok, string $message): void
{
if (!$ok) {
throw new RuntimeException($message);
}
}
$db = new Capsule();
$db->addConnection(['driver' => 'sqlite', 'database' => ':memory:']);
$db->setAsGlobal();
$db->bootEloquent();
$db->getConnection()->getSchemaBuilder()->create('configurations', function (Blueprint $table): void {
$table->increments('id');
$table->string('name')->unique();
$table->text('value');
});
$now = strtotime('2026-11-20T17:00:00Z');
$ecbBody = "KEY,FREQ,CURRENCY,CURRENCY_DENOM,EXR_TYPE,EXR_SUFFIX,TIME_PERIOD,OBS_VALUE\r\n"
. "EXR.D.CNY.EUR.SP00.A,D,CNY,EUR,SP00,A,2026-11-20,7.92\r\n"
. "EXR.D.USD.EUR.SP00.A,D,USD,EUR,SP00,A,2026-11-20,1.10\r\n";
$krakenBody = '{"error":[],"result":{"USDTZUSD":{"a":["1.000"],"b":["0.99800000"]}}}';
Capsule::table('configurations')->insert([
'name' => 'payment_rate_usd_cny',
'value' => json_encode([
'attempted_at' => $now,
'snapshot' => ['source' => 'wise', 'rate' => '7.1', 'source_at' => $now - 60, 'fetched_at' => $now],
'blocked' => false,
], JSON_THROW_ON_ERROR),
]);
$mock = new MockHandler([
new Response(200, [], $ecbBody), new Response(200, [], $krakenBody),
new Response(429),
new Response(200, [], '{"error":[],"result":{"USDCZUSD":{"b":["0.999"]}}}'),
new ConnectException('simulated timeout', new Request('GET', 'https://api.kraken.com/')),
new Response(200, [], $ecbBody),
new Response(200, [], '{"error":[],"result":{"USDTZUSD":{"b":["0.85"]}}}'),
new Response(503), new Response(200, [], $krakenBody),
]);
$fx = new PaymentRateService(new Client(['handler' => HandlerStack::create($mock)]));
requirePaymentRate($fx->refreshDue($now) === ['USD/CNY' => 'updated', 'USDT/USD' => 'updated'],
'Both feed snapshots should become available independently.');
requirePaymentRate(PaymentRateService::current('USD/CNY', $now)['rate'] === '7.2', 'ECB cross rate is not CNY per USD.');
requirePaymentRate(PaymentRateService::current('USD/CNY', strtotime('2026-11-25T14:59:59Z')) !== null
&& PaymentRateService::current('USD/CNY', strtotime('2026-11-25T15:00:00Z')) === null,
'The ECB observation did not expire after five days.');
requirePaymentRate(PaymentRateService::current('USDT/USD', $now)['rate'] === '0.998', 'Kraken ask or wrong precision used.');
requirePaymentRate(PaymentRateService::status('USD/CNY', $now)['source_at'] === strtotime('2026-11-20T15:00:00Z')
&& PaymentRateService::status('USDT/USD', $now)['source_at'] === null,
'The exchange time and local retrieval time have different meanings.');
foreach ([300 => 'rate_limited', 600 => 'invalid_response', 900 => 'unreachable'] as $offset => $reason) {
requirePaymentRate($fx->refreshDue($now + $offset)['USDT/USD'] === $reason, 'The feed problem was not classified.');
requirePaymentRate(PaymentRateService::current('USD/CNY', $now + $offset)['rate'] === '7.2', 'USDT disabled CNY.');
}
requirePaymentRate(PaymentRateService::current('USDT/USD', $now + 901) === null, 'The stale snapshot was still usable.');
requirePaymentRate($fx->refreshDue($now + 1800)['USD/CNY'] === 'updated', 'A valid ECB snapshot could not be read twice.');
requirePaymentRate(PaymentRateService::status('USD/CNY', $now + 1800)['source_at'] === strtotime('2026-11-20T15:00:00Z'),
'Re-reading an ECB observation advanced the source timestamp.');
requirePaymentRate(PaymentRateService::status('USDT/USD', $now + 1800)['error'] === 'out_of_range'
&& PaymentRateService::current('USDT/USD', $now + 1800) === null, 'A depeg quote became usable.');
PaymentRateService::override('USD/CNY', '7.25', $now + 1900, 9, 'Scheduled acceptance test', $now + 1800);
requirePaymentRate(PaymentRateService::current('USD/CNY', $now + 1899)['source'] === 'manual'
&& PaymentRateService::current('USD/CNY', $now + 1900)['source'] === 'ecb', 'Override failed to expire.');
requirePaymentRate($fx->refreshDue($now + 3600) === ['USD/CNY' => 'unreachable', 'USDT/USD' => 'updated'],
'An ECB failure prevented Kraken from refreshing.');
echo "Payment rate feeds, source time, expiry and isolation passed.\n";