CaiWuXiTong/tests/Feature/Console/ImportVipshopJxBankPaymentsTest.php

83 lines
3.0 KiB
PHP

<?php
namespace Tests\Feature\Console;
use Illuminate\Support\Facades\DB;
use Modules\EcommerceFinance\Repositories\VipshopJxBankPaymentRepository;
use Tests\TestCase;
class ImportVipshopJxBankPaymentsTest extends TestCase
{
private ?string $file = null;
protected function setUp(): void
{
parent::setUp();
config([
'database.connections.business_mirror' => [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
],
'upstream_mirrors.connection' => 'business_mirror',
]);
DB::purge('business_mirror');
(require database_path('migrations/2026_11_07_180000_create_vipshop_jx_bank_payments.php'))->up();
}
protected function tearDown(): void
{
if ($this->file !== null && is_file($this->file)) {
unlink($this->file);
}
parent::tearDown();
}
public function test_it_imports_only_successful_bank_account_payments_and_replaces_the_snapshot(): void
{
$payDate = (int) now()->setDate(2026, 10, 10)->startOfDay()->getTimestampMs();
$rows = [
$this->row(10, 'VIP-20260701-672378-P-003672', 127399.77, $payDate),
$this->row(11, 'VIP-20260711-672378-P-009568', 149428.92, $payDate),
$this->row(12, 'JIT_CZ-20260705-672378-M-029407', -71300.46, $payDate),
array_replace($this->row(13, 'VIP-20260721-672378-P-017554', 31226.97, $payDate), [
'vendorAccountNum' => '银行存款-中转',
]),
array_replace($this->row(14, 'VIP-20260801-672378-P-003230', 100, $payDate), [
'payStatus' => 'FAILED',
]),
];
$this->file = tempnam(sys_get_temp_dir(), 'vipshop-jx-bank-');
file_put_contents($this->file, json_encode($rows, JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR));
$this->artisan('vipshop-jx:import-bank-payments', [
'file' => $this->file,
'--replace' => true,
])->assertSuccessful();
$payments = DB::connection('business_mirror')
->table(VipshopJxBankPaymentRepository::TABLE)
->orderBy('source_payment_id')
->get(['source_payment_id', 'bill_period', 'amount']);
$this->assertCount(3, $payments);
$this->assertSame([10, 11, 12], $payments->pluck('source_payment_id')->all());
$this->assertSame(['2026-07', '2026-07', '2026-07'], $payments->pluck('bill_period')->all());
$this->assertSame(205528.23, round((float) $payments->sum('amount'), 2));
}
private function row(int $id, string $billNumber, float $amount, int $payDate): array
{
return [
'id' => $id,
'billNumber' => $billNumber,
'billPayAmount' => $amount,
'payDate' => $payDate,
'batchNumber' => 'BATCH-' . $id,
'vendorCode' => '672378',
'vendorAccountNum' => '1105055609100026936',
'currencyCode' => 'CNY',
'payStatus' => 'SUCCESS',
];
}
}