CaiWuXiTong/tests/Feature/Console/BillImportPerformanceResyncTest.php
qiuxiang 37b9a17b65 修复原始账单更新后自动重算业绩明细
京东、京喜、抖音原始账单导入后自动排队重算业绩明细;补齐 10 月最新数据。
2026-11-26 11:53:47 +08:00

165 lines
6.3 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace Tests\Feature\Console;
use Mockery;
use Modules\EcommerceFinance\Services\BillPerformanceResyncDispatcher;
use Modules\EcommerceFinance\Services\DewuSettlementBillImporter;
use Modules\EcommerceFinance\Services\DouyinAuthoritativeBillImporter;
use Modules\EcommerceFinance\Services\JdDailyBillImporter;
use Modules\EcommerceFinance\Services\PinduoduoSettlementBillImporter;
use Modules\EcommerceFinance\Services\TaobaoAlipaySettlementBillImporter;
use Modules\EcommerceFinance\Services\TaobaoWechatSettlementBillImporter;
use PHPUnit\Framework\Attributes\DataProvider;
use Tests\TestCase;
class BillImportPerformanceResyncTest extends TestCase
{
public function test_jd_daily_import_queues_both_receipt_accounts_for_the_closed_month(): void
{
$importer = Mockery::mock(JdDailyBillImporter::class);
$importer->shouldReceive('import')
->once()
->with('jd.csv', '2026-10', 321, '100.00', '20.00', '2026-10-31')
->andReturn([
'row_count' => 321,
'period_start' => '2026-10-01',
'period_end' => '2026-11-01',
]);
$resync = Mockery::mock(BillPerformanceResyncDispatcher::class);
foreach (['jd-transactions', 'jingxi'] as $platform) {
$resync->shouldReceive('dispatch')
->once()
->with($platform, '2026-10-01', '2026-10-31')
->andReturn(['jobs' => 1, 'months' => ['2026-10']]);
}
$this->app->instance(JdDailyBillImporter::class, $importer);
$this->app->instance(BillPerformanceResyncDispatcher::class, $resync);
$this->artisan('business-mirror:import-jd-daily', [
'path' => 'jd.csv',
'month' => '2026-10',
'--through' => '2026-10-31',
'--count' => 321,
'--income' => '100.00',
'--expense' => '20.00',
])->expectsOutputToContain('业绩明细重算已排队:2026-10(新增 1 个任务)')
->assertSuccessful();
}
public function test_douyin_import_queues_the_closed_month_without_enqueueing_next_month(): void
{
$importer = Mockery::mock(DouyinAuthoritativeBillImporter::class);
$importer->shouldReceive('import')
->once()
->with('douyin.csv', '2026-10', 10, '100.00', '20.00', '2026-10-31')
->andReturn([
'row_count' => 10,
'period_start' => '2026-10-01',
'period_end' => '2026-11-01',
]);
$resync = Mockery::mock(BillPerformanceResyncDispatcher::class);
$resync->shouldReceive('dispatch')
->once()
->with('douyin', '2026-10-01', '2026-10-31')
->andReturn(['jobs' => 1, 'months' => ['2026-10']]);
$this->app->instance(DouyinAuthoritativeBillImporter::class, $importer);
$this->app->instance(BillPerformanceResyncDispatcher::class, $resync);
$this->artisan('business-mirror:import-douyin-authoritative', [
'path' => 'douyin.csv',
'month' => '2026-10',
'--through' => '2026-10-31',
'--count' => 10,
'--income' => '100.00',
'--expense' => '20.00',
])->expectsOutputToContain('业绩明细重算已排队:2026-10(新增 1 个任务)')
->assertSuccessful();
}
/** @return iterable<string,array{class-string,string,array<string,int>}> */
public static function importCommands(): iterable
{
yield 'taobao alipay' => [
TaobaoAlipaySettlementBillImporter::class,
'taobao-alipay',
['business_type_counts' => []],
];
yield 'taobao wechat' => [
TaobaoWechatSettlementBillImporter::class,
'taobao-wechat',
['entry_type_counts' => []],
];
yield 'pinduoduo' => [
PinduoduoSettlementBillImporter::class,
'pinduoduo',
['account_type_counts' => []],
];
yield 'dewu' => [
DewuSettlementBillImporter::class,
'dewu',
['channel_counts' => []],
];
}
#[DataProvider('importCommands')]
public function test_authoritative_import_queues_the_affected_performance_month(
string $importerClass,
string $platform,
array $extraResult,
): void {
$result = array_replace([
'status' => 'succeeded',
'row_count' => 10,
'excluded_rows' => 0,
'excluded_account_rows' => 0,
'store_count' => 2,
'bill_item_count' => 3,
'income_total' => '100.00',
'expense_total' => '20.00',
'net_total' => '80.00',
'period_start' => '2026-10-29',
'period_end' => '2026-11-02',
'source_sha256' => str_repeat('a', 64),
], $extraResult);
$importer = Mockery::mock($importerClass);
$importer->shouldReceive('import')
->once()
->with('bill.xlsx', '2026-10-29', '2026-11-02', 10, '100.00', '20.00', 1000)
->andReturn($result);
$resync = Mockery::mock(BillPerformanceResyncDispatcher::class);
$resync->shouldReceive('dispatch')
->once()
->with($platform, '2026-10-29', '2026-11-02')
->andReturn(['jobs' => 2, 'months' => ['2026-10', '2026-11']]);
$this->app->instance($importerClass, $importer);
$this->app->instance(BillPerformanceResyncDispatcher::class, $resync);
$command = [
TaobaoAlipaySettlementBillImporter::class => 'taobao-alipay-bills:import-authoritative',
TaobaoWechatSettlementBillImporter::class => 'taobao-wechat-bills:import-authoritative',
PinduoduoSettlementBillImporter::class => 'pinduoduo-bills:import-authoritative',
DewuSettlementBillImporter::class => 'dewu-bills:import-authoritative',
][$importerClass];
$this->artisan($command, [
'path' => 'bill.xlsx',
'--from' => '2026-10-29',
'--to' => '2026-11-02',
'--expected-count' => 10,
'--expected-income' => '100.00',
'--expected-expense' => '20.00',
'--batch' => 1000,
])
->expectsOutputToContain('业绩明细重算已排队:2026-10、2026-11(新增 2 个任务)')
->assertSuccessful();
}
}