CaiWuXiTong/tests/Unit/EcommerceFinance/ShopInfoFlowMirrorRepositoryTest.php

212 lines
11 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\EcommerceFinance;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Modules\EcommerceFinance\Repositories\GenericBusinessMirrorRepository;
use Modules\EcommerceFinance\Repositories\ShopInfoFlowMirrorRepository;
use Modules\EcommerceFinance\Services\BusinessMirrorDatasetRegistry;
use Tests\TestCase;
class ShopInfoFlowMirrorRepositoryTest extends TestCase
{
private ShopInfoFlowMirrorRepository $repository;
protected function setUp(): void
{
parent::setUp();
config([
'database.connections.business_mirror' => [
'driver' => 'sqlite', 'database' => ':memory:', 'prefix' => '',
],
'upstream_mirrors.connection' => 'business_mirror',
'upstream_mirrors.datasets' => [
ShopInfoFlowMirrorRepository::FORM_DATASET => [
'source_key' => 'jiansun', 'schema' => 'dbo', 'object' => 'fys_shopInfo_flow',
'target' => 'shop_info_flow_form', 'keys' => ['Id'],
],
ShopInfoFlowMirrorRepository::FLOW_DATASET => [
'source_key' => 'jiansun', 'schema' => 'dbo', 'object' => 'flow_flow',
'target' => 'oa_flow_flow', 'keys' => ['Id'],
],
],
]);
DB::purge('business_mirror');
$schema = Schema::connection('business_mirror');
$schema->create('business_mirror_table_states', function (Blueprint $table): void {
$table->string('dataset_key')->primary();
$table->dateTime('initial_completed_at')->nullable();
});
$schema->create('shop_info_flow_form', function (Blueprint $table): void {
foreach ([
'Id', 'flowId', 'flowType', 'applyUser', 'applyTime', 'applyDepartment', 'remark', 'platform',
'responsibleDepartment', 'shopHead', 'customerManager', 'shopType', 'shopState', 'shopName',
'erpShopName', 'trademarkNumber', 'unifiedSocialCreditCode', 'informationChangeType',
'changeReason', 'changeSuccessful', 'closeReason', 'expectedClosingTime', 'refundType',
'refundAmount', 'refundIssue',
] as $column) {
$table->string($column)->nullable();
}
});
$schema->create('oa_flow_flow', function (Blueprint $table): void {
foreach (['Id', 'flowId', 'flowType', 'flowStatus', 'subject', 'approver', 'updateTime'] as $column) {
$table->string($column)->nullable();
}
});
DB::connection('business_mirror')->table('business_mirror_table_states')->insert([
'dataset_key' => ShopInfoFlowMirrorRepository::FORM_DATASET,
'initial_completed_at' => '2026-10-08 10:00:00',
]);
// 三单:开店(完成)、变更(流程中,有当前审批人)、关店(拒绝)。
$this->insertApplication(1, 'shp-1', 8, '拼多多', '甲店', '拼多多-甲店', '张三', '2026-10-01 09:00:00');
$this->insertApplication(2, 'shp-2', 9, '京东', '乙店', '京东-乙店', '李四', '2026-10-02 09:00:00');
$this->insertApplication(3, 'shp-3', 10, '抖音小店', '丙店', '抖音-丙店', '王五', '2026-10-03 09:00:00');
DB::connection('business_mirror')->table('oa_flow_flow')->insert([
['Id' => 1, 'flowId' => 'shp-1', 'flowType' => 8, 'flowStatus' => 3, 'subject' => '[店铺信息-开店流程]', 'approver' => '', 'updateTime' => '2026-10-01 10:00:00'],
['Id' => 2, 'flowId' => 'shp-2', 'flowType' => 9, 'flowStatus' => 1, 'subject' => '[店铺信息-变更流程]', 'approver' => '审核人A', 'updateTime' => '2026-10-02 10:00:00'],
['Id' => 3, 'flowId' => 'shp-3', 'flowType' => 10, 'flowStatus' => 2, 'subject' => '[店铺信息-关店流程]', 'approver' => '', 'updateTime' => '2026-10-03 10:00:00'],
]);
$this->repository = new ShopInfoFlowMirrorRepository(
new BusinessMirrorDatasetRegistry(),
new GenericBusinessMirrorRepository(app('db')),
);
}
private function insertApplication(int $id, string $flowId, int $flowType, string $platform, string $shop, string $erpShop, string $user, string $applyTime): void
{
DB::connection('business_mirror')->table('shop_info_flow_form')->insert([
'Id' => $id, 'flowId' => $flowId, 'flowType' => $flowType, 'platform' => $platform,
'shopName' => $shop, 'erpShopName' => $erpShop, 'applyUser' => $user, 'applyTime' => $applyTime,
'shopHead' => $user, 'responsibleDepartment' => '运营中心A',
]);
}
public function test_it_lists_applications_with_oa_approval_state(): void
{
Carbon::setTestNow('2026-10-02 12:30:00');
try {
$result = $this->repository->paginate(1, 10);
$this->assertSame(3, $result['total']);
$rows = collect($result['data'])->keyBy('flow_id');
$this->assertSame('店铺开店', $rows['shp-1']['flow_type']);
$this->assertSame('完成', $rows['shp-1']['approval_status']);
$this->assertSame('已完成', $rows['shp-1']['current_node']);
$this->assertSame('', $rows['shp-1']['node_waiting_duration']);
$this->assertSame('', $rows['shp-1']['node_waiting_days']);
$this->assertSame('店铺变更', $rows['shp-2']['flow_type']);
$this->assertSame('流程中', $rows['shp-2']['approval_status']);
$this->assertSame('审核人A', $rows['shp-2']['current_approver']);
$this->assertSame('审批节点(审核人A)', $rows['shp-2']['current_node']);
$this->assertSame('2026-10-02 10:00:00', $rows['shp-2']['node_entered_at']);
$this->assertSame('2小时30分钟', $rows['shp-2']['node_waiting_duration']);
$this->assertSame('1天内', $rows['shp-2']['node_waiting_days']);
$this->assertSame('店铺关店', $rows['shp-3']['flow_type']);
$this->assertSame('拒绝', $rows['shp-3']['approval_status']);
$this->assertSame('已拒绝', $rows['shp-3']['current_node']);
$this->assertSame('', $rows['shp-3']['node_waiting_duration']);
$this->assertSame('', $rows['shp-3']['node_waiting_days']);
// 默认按申请时间倒序。
$this->assertSame(['shp-3', 'shp-2', 'shp-1'], array_column($result['data'], 'flow_id'));
} finally {
Carbon::setTestNow();
}
}
public function test_it_filters_by_type_status_platform_and_dates(): void
{
$this->assertSame(1, $this->repository->paginate(1, 10, ['flow_type' => ['店铺开店']])['total']);
$this->assertSame(
2,
$this->repository->paginate(1, 10, ['flow_type' => ['店铺开店', '店铺变更']])['total'],
);
$this->assertSame(1, $this->repository->paginate(1, 10, ['approval_status' => ['流程中']])['total']);
$this->assertSame(2, $this->repository->paginate(1, 10, ['platform' => ['拼多多', '京东']])['total']);
$this->assertSame(1, $this->repository->paginate(1, 10, ['apply_from' => '2026-10-03'])['total']);
$this->assertSame(2, $this->repository->paginate(1, 10, ['apply_to' => '2026-10-02'])['total']);
$this->assertSame(1, $this->repository->paginate(1, 10, ['apply_user' => '李四'])['total']);
$this->assertSame(1, $this->repository->paginate(1, 10, ['erp_shop_name' => '丙店'])['total']);
$this->assertSame(0, $this->repository->paginate(1, 10, ['flow_id' => '不存在'])['total']);
}
public function test_it_groups_waiting_time_by_days_and_filters_selected_day_buckets(): void
{
Carbon::setTestNow('2026-11-05 12:00:00');
try {
$fixtures = [
[10, 'waiting-5d', '流程中', '2026-10-31 11:00:00', '5天'],
[11, 'waiting-6d', '流程中', '2026-10-30 10:00:00', '6天'],
[12, 'waiting-7d', '流程中', '2026-10-29 10:00:00', '7天'],
[13, 'waiting-8d', '流程中', '2026-10-28 10:00:00', '8-14天'],
[14, 'waiting-31d', '流程中', '2026-10-05 10:00:00', '31天以上'],
[15, 'finished-40d', '完成', '2026-09-26 10:00:00', ''],
];
foreach ($fixtures as [$id, $flowId, $status, $updatedAt, $expectedDays]) {
$this->insertApplication(
$id,
$flowId,
10,
'拼多多',
'等待店' . $id,
'拼多多-等待店' . $id,
'申请人' . $id,
'2026-09-01 09:00:00',
);
DB::connection('business_mirror')->table('oa_flow_flow')->insert([
'Id' => $id,
'flowId' => $flowId,
'flowType' => 10,
'flowStatus' => $status === '完成' ? 3 : 1,
'subject' => '[店铺关店-等待店' . $id . ']',
'approver' => $status === '完成' ? '' : '审核人' . $id,
'updateTime' => $updatedAt,
]);
}
$rows = collect($this->repository->paginate(1, 100)['data'])->keyBy('flow_id');
foreach ($fixtures as [, $flowId, , , $expectedDays]) {
$this->assertSame($expectedDays, $rows[$flowId]['node_waiting_days']);
}
$count = fn (array $days): int => $this->repository->paginate(1, 100, [
'flow_id' => 'waiting-',
'node_waiting_days' => $days,
])['total'];
$this->assertSame(1, $count(['5天']));
$this->assertSame(2, $count(['6天', '7天']));
$this->assertSame(1, $count(['8-14天']));
$this->assertSame(1, $count(['31天以上']));
} finally {
Carbon::setTestNow();
}
}
public function test_missing_oa_row_leaves_approval_state_blank(): void
{
DB::connection('business_mirror')->table('shop_info_flow_form')->insert([
'Id' => 4, 'flowId' => 'shp-4', 'flowType' => 8, 'platform' => '天猫',
'shopName' => '丁店', 'applyUser' => '赵六', 'applyTime' => '2026-10-04 09:00:00',
]);
$rows = collect($this->repository->paginate(1, 10)['data'])->keyBy('flow_id');
$this->assertSame('', $rows['shp-4']['approval_status']);
$this->assertSame('', $rows['shp-4']['current_approver']);
$this->assertSame('', $rows['shp-4']['current_node']);
$this->assertSame('', $rows['shp-4']['node_waiting_duration']);
$this->assertSame('', $rows['shp-4']['node_waiting_days']);
}
}