You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

133 lines
4.6 KiB

2 months ago
<?php
namespace Tests\Unit;
use App\Support\DefaultTrackScoringSheets;
use App\Support\TrackScoringSheet;
use PHPUnit\Framework\TestCase;
class TrackScoringSheetTest extends TestCase
{
1 month ago
public function test_optical_track_sheets_sum_to_100(): void
{
foreach (['guang_xinpian', 'zhizao_fengzhuang', 'guangdian_cailiao', 'guangtongxin_yingyong'] as $code) {
$sheet = DefaultTrackScoringSheets::forTrackCode($code);
$this->assertNotNull($sheet, $code);
$parsed = TrackScoringSheet::parseAndValidateConfig($sheet);
$this->assertTrue($parsed['ok'], $code.': '.($parsed['message'] ?? ''));
$this->assertSame(100.0, $parsed['sheet']['full_score']);
}
}
2 months ago
public function test_default_ai_track_sheet_sums_to_100(): void
{
$sheet = DefaultTrackScoringSheets::forTrackCode('xiaofei_ai', '消费+人工智能');
$this->assertNotNull($sheet);
$parsed = TrackScoringSheet::parseAndValidateConfig($sheet);
$this->assertTrue($parsed['ok']);
$this->assertSame(100.0, $parsed['sheet']['full_score']);
$this->assertCount(8, $parsed['sheet']['items']);
}
public function test_config_rejects_sum_not_100(): void
{
$parsed = TrackScoringSheet::parseAndValidateConfig([
'version' => 1,
'title' => 't',
'items' => [
[
'key' => 'a',
'sort' => 1,
'category' => '通用指标',
'title' => 'A',
'criteria' => '',
'max_score' => 40,
],
],
]);
$this->assertFalse($parsed['ok']);
$this->assertStringContainsString('100', $parsed['message']);
}
4 weeks ago
public function test_submit_sums_item_scores_and_rejects_invalid_items(): void
2 months ago
{
4 weeks ago
$parsed = TrackScoringSheet::parseAndValidateConfig($this->twoItemSheet());
2 months ago
$this->assertTrue($parsed['ok']);
4 weeks ago
$sheet = $parsed['sheet'];
2 months ago
4 weeks ago
$ok = TrackScoringSheet::normalizeSubmitPayload($sheet, [
'scores' => ['a' => 10.5, 'b' => 20],
]);
2 months ago
$this->assertTrue($ok['ok']);
4 weeks ago
$this->assertSame(30.5, $ok['line_total']);
$this->assertSame(10.5, $ok['scores']['a']);
$this->assertSame(20.0, $ok['scores']['b']);
$this->assertSame(30.5, $ok['payload']['line_total']);
2 months ago
4 weeks ago
$zero = TrackScoringSheet::normalizeSubmitPayload($sheet, [
'scores' => ['a' => 0, 'b' => 0],
]);
$this->assertTrue($zero['ok']);
$this->assertSame(0.0, $zero['line_total']);
2 months ago
4 weeks ago
$full = TrackScoringSheet::normalizeSubmitPayload($sheet, [
'scores' => ['a' => 40, 'b' => 60],
]);
$this->assertTrue($full['ok']);
$this->assertSame(100.0, $full['line_total']);
2 months ago
4 weeks ago
$missing = TrackScoringSheet::normalizeSubmitPayload($sheet, ['line_total' => 88]);
2 months ago
$this->assertFalse($missing['ok']);
4 weeks ago
$this->assertStringContainsString('每一项', $missing['message']);
2 months ago
4 weeks ago
$overItem = TrackScoringSheet::normalizeSubmitPayload($sheet, [
'scores' => ['a' => 40.01, 'b' => 0],
]);
$this->assertFalse($overItem['ok']);
$this->assertStringContainsString('不能高于该项分值', $overItem['message']);
2 months ago
4 weeks ago
$neg = TrackScoringSheet::normalizeSubmitPayload($sheet, [
'scores' => ['a' => -0.01, 'b' => 0],
]);
2 months ago
$this->assertFalse($neg['ok']);
4 weeks ago
$this->assertStringContainsString('不能小于 0', $neg['message']);
2 months ago
4 weeks ago
$tooManyDecimals = TrackScoringSheet::normalizeSubmitPayload($sheet, [
'scores' => ['a' => '10.001', 'b' => 0],
]);
2 months ago
$this->assertFalse($tooManyDecimals['ok']);
$this->assertStringContainsString('两位小数', $tooManyDecimals['message']);
2 months ago
}
4 weeks ago
/**
* @return array<string, mixed>
*/
private function twoItemSheet(): array
{
return [
'version' => 1,
'title' => 't',
'items' => [
[
'key' => 'a',
'sort' => 1,
'category' => '通用指标',
'title' => 'A',
'criteria' => '',
'max_score' => 40,
],
[
'key' => 'b',
'sort' => 2,
'category' => '通用指标',
'title' => 'B',
'criteria' => '',
'max_score' => 60,
],
],
];
}
2 months ago
}