CaiWuXiTong/tests/Unit/EcommerceFinance/ShippingReplicaSynchronizerTest.php

108 lines
4.4 KiB
PHP

<?php
namespace Tests\Unit\EcommerceFinance;
use Illuminate\Support\Facades\DB;
use Modules\EcommerceFinance\Services\Jushuitan\ShippingReplicaSynchronizer;
use Modules\EcommerceFinance\Services\UpstreamReadOnlyQuery;
use Modules\EcommerceFinance\Services\UpstreamReadOnlyQueryFactory;
use Tests\TestCase;
class ShippingReplicaSynchronizerTest extends TestCase
{
public function test_windowed_outbound_lookup_preloads_date_range_and_reuses_cache_without_in_queries(): void
{
$from = '2026-09-22 00:00:00';
$to = '2026-09-25 00:00:00';
$upstream = $this->createMock(UpstreamReadOnlyQuery::class);
$upstream->expects($this->once())
->method('select')
->with(
$this->callback(fn (string $sql): bool => str_contains($sql, 'WHERE io_date >= ? AND io_date < ?')
&& ! str_contains($sql, 'l_id IN')
&& ! str_contains($sql, 'o_id IN')),
[$from, $to],
)
->willReturn([
['l_id' => 'logistic-1', 'o_id' => 'order-1', 'io_id' => 'outbound-1'],
['l_id' => 'logistic-2', 'o_id' => 'order-2', 'io_id' => 'outbound-2'],
]);
$factory = $this->createMock(UpstreamReadOnlyQueryFactory::class);
$factory->method('forSource')->with('operations_replica')->willReturn($upstream);
$synchronizer = new ShippingReplicaSynchronizer($factory);
$preload = new \ReflectionMethod($synchronizer, 'preloadOutboundMap');
$preload->setAccessible(true);
$preload->invoke($synchronizer, $from, $to);
$lookup = new \ReflectionMethod($synchronizer, 'ioMapFor');
$lookup->setAccessible(true);
$map = $lookup->invoke($synchronizer, true, ['logistic-1'], ['order-2']);
$this->assertSame('outbound-1', $map['byL']['logistic-1']);
$this->assertSame('outbound-2', $map['byO']['order-2']);
}
public function test_windowed_item_lookup_skips_unindexed_raw_so_id_fallback(): void
{
$upstream = $this->createMock(UpstreamReadOnlyQuery::class);
$upstream->expects($this->once())
->method('select')
->with(
$this->stringContains('WHERE o_id IN'),
['order-1'],
)
->willReturn([
[
'o_id' => 'order-1',
'raw_so_id' => 'trade-1',
'sku_id' => 'sku-1',
'i_id' => 'item-1',
'shop_i_id' => 'shop-item-1',
'name' => '商品',
'qty' => 1,
'amount' => 10,
'send_warehouse' => '仓库',
'Id' => 1,
],
]);
$factory = $this->createMock(UpstreamReadOnlyQueryFactory::class);
$factory->method('forSource')->with('operations_replica')->willReturn($upstream);
$synchronizer = new ShippingReplicaSynchronizer($factory);
$method = new \ReflectionMethod($synchronizer, 'loadAllItems');
$method->setAccessible(true);
[$byOrder, $byTrade] = $method->invoke($synchronizer, ['order-1'], ['trade-1'], false);
$this->assertArrayHasKey('order-1', $byOrder);
$this->assertSame([], $byTrade);
}
public function test_sync_removes_only_the_known_demo_source_ids(): void
{
$db = DB::connection('business_mirror');
$db->table('jst_oms_shipping')->insert([
['source_id' => 9000001, 'sku_source_id' => 9000001],
['source_id' => 9000006, 'sku_source_id' => 9000006],
['source_id' => 9000007, 'sku_source_id' => 9000007],
['source_id' => 999999999, 'sku_source_id' => 999999999],
]);
$upstream = $this->createMock(UpstreamReadOnlyQuery::class);
$upstream->expects($this->once())->method('select')->willReturn([]);
$factory = $this->createMock(UpstreamReadOnlyQueryFactory::class);
$factory->expects($this->once())
->method('forSource')
->with('operations_replica')
->willReturn($upstream);
$result = (new ShippingReplicaSynchronizer($factory))->sync();
$this->assertSame(['processed' => 0, 'autoship' => 0], $result);
$this->assertSame(
[9000007, 999999999],
$db->table('jst_oms_shipping')->orderBy('source_id')->pluck('source_id')->map(fn ($id) => (int) $id)->all(),
);
}
}