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

72 lines
3.8 KiB
PHTML

<?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-19T15:00:00Z');
$wiseBody = '[{"source":"USD","target":"CNY","rate":7.2,"time":"2026-11-19T14:59:00Z"}]';
$krakenBody = '{"error":[],"result":{"USDTZUSD":{"a":["1.000"],"b":["0.99800000"]}}}';
$mock = new MockHandler([
new Response(200, [], $wiseBody), 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, [], $wiseBody),
new Response(200, [], '{"error":[],"result":{"USDTZUSD":{"b":["0.85"]}}}'),
new Response(401), new Response(200, [], $krakenBody),
]);
$fx = new PaymentRateService(new Client(['handler' => HandlerStack::create($mock)]), 'test-rate-access');
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', 'Wise did not return CNY per USD.');
requirePaymentRate(PaymentRateService::current('USDT/USD', $now)['rate'] === '0.998', 'Kraken ask or wrong precision used.');
requirePaymentRate(PaymentRateService::status('USD/CNY', $now)['source_at'] === $now - 60
&& 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 Wise snapshot could not be read twice.');
requirePaymentRate(PaymentRateService::status('USD/CNY', $now + 1800)['source_at'] === $now - 60,
'Re-reading an old Wise quote 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'] === 'wise', 'Override failed to expire.');
requirePaymentRate($fx->refreshDue($now + 3600) === ['USD/CNY' => 'unauthorized', 'USDT/USD' => 'updated'],
'A Wise authorization failure prevented Kraken from refreshing.');
echo "Payment rate feeds, source time, expiry and isolation passed.\n";