['id' => 'remote_tag', 'name' => $name], 'group_id' => 'remote_group', 'group_name' => '推广渠道', 'reused' => false]; } } } namespace { use app\adminapi\controller\firstvisit\WecomPromotionController; use app\adminapi\logic\auth\AuthLogic; use app\common\service\qywx\QywxPromotionContactApiService; require dirname(__DIR__) . '/vendor/autoload.php'; new think\App(); final class CreateTagControllerFixture extends WecomPromotionController { public function __construct(think\Request $request, bool $root = false) { $this->request = $request; $this->adminId = 12; $this->adminInfo = ['root' => $root ? 1 : 0]; } protected function data($data) { return ['code' => 1, 'data' => $data]; } protected function fail(string $msg = 'fail', array $data = [], int $code = 0, int $show = 1) { return ['code' => $code, 'msg' => $msg, 'data' => $data]; } } function tagControllerCheck(bool $ok, string $message): void { if (!$ok) { throw new RuntimeException($message); } } $request = static function (string $method = 'POST', mixed $name = '直播'): think\Request { return (new think\Request())->setMethod($method)->withPost(['name' => $name, 'group_name' => '不允许客户端改组']); }; $result = (new CreateTagControllerFixture($request()))->createTag(); tagControllerCheck($result['code'] === 0 && QywxPromotionContactApiService::$calls === [], 'no page permission cannot create'); AuthLogic::$permissions = ['firstvisit.wecomPromotion/overview']; $result = (new CreateTagControllerFixture($request('GET')))->createTag(); tagControllerCheck($result['code'] === 0 && QywxPromotionContactApiService::$calls === [], 'GET cannot create'); foreach ([['array_name'], 123, true] as $invalid) { $result = (new CreateTagControllerFixture($request('POST', $invalid)))->createTag(); tagControllerCheck($result['code'] === 0 && QywxPromotionContactApiService::$calls === [], 'non-string name rejected before API'); } $result = (new CreateTagControllerFixture($request()))->createTag(); tagControllerCheck($result['code'] === 1 && $result['data']['tag']['id'] === 'remote_tag' && $result['data']['group_name'] === '推广渠道' && QywxPromotionContactApiService::$calls === ['直播'], 'page permission and response envelope'); AuthLogic::$permissions = []; $result = (new CreateTagControllerFixture($request('POST', '自定义'), true))->createTag(); tagControllerCheck($result['code'] === 1 && $result['data']['tag']['name'] === '自定义', 'root permitted through same controller guard'); QywxPromotionContactApiService::$fail = true; $result = (new CreateTagControllerFixture($request(), true))->createTag(); tagControllerCheck($result['code'] === 0 && $result['msg'] === '模拟企微标签失败', 'upstream failure not reported as success'); echo "WECOM_PROMOTION_CREATE_TAG_CONTROLLER_OK\n"; }