score_config = config('score'); $factory = new ScoreStrategyFactory($this->score_config); $engine = new ScoreEngine($factory); $this->score_service = new ScoreService($engine); } /* ---------------- 数据结构 ---------------- */ /** 题库声明与实际必须一致,换数据时最容易悄悄对不上的地方 */ public function testQuizDataShape(): void { $paper = QuizModel::getPaperInfo(); self::assertNotEmpty($paper); self::assertSame( intval($paper['question_count']), QuizModel::getQuestionTotal(), 'paper.question_count 与实际题目数不一致' ); $axis_list = QuizModel::getAxisList(); $pole_list = QuizModel::getPoleList(); self::assertGreaterThan(0, count($axis_list)); self::assertSame(count($axis_list) * 2, count($pole_list), '极点应为轴数的 2 倍'); // 每题必须完整:两个选项、指向已定义的极点、题号连续 $q_no_list = []; foreach (QuizModel::getQuestionMap() as $q_no => $question) { $q_no_list[] = $q_no; self::assertCount(2, $question['options'], '第 ' . $q_no . ' 题选项数不为 2'); foreach ($question['options'] as $option) { self::assertArrayHasKey( $option['dimension_code'], $pole_list, '第 ' . $q_no . ' 题选项指向未定义的极点' ); } } sort($q_no_list); self::assertSame(range(1, count($q_no_list)), $q_no_list, '题号必须从 1 连续编号'); } /** 类型字典必须覆盖所有轴的组合,否则会出现算出来但没有文案的类型 */ public function testTypeDictionaryCoversAllCombinations(): void { $type_list = QuizModel::getTypeList(); $expect_count = 1; foreach (QuizModel::getAxisList() as $axis) { $expect_count = $expect_count * 2; } self::assertCount($expect_count, $type_list); } /* ---------------- 评分正确性 ---------------- */ /** 每一型都必须能通过「老实按该型作答」复现;缺一个说明题库在该轴有缺口 */ public function testEveryTypeReachable(): void { $type_list = QuizModel::getTypeList(); foreach (array_keys($type_list) as $type_code) { $result = $this->score_service->score($this->getAnswerList($type_code)); self::assertSame($type_code, $result->type_code, '类型 ' . $type_code . ' 无法由一致的作答复现'); } } /** 同型作答下各策略结论一致 */ public function testAllStrategiesAgree(): void { $type_list = QuizModel::getTypeList(); $first = (string) array_key_first($type_list); $answer_list = $this->getAnswerList($first); foreach (array_keys($this->score_config['strategies']) as $alias) { $result = $this->score_service->score($answer_list, $alias); self::assertSame($first, $result->type_code, '策略 ' . $alias . ' 在类型 ' . $first . ' 上偏离'); } } /** 输出契约必须完全同构 —— 多策略能成立的核心 */ public function testOutputIsIsomorphic(): void { $type_list = QuizModel::getTypeList(); $answer_list = $this->getAnswerList((string) array_key_first($type_list)); $shape = []; foreach (array_keys($this->score_config['strategies']) as $alias) { $keys = array_keys($this->score_service->score($answer_list, $alias)->getData()); if (empty($shape)) { $shape = $keys; continue; } self::assertSame($shape, $keys, '策略 ' . $alias . ' 的输出字段与首个策略不一致'); } foreach (['strategy', 'strategy_version', 'functions', 'radar'] as $key) { self::assertContains($key, $shape); } } /** 纯计算:同输入连续两次结果完全相同,否则 A/B 对比没有意义 */ public function testStrategyIsPure(): void { $type_list = QuizModel::getTypeList(); $answer_list = $this->getAnswerList((string) array_key_first($type_list)); foreach (array_keys($this->score_config['strategies']) as $alias) { self::assertSame( $this->score_service->score($answer_list, $alias)->getData(true), $this->score_service->score($answer_list, $alias)->getData(true), '策略 ' . $alias . ' 不是纯计算' ); } } /** 雷达图必须覆盖全部极点,点数 = 轴数 × 2 */ public function testRadarCoversAllPoles(): void { $type_list = QuizModel::getTypeList(); $answer_list = $this->getAnswerList((string) array_key_first($type_list)); $result = $this->score_service->score($answer_list); $paper = QuizModel::getPaperInfo(); $axis_list = QuizModel::getAxisList(); self::assertSame($paper['radar']['order'], $result->radar['codes'], '雷达极点顺序与题库声明不一致'); self::assertCount(count($axis_list) * 2, $result->radar['codes']); self::assertCount(count($result->radar['codes']), $result->radar['values']); self::assertCount(count($result->radar['codes']), $result->radar['axes']); self::assertCount(count($result->radar['codes']), $result->radar['norm']); self::assertCount(count($result->radar['codes']), $result->radar['colors']); foreach ($result->radar['values'] as $value) { self::assertGreaterThanOrEqual(0, $value); self::assertLessThanOrEqual(100, $value); } } /** * 认知功能线图:数量与声明一致,且 product 公式算得对 * 这条是防「图能画但数算错」的唯一保障 —— 数值错了前端看不出来 */ public function testFunctionChartMatchesFormula(): void { $function_list = QuizModel::getFunctionList(); if (empty($function_list)) { self::markTestSkipped('该题库未配置 functions'); } $type_list = QuizModel::getTypeList(); $answer_list = $this->getAnswerList((string) array_key_first($type_list)); $result = $this->score_service->score($answer_list); self::assertTrue($result->checkFunctions()); self::assertCount(count($function_list), $result->functions); $pct_list = []; foreach ($result->dimension_scores as $pole_code => $pole_info) { $pct_list[$pole_code] = (float) $pole_info['pct']; } foreach ($result->functions as $index => $function) { $def = $function_list[$index]; self::assertSame((string) $def['code'], $function['code']); $pole_pct = $pct_list[(string) $def['pole']]; $attitude_pct = $pct_list[(string) $def['attitude']]; $expect = intval(round($pole_pct * $attitude_pct / 100)); self::assertSame($expect, $function['value'], '功能 ' . $function['code'] . ' 数值与 product 公式不符'); } } /* ---------------- 边界与异常 ---------------- */ /** 全选同一位置 = 响应不一致,各轴都该判为中间型 */ public function testIdenticalAnswersAreJudgedMiddle(): void { $question_map = QuizModel::getQuestionMap(); foreach (['A', 'B'] as $pick) { $answer_list = []; foreach (array_keys($question_map) as $q_no) { $answer_list[$q_no] = $pick; } $result = $this->score_service->score($answer_list); self::assertTrue($result->checkMiddleAxis(), '全选 ' . $pick . ' 未识别为倾向不显著'); self::assertLessThan(50, $result->confidence, '全选 ' . $pick . ' 的置信度不应偏高'); } } /** 缺题必须显式失败,不能静默补零 */ public function testMissingAnswerThrows(): void { $type_list = QuizModel::getTypeList(); $answer_list = $this->getAnswerList((string) array_key_first($type_list)); unset($answer_list[1]); $this->expectException(BusinessException::class); $this->score_service->score($answer_list); } /** 不存在的选项键同样必须失败 */ public function testUnknownOptionThrows(): void { $type_list = QuizModel::getTypeList(); $answer_list = $this->getAnswerList((string) array_key_first($type_list)); $answer_list[1] = 'Z'; $this->expectException(BusinessException::class); $this->score_service->score($answer_list); } /** strict = true 时未注册策略必须抛异常,不允许静默回退 */ public function testUnknownStrategyThrows(): void { $type_list = QuizModel::getTypeList(); $answer_list = $this->getAnswerList((string) array_key_first($type_list)); $this->expectException(UnsupportedStrategyException::class); $this->score_service->score($answer_list, 'no_such_strategy'); } /** * 答案权重不得流出到前端 * 用白名单断言实际字段,而不是「检查有没有 score」 * 后者在题库新增字段时会漏判 */ public function testPublicQuestionsDoNotLeakScoring(): void { $question_list = QuizModel::getPublicQuestionList(); self::assertCount(QuizModel::getQuestionTotal(), $question_list); foreach ($question_list as $question) { self::assertSame(['q_no', 'title', 'options'], array_keys($question)); foreach ($question['options'] as $option) { self::assertSame(['key', 'text'], array_keys($option)); } } } /** * 交叉验证:tools/verify-mock.mjs 里有一套独立实现的同规格算法 * 它会导出各型夹具,这里断言生产实现对同一批夹具给出同样类型 * * 为什么需要它:本机没有 PHP,算法规格只能先用 JS 仿真验证 * 两个独立实现对同一份夹具必须一致 —— 这是当前唯一能防住「两边算得不一样」的手段 */ public function testMatchesJsGeneratedFixtures(): void { $path = __DIR__ . '/fixtures/answers-by-type.json'; self::assertFileExists($path, '夹具缺失,先跑:node tools/verify-mock.mjs'); $fixtures = json_decode((string) file_get_contents($path), true); self::assertIsArray($fixtures['cases'] ?? null, '夹具格式不符'); self::assertSame( array_keys(QuizModel::getTypeList()), array_keys($fixtures['cases']), '夹具覆盖的类型与题库类型字典不一致' ); foreach ($fixtures['cases'] as $type_code => $answer_list) { $result = $this->score_service->score($answer_list); self::assertSame($type_code, $result->type_code, 'JS 夹具 ' . $type_code . ' 在 PHP 实现下偏离'); } } /* ---------------- 辅助 ---------------- */ /** * 生成「完全符合目标类型」的作答 * getAnswerList * @param string $type_code 目标类型码 * @return array 键为题号,值为选项 key */ private function getAnswerList($type_code) { $axis_list = QuizModel::getAxisList(); $pole_list = QuizModel::getPoleList(); $question_map = QuizModel::getQuestionMap(); $answer_list = []; foreach ($question_map as $q_no => $question) { $axis_index = 0; foreach ($axis_list as $index => $axis) { if ($axis['code'] === $pole_list[$question['dimension_code']]['axis']) { $axis_index = $index; break; } } $want_pole = ''; if (isset($type_code[$axis_index])) { $want_pole = $type_code[$axis_index]; } foreach ($question['options'] as $option_key => $option) { if ($option['dimension_code'] === $want_pole) { $answer_list[$q_no] = $option_key; break; } } } return $answer_list; } }