CaiWuXiTong/tests/Feature/Business/DewuReceivingBillsTest.php

145 lines
6.0 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' => '得物',
'source_channel' => 'Dewu',
],
], [
'id' => '14654481',
'record_code' => '2000000001248775180',
'store_name' => '得物-测试店',
'income' => 0.0,
'expense' => 18.07,
'payload' => ['channel' => '退货退款订单', 'source_channel' => 'DewuTK'],
], [
'id' => '14654482',
'record_code' => '2000000001248775181',
'store_name' => '得物-测试店',
'income' => 0.0,
'expense' => 3.50,
'payload' => ['channel' => '扣减其他费用明细', 'source_channel' => 'DewuKJ'],
]],
'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', '得物')
->assertJsonPath('data.0.payload.source_channel', 'Dewu')
->assertJsonPath('data.1.payload.channel', '退货退款订单')
->assertJsonPath('data.1.payload.source_channel', 'DewuTK')
->assertJsonPath('data.2.payload.channel', '扣减其他费用明细')
->assertJsonPath('data.2.payload.source_channel', 'DewuKJ')
->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' => '退货退款订单', 'source_channel' => 'DewuTK'],
]]);
$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,payload.source_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('退货退款订单,DewuTK,"=""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()
);
}
}