CaiWuXiTong/tests/Unit/EcommerceFinance/CostFreightAnomalyRepositoryTest.php
renshen 1b6ff0bb75 feat: 新增成本运费异常监控 (#992)
成本/运费异常页监控商品成本和快递费同时缺失的订单。
2026-11-07 18:04:44 +08:00

85 lines
3.4 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],
] 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_only_orders_with_both_cost_and_freight_missing_are_returned(): void
{
$result = app(CostFreightAnomalyRepository::class)->paginate([
'from' => '2026-11-01',
'to' => '2026-11-07',
'order_no' => '',
'shop_names' => [],
], 1, 25);
$this->assertSame(1, $result['total']);
$this->assertSame('NO-1', $result['rows'][0]['online_order_no']);
$this->assertSame('商品成本及快递费缺失', $result['rows'][0]['anomaly_type']);
}
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']);
}
}