ExamPdfPreviewTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace Tests\Feature;
  3. use App\Models\ExamPaper;
  4. use Illuminate\Foundation\Testing\RefreshDatabase;
  5. use Tests\TestCase;
  6. class ExamPdfPreviewTest extends TestCase
  7. {
  8. use RefreshDatabase;
  9. protected function setUp(): void
  10. {
  11. parent::setUp();
  12. // 创建测试用的 exam_papers 表
  13. \Illuminate\Support\Facades\Schema::connection('remote_mysql')->dropIfExists('exam_papers');
  14. \Illuminate\Support\Facades\Schema::connection('remote_mysql')->create('exam_papers', function (\Illuminate\Database\Schema\Blueprint $table) {
  15. $table->string('id', 191)->primary();
  16. $table->string('title');
  17. $table->integer('total_score');
  18. $table->integer('duration');
  19. $table->timestamps();
  20. });
  21. }
  22. /** @test */
  23. public function it_displays_pdf_preview_for_existing_exam()
  24. {
  25. $exam = ExamPaper::create([
  26. 'id' => 'test_pdf_exam',
  27. 'title' => 'PDF 测试试卷',
  28. 'total_score' => 100,
  29. 'duration' => 120,
  30. 'created_at' => now(),
  31. 'updated_at' => now(),
  32. ]);
  33. $response = $this->get(route('filament.admin.auth.intelligent-exam.pdf', ['paper_id' => $exam->id]));
  34. $response->assertStatus(200)
  35. ->assertSee('PDF 测试试卷')
  36. ->assertSee('选择题')
  37. ->assertSee('填空题')
  38. ->assertSee('解答题');
  39. }
  40. /** @test */
  41. public function it_returns_404_for_non_existent_exam()
  42. {
  43. $response = $this->get(route('filament.admin.auth.intelligent-exam.pdf', ['paper_id' => 'non_existent']));
  44. $response->assertStatus(404);
  45. }
  46. /** @test */
  47. public function it_displays_omr_markers_for_all_question_types()
  48. {
  49. $exam = ExamPaper::create([
  50. 'id' => 'omr_test_exam',
  51. 'title' => 'OMR 标记测试',
  52. 'total_score' => 100,
  53. 'duration' => 120,
  54. 'created_at' => now(),
  55. 'updated_at' => now(),
  56. ]);
  57. $response = $this->get(route('filament.admin.auth.intelligent-exam.pdf', ['paper_id' => $exam->id]));
  58. // 检查 OMR 标记的 CSS 类是否存在
  59. $response->assertSee('omr-marker', false);
  60. }
  61. /** @test */
  62. public function it_correctly_renders_newlines_in_questions()
  63. {
  64. $exam = ExamPaper::create([
  65. 'id' => 'newline_test_exam',
  66. 'title' => '换行测试试卷',
  67. 'total_score' => 100,
  68. 'duration' => 120,
  69. 'created_at' => now(),
  70. 'updated_at' => now(),
  71. ]);
  72. $response = $this->get(route('filament.admin.auth.intelligent-exam.pdf', ['paper_id' => $exam->id]));
  73. // 检查换行符是否被转换为 <br> 标签
  74. $response->assertSee('<br', false);
  75. }
  76. /** @test */
  77. public function it_creates_exam_papers_table_if_not_exists()
  78. {
  79. // 删除表
  80. \Illuminate\Support\Facades\Schema::connection('remote_mysql')->dropIfExists('exam_papers');
  81. // 访问 PDF 页面应该自动创建表
  82. $response = $this->get(route('filament.admin.auth.intelligent-exam.pdf', ['paper_id' => 'test']));
  83. // 表应该已经被创建
  84. $this->assertTrue(\Illuminate\Support\Facades\Schema::connection('remote_mysql')->hasTable('exam_papers'));
  85. }
  86. protected function tearDown(): void
  87. {
  88. \Illuminate\Support\Facades\Schema::connection('remote_mysql')->dropIfExists('exam_papers');
  89. parent::tearDown();
  90. }
  91. }