CaiWuXiTong/tests/Unit/EcommerceFinance/CostFreightAnomalyRepositoryTest.php
renshen a9c43271d0 fix: 拆分成本与运费异常标记 (#1002)
合并 PR #1002:成本与运费按 0/空分别标记并独立监控。
2026-11-09 10:22:36 +08:00

93 lines
4.2 KiB
PHP

<?php
namespace Tests\Unit\EcommerceFinance;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Modules\EcommerceFinance\Repositories\CostFreightAnomalyRepository;
use Tests\TestCase;
class CostFreightAnomalyRepositoryTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();
config([
'database.connections.business_mirror' => [
'driver' => 'sqlite', 'database' => ':memory:', 'prefix' => '',
],
'upstream_mirrors.connection' => 'business_mirror',
]);
DB::purge('business_mirror');
Schema::connection('business_mirror')->create('ops_shengsuan_order_profit_rows', function (Blueprint $table): void {
$table->bigIncrements('id');
$table->string('shop_name');
$table->string('online_order_no')->nullable();
$table->string('internal_order_no')->nullable();
$table->string('order_key');
$table->dateTime('ship_time')->nullable();
$table->decimal('product_cost', 18, 2)->nullable();
$table->decimal('matched_package_fee', 18, 2)->nullable();
$table->decimal('matched_freight_fee', 18, 2)->nullable();
$table->decimal('matched_bill_fee', 18, 2)->nullable();
$table->dateTime('synced_at')->nullable();
$table->timestamps();
});
$db = DB::connection('business_mirror');
foreach ([
['shop_name' => '异常店', 'online_order_no' => 'NO-1', 'order_key' => '异常店|NO-1', 'ship_time' => '2026-11-05 10:00:00', 'product_cost' => 0, 'matched_freight_fee' => 0],
['shop_name' => '运费异常店', 'online_order_no' => 'NO-2', 'order_key' => '运费异常店|NO-2', 'ship_time' => '2026-11-05 11:00:00', 'product_cost' => 100, 'matched_freight_fee' => 0],
['shop_name' => '成本异常店', 'online_order_no' => 'NO-3', 'order_key' => '成本异常店|NO-3', 'ship_time' => '2026-11-06 11:00:00', 'product_cost' => 0, 'matched_freight_fee' => 7],
['shop_name' => '正常店', 'online_order_no' => 'NO-4', 'order_key' => '正常店|NO-4', 'ship_time' => '2026-11-06 12:00:00', 'product_cost' => 100, 'matched_freight_fee' => 7],
['shop_name' => '成本缺失店', 'online_order_no' => 'NO-5', 'order_key' => '成本缺失店|NO-5', 'ship_time' => '2026-11-06 13:00:00', 'product_cost' => null, 'matched_freight_fee' => 9],
] as $row) {
$db->table('ops_shengsuan_order_profit_rows')->insert($row + [
'internal_order_no' => null,
'matched_package_fee' => 0,
'matched_bill_fee' => 0,
'synced_at' => '2026-11-07 10:00:00',
'created_at' => now(),
'updated_at' => now(),
]);
}
}
public function test_orders_with_either_cost_or_freight_missing_are_classified_independently(): void
{
$result = app(CostFreightAnomalyRepository::class)->paginate([
'from' => '2026-11-01',
'to' => '2026-11-07',
'order_no' => '',
'shop_names' => [],
], 1, 25);
$rows = array_column($result['rows'], null, 'online_order_no');
$this->assertSame(4, $result['total']);
$this->assertSame(['成本异常', '运费异常'], $rows['NO-1']['anomaly_types']);
$this->assertSame('成本异常、运费异常', $rows['NO-1']['anomaly_type']);
$this->assertSame(['运费异常'], $rows['NO-2']['anomaly_types']);
$this->assertSame(['成本异常'], $rows['NO-3']['anomaly_types']);
$this->assertSame(['成本异常'], $rows['NO-5']['anomaly_types']);
$this->assertArrayNotHasKey('NO-4', $rows);
}
public function test_order_and_shop_batch_filters_are_applied(): void
{
$repository = app(CostFreightAnomalyRepository::class);
$result = $repository->paginate([
'from' => '2026-11-01',
'to' => '2026-11-07',
'order_no' => 'NO-1',
'shop_names' => ['异常店'],
], 1, 25);
$this->assertSame(1, $result['total']);
$this->assertSame('异常店', $result['rows'][0]['shop_name']);
}
}