StudentKnowledgeGraphTest.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. namespace Tests\Unit;
  3. use Tests\TestCase;
  4. use App\Filament\Pages\StudentKnowledgeGraphPage;
  5. use App\Livewire\StudentKnowledgeGraph;
  6. use Illuminate\Foundation\Testing\LazilyRefreshDatabase;
  7. use Illuminate\Support\Facades\Http;
  8. use Illuminate\Support\Facades\DB;
  9. class StudentKnowledgeGraphTest extends TestCase
  10. {
  11. use LazilyRefreshDatabase;
  12. /** @test */
  13. public function page_can_be_accessed()
  14. {
  15. $response = $this->get('/admin/student-knowledge-graph-page');
  16. $response->assertStatus(200);
  17. $response->assertSee('学生知识图谱');
  18. }
  19. /** @test */
  20. public function page_has_correct_navigation()
  21. {
  22. $page = new StudentKnowledgeGraphPage();
  23. $this->assertEquals('学习分析', $page->getNavigationGroup());
  24. $this->assertEquals('学生知识图谱', $page->getNavigationLabel());
  25. $this->assertEquals(2, $page->getNavigationSort());
  26. }
  27. /** @test */
  28. public function page_renders_correct_view()
  29. {
  30. $page = new StudentKnowledgeGraphPage();
  31. $this->assertEquals('filament.pages.student-knowledge-graph-page', $page->getView());
  32. }
  33. /** @test */
  34. public function livewire_component_initializes_with_empty_data()
  35. {
  36. $component = new StudentKnowledgeGraph();
  37. $this->assertNull($component->selectedStudentId);
  38. $this->assertNull($component->selectedStudent);
  39. $this->assertEmpty($component->knowledgePoints);
  40. $this->assertEmpty($component->masteryData);
  41. $this->assertEmpty($component->statistics);
  42. $this->assertFalse($component->isLoading);
  43. }
  44. /** @test */
  45. public function load_students_populates_student_list()
  46. {
  47. // 创建测试数据
  48. DB::table('students')->insert([
  49. 'student_id' => 1001,
  50. 'name' => '张三',
  51. 'grade' => '高一',
  52. 'class_name' => '1班',
  53. 'teacher_id' => 1,
  54. ]);
  55. DB::table('students')->insert([
  56. 'student_id' => 1002,
  57. 'name' => '李四',
  58. 'grade' => '高一',
  59. 'class_name' => '2班',
  60. 'teacher_id' => 1,
  61. ]);
  62. $component = new StudentKnowledgeGraph();
  63. $component->loadStudents();
  64. $this->assertCount(2, $component->students);
  65. $this->assertEquals('张三 (高一-1班)', $component->students[0]['label']);
  66. $this->assertEquals(1001, $component->students[0]['id']);
  67. }
  68. /** @test */
  69. public function selecting_student_loads_data()
  70. {
  71. // 创建测试数据
  72. DB::table('students')->insert([
  73. 'student_id' => 1001,
  74. 'name' => '张三',
  75. 'grade' => '高一',
  76. 'class_name' => '1班',
  77. 'teacher_id' => 1,
  78. ]);
  79. $component = new StudentKnowledgeGraph();
  80. // 模拟HTTP响应
  81. Http::fake([
  82. 'localhost:5010/*' => Http::response([
  83. 'masteries' => [
  84. ['kp_code' => 'R01', 'mastery_level' => 0.85, 'confidence_level' => 0.8],
  85. ],
  86. 'total_knowledge_points' => 1,
  87. 'average_mastery' => 0.85,
  88. 'high_mastery_count' => 1,
  89. 'medium_mastery_count' => 0,
  90. 'low_mastery_count' => 0,
  91. ], 200),
  92. ]);
  93. $component->loadStudentData(1001);
  94. $this->assertNotNull($component->selectedStudent);
  95. $this->assertEquals('张三', $component->selectedStudent->name);
  96. $this->assertNotEmpty($component->masteryData);
  97. }
  98. /** @test */
  99. public function mastery_color_returns_correct_colors()
  100. {
  101. $component = new StudentKnowledgeGraph();
  102. // 使用反射获取私有方法
  103. $reflection = new \ReflectionClass($component);
  104. $method = $reflection->getMethod('getMasteryColor');
  105. $method->setAccessible(true);
  106. $this->assertEquals('#10b981', $method->invoke($component, 0.9)); // 优秀
  107. $this->assertEquals('#3b82f6', $method->invoke($component, 0.7)); // 良好
  108. $this->assertEquals('#f59e0b', $method->invoke($component, 0.5)); // 中等
  109. $this->assertEquals('#f97316', $method->invoke($component, 0.3)); // 待提高
  110. $this->assertEquals('#ef4444', $method->invoke($component, 0.1)); // 薄弱
  111. }
  112. /** @test */
  113. public function mastery_size_returns_correct_sizes()
  114. {
  115. $component = new StudentKnowledgeGraph();
  116. // 使用反射获取私有方法
  117. $reflection = new \ReflectionClass($component);
  118. $method = $reflection->getMethod('getMasterySize');
  119. $method->setAccessible(true);
  120. $this->assertEquals(36, $method->invoke($component, 0.9)); // 36px
  121. $this->assertEquals(28, $method->invoke($component, 0.7)); // 28px
  122. $this->assertEquals(10, $method->invoke($component, 0.1)); // 最小10px
  123. }
  124. /** @test */
  125. public function mock_data_is_used_when_api_fails()
  126. {
  127. // 创建测试数据
  128. DB::table('students')->insert([
  129. 'student_id' => 1001,
  130. 'name' => '张三',
  131. 'grade' => '高一',
  132. 'class_name' => '1班',
  133. 'teacher_id' => 1,
  134. ]);
  135. $component = new StudentKnowledgeGraph();
  136. // 模拟API调用失败
  137. Http::fake([
  138. 'localhost:5010/*' => Http::response([], 500),
  139. ]);
  140. $component->loadStudentData(1001);
  141. // 应该使用模拟数据
  142. $this->assertNotEmpty($component->knowledgePoints);
  143. $this->assertArrayHasKey('nodes', $component->knowledgePoints);
  144. $this->assertArrayHasKey('links', $component->knowledgePoints);
  145. $this->assertNotEmpty($component->masteryData);
  146. $this->assertNotEmpty($component->statistics);
  147. }
  148. /** @test */
  149. public function knowledge_graph_data_structure_is_correct()
  150. {
  151. $component = new StudentKnowledgeGraph();
  152. // 设置模拟数据
  153. $component->knowledgePoints = [
  154. 'nodes' => [
  155. [
  156. 'id' => 'R01',
  157. 'label' => '有理数',
  158. 'mastery' => 0.85,
  159. 'color' => '#10b981',
  160. 'size' => 34,
  161. ],
  162. ],
  163. 'links' => [
  164. [
  165. 'source' => 'R01',
  166. 'target' => 'R02',
  167. 'strength' => 0.9,
  168. 'type' => 'must',
  169. ],
  170. ],
  171. ];
  172. $this->assertCount(1, $component->knowledgePoints['nodes']);
  173. $this->assertCount(1, $component->knowledgePoints['links']);
  174. $this->assertEquals('R01', $component->knowledgePoints['nodes'][0]['id']);
  175. $this->assertEquals('#10b981', $component->knowledgePoints['nodes'][0]['color']);
  176. }
  177. /** @test */
  178. public function reset_data_clears_all_properties()
  179. {
  180. $component = new StudentKnowledgeGraph();
  181. // 设置一些数据
  182. $component->selectedStudentId = 1001;
  183. $component->knowledgePoints = ['nodes' => []];
  184. $component->masteryData = ['test' => 'data'];
  185. $component->statistics = ['count' => 1];
  186. // 使用反射调用私有方法
  187. $reflection = new \ReflectionClass($component);
  188. $method = $reflection->getMethod('resetData');
  189. $method->setAccessible(true);
  190. $method->invoke($component);
  191. $this->assertNull($component->selectedStudent);
  192. $this->assertEmpty($component->knowledgePoints);
  193. $this->assertEmpty($component->masteryData);
  194. $this->assertEmpty($component->statistics);
  195. $this->assertEmpty($component->dependencies);
  196. $this->assertEmpty($component->learningPath);
  197. }
  198. }