71 lines
4.2 KiB
PHP
71 lines
4.2 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Business;
|
|
|
|
use App\Support\UnshippedSourceSynchronizer;
|
|
use App\Support\UnshippedWaybills;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Database\Schema\Blueprint;
|
|
use Illuminate\Support\Facades\Schema;
|
|
use Modules\EcommerceFinance\Services\ReturnedParcelSynchronizer;
|
|
use RuntimeException;
|
|
use Tests\Feature\FeatureTestCase;
|
|
|
|
class UnshippedSourceSynchronizerTest extends FeatureTestCase
|
|
{
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
$schema = Schema::connection('business_mirror');
|
|
$schema->create('business_mirror_table_states', function (Blueprint $table): void {
|
|
$table->string('dataset_key')->primary();
|
|
foreach (['source_key','source_object','target_table','status','run_mode','generation','last_error_code'] as $key) $table->string($key)->nullable();
|
|
$table->text('cursor')->nullable();
|
|
foreach (['window_started_at','window_ends_at','initial_completed_at','last_full_succeeded_at','last_attempted_at','last_succeeded_at'] as $key) $table->dateTime($key)->nullable();
|
|
$table->integer('last_upserted_rows')->default(0);
|
|
$table->integer('mirror_row_count')->default(0);
|
|
$table->timestamps();
|
|
});
|
|
(require base_path('database/migrations/2026_12_06_070100_add_returned_parcel_source.php'))->up();
|
|
}
|
|
|
|
public function test_warehouse_reads_current_source_and_only_publishes_complete_run(): void
|
|
{
|
|
$service=app(UnshippedWaybills::class);$sync=new FakeUnshippedSources($service);
|
|
$result=$sync->warehouse(1);
|
|
$this->assertSame('running',$result['status']);
|
|
$this->assertSame(0,$service->results()->count());
|
|
$result=$sync->warehouse();
|
|
$this->assertSame('succeeded',$result['status']);
|
|
$this->assertSame(['W1'], $service->results()->pluck('waybill_no')->all());
|
|
$this->assertSame(2, $result['scanned']);
|
|
}
|
|
|
|
public function test_intercept_uses_both_sources_and_original_order_waybill(): void
|
|
{
|
|
$service=app(UnshippedWaybills::class);$db=$service->connection();$sync=new FakeUnshippedSources($service);
|
|
$db->table('business_mirror_table_states')->insert(['dataset_key'=>ReturnedParcelSynchronizer::DATASET,'source_key'=>'operations_replica',
|
|
'source_object'=>'test','target_table'=>ReturnedParcelSynchronizer::TABLE,'status'=>'succeeded','last_succeeded_at'=>now(),
|
|
'window_started_at'=>'2026-09-01','created_at'=>now(),'updated_at'=>now()]);
|
|
foreach(['regular','taobao'] as $kind)$db->table(ReturnedParcelSynchronizer::TABLE)->insert([
|
|
'source_kind'=>$kind,'source_id'=>1,'after_sales_id'=>10,'order_id'=>'100','online_order_no'=>'TRADE',
|
|
'waybill'=>'RETURN','received_at'=>'2026-11-28','shop'=>'测试店','generation'=>'one','synced_at'=>now(),
|
|
'reason'=>'现场拦截入库签收']);
|
|
$this->assertSame(1,$sync->intercept('intercept_regular'));
|
|
$this->assertSame(1,$sync->intercept('intercept_taobao'));
|
|
$this->assertSame(['ORIGINAL'=>['现场拦截入库签收']],$service->unshippedSources(['ORIGINAL','RETURN']));
|
|
$db->table('business_mirror_table_states')->update(['status'=>'failed']);
|
|
$this->expectException(RuntimeException::class);$sync->intercept('intercept_regular');
|
|
}
|
|
}
|
|
|
|
class FakeUnshippedSources extends UnshippedSourceSynchronizer
|
|
{
|
|
public function __construct(UnshippedWaybills $waybills){parent::__construct(app(\Modules\EcommerceFinance\Services\UpstreamReadOnlyQueryFactory::class),$waybills);}
|
|
protected function warehouseHighId(): int{return 2;}
|
|
protected function warehouseKeys(int $last,int $high,int $limit): array{return $last<2?[['Id'=>$last+1,'woTypeId'=>UnshippedWaybills::WAREHOUSE_WORK_TYPE]]:[];}
|
|
protected function warehouseDetails(array $ids): array{return [['Id'=>$ids[0],'woNo'=>'GD'.$ids[0],'created'=>'2026-11-28','title'=>'测试工单',
|
|
'status'=>$ids[0]===1?'WAIT_AUDIT':'REJECT_AUDIT','businessFieldList'=>json_encode([['fieldId'=>'lIdField','value'=>'W'.$ids[0]]])]];}
|
|
protected function originalOrders(string $kind,array $ids): array{return [['Id'=>1,'o_id'=>'100','l_id'=>'ORIGINAL','logistics_company'=>'测试快递']];}
|
|
}
|