string('id', 191)->primary(); $table->string('title'); $table->integer('total_score'); $table->integer('duration'); $table->timestamps(); }); } /** @test */ public function it_displays_exam_list() { // 临时设置 ExamPaper 使用默认连接 $this->setExamPaperConnection('sqlite'); // 创建测试试卷 ExamPaper::create([ 'id' => 'test_exam_1', 'title' => '测试试卷 1', 'total_score' => 100, 'duration' => 120, 'created_at' => now(), 'updated_at' => now(), ]); ExamPaper::create([ 'id' => 'test_exam_2', 'title' => '测试试卷 2', 'total_score' => 150, 'duration' => 90, 'created_at' => now(), 'updated_at' => now(), ]); $component = Livewire::test(ExamHistory::class); $component->assertSee('测试试卷 1') ->assertSee('测试试卷 2') ->assertSee('100 分') ->assertSee('150 分'); } /** @test */ public function it_shows_empty_state_when_no_exams() { $this->setExamPaperConnection('sqlite'); $component = Livewire::test(ExamHistory::class); $component->assertSee('暂无试卷记录') ->assertSee('请前往"智能出卷"页面生成您的第一份试卷'); } /** @test */ public function it_filters_exams_by_search() { $this->setExamPaperConnection('sqlite'); ExamPaper::create([ 'id' => 'test_exam_1', 'title' => '数学试卷', 'total_score' => 100, 'duration' => 120, 'created_at' => now(), 'updated_at' => now(), ]); ExamPaper::create([ 'id' => 'test_exam_2', 'title' => '英语试卷', 'total_score' => 150, 'duration' => 90, 'created_at' => now(), 'updated_at' => now(), ]); $component = Livewire::test(ExamHistory::class) ->set('search', '数学'); $component->assertSee('数学试卷') ->assertDontSee('英语试卷'); } /** @test */ public function it_paginates_exam_list() { $this->setExamPaperConnection('sqlite'); // 创建 25 份试卷(超过默认每页 20 条) for ($i = 1; $i <= 25; $i++) { ExamPaper::create([ 'id' => "test_exam_{$i}", 'title' => "测试试卷 {$i}", 'total_score' => 100, 'duration' => 120, 'created_at' => now()->subMinutes(25 - $i), 'updated_at' => now(), ]); } $component = Livewire::test(ExamHistory::class); // 第一页应该显示最新的 20 份 $component->assertSee('测试试卷 25') ->assertSee('测试试卷 6') ->assertDontSee('测试试卷 5'); // 检查分页信息 $meta = $component->get('meta'); $this->assertEquals(25, $meta['total']); $this->assertEquals(2, $meta['total_pages']); } /** * 设置 ExamPaper 模型使用指定连接 */ protected function setExamPaperConnection(string $connection) { // 使用反射修改 protected 属性 $reflection = new \ReflectionClass(ExamPaper::class); $property = $reflection->getProperty('connection'); $property->setAccessible(true); $property->setValue(new ExamPaper(), $connection); // 重新绑定模型到容器 $this->app->bind(ExamPaper::class, function () use ($connection) { $model = new ExamPaper(); $model->setConnection($connection); return $model; }); } }