CaiWuXiTong/tests/Unit/EcommerceFinance/AlignShippingWeightsCommandTest.php

97 lines
5.8 KiB
PHP

<?php
namespace Tests\Unit\EcommerceFinance;
use Illuminate\Support\Facades\DB;
use Modules\EcommerceFinance\Services\UpstreamReadOnlyQuery;
use Modules\EcommerceFinance\Services\UpstreamReadOnlyQueryFactory;
use Modules\EcommerceFinance\Services\Freight\ShippingFreightService;
use Modules\EcommerceFinance\Services\Jushuitan\ShippingReplicaSynchronizer;
use Tests\TestCase;
class AlignShippingWeightsCommandTest extends TestCase
{
public function test_aligns_weights_month_by_month_and_recalculates_freight_without_scanning_express_details(): void
{
$db = DB::connection('business_mirror');
$db->table('jst_oms_shipping')->insert([
// 旧数据:实称记成了逐件重量
['source_id' => 7001, 'sku_source_id' => 1, 'ship_time' => '2026-10-31 18:00:00', 'express_no' => 'W1001', 'express_name1' => '新北中通', 'online_weight' => 1.2, 'actual_weight' => 0.6],
['source_id' => 7001, 'sku_source_id' => 2, 'ship_time' => '2026-10-31 18:00:00', 'express_no' => 'W1001', 'express_name1' => '新北中通', 'online_weight' => 1.2, 'actual_weight' => 0.6],
['source_id' => 7002, 'sku_source_id' => 1, 'ship_time' => '2026-11-02 09:00:00', 'express_no' => 'W1002', 'express_name1' => '新北中通', 'online_weight' => 272000, 'actual_weight' => null],
]);
$sqls = [];
$upstream = $this->createMock(UpstreamReadOnlyQuery::class);
$upstream->method('select')->willReturnCallback(function (string $sql, array $bindings) use (&$sqls): array {
$sqls[] = $sql;
if (! str_contains($sql, 'so.io_date >= ?')) {
return [];
}
$rows = [
['io_id' => 1, 'l_id' => 'W1001', 'io_date' => '2026-10-31 18:00:00', 'status' => '已出库', 'weight' => 2.4, 'f_weight' => 2.31],
['io_id' => 2, 'l_id' => 'W1002', 'io_date' => '2026-11-02 09:00:00', 'status' => '已出库', 'weight' => 2720, 'f_weight' => null],
];
return array_values(array_filter($rows, static fn (array $r): bool => $r['io_date'] >= $bindings[0] && $r['io_date'] < $bindings[1]));
});
$factory = $this->createMock(UpstreamReadOnlyQueryFactory::class);
$factory->method('forSource')->willReturn($upstream);
$this->app->instance(UpstreamReadOnlyQueryFactory::class, $factory);
$this->artisan('jushuitan:align-weights', ['--from' => '2026-10-01', '--to' => '2026-12-01'])
->expectsOutputToContain('2026-10-01 ~ 2026-11-01 按销售出库单校正')
->expectsOutputToContain('2026-11-01 ~ 2026-12-01 重算运费')
->assertSuccessful();
$w = fn (int $sid, int $sku) => $db->table('jst_oms_shipping')->where('source_id', $sid)->where('sku_source_id', $sku)->first(['online_weight', 'actual_weight']);
$this->assertEquals([2.4, 2.31], [(float) $w(7001, 1)->online_weight, (float) $w(7001, 1)->actual_weight]);
$this->assertNull($w(7001, 2)->online_weight); // 整单重量只记在包裹首行
$this->assertEquals(2720, (float) $w(7002, 1)->online_weight);
$this->assertSame([], array_filter($sqls, static fn (string $s): bool => str_contains($s, 'jst_order_express_details') || str_contains($s, 'jst_Orders')));
$this->assertSame(2, $db->table('jst_oms_shipping_freight')->whereIn('source_id', [7001, 7002])->count()); // 运费已重算(有核算结果)
}
public function test_rejects_bad_dates(): void
{
$this->artisan('jushuitan:align-weights', ['--from' => '2026-12-01', '--to' => '2026-11-01'])->assertFailed();
$this->artisan('jushuitan:align-weights', ['--from' => '2026-02-30', '--to' => '2026-12-01'])->assertFailed();
}
public function test_month_failure_is_reported_and_later_months_continue_through_partial_end_date(): void
{
$sync = $this->createMock(ShippingReplicaSynchronizer::class);
$windows = [];
$sync->expects($this->exactly(3))->method('alignWithOutbound')->willReturnCallback(function ($from, $to) use (&$windows): array {
$windows[] = [$from, $to];
if ($from === '2026-10-15 00:00:00') throw new \RuntimeException('测试上游超时');
return ['ship_time' => 0, 'weight' => 1];
});
$freight = $this->createMock(ShippingFreightService::class);
$freight->expects($this->exactly(2))->method('recalculate')->willReturn(['packages' => 1, 'ok' => 1]);
$this->app->instance(ShippingReplicaSynchronizer::class, $sync);
$this->app->instance(ShippingFreightService::class, $freight);
$this->artisan('jushuitan:align-weights', ['--from' => '2026-10-15', '--to' => '2026-12-02'])
->expectsOutputToContain('2026-10-15 ~ 2026-11-01 失败:测试上游超时')
->expectsOutputToContain('2026-12-01 ~ 2026-12-02 重算运费')
->assertFailed();
$this->assertSame([
['2026-10-15 00:00:00', '2026-11-01 00:00:00'],
['2026-11-01 00:00:00', '2026-12-01 00:00:00'],
['2026-12-01 00:00:00', '2026-12-02 00:00:00'],
], $windows);
}
public function test_skip_freight_only_aligns_weights(): void
{
$sync = $this->createMock(ShippingReplicaSynchronizer::class);
$sync->expects($this->once())->method('alignWithOutbound')->willReturn(['ship_time' => 0, 'weight' => 1]);
$freight = $this->createMock(ShippingFreightService::class);
$freight->expects($this->never())->method('recalculate');
$this->app->instance(ShippingReplicaSynchronizer::class, $sync);
$this->app->instance(ShippingFreightService::class, $freight);
$this->artisan('jushuitan:align-weights', ['--from' => '2026-11-01', '--to' => '2026-11-02', '--skip-freight' => true])
->assertSuccessful();
}
}