CaiWuXiTong/tests/Feature/Business/DewuReceivingBillsTest.php
yueyang edb32c33cf 接入得物收款账单镜像 (#185)
固定同步 Dewu 主收款渠道,接入得物账户分页、筛选、汇总和导出,并补充镜像与回归测试。

未部署,未执行生产首次同步。
2026-09-15 11:09:03 +08:00

125 lines
4.8 KiB
PHP

<?php
namespace Tests\Feature\Business;
use App\Models\Auth\Permission;
use Mockery;
use Modules\EcommerceFinance\Repositories\StoreReceivingDewuBillMirrorRepository;
use RuntimeException;
use Tests\Feature\FeatureTestCase;
class DewuReceivingBillsTest extends FeatureTestCase
{
public function test_endpoint_reads_dewu_receiving_mirror_with_all_filters(): void
{
$this->allowPage();
$filters = [
'store_name' => '得物-测试店',
'bill_item' => '交易收入',
'start' => '2026-08-01',
'end' => '2026-08-31',
'keyword' => '110211',
];
$repository = Mockery::mock(StoreReceivingDewuBillMirrorRepository::class);
$repository->shouldReceive('paginate')->once()->with(
$filters, 'occurred_at', 'desc', 2, 20,
)->andReturn([
'current_page' => 2,
'last_page' => 3,
'per_page' => 20,
'total' => 41,
'data' => [[
'id' => '14654480',
'record_code' => '2000000001248775179',
'store_name' => '得物-测试店',
'income' => 21.0,
'expense' => 0.0,
'payload' => [
'online_order_no' => '110211311661075313',
'channel' => 'Dewu',
],
]],
'summary' => ['income' => 21.0, 'expense' => 0.0, 'balance' => 21.0, 'count' => 41],
]);
$this->app->instance(StoreReceivingDewuBillMirrorRepository::class, $repository);
$this->withExceptionHandling()
->loginAs()
->getJson(route('ecommerce-finance.account-bills.index', [
'bill_type' => 'dewu',
...$filters,
'page' => 2,
'limit' => 20,
]))
->assertOk()
->assertJsonPath('data.0.payload.channel', 'Dewu')
->assertJsonPath('columns.title', '得物账户')
->assertJsonPath('summary.count', 41);
}
public function test_filter_options_and_selected_column_export_use_the_same_mirror(): void
{
$this->allowPage();
$repository = Mockery::mock(StoreReceivingDewuBillMirrorRepository::class);
$repository->shouldReceive('filterOptions')->once()->andReturn([
'shops' => ['得物-A店', '得物-B店'],
'bill_items' => ['交易收入', '平台服务费'],
]);
$repository->shouldReceive('exportRows')->once()->with(
['store_name' => '得物-A店'], 'record_code', 'asc',
)->andReturn([[
'record_code' => '2000000001248775179',
'store_name' => '得物-A店',
'payload' => ['channel' => 'Dewu'],
]]);
$this->app->instance(StoreReceivingDewuBillMirrorRepository::class, $repository);
$this->loginAs()
->withHeader('X-Requested-With', 'XMLHttpRequest')
->get(route('ecommerce-finance.account-bills.filter-options', ['bill_type' => 'dewu']))
->assertOk()
->assertExactJson([
'shops' => ['得物-A店', '得物-B店'],
'bill_items' => ['交易收入', '平台服务费'],
]);
$response = $this->loginAs()->get(route('ecommerce-finance.account-bills.export', [
'bill_type' => 'dewu',
'store_name' => '得物-A店',
'sort' => 'record_code',
'dir' => 'asc',
'columns' => 'payload.channel,record_code,store_name',
]));
$response->assertOk()
->assertHeader('content-type', 'text/csv; charset=UTF-8')
->assertDownload('dewu_account_bills.csv');
$csv = $response->streamedContent();
$this->assertStringContainsString('渠道,记录编码,店铺', $csv);
$this->assertStringContainsString('Dewu,"=""2000000001248775179""",得物-A店', $csv);
}
public function test_unready_mirror_returns_a_clear_retryable_response(): void
{
$this->allowPage();
$repository = Mockery::mock(StoreReceivingDewuBillMirrorRepository::class);
$repository->shouldReceive('paginate')->once()->andThrow(
new RuntimeException('store_receiving_alipay_bill_mirror_not_ready')
);
$this->app->instance(StoreReceivingDewuBillMirrorRepository::class, $repository);
$this->loginAs()
->withHeader('X-Requested-With', 'XMLHttpRequest')
->get(route('ecommerce-finance.account-bills.index', ['bill_type' => 'dewu']))
->assertStatus(503)
->assertJsonPath('message', '得物账户账单共享业务镜像正在首次同步,完成后将自动显示。');
}
private function allowPage(): void
{
$this->user->attachPermission(
Permission::where('name', 'read-admin-panel')->firstOrFail()
);
}
}