CaiWuXiTong/tests/Unit/Business/StoreViolationMapperTest.php
苏木 8e93e71024 店铺违规预警:新增违规原因与处置方式两列(页面/统一表/导入导出)
- 页面在「违规分类」后新增「违规原因」「处置方式」,统一表头、导出、模板、导入同步支持
- 新迁移:统一表 store_violation_records 增加 violation_reason、disposition 两列
- 同步命令:上游视图补齐 violation_reason/disposition(或 reason/handling_method)后自动参与同步;未提供时不触碰这两列,导入补充的数据不会被每日同步清空
- 导入:违规原因/处置方式独立映射;仅提供违规原因时,违规分类按原因关键词自动归类
- 测试:StoreViolation*、NavigationTest 共 23 项通过(含新导入用例与列探测用例)
2026-10-13 11:31:52 +08:00

108 lines
4.5 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace Tests\Unit\Business;
use App\Business\StoreViolationMapper;
use InvalidArgumentException;
use Tests\TestCase;
class StoreViolationMapperTest extends TestCase
{
private StoreViolationMapper $mapper;
protected function setUp(): void
{
parent::setUp();
$this->mapper = new StoreViolationMapper();
}
public function test_platform_mapping_accepts_source_values_and_slugs(): void
{
$this->assertSame('jd', $this->mapper->platformForSourceValue('京东'));
$this->assertSame('taobao', $this->mapper->platformForSourceValue('天猫'));
$this->assertSame('jingxi', $this->mapper->platformForSourceValue(' JINGXI '));
$this->assertNull($this->mapper->platformForSourceValue('未知平台'));
$this->assertNull($this->mapper->platformForSourceValue(null));
}
public function test_category_mapping_uses_keyword_rules(): void
{
$this->assertSame('发货履约', $this->mapper->categoryForContent('延迟发货/缺货无货'));
$this->assertSame('交易违规', $this->mapper->categoryForContent('虚假交易(商品降权)'));
$this->assertSame('发票与考核', $this->mapper->categoryForContent('您的政企订单发票及时上传率未达到90%的考核标准'));
$this->assertSame('其他', $this->mapper->categoryForContent('未收录的违规内容'));
$this->assertSame('其他', $this->mapper->categoryForContent(null));
}
public function test_map_crawl_row_returns_unified_fields(): void
{
$mapped = $this->mapper->mapCrawlRow([
'id' => 1,
'shop_name' => '京东-亿家达全屋旗舰店',
'violation_no' => '86799651',
'violation_date' => '2026-10-04',
'deadline' => '2026-10-11',
'violation_content' => '延迟发货/缺货无货',
'status' => '待处理',
'window_name' => '京东-亿家达全屋旗舰店_商家后台',
'platform' => '京东',
'crawl_date' => '2026-10-04',
]);
$this->assertSame('jd', $mapped['platform']);
$this->assertSame('发货履约', $mapped['violation_category']);
$this->assertSame('86799651', $mapped['violation_no']);
$this->assertSame('2026-10-11', $mapped['appeal_deadline']);
$this->assertSame('crawl', $mapped['source']);
}
public function test_date_and_points_normalization(): void
{
$this->assertSame('2026-10-04', $this->mapper->normalizeDate('2026-10-04'));
$this->assertSame('2026-10-04', $this->mapper->normalizeDate('2026/10/4'));
$this->assertSame('2026-10-04', $this->mapper->normalizeDate('2026年10月4日'));
$this->assertSame('2020-09-15', $this->mapper->normalizeDate(44089));
$this->assertNull($this->mapper->normalizeDate(null));
$this->assertNull($this->mapper->normalizeDate('-'));
$this->assertSame(2.0, $this->mapper->normalizePoints('2分'));
$this->assertSame(0.5, $this->mapper->normalizePoints('0.5'));
$this->assertNull($this->mapper->normalizePoints('不扣分'));
}
public function test_map_crawl_row_includes_reason_and_disposition_when_upstream_provides_them(): void
{
$base = [
'shop_name' => '京喜-久伴吾家具京喜自营工厂店',
'violation_no' => '86522327',
'violation_date' => '2026-09-30',
'deadline' => '2026-10-07',
'violation_content' => '订单存在揽收超时行为',
'status' => '待处理',
'window_name' => '京喜-久伴吾家具京喜自营工厂店_商家后台',
'platform' => '京喜',
'crawl_date' => '2026-10-04',
];
$mapped = $this->mapper->mapCrawlRow($base + [
'violation_reason' => '订单存在揽收超时行为',
'disposition' => '警告;扣2分',
]);
$this->assertSame('订单存在揽收超时行为', $mapped['violation_reason']);
$this->assertSame('警告;扣2分', $mapped['disposition']);
// 上游尚未提供两列时保持 null,避免同步误覆盖导入补充的数据。
$legacy = $this->mapper->mapCrawlRow($base);
$this->assertNull($legacy['violation_reason']);
$this->assertNull($legacy['disposition']);
}
public function test_unrecognized_points_throw(): void
{
$this->expectException(InvalidArgumentException::class);
$this->mapper->normalizePoints('扣分若干');
}
}