Files
coruna-lab/tests/Unit/WalletBalanceServiceTest.php
T
2026-08-10 05:08:17 +08:00

79 lines
2.4 KiB
PHP

<?php
namespace Tests\Unit;
use App\Models\Device;
use App\Models\WalletAddress;
use App\Services\WalletBalanceService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Http;
use PHPUnit\Framework\Attributes\Test;
use Tests\TestCase;
class WalletBalanceServiceTest extends TestCase
{
use RefreshDatabase;
#[Test]
public function refreshes_tron_trx_and_usdt_only(): void
{
Http::fake([
'*/v1/accounts/*' => Http::response([
'data' => [[
'balance' => 1_000_000,
'trc20' => [
['TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t' => '3000000'],
],
]],
'success' => true,
], 200),
]);
$device = Device::query()->create([
'device_id' => 'dev-bal-1',
'channel_id' => 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
]);
$addr = WalletAddress::query()->create([
'device_id' => $device->id,
'address' => 'TKKyetwdwuv6fTWVMPsdQUZYwB7yiNwRp6',
'chain_type' => 'TRON',
'source' => 'imToken',
'btc' => 1.23,
'eth' => 4.56,
'monitor' => 1,
]);
$ok = app(WalletBalanceService::class)->refresh($addr);
$this->assertTrue($ok);
$addr->refresh();
$this->assertEqualsWithDelta(1.0, (float) $addr->trx, 0.0000001);
$this->assertEqualsWithDelta(3.0, (float) $addr->usdt, 0.0000001);
// Non-Tron columns are not overwritten.
$this->assertEqualsWithDelta(1.23, (float) $addr->btc, 0.0000001);
$this->assertEqualsWithDelta(4.56, (float) $addr->eth, 0.0000001);
}
#[Test]
public function skips_non_tron_chains(): void
{
$device = Device::query()->create([
'device_id' => 'dev-bal-2',
'channel_id' => 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
]);
$addr = WalletAddress::query()->create([
'device_id' => $device->id,
'address' => '0xab5c66752a9e8167967685f1450532fb96d5d24f',
'chain_type' => 'ETH',
'source' => 'MetaMask',
'eth' => 1.0,
'monitor' => 1,
]);
$ok = app(WalletBalanceService::class)->refresh($addr);
$this->assertFalse($ok);
$addr->refresh();
$this->assertEqualsWithDelta(1.0, (float) $addr->eth, 0.0000001);
}
}