Files
zyt/server/tests/QywxPromotionMemberRangeTest.php
T
2026-08-25 11:40:58 +08:00

41 lines
2.7 KiB
PHP

<?php
declare(strict_types=1);
use app\common\service\qywx\QywxPromotionMemberRange;
use app\common\service\qywx\QywxPromotionMemberSchedulerService;
require dirname(__DIR__) . '/vendor/autoload.php';
$assert = static function (bool $condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$today = '2026-08-25';
$now = strtotime($today . ' 12:00:00');
$range = QywxPromotionMemberRange::evaluate([
['id' => 1, 'userid' => 'A', 'enabled' => 1, 'weight' => 100, 'current_weight' => 9, 'daily_limit' => 0, 'today_count' => 0, 'today_date' => $today],
['id' => 2, 'userid' => 'B', 'enabled' => 0, 'weight' => 1, 'current_weight' => -3, 'daily_limit' => 0, 'today_count' => 0, 'today_date' => $today],
['id' => 3, 'userid' => 'C', 'enabled' => 1, 'weight' => 1, 'current_weight' => 4, 'daily_limit' => 3, 'today_count' => 3, 'today_date' => $today],
['id' => 4, 'userid' => 'D', 'enabled' => 1, 'weight' => 1, 'current_weight' => 1, 'daily_limit' => 1, 'today_count' => 1, 'today_date' => '2026-08-24'],
['id' => 5, 'userid' => 'E', 'enabled' => 1, 'weight' => 1, 'current_weight' => 0, 'daily_limit' => 0, 'today_count' => 0, 'today_date' => $today, 'active_start' => $now + 1],
['id' => 6, 'userid' => 'F', 'enabled' => 1, 'weight' => 1, 'current_weight' => 0, 'daily_limit' => 0, 'today_count' => 0, 'today_date' => $today, 'active_end' => $now - 1],
], $today, $now);
$assert($range['userids'] === ['A', 'D'], '官方多人路由范围没有正确排除禁用或达到上限的成员');
$assert($range['userids'] === ['A', 'D'], '逐成员权重不得改变企业微信原生多人范围');
$assert($range['eligible_count'] === 2, '可用成员数量错误');
$assert($range['members'][3]['today_count'] === 0, '跨日数量没有自动重置');
$assert(array_column($range['members'], 'current_weight') === [0, 0, 0, 0, 0, 0], '旧版调度游标没有归零');
$assert(QywxPromotionMemberRange::same(['B', 'A', 'A'], ['A', 'B']), '成员范围比较不应受顺序和重复值影响');
$assert(!QywxPromotionMemberRange::same(['A'], ['A', 'B']), '不同成员范围被错误判定为相同');
$assert(QywxPromotionMemberSchedulerService::poolIdFromState('zyt_pool:123') === 123, 'customer_channel 方案 ID 解析失败');
$assert(QywxPromotionMemberSchedulerService::poolIdFromState('qywx_ca:123') === 0, '不应接管其他系统的 customer_channel');
$empty = QywxPromotionMemberRange::evaluate([
['id' => 7, 'userid' => 'G', 'enabled' => 0, 'today_date' => $today],
], $today, $now);
$assert($empty['userids'] === [] && $empty['eligible_count'] === 0, '零可用成员必须返回空范围并交由同步层阻止远端更新');
echo "QYWX_PROMOTION_MEMBER_RANGE_OK\n";