ExamHistoryTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. namespace Tests\Feature\Livewire;
  3. use App\Filament\Pages\ExamHistory;
  4. use App\Models\ExamPaper;
  5. use Illuminate\Foundation\Testing\RefreshDatabase;
  6. use Livewire\Livewire;
  7. use Tests\TestCase;
  8. class ExamHistoryTest extends TestCase
  9. {
  10. use RefreshDatabase;
  11. protected function setUp(): void
  12. {
  13. parent::setUp();
  14. // 在测试环境中,ExamPaper 会使用默认的 SQLite 连接
  15. // 创建 exam_papers 表
  16. \Illuminate\Support\Facades\Schema::create('exam_papers', function (\Illuminate\Database\Schema\Blueprint $table) {
  17. $table->string('id', 191)->primary();
  18. $table->string('title');
  19. $table->integer('total_score');
  20. $table->integer('duration');
  21. $table->timestamps();
  22. });
  23. }
  24. /** @test */
  25. public function it_displays_exam_list()
  26. {
  27. // 临时设置 ExamPaper 使用默认连接
  28. $this->setExamPaperConnection('sqlite');
  29. // 创建测试试卷
  30. ExamPaper::create([
  31. 'id' => 'test_exam_1',
  32. 'title' => '测试试卷 1',
  33. 'total_score' => 100,
  34. 'duration' => 120,
  35. 'created_at' => now(),
  36. 'updated_at' => now(),
  37. ]);
  38. ExamPaper::create([
  39. 'id' => 'test_exam_2',
  40. 'title' => '测试试卷 2',
  41. 'total_score' => 150,
  42. 'duration' => 90,
  43. 'created_at' => now(),
  44. 'updated_at' => now(),
  45. ]);
  46. $component = Livewire::test(ExamHistory::class);
  47. $component->assertSee('测试试卷 1')
  48. ->assertSee('测试试卷 2')
  49. ->assertSee('100 分')
  50. ->assertSee('150 分');
  51. }
  52. /** @test */
  53. public function it_shows_empty_state_when_no_exams()
  54. {
  55. $this->setExamPaperConnection('sqlite');
  56. $component = Livewire::test(ExamHistory::class);
  57. $component->assertSee('暂无试卷记录')
  58. ->assertSee('请前往"智能出卷"页面生成您的第一份试卷');
  59. }
  60. /** @test */
  61. public function it_filters_exams_by_search()
  62. {
  63. $this->setExamPaperConnection('sqlite');
  64. ExamPaper::create([
  65. 'id' => 'test_exam_1',
  66. 'title' => '数学试卷',
  67. 'total_score' => 100,
  68. 'duration' => 120,
  69. 'created_at' => now(),
  70. 'updated_at' => now(),
  71. ]);
  72. ExamPaper::create([
  73. 'id' => 'test_exam_2',
  74. 'title' => '英语试卷',
  75. 'total_score' => 150,
  76. 'duration' => 90,
  77. 'created_at' => now(),
  78. 'updated_at' => now(),
  79. ]);
  80. $component = Livewire::test(ExamHistory::class)
  81. ->set('search', '数学');
  82. $component->assertSee('数学试卷')
  83. ->assertDontSee('英语试卷');
  84. }
  85. /** @test */
  86. public function it_paginates_exam_list()
  87. {
  88. $this->setExamPaperConnection('sqlite');
  89. // 创建 25 份试卷(超过默认每页 20 条)
  90. for ($i = 1; $i <= 25; $i++) {
  91. ExamPaper::create([
  92. 'id' => "test_exam_{$i}",
  93. 'title' => "测试试卷 {$i}",
  94. 'total_score' => 100,
  95. 'duration' => 120,
  96. 'created_at' => now()->subMinutes(25 - $i),
  97. 'updated_at' => now(),
  98. ]);
  99. }
  100. $component = Livewire::test(ExamHistory::class);
  101. // 第一页应该显示最新的 20 份
  102. $component->assertSee('测试试卷 25')
  103. ->assertSee('测试试卷 6')
  104. ->assertDontSee('测试试卷 5');
  105. // 检查分页信息
  106. $meta = $component->get('meta');
  107. $this->assertEquals(25, $meta['total']);
  108. $this->assertEquals(2, $meta['total_pages']);
  109. }
  110. /**
  111. * 设置 ExamPaper 模型使用指定连接
  112. */
  113. protected function setExamPaperConnection(string $connection)
  114. {
  115. // 使用反射修改 protected 属性
  116. $reflection = new \ReflectionClass(ExamPaper::class);
  117. $property = $reflection->getProperty('connection');
  118. $property->setAccessible(true);
  119. $property->setValue(new ExamPaper(), $connection);
  120. // 重新绑定模型到容器
  121. $this->app->bind(ExamPaper::class, function () use ($connection) {
  122. $model = new ExamPaper();
  123. $model->setConnection($connection);
  124. return $model;
  125. });
  126. }
  127. }