IntelligentExamGenerationTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace Tests\Unit;
  3. use Tests\TestCase;
  4. use App\Filament\Pages\IntelligentExamGeneration;
  5. use App\Services\KnowledgeGraphService;
  6. use App\Services\LearningAnalyticsService;
  7. use App\Services\QuestionBankService;
  8. use Illuminate\Support\Facades\Http;
  9. class IntelligentExamGenerationTest extends TestCase
  10. {
  11. protected function setUp(): void
  12. {
  13. parent::setUp();
  14. }
  15. /** @test */
  16. public function it_can_access_intelligent_exam_generation_page()
  17. {
  18. // 模拟访问智能出卷页面
  19. $response = $this->get('/admin/intelligent-exam-generation');
  20. $response->assertStatus(200);
  21. }
  22. /** @test */
  23. public function it_can_load_knowledge_points()
  24. {
  25. // 测试知识点获取功能
  26. $knowledgeGraphService = app(KnowledgeGraphService::class);
  27. // 模拟listKnowledgePoints方法返回
  28. $result = $knowledgeGraphService->listKnowledgePoints(1, 100);
  29. $this->assertIsArray($result);
  30. $this->assertArrayHasKey('data', $result);
  31. }
  32. /** @test */
  33. public function it_can_validate_form_before_generation()
  34. {
  35. // 创建页面实例
  36. $page = new IntelligentExamGeneration();
  37. // 设置必填字段为空,应该验证失败
  38. $this->assertFalse($page->paperName === 'required');
  39. }
  40. /** @test */
  41. public function it_can_generate_exam_with_basic_params()
  42. {
  43. // 模拟智能出卷参数
  44. $params = [
  45. 'student_id' => 'test_student_001',
  46. 'total_questions' => 10,
  47. 'kp_codes' => ['KP001', 'KP002'],
  48. 'skills' => [],
  49. 'question_type_ratio' => [
  50. '选择题' => 40,
  51. '填空题' => 30,
  52. '解答题' => 30,
  53. ],
  54. 'difficulty_ratio' => [
  55. '基础' => 50,
  56. '中等' => 35,
  57. '拔高' => 15,
  58. ],
  59. ];
  60. $learningAnalyticsService = app(LearningAnalyticsService::class);
  61. // 调用智能出卷服务
  62. $result = $learningAnalyticsService->generateIntelligentExam($params);
  63. $this->assertIsArray($result);
  64. $this->assertArrayHasKey('success', $result);
  65. }
  66. /** @test */
  67. public function it_can_save_exam_to_database()
  68. {
  69. $examData = [
  70. 'paper_name' => '测试试卷',
  71. 'paper_description' => '这是一份测试试卷',
  72. 'difficulty_category' => '基础',
  73. 'questions' => [],
  74. 'total_score' => 100,
  75. 'total_questions' => 10,
  76. ];
  77. $questionBankService = app(QuestionBankService::class);
  78. $paperId = $questionBankService->saveExamToDatabase($examData);
  79. $this->assertNotNull($paperId);
  80. $this->assertIsString($paperId);
  81. }
  82. /** @test */
  83. public function it_can_filter_by_student_weakness()
  84. {
  85. $studentId = 'test_student_001';
  86. $learningAnalyticsService = app(LearningAnalyticsService::class);
  87. // 获取学生薄弱点
  88. $weaknesses = $learningAnalyticsService->getStudentWeaknesses($studentId, 10);
  89. $this->assertIsArray($weaknesses);
  90. // 验证薄弱点数据结构
  91. if (!empty($weaknesses)) {
  92. $firstWeakness = $weaknesses[0];
  93. $this->assertArrayHasKey('kp_code', $firstWeakness);
  94. $this->assertArrayHasKey('mastery', $firstWeakness);
  95. }
  96. }
  97. /** @test */
  98. public function it_can_auto_generate_questions()
  99. {
  100. $filters = [
  101. 'kp_code' => 'KP001',
  102. 'count' => 5,
  103. 'difficulty_distribution' => [
  104. '基础' => 50,
  105. '中等' => 35,
  106. '拔高' => 15,
  107. ],
  108. ];
  109. $questionBankService = app(QuestionBankService::class);
  110. $result = $questionBankService->generateIntelligentQuestions($filters);
  111. $this->assertIsArray($result);
  112. $this->assertArrayHasKey('success', $result);
  113. }
  114. /** @test */
  115. public function it_can_export_exam_to_pdf()
  116. {
  117. $paperId = 'test_paper_001';
  118. $questionBankService = app(QuestionBankService::class);
  119. // 模拟导出PDF
  120. $pdfUrl = $questionBankService->exportExamToPdf($paperId);
  121. // 返回URL(可能是null或字符串)
  122. $this->assertTrue($pdfUrl === null || is_string($pdfUrl));
  123. }
  124. /** @test */
  125. public function it_validates_question_type_ratios_sum_to_100()
  126. {
  127. $page = new IntelligentExamGeneration();
  128. $ratios = $page->questionTypeRatio;
  129. $total = array_sum($ratios);
  130. $this->assertEquals(100, $total, '题型配比总和必须为100%');
  131. }
  132. /** @test */
  133. public function it_validates_difficulty_ratios_sum_to_100()
  134. {
  135. $page = new IntelligentExamGeneration();
  136. $ratios = $page->difficultyRatio;
  137. $total = array_sum($ratios);
  138. $this->assertEquals(100, $total, '难度配比总和必须为100%');
  139. }
  140. }