92 lines
2.9 KiB
PHP
92 lines
2.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
use app\adminapi\logic\firstvisit\FirstVisitConversionLogic;
|
|
use app\common\service\DataScope\DataScopeService;
|
|
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
|
|
|
function conversionRankingExpect(bool $condition, string $message): void
|
|
{
|
|
if (!$condition) {
|
|
throw new RuntimeException($message);
|
|
}
|
|
}
|
|
|
|
$reflection = new ReflectionClass(FirstVisitConversionLogic::class);
|
|
$rankingKind = $reflection->getMethod('rankingKind');
|
|
$rankingRows = $reflection->getMethod('rankingRows');
|
|
$topRows = $reflection->getMethod('topRows');
|
|
$rankingKind->setAccessible(true);
|
|
$rankingRows->setAccessible(true);
|
|
$topRows->setAccessible(true);
|
|
|
|
$member = static fn (int $id, string $name, int $orders, float $amount): array => [
|
|
'id' => "M{$id}_11",
|
|
'admin_id' => $id,
|
|
'name' => $name,
|
|
'type' => 'member',
|
|
'completed_order_count' => $orders,
|
|
'completed_order_amount' => $amount,
|
|
'children' => [],
|
|
];
|
|
|
|
$groupRows = [[
|
|
'id' => 10,
|
|
'name' => '一诊中心',
|
|
'children' => [
|
|
[
|
|
'id' => 11,
|
|
'name' => '一组',
|
|
'completed_order_count' => 8,
|
|
'completed_order_amount' => 800,
|
|
'children' => [$member(101, '甲', 5, 500), $member(102, '乙', 3, 300)],
|
|
],
|
|
[
|
|
'id' => 12,
|
|
'name' => '二组',
|
|
'completed_order_count' => 6,
|
|
'completed_order_amount' => 600,
|
|
'children' => [$member(103, '丙', 6, 600)],
|
|
],
|
|
],
|
|
]];
|
|
|
|
conversionRankingExpect(
|
|
$rankingKind->invoke(null, DataScopeService::SCOPE_SELF, 0) === 'hidden',
|
|
'Self-only data range must hide rankings'
|
|
);
|
|
conversionRankingExpect(
|
|
$rankingRows->invoke(null, $groupRows, 'hidden') === [],
|
|
'Hidden ranking mode must not return ranking rows'
|
|
);
|
|
conversionRankingExpect(
|
|
$rankingKind->invoke(null, DataScopeService::SCOPE_DEPT_AND_CHILD, 101) === 'hidden',
|
|
'Selecting one employee must switch the ranking to personal mode'
|
|
);
|
|
|
|
$memberRows = $rankingRows->invoke(null, $groupRows, 'member');
|
|
conversionRankingExpect(count($memberRows) === 3, 'Member ranking mode must include visible group members');
|
|
$rankedMembers = $topRows->invoke(null, $memberRows, 'completed_order_count');
|
|
conversionRankingExpect(
|
|
array_column($rankedMembers, 'name') === ['丙', '甲', '乙'],
|
|
'Member ranking must be ordered by the selected metric'
|
|
);
|
|
conversionRankingExpect(
|
|
$rankedMembers[0]['id'] === 'M103_11',
|
|
'Member ranking must preserve its string row key'
|
|
);
|
|
|
|
$teamRows = $rankingRows->invoke(null, $groupRows, 'group');
|
|
conversionRankingExpect(
|
|
array_column($teamRows, 'name') === ['一组', '二组'],
|
|
'Group ranking mode must use direct child departments'
|
|
);
|
|
conversionRankingExpect(
|
|
$rankingKind->invoke(null, DataScopeService::SCOPE_ALL, 0) === 'group',
|
|
'All-data range must use the group ranking dimension'
|
|
);
|
|
|
|
echo "FirstVisitConversionRankingTest passed\n";
|